| 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 |
|
| 14 |
if {![info exists testdir]} { |
| 15 |
set testdir [file join [file dirname [info script]] .. .. test] |
| 16 |
} |
| 17 |
source [file join [file dirname [info script]] rtree_util.tcl] |
| 18 |
source $testdir/tester.tcl |
| 19 |
ifcapable !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 |
# |
| 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) } |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
# A DELETE while a cursor is reading the table. |
| 33 |
# |
| 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) } |
| 37 |
populate_t1 5 |
| 38 |
} {} |
| 39 |
do_test rtree8-1.1.2 { |
| 40 |
set res [list] |
| 41 |
set rc [catch { |
| 42 |
db eval { SELECT * FROM t1 } { |
| 43 |
lappend res $x1 $x2 |
| 44 |
if {$id==3} { db eval { DELETE FROM t1 WHERE id>3 } } |
| 45 |
} |
| 46 |
} msg]; |
| 47 |
lappend rc $msg |
| 48 |
set rc |
| 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 } } |
| 53 |
} |
| 54 |
db eval {SELECT x1, x2 FROM t1} |
| 55 |
} {1 3 2 4 3 5} |
| 56 |
do_test rtree8-1.1.3 { |
| 57 |
execsql { SELECT * FROM t1 } |
| 58 |
} {1 1 3 2 2 4 3 3 5} |
| 59 |
|
| 60 |
# Many SELECTs on the same small table. |
| 61 |
# |
| 62 |
proc nested_select {n} { |
| 63 |
set ::max $n |
| 64 |
db eval { SELECT * FROM t1 } { |
| 65 |
if {$id == $n} { nested_select [expr $n+1] } |
| 66 |
} |
| 67 |
return $::max |
| 68 |
} |
| 69 |
do_test rtree8-1.2.1 { populate_t1 50 } {} |
| 70 |
do_test rtree8-1.2.2 { nested_select 1 } {51} |
| 71 |
|
| 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 |
| 74 |
# nodes internally. |
| 75 |
# |
| 76 |
populate_t1 1500 |
| 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}] |
| 81 |
set stmt_list [list] |
| 82 |
foreach row $rowids { |
| 83 |
set stmt [sqlite3_prepare db "SELECT * FROM t1 WHERE id = $row" -1 tail] |
| 84 |
sqlite3_step $stmt |
| 85 |
lappend res_list [sqlite3_column_int $stmt 0] |
| 86 |
lappend stmt_list $stmt |
| 87 |
} |
| 88 |
} {} |
| 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 } |
| 93 |
} {} |
| 94 |
|
| 95 |
|
| 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. |
| 101 |
# |
| 102 |
populate_t1 50 |
| 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}} |
| 110 |
} |
| 111 |
do_catchsql_test rtree8-2.1.4 { |
| 112 |
SELECT * FROM t1 |
| 113 |
} {1 {database disk image is malformed}} |
| 114 |
do_catchsql_test rtree8-2.1.5 { |
| 115 |
DELETE FROM t1 |
| 116 |
} {1 {database disk image is malformed}} |
| 117 |
|
| 118 |
do_execsql_test rtree8-2.1.6 { |
| 119 |
DROP TABLE t1; |
| 120 |
CREATE VIRTUAL TABLE t1 USING rtree_i32(id, x1, x2); |
| 121 |
} {} |
| 122 |
|
| 123 |
|
| 124 |
populate_t1 50 |
| 125 |
sqlite3_db_config db DEFENSIVE 0 |
| 126 |
do_execsql_test rtree8-2.2.1 { |
| 127 |
DELETE FROM t1_parent |
| 128 |
} {} |
| 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 { |
| 133 |
DROP TABLE t1; |
| 134 |
CREATE VIRTUAL TABLE t1 USING rtree_i32(id, x1, x2); |
| 135 |
} {} |
| 136 |
|
| 137 |
|
| 138 |
#------------------------------------------------------------------------- |
| 139 |
# Test that trying to use the MATCH operator with the r-tree module does |
| 140 |
# not confuse it. |
| 141 |
# |
| 142 |
populate_t1 10 |
| 143 |
do_catchsql_test rtree8-3.1 { |
| 144 |
SELECT * FROM t1 WHERE x1 MATCH '1234' |
| 145 |
} {1 {SQL logic error}} |
| 146 |
|
| 147 |
#------------------------------------------------------------------------- |
| 148 |
# Test a couple of invalid arguments to rtreedepth(). |
| 149 |
# |
| 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()}} |
| 156 |
|
| 157 |
|
| 158 |
#------------------------------------------------------------------------- |
| 159 |
# Delete half of a lopsided tree. |
| 160 |
# |
| 161 |
do_execsql_test rtree8-5.1 { |
| 162 |
CREATE VIRTUAL TABLE t2 USING rtree_i32(id, x1, x2) |
| 163 |
} {} |
| 164 |
do_test rtree8-5.2 { |
| 165 |
execsql BEGIN |
| 166 |
for {set i 0} {$i < 100} {incr i} { |
| 167 |
execsql { INSERT INTO t2 VALUES($i, 100, 101) } |
| 168 |
} |
| 169 |
for {set i 100} {$i < 200} {incr i} { |
| 170 |
execsql { INSERT INTO t2 VALUES($i, 1000, 1001) } |
| 171 |
} |
| 172 |
execsql COMMIT |
| 173 |
} {} |
| 174 |
do_rtree_integrity_test rtree8-5.3 t2 |
| 175 |
do_test rtree8-5.4 { |
| 176 |
execsql BEGIN |
| 177 |
for {set i 0} {$i < 200} {incr i} { |
| 178 |
execsql { DELETE FROM t2 WHERE id = $i } |
| 179 |
} |
| 180 |
execsql COMMIT |
| 181 |
} {} |
| 182 |
do_rtree_integrity_test rtree8-5.5 t2 |
| 183 |
|
| 184 |
# 2018-05-24 |
| 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. |
| 188 |
# |
| 189 |
do_test rtree8-6.1 { |
| 190 |
db close |
| 191 |
sqlite3 db :memory: |
| 192 |
db eval { |
| 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; |
| 197 |
} |
| 198 |
set rc [catch { |
| 199 |
db eval {SELECT id FROM t1} x { |
| 200 |
db eval {DELETE FROM t1 WHERE id=$x(id)} |
| 201 |
} |
| 202 |
} msg] |
| 203 |
lappend rc $msg |
| 204 |
} {1 {database table is locked}} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
finish_test |