rtree4.test 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # 2008 May 23
  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. # Randomized test cases for the rtree extension.
  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 {
  20. finish_test
  21. return
  22. }
  23. set ::NROW 2500
  24. if {[info exists G(isquick)] && $G(isquick)} {
  25. set ::NROW 250
  26. }
  27. ifcapable !rtree_int_only {
  28. # Return a floating point number between -X and X.
  29. #
  30. proc rand {X} {
  31. return [expr {int((rand()-0.5)*1024.0*$X)/512.0}]
  32. }
  33. # Return a positive floating point number less than or equal to X
  34. #
  35. proc randincr {X} {
  36. while 1 {
  37. set r [expr {int(rand()*$X*32.0)/32.0}]
  38. if {$r>0.0} {return $r}
  39. }
  40. }
  41. } else {
  42. # For rtree_int_only, return an number between -X and X.
  43. #
  44. proc rand {X} {
  45. return [expr {int((rand()-0.5)*2*$X)}]
  46. }
  47. # Return a positive integer less than or equal to X
  48. #
  49. proc randincr {X} {
  50. while 1 {
  51. set r [expr {int(rand()*$X)+1}]
  52. if {$r>0} {return $r}
  53. }
  54. }
  55. }
  56. # Scramble the $inlist into a random order.
  57. #
  58. proc scramble {inlist} {
  59. set y {}
  60. foreach x $inlist {
  61. lappend y [list [expr {rand()}] $x]
  62. }
  63. set y [lsort $y]
  64. set outlist {}
  65. foreach x $y {
  66. lappend outlist [lindex $x 1]
  67. }
  68. return $outlist
  69. }
  70. # Always use the same random seed so that the sequence of tests
  71. # is repeatable.
  72. #
  73. expr {srand(1234)}
  74. # Run these tests for all number of dimensions between 1 and 5.
  75. #
  76. for {set nDim 1} {$nDim<=5} {incr nDim} {
  77. # Construct an rtree virtual table and an ordinary btree table
  78. # to mirror it. The ordinary table should be much slower (since
  79. # it has to do a full table scan) but should give the exact same
  80. # answers.
  81. #
  82. do_test rtree4-$nDim.1 {
  83. set clist {}
  84. set cklist {}
  85. for {set i 0} {$i<$nDim} {incr i} {
  86. lappend clist mn$i mx$i
  87. lappend cklist "mn$i<mx$i"
  88. }
  89. db eval "DROP TABLE IF EXISTS rx"
  90. db eval "DROP TABLE IF EXISTS bx"
  91. db eval "CREATE VIRTUAL TABLE rx USING rtree(id, [join $clist ,])"
  92. db eval "CREATE TABLE bx(id INTEGER PRIMARY KEY,\
  93. [join $clist ,], CHECK( [join $cklist { AND }] ))"
  94. } {}
  95. # Do many insertions of small objects. Do both overlapping and
  96. # contained-within queries after each insert to verify that all
  97. # is well.
  98. #
  99. unset -nocomplain where
  100. for {set i 1} {$i<$::NROW} {incr i} {
  101. # Do a random insert
  102. #
  103. do_test rtree4-$nDim.2.$i.1 {
  104. set vlist {}
  105. for {set j 0} {$j<$nDim} {incr j} {
  106. set mn [rand 10000]
  107. set mx [expr {$mn+[randincr 50]}]
  108. lappend vlist $mn $mx
  109. }
  110. db eval "INSERT INTO rx VALUES(NULL, [join $vlist ,])"
  111. db eval "INSERT INTO bx VALUES(NULL, [join $vlist ,])"
  112. } {}
  113. # Do a contained-in query on all dimensions
  114. #
  115. set where {}
  116. for {set j 0} {$j<$nDim} {incr j} {
  117. set mn [rand 10000]
  118. set mx [expr {$mn+[randincr 500]}]
  119. lappend where mn$j>=$mn mx$j<=$mx
  120. }
  121. set where "WHERE [join $where { AND }]"
  122. do_test rtree4-$nDim.2.$i.2 {
  123. list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
  124. } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
  125. # Do an overlaps query on all dimensions
  126. #
  127. set where {}
  128. for {set j 0} {$j<$nDim} {incr j} {
  129. set mn [rand 10000]
  130. set mx [expr {$mn+[randincr 500]}]
  131. lappend where mx$j>=$mn mn$j<=$mx
  132. }
  133. set where "WHERE [join $where { AND }]"
  134. do_test rtree4-$nDim.2.$i.3 {
  135. list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
  136. } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
  137. # Do a contained-in query with surplus contraints at the beginning.
  138. # This should force a full-table scan on the rtree.
  139. #
  140. set where {}
  141. for {set j 0} {$j<$nDim} {incr j} {
  142. lappend where mn$j>-10000 mx$j<10000
  143. }
  144. for {set j 0} {$j<$nDim} {incr j} {
  145. set mn [rand 10000]
  146. set mx [expr {$mn+[randincr 500]}]
  147. lappend where mn$j>=$mn mx$j<=$mx
  148. }
  149. set where "WHERE [join $where { AND }]"
  150. do_test rtree4-$nDim.2.$i.3 {
  151. list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
  152. } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
  153. # Do an overlaps query with surplus contraints at the beginning.
  154. # This should force a full-table scan on the rtree.
  155. #
  156. set where {}
  157. for {set j 0} {$j<$nDim} {incr j} {
  158. lappend where mn$j>=-10000 mx$j<=10000
  159. }
  160. for {set j 0} {$j<$nDim} {incr j} {
  161. set mn [rand 10000]
  162. set mx [expr {$mn+[randincr 500]}]
  163. lappend where mx$j>$mn mn$j<$mx
  164. }
  165. set where "WHERE [join $where { AND }]"
  166. do_test rtree4-$nDim.2.$i.4 {
  167. list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
  168. } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
  169. # Do a contained-in query with surplus contraints at the end
  170. #
  171. set where {}
  172. for {set j 0} {$j<$nDim} {incr j} {
  173. set mn [rand 10000]
  174. set mx [expr {$mn+[randincr 500]}]
  175. lappend where mn$j>=$mn mx$j<$mx
  176. }
  177. for {set j [expr {$nDim-1}]} {$j>=0} {incr j -1} {
  178. lappend where mn$j>=-10000 mx$j<10000
  179. }
  180. set where "WHERE [join $where { AND }]"
  181. do_test rtree4-$nDim.2.$i.5 {
  182. list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
  183. } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
  184. # Do an overlaps query with surplus contraints at the end
  185. #
  186. set where {}
  187. for {set j [expr {$nDim-1}]} {$j>=0} {incr j -1} {
  188. set mn [rand 10000]
  189. set mx [expr {$mn+[randincr 500]}]
  190. lappend where mx$j>$mn mn$j<=$mx
  191. }
  192. for {set j 0} {$j<$nDim} {incr j} {
  193. lappend where mx$j>-10000 mn$j<=10000
  194. }
  195. set where "WHERE [join $where { AND }]"
  196. do_test rtree4-$nDim.2.$i.6 {
  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"]]
  199. # Do a contained-in query with surplus contraints where the
  200. # constraints appear in a random order.
  201. #
  202. set where {}
  203. for {set j 0} {$j<$nDim} {incr j} {
  204. set mn1 [rand 10000]
  205. set mn2 [expr {$mn1+[randincr 100]}]
  206. set mx1 [expr {$mn2+[randincr 400]}]
  207. set mx2 [expr {$mx1+[randincr 100]}]
  208. lappend where mn$j>=$mn1 mn$j>$mn2 mx$j<$mx1 mx$j<=$mx2
  209. }
  210. set where "WHERE [join [scramble $where] { AND }]"
  211. do_test rtree4-$nDim.2.$i.7 {
  212. list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
  213. } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
  214. # Do an overlaps query with surplus contraints where the
  215. # constraints appear in a random order.
  216. #
  217. set where {}
  218. for {set j 0} {$j<$nDim} {incr j} {
  219. set mn1 [rand 10000]
  220. set mn2 [expr {$mn1+[randincr 100]}]
  221. set mx1 [expr {$mn2+[randincr 400]}]
  222. set mx2 [expr {$mx1+[randincr 100]}]
  223. lappend where mx$j>=$mn1 mx$j>$mn2 mn$j<$mx1 mn$j<=$mx2
  224. }
  225. set where "WHERE [join [scramble $where] { AND }]"
  226. do_test rtree4-$nDim.2.$i.8 {
  227. list $where [db eval "SELECT id FROM rx $where ORDER BY id"]
  228. } [list $where [db eval "SELECT id FROM bx $where ORDER BY id"]]
  229. }
  230. do_rtree_integrity_test rtree4-$nDim.3 rx
  231. }
  232. expand_all_sql db
  233. finish_test