Debian Codegraph @

ext/rtree/rtreecheck.test

raw
1 # 2017 August 17
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
15 if {![info exists testdir]} {
16 set testdir [file join [file dirname [info script]] .. .. test]
17 }
18 source $testdir/tester.tcl
19 set testprefix rtreecheck
20
21 ifcapable !rtree {
22 finish_test
23 return
24 }
25
26 proc swap_int32 {blob i0 i1} {
27 binary scan $blob I* L
28
29 set a [lindex $L $i0]
30 set b [lindex $L $i1]
31
32 lset L $i0 $b
33 lset L $i1 $a
34
35 binary format I* $L
36 }
37
38 proc set_int32 {blob idx val} {
39 binary scan $blob I* L
40 lset L $idx $val
41 binary format I* $L
42 }
43
44 do_catchsql_test 1.0 {
45 SELECT rtreecheck();
46 } {1 {wrong number of arguments to function rtreecheck()}}
47
48 do_catchsql_test 1.1 {
49 SELECT rtreecheck(0,0,0);
50 } {1 {wrong number of arguments to function rtreecheck()}}
51
52
53 proc setup_simple_db {{module rtree}} {
54 reset_db
55 db func swap_int32 swap_int32
56 execsql "
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
63 "
64 sqlite3_db_config db DEFENSIVE 0
65 }
66
67 setup_simple_db
68 do_execsql_test 2.1 {
69 SELECT rtreecheck('r1')
70 } {ok}
71
72 do_execsql_test 2.2 {
73 UPDATE r1_node SET data = swap_int32(data, 3, 9);
74 UPDATE r1_node SET data = swap_int32(data, 23, 29);
75 }
76
77 do_execsql_test 2.3 {
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;
83 } {{In RTree main.r1:
84 Dimension 0 of cell 0 on node 1 is corrupt
85 Dimension 1 of cell 3 on node 1 is corrupt}}
86
87 setup_simple_db
88 do_execsql_test 2.4 {
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
95 } {{In RTree main.r1:
96 Mapping (3 -> 1) missing from %_rowid table
97 Wrong number of entries in %_rowid table - expected 5, actual 4}}
98
99 setup_simple_db
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)}}
108
109 reset_db
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;
123 } {ok ok}
124
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;
130 } {ok ok}
131
132 sqlite3_db_config db DEFENSIVE 0
133 do_execsql_test 3.2 {
134 BEGIN;
135 UPDATE r2_node SET data = X'123456';
136 SELECT rtreecheck('r2')!='ok';
137 } {1}
138
139 do_execsql_test 3.3 {
140 ROLLBACK;
141 UPDATE r2_node SET data = X'00001234';
142 SELECT rtreecheck('r2')!='ok';
143 } {1}
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}}
149
150 do_execsql_test 4.0 {
151 CREATE TABLE notanrtree(i);
152 SELECT rtreecheck('notanrtree');
153 } {{Schema corrupt or not an rtree}}
154
155 #-------------------------------------------------------------------------
156 #
157 reset_db
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);
161 WITH x(i) AS (
162 SELECT 1 UNION ALL SELECT i+1 FROM x WHERE i<1000
163 )
164 INSERT INTO r3 SELECT i, i, i, i, i FROM x;
165 }
166 sqlite3_db_config db DEFENSIVE 0
167 do_execsql_test 5.1 {
168 BEGIN;
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'
172 } 0
173 do_execsql_test 5.2 {
174 ROLLBACK;
175 BEGIN;
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'
179 } 0
180
181 #-------------------------------------------------------------------------
182 # dbsqlfuzz 4a1399d39bf9feccbf6b290da51d3b30103a4bf6
183 #
184 reset_db
185 do_execsql_test 6.0 {
186 PRAGMA encoding = 'utf16';
187 CREATE VIRTUAL TABLE t1 USING rtree(id, x, y);
188 }
189 db close
190 sqlite3 db test.db
191
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;
197 } {0 0}
198 } else {
199 do_catchsql_test 6.1 {
200 SELECT ( 'elvis' IN(SELECT rtreecheck('t1')) ) FROM (SELECT 1) GROUP BY 1;
201 } {0 0}
202 }
203
204 finish_test