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 #***********************************************************************
15 if {![info exists testdir]} {
16 set testdir [file join [file dirname [info script]] .. .. test]
18 source $testdir/tester.tcl
19 set testprefix rtreecheck
26 proc swap_int32 {blob i0 i1} {
27 binary scan $blob I* L
38 proc set_int32 {blob idx val} {
39 binary scan $blob I* L
44 do_catchsql_test 1.0 {
46 } {1 {wrong number of arguments to function rtreecheck()}}
48 do_catchsql_test 1.1 {
49 SELECT rtreecheck(0,0,0);
50 } {1 {wrong number of arguments to function rtreecheck()}}
53 proc setup_simple_db {{module rtree}} {
55 db func swap_int32 swap_int32
57 CREATE VIRTUAL TABLE r1 USING $module (id, x1, x2, y1, y2);
58 INSERT INTO r1 VALUES(1, 5, 5, 5, 5); -- 3
59 INSERT INTO r1 VALUES(2, 6, 6, 6, 6); -- 9
60 INSERT INTO r1 VALUES(3, 7, 7, 7, 7); -- 15
61 INSERT INTO r1 VALUES(4, 8, 8, 8, 8); -- 21
62 INSERT INTO r1 VALUES(5, 9, 9, 9, 9); -- 27
64 sqlite3_db_config db DEFENSIVE 0
69 SELECT rtreecheck('r1')
73 UPDATE r1_node SET data = swap_int32(data, 3, 9);
74 UPDATE r1_node SET data = swap_int32(data, 23, 29);
78 SELECT rtreecheck('r1')
79 } {{Dimension 0 of cell 0 on node 1 is corrupt
80 Dimension 1 of cell 3 on node 1 is corrupt}}
81 do_execsql_test 2.3b {
82 PRAGMA integrity_check;
84 Dimension 0 of cell 0 on node 1 is corrupt
85 Dimension 1 of cell 3 on node 1 is corrupt}}
89 DELETE FROM r1_rowid WHERE rowid = 3;
90 SELECT rtreecheck('r1')
91 } {{Mapping (3 -> 1) missing from %_rowid table
92 Wrong number of entries in %_rowid table - expected 5, actual 4}}
93 do_execsql_test 2.4b {
94 PRAGMA integrity_check
96 Mapping (3 -> 1) missing from %_rowid table
97 Wrong number of entries in %_rowid table - expected 5, actual 4}}
100 do_execsql_test 2.5 {
101 UPDATE r1_rowid SET nodeno=2 WHERE rowid=3;
102 SELECT rtreecheck('r1')
103 } {{Found (3 -> 2) in %_rowid table, expected (3 -> 1)}}
104 do_execsql_test 2.5b {
105 PRAGMA integrity_check
106 } {{In RTree main.r1:
107 Found (3 -> 2) in %_rowid table, expected (3 -> 1)}}
110 do_execsql_test 3.0 {
111 CREATE VIRTUAL TABLE r1 USING rtree_i32(id, x1, x2);
112 INSERT INTO r1 VALUES(1, 0x7FFFFFFF*-1, 0x7FFFFFFF);
113 INSERT INTO r1 VALUES(2, 0x7FFFFFFF*-1, 5);
114 INSERT INTO r1 VALUES(3, -5, 5);
115 INSERT INTO r1 VALUES(4, 5, 0x11111111);
116 INSERT INTO r1 VALUES(5, 5, 0x00800000);
117 INSERT INTO r1 VALUES(6, 5, 0x00008000);
118 INSERT INTO r1 VALUES(7, 5, 0x00000080);
119 INSERT INTO r1 VALUES(8, 5, 0x40490fdb);
120 INSERT INTO r1 VALUES(9, 0x7f800000, 0x7f900000);
121 SELECT rtreecheck('r1');
122 PRAGMA integrity_check;
125 do_execsql_test 3.1 {
126 CREATE VIRTUAL TABLE r2 USING rtree_i32(id, x1, x2);
127 INSERT INTO r2 VALUES(2, -1*(1<<31), -1*(1<<31)+5);
128 SELECT rtreecheck('r2');
129 PRAGMA integrity_check;
132 sqlite3_db_config db DEFENSIVE 0
133 do_execsql_test 3.2 {
135 UPDATE r2_node SET data = X'123456';
136 SELECT rtreecheck('r2')!='ok';
139 do_execsql_test 3.3 {
141 UPDATE r2_node SET data = X'00001234';
142 SELECT rtreecheck('r2')!='ok';
144 do_execsql_test 3.4 {
145 PRAGMA integrity_check;
146 } {{In RTree main.r2:
147 Node 1 is too small for cell count of 4660 (4 bytes)
148 Wrong number of entries in %_rowid table - expected 0, actual 1}}
150 do_execsql_test 4.0 {
151 CREATE TABLE notanrtree(i);
152 SELECT rtreecheck('notanrtree');
153 } {{Schema corrupt or not an rtree}}
155 #-------------------------------------------------------------------------
158 db func set_int32 set_int32
159 do_execsql_test 5.0 {
160 CREATE VIRTUAL TABLE r3 USING rtree_i32(id, x1, x2, y1, y2);
162 SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<1000
164 INSERT INTO r3 SELECT i, i, i, i, i FROM x;
166 sqlite3_db_config db DEFENSIVE 0
167 do_execsql_test 5.1 {
169 UPDATE r3_node SET data = set_int32(data, 3, 5000);
170 UPDATE r3_node SET data = set_int32(data, 4, 5000);
171 SELECT rtreecheck('r3')=='ok'
173 do_execsql_test 5.2 {
176 UPDATE r3_node SET data = set_int32(data, 3, 0);
177 UPDATE r3_node SET data = set_int32(data, 4, 0);
178 SELECT rtreecheck('r3')=='ok'
181 #-------------------------------------------------------------------------
182 # dbsqlfuzz 4a1399d39bf9feccbf6b290da51d3b30103a4bf6
185 do_execsql_test 6.0 {
186 PRAGMA encoding = 'utf16';
187 CREATE VIRTUAL TABLE t1 USING rtree(id, x, y);
192 if {[permutation]=="inmemory_journal"} {
193 # This doesn't hit an SQLITE_LOCKED in this permutation as the schema
194 # has already been loaded.
195 do_catchsql_test 6.1.inmemory_journal {
196 SELECT ( 'elvis' IN(SELECT rtreecheck('t1')) ) FROM (SELECT 1) GROUP BY 1;
199 do_catchsql_test 6.1 {
200 SELECT ( 'elvis' IN(SELECT rtreecheck('t1')) ) FROM (SELECT 1) GROUP BY 1;
201 } {1 {database table is locked}}