1# 2010 February 16 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 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. 9# 10#*********************************************************************** 11# 12# 13 14if {![info exists testdir]} { 15 set testdir [file join [file dirname [info script]] .. .. test] 16} 17source [file join [file dirname [info script]] rtree_util.tcl] 18source $testdir/tester.tcl 19ifcapable !rtree { finish_test ; return } 20 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. 24# 25proc 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) } 29 } 30} 31 32# A DELETE while a cursor is reading the table. 33# 34do_test rtree8-1.1.1 { 35 execsql { PRAGMA page_size = 512 } 36 execsql { CREATE VIRTUAL TABLE t1 USING rtree_i32(id, x1, x2) } 37 populate_t1 5 38} {} 39do_test rtree8-1.1.2 { 40 set res [list] 41 db eval { SELECT * FROM t1 } { 42 lappend res $x1 $x2 43 if {$id==3} { db eval { DELETE FROM t1 WHERE id>3 } } 44 } 45 set res 46} {1 3 2 4 3 5} 47do_test rtree8-1.1.3 { 48 execsql { SELECT * FROM t1 } 49} {1 1 3 2 2 4 3 3 5} 50 51# Many SELECTs on the same small table. 52# 53proc nested_select {n} { 54 set ::max $n 55 db eval { SELECT * FROM t1 } { 56 if {$id == $n} { nested_select [expr $n+1] } 57 } 58 return $::max 59} 60do_test rtree8-1.2.1 { populate_t1 50 } {} 61do_test rtree8-1.2.2 { nested_select 1 } {51} 62 63# This test runs many SELECT queries simultaneously against a large 64# table, causing a collision in the hash-table used to store r-tree 65# nodes internally. 66# 67populate_t1 1500 68do_rtree_integrity_test rtree8-1.3.0 t1 69do_execsql_test rtree8-1.3.1 { SELECT max(nodeno) FROM t1_node } {164} 70do_test rtree8-1.3.2 { 71 set rowids [execsql {SELECT min(rowid) FROM t1_rowid GROUP BY nodeno}] 72 set stmt_list [list] 73 foreach row $rowids { 74 set stmt [sqlite3_prepare db "SELECT * FROM t1 WHERE id = $row" -1 tail] 75 sqlite3_step $stmt 76 lappend res_list [sqlite3_column_int $stmt 0] 77 lappend stmt_list $stmt 78 } 79} {} 80do_test rtree8-1.3.3 { set res_list } $rowids 81do_execsql_test rtree8-1.3.4 { SELECT count(*) FROM t1 } {1500} 82do_test rtree8-1.3.5 { 83 foreach stmt $stmt_list { sqlite3_finalize $stmt } 84} {} 85 86 87#------------------------------------------------------------------------- 88# The following block of tests - rtree8-2.* - test a couple of database 89# corruption cases. In this case things are not corrupted at the b-tree 90# level, but the contents of the various tables used internally by an 91# r-tree table are inconsistent. 92# 93populate_t1 50 94do_execsql_test rtree8-2.1.1 { SELECT max(nodeno) FROM t1_node } {5} 95do_execsql_test rtree8-2.1.2 { DELETE FROM t1_node } {} 96for {set i 1} {$i <= 50} {incr i} { 97 do_catchsql_test rtree8-2.1.3.$i { 98 SELECT * FROM t1 WHERE id = $i 99 } {1 {database disk image is malformed}} 100} 101do_catchsql_test rtree8-2.1.4 { 102 SELECT * FROM t1 103} {1 {database disk image is malformed}} 104do_catchsql_test rtree8-2.1.5 { 105 DELETE FROM t1 106} {1 {database disk image is malformed}} 107 108do_execsql_test rtree8-2.1.6 { 109 DROP TABLE t1; 110 CREATE VIRTUAL TABLE t1 USING rtree_i32(id, x1, x2); 111} {} 112 113 114populate_t1 50 115do_execsql_test rtree8-2.2.1 { 116 DELETE FROM t1_parent 117} {} 118do_catchsql_test rtree8-2.2.2 { 119 DELETE FROM t1 WHERE id=25 120} {1 {database disk image is malformed}} 121do_execsql_test rtree8-2.2.3 { 122 DROP TABLE t1; 123 CREATE VIRTUAL TABLE t1 USING rtree_i32(id, x1, x2); 124} {} 125 126 127#------------------------------------------------------------------------- 128# Test that trying to use the MATCH operator with the r-tree module does 129# not confuse it. 130# 131populate_t1 10 132do_catchsql_test rtree8-3.1 { 133 SELECT * FROM t1 WHERE x1 MATCH '1234' 134} {1 {SQL logic error}} 135 136#------------------------------------------------------------------------- 137# Test a couple of invalid arguments to rtreedepth(). 138# 139do_catchsql_test rtree8-4.1 { 140 SELECT rtreedepth('hello world') 141} {1 {Invalid argument to rtreedepth()}} 142do_catchsql_test rtree8-4.2 { 143 SELECT rtreedepth(X'00') 144} {1 {Invalid argument to rtreedepth()}} 145 146 147#------------------------------------------------------------------------- 148# Delete half of a lopsided tree. 149# 150do_execsql_test rtree8-5.1 { 151 CREATE VIRTUAL TABLE t2 USING rtree_i32(id, x1, x2) 152} {} 153do_test rtree8-5.2 { 154 execsql BEGIN 155 for {set i 0} {$i < 100} {incr i} { 156 execsql { INSERT INTO t2 VALUES($i, 100, 101) } 157 } 158 for {set i 100} {$i < 200} {incr i} { 159 execsql { INSERT INTO t2 VALUES($i, 1000, 1001) } 160 } 161 execsql COMMIT 162} {} 163do_rtree_integrity_test rtree8-5.3 t2 164do_test rtree8-5.4 { 165 execsql BEGIN 166 for {set i 0} {$i < 200} {incr i} { 167 execsql { DELETE FROM t2 WHERE id = $i } 168 } 169 execsql COMMIT 170} {} 171do_rtree_integrity_test rtree8-5.5 t2 172 173 174finish_test 175