3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
14 if {![info exists testdir]} {
15 set testdir [file join [file dirname [info script]] .. .. test]
17 source [file join [file dirname [info script]] rtree_util.tcl]
18 source $testdir/tester.tcl
19 ifcapable !rtree { finish_test ; return }
21 #-------------------------------------------------------------------------
22 # The following block of tests - rtree8-1.* - feature reading and writing
23 # an r-tree table while there exist open cursors on it.
25 proc populate_t1 {n} {
26 execsql { DELETE FROM t1 }
27 for {set i 1} {$i <= $n} {incr i} {
28 execsql { INSERT INTO t1 VALUES($i, $i, $i+2) }
32 # A DELETE while a cursor is reading the table.
34 do_test rtree8-1.1.1 {
35 execsql { PRAGMA page_size = 512 }
36 execsql { CREATE VIRTUAL TABLE t1 USING rtree_i32(id, x1, x2) }
39 do_test rtree8-1.1.2 {
42 db eval { SELECT * FROM t1 } {
44 if {$id==3} { db eval { DELETE FROM t1 WHERE id>3 } }
49 } {1 {database table is locked}}
50 do_test rtree8-1.1.2b {
51 db eval { SELECT * FROM t1 ORDER BY +id } {
52 if {$id==3} { db eval { DELETE FROM t1 WHERE id>3 } }
54 db eval {SELECT x1, x2 FROM t1}
56 do_test rtree8-1.1.3 {
57 execsql { SELECT * FROM t1 }
60 # Many SELECTs on the same small table.
62 proc nested_select {n} {
64 db eval { SELECT * FROM t1 } {
65 if {$id == $n} { nested_select [expr $n+1] }
69 do_test rtree8-1.2.1 { populate_t1 50 } {}
70 do_test rtree8-1.2.2 { nested_select 1 } {51}
72 # This test runs many SELECT queries simultaneously against a large
73 # table, causing a collision in the hash-table used to store r-tree
77 do_rtree_integrity_test rtree8-1.3.0 t1
78 do_execsql_test rtree8-1.3.1 { SELECT max(nodeno) FROM t1_node } {183}
79 do_test rtree8-1.3.2 {
80 set rowids [execsql {SELECT min(rowid) FROM t1_rowid GROUP BY nodeno}]
83 set stmt [sqlite3_prepare db "SELECT * FROM t1 WHERE id = $row" -1 tail]
85 lappend res_list [sqlite3_column_int $stmt 0]
86 lappend stmt_list $stmt
89 do_test rtree8-1.3.3 { set res_list } $rowids
90 do_execsql_test rtree8-1.3.4 { SELECT count(*) FROM t1 } {1500}
91 do_test rtree8-1.3.5 {
92 foreach stmt $stmt_list { sqlite3_finalize $stmt }
96 #-------------------------------------------------------------------------
97 # The following block of tests - rtree8-2.* - test a couple of database
98 # corruption cases. In this case things are not corrupted at the b-tree
99 # level, but the contents of the various tables used internally by an
100 # r-tree table are inconsistent.
103 do_execsql_test rtree8-2.1.1 { SELECT max(nodeno) FROM t1_node } {5}
104 sqlite3_db_config db DEFENSIVE 0
105 do_execsql_test rtree8-2.1.2 { DELETE FROM t1_node } {}
106 for {set i 1} {$i <= 50} {incr i} {
107 do_catchsql_test rtree8-2.1.3.$i {
108 SELECT * FROM t1 WHERE id = $i
109 } {1 {database disk image is malformed}}
111 do_catchsql_test rtree8-2.1.4 {
113 } {1 {database disk image is malformed}}
114 do_catchsql_test rtree8-2.1.5 {
116 } {1 {database disk image is malformed}}
118 do_execsql_test rtree8-2.1.6 {
120 CREATE VIRTUAL TABLE t1 USING rtree_i32(id, x1, x2);
125 sqlite3_db_config db DEFENSIVE 0
126 do_execsql_test rtree8-2.2.1 {
127 DELETE FROM t1_parent
129 do_catchsql_test rtree8-2.2.2 {
130 DELETE FROM t1 WHERE id=25
131 } {1 {database disk image is malformed}}
132 do_execsql_test rtree8-2.2.3 {
134 CREATE VIRTUAL TABLE t1 USING rtree_i32(id, x1, x2);
138 #-------------------------------------------------------------------------
139 # Test that trying to use the MATCH operator with the r-tree module does
143 do_catchsql_test rtree8-3.1 {
144 SELECT * FROM t1 WHERE x1 MATCH '1234'
145 } {1 {SQL logic error}}
147 #-------------------------------------------------------------------------
148 # Test a couple of invalid arguments to rtreedepth().
150 do_catchsql_test rtree8-4.1 {
151 SELECT rtreedepth('hello world')
152 } {1 {Invalid argument to rtreedepth()}}
153 do_catchsql_test rtree8-4.2 {
154 SELECT rtreedepth(X'00')
155 } {1 {Invalid argument to rtreedepth()}}
158 #-------------------------------------------------------------------------
159 # Delete half of a lopsided tree.
161 do_execsql_test rtree8-5.1 {
162 CREATE VIRTUAL TABLE t2 USING rtree_i32(id, x1, x2)
166 for {set i 0} {$i < 100} {incr i} {
167 execsql { INSERT INTO t2 VALUES($i, 100, 101) }
169 for {set i 100} {$i < 200} {incr i} {
170 execsql { INSERT INTO t2 VALUES($i, 1000, 1001) }
174 do_rtree_integrity_test rtree8-5.3 t2
177 for {set i 0} {$i < 200} {incr i} {
178 execsql { DELETE FROM t2 WHERE id = $i }
182 do_rtree_integrity_test rtree8-5.5 t2
185 # The following script caused an assertion fault and/or segfault
186 # prior to the fix that prevents simultaneous reads and writes on
187 # the same rtree virtual table.
193 PRAGMA page_size=512;
194 CREATE VIRTUAL TABLE t1 USING rtree(id,x1,x2,y1,y2);
195 WITH RECURSIVE c(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c WHERE x<49)
196 INSERT INTO t1 SELECT x, x, x+1, x, x+1 FROM c;
199 db eval {SELECT id FROM t1} x {
200 db eval {DELETE FROM t1 WHERE id=$x(id)}
204 } {1 {database table is locked}}