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 #***********************************************************************
12 # Randomized test cases for the rtree extension.
15 if {![info exists testdir]} {
16 set testdir [file join [file dirname [info script]] .. .. test]
18 source $testdir/tester.tcl
26 if {[info exists G(isquick)] && $G(isquick)} {
30 ifcapable !rtree_int_only {
31 # Return a floating point number between -X and X.
34 return [expr {int((rand()-0.5)*1024.0*$X)/512.0}]
37 # Return a positive floating point number less than or equal to X
41 set r [expr {int(rand()*$X*32.0)/32.0}]
42 if {$r>0.0} {return $r}
46 # For rtree_int_only, return an number between -X and X.
49 return [expr {int((rand()-0.5)*2*$X)}]
52 # Return a positive integer less than or equal to X
56 set r [expr {int(rand()*$X)+1}]
62 # Scramble the $inlist into a random order.
64 proc scramble {inlist} {
67 lappend y [list [expr {rand()}] $x]
72 lappend outlist [lindex $x 1]
77 # Always use the same random seed so that the sequence of tests
82 # Run these tests for all number of dimensions between 1 and 5.
84 for {set nDim 1} {$nDim<=5} {incr nDim} {
86 # Construct an rtree virtual table and an ordinary btree table
87 # to mirror it. The ordinary table should be much slower (since
88 # it has to do a full table scan) but should give the exact same
91 do_test rtree4-$nDim.1 {
94 for {set i 0} {$i<$nDim} {incr i} {
95 lappend clist mn$i mx$i
96 lappend cklist "mn$i<mx$i"
98 db eval "DROP TABLE IF EXISTS rx"
99 db eval "DROP TABLE IF EXISTS bx"
100 db eval "CREATE VIRTUAL TABLE rx USING rtree(id, [join $clist ,])"
101 db eval "CREATE TABLE bx(id INTEGER PRIMARY KEY,\
102 [join $clist ,], CHECK( [join $cklist { AND }] ))"
105 # Do many insertions of small objects. Do both overlapping and
106 # contained-within queries after each insert to verify that all
109 unset -nocomplain where
110 for {set i 1} {$i<$::NROW} {incr i} {
113 do_test rtree4-$nDim.2.$i.1 {
115 for {set j 0} {$j<$nDim} {incr j} {
117 set mx [expr {$mn+[randincr 50]}]
118 lappend vlist $mn $mx
120 db eval "INSERT INTO rx VALUES(NULL, [join $vlist ,])"
121 db eval "INSERT INTO bx VALUES(NULL, [join $vlist ,])"
124 # Do a contained-in query on all dimensions
127 for {set j 0} {$j<$nDim} {incr j} {
129 set mx [expr {$mn+[randincr 500]}]
130 lappend where mn$j>=$mn mx$j<=$mx
132 set where "WHERE [join $where { AND }]"
133 do_test rtree4-$nDim.2.$i.2 {
134 list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
135 } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
137 # Do an overlaps query on all dimensions
140 for {set j 0} {$j<$nDim} {incr j} {
142 set mx [expr {$mn+[randincr 500]}]
143 lappend where mx$j>=$mn mn$j<=$mx
145 set where "WHERE [join $where { AND }]"
146 do_test rtree4-$nDim.2.$i.3 {
147 list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
148 } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
150 # Do a contained-in query with surplus contraints at the beginning.
151 # This should force a full-table scan on the rtree.
154 for {set j 0} {$j<$nDim} {incr j} {
155 lappend where mn$j>-10000 mx$j<10000
157 for {set j 0} {$j<$nDim} {incr j} {
159 set mx [expr {$mn+[randincr 500]}]
160 lappend where mn$j>=$mn mx$j<=$mx
162 set where "WHERE [join $where { AND }]"
163 do_test rtree4-$nDim.2.$i.3 {
164 list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
165 } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
167 # Do an overlaps query with surplus contraints at the beginning.
168 # This should force a full-table scan on the rtree.
171 for {set j 0} {$j<$nDim} {incr j} {
172 lappend where mn$j>=-10000 mx$j<=10000
174 for {set j 0} {$j<$nDim} {incr j} {
176 set mx [expr {$mn+[randincr 500]}]
177 lappend where mx$j>$mn mn$j<$mx
179 set where "WHERE [join $where { AND }]"
180 do_test rtree4-$nDim.2.$i.4 {
181 list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
182 } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
184 # Do a contained-in query with surplus contraints at the end
187 for {set j 0} {$j<$nDim} {incr j} {
189 set mx [expr {$mn+[randincr 500]}]
190 lappend where mn$j>=$mn mx$j<$mx
192 for {set j [expr {$nDim-1}]} {$j>=0} {incr j -1} {
193 lappend where mn$j>=-10000 mx$j<10000
195 set where "WHERE [join $where { AND }]"
196 do_test rtree4-$nDim.2.$i.5 {
197 list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
198 } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
200 # Do an overlaps query with surplus contraints at the end
203 for {set j [expr {$nDim-1}]} {$j>=0} {incr j -1} {
205 set mx [expr {$mn+[randincr 500]}]
206 lappend where mx$j>$mn mn$j<=$mx
208 for {set j 0} {$j<$nDim} {incr j} {
209 lappend where mx$j>-10000 mn$j<=10000
211 set where "WHERE [join $where { AND }]"
212 do_test rtree4-$nDim.2.$i.6 {
213 list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
214 } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
216 # Do a contained-in query with surplus contraints where the
217 # constraints appear in a random order.
220 for {set j 0} {$j<$nDim} {incr j} {
222 set mn2 [expr {$mn1+[randincr 100]}]
223 set mx1 [expr {$mn2+[randincr 400]}]
224 set mx2 [expr {$mx1+[randincr 100]}]
225 lappend where mn$j>=$mn1 mn$j>$mn2 mx$j<$mx1 mx$j<=$mx2
227 set where "WHERE [join [scramble $where] { AND }]"
228 do_test rtree4-$nDim.2.$i.7 {
229 list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
230 } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
232 # Do an overlaps query with surplus contraints where the
233 # constraints appear in a random order.
236 for {set j 0} {$j<$nDim} {incr j} {
238 set mn2 [expr {$mn1+[randincr 100]}]
239 set mx1 [expr {$mn2+[randincr 400]}]
240 set mx2 [expr {$mx1+[randincr 100]}]
241 lappend where mx$j>=$mn1 mx$j>$mn2 mn$j<$mx1 mn$j<=$mx2
243 set where "WHERE [join [scramble $where] { AND }]"
244 do_test rtree4-$nDim.2.$i.8 {
245 list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
246 } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]