blob: f5d15cc6b935d1120df9e7c8e58a4a24acca5a65 [file] [log] [blame]
danielk1977ebaecc12008-05-26 18:41:54 +00001# 2008 Feb 19
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# The focus of this file is testing the r-tree extension.
13#
danielk1977ebaecc12008-05-26 18:41:54 +000014
danielk1977b13dee92008-06-23 15:55:52 +000015if {![info exists testdir]} {
dan897230e2010-08-26 14:15:37 +000016 set testdir [file join [file dirname [info script]] .. .. test]
danielk1977b13dee92008-06-23 15:55:52 +000017}
18source [file join [file dirname [info script]] rtree_util.tcl]
danielk1977ebaecc12008-05-26 18:41:54 +000019source $testdir/tester.tcl
danielk1977ebaecc12008-05-26 18:41:54 +000020
21ifcapable !rtree {
22 finish_test
23 return
24}
25
26set ::NROW 1000
27set ::NDEL 10
28set ::NSELECT 100
29
dan92e01aa2010-08-25 17:53:17 +000030if {[info exists G(isquick)] && $G(isquick)} {
danielk1977b13dee92008-06-23 15:55:52 +000031 set ::NROW 100
32 set ::NSELECT 10
33}
34
danielk19773ddb5a52008-07-14 15:37:00 +000035foreach module {rtree_i32 rtree} {
36 for {set nDim 1} {$nDim <= 5} {incr nDim} {
37
38 do_test rtree2-$module.$nDim.1 {
39 set cols [list]
40 foreach c [list c0 c1 c2 c3 c4 c5 c6 c7 c8 c9] {
41 lappend cols "$c REAL"
42 }
43 set cols [join [lrange $cols 0 [expr {$nDim*2-1}]] ", "]
44 execsql "
45 CREATE VIRTUAL TABLE t1 USING ${module}(ii, $cols);
46 CREATE TABLE t2 (ii, $cols);
47 "
48 } {}
49
50 do_test rtree2-$module.$nDim.2 {
51 db transaction {
52 for {set ii 0} {$ii < $::NROW} {incr ii} {
53 #puts "Row $ii"
54 set values [list]
55 for {set jj 0} {$jj<$nDim*2} {incr jj} {
56 lappend values [expr int(rand()*1000)]
57 }
58 set values [join $values ,]
drh4b4f7802008-05-27 00:06:02 +000059 #puts [rtree_treedump db t1]
danielk19773ddb5a52008-07-14 15:37:00 +000060 #puts "INSERT INTO t2 VALUES($ii, $values)"
61 set rc [catch {db eval "INSERT INTO t1 VALUES($ii, $values)"}]
62 if {$rc} {
63 incr ii -1
64 } else {
65 db eval "INSERT INTO t2 VALUES($ii, $values)"
66 }
67 #if {[rtree_check db t1]} {
68 #puts [rtree_treedump db t1]
69 #exit
70 #}
danielk1977ebaecc12008-05-26 18:41:54 +000071 }
danielk1977ebaecc12008-05-26 18:41:54 +000072 }
danielk19773ddb5a52008-07-14 15:37:00 +000073
danielk1977ebaecc12008-05-26 18:41:54 +000074 set t1 [execsql {SELECT * FROM t1 ORDER BY ii}]
75 set t2 [execsql {SELECT * FROM t2 ORDER BY ii}]
76 set rc [expr {$t1 eq $t2}]
77 if {$rc != 1} {
78 puts $t1
79 puts $t2
80 }
81 set rc
82 } {1}
danielk19773ddb5a52008-07-14 15:37:00 +000083
84 do_test rtree2-$module.$nDim.3 {
danielk1977ebaecc12008-05-26 18:41:54 +000085 rtree_check db t1
danielk19773ddb5a52008-07-14 15:37:00 +000086 } 0
87
88 set OPS [list < > <= >= =]
89 for {set ii 0} {$ii < $::NSELECT} {incr ii} {
90 do_test rtree2-$module.$nDim.4.$ii.1 {
91 set where [list]
92 foreach look_three_dots! {. . .} {
93 set colidx [expr int(rand()*($nDim*2+1))-1]
94 if {$colidx<0} {
95 set col ii
96 } else {
97 set col "c$colidx"
98 }
99 set op [lindex $OPS [expr int(rand()*[llength $OPS])]]
100 set val [expr int(rand()*1000)]
101 lappend where "$col $op $val"
102 }
103 set where [join $where " AND "]
104
105 set t1 [execsql "SELECT * FROM t1 WHERE $where ORDER BY ii"]
106 set t2 [execsql "SELECT * FROM t2 WHERE $where ORDER BY ii"]
107 set rc [expr {$t1 eq $t2}]
108 if {$rc != 1} {
109 #puts $where
110 puts $t1
111 puts $t2
112 #puts [rtree_treedump db t1]
113 #breakpoint
114 #set t1 [execsql "SELECT * FROM t1 WHERE $where ORDER BY ii"]
115 #exit
116 }
117 set rc
118 } {1}
danielk1977ebaecc12008-05-26 18:41:54 +0000119 }
danielk19773ddb5a52008-07-14 15:37:00 +0000120
121 for {set ii 0} {$ii < $::NROW} {incr ii $::NDEL} {
122 #puts [rtree_treedump db t1]
123 do_test rtree2-$module.$nDim.5.$ii.1 {
124 execsql "DELETE FROM t2 WHERE ii <= $::ii"
125 execsql "DELETE FROM t1 WHERE ii <= $::ii"
126
127 set t1 [execsql {SELECT * FROM t1 ORDER BY ii}]
128 set t2 [execsql {SELECT * FROM t2 ORDER BY ii}]
129 set rc [expr {$t1 eq $t2}]
130 if {$rc != 1} {
131 puts $t1
132 puts $t2
133 }
134 set rc
135 } {1}
136 do_test rtree2-$module.$nDim.5.$ii.2 {
137 rtree_check db t1
138 } {0}
139 }
140
141 do_test rtree2-$module.$nDim.6 {
142 execsql {
143 DROP TABLE t1;
144 DROP TABLE t2;
145 }
146 } {}
147 }
danielk1977ebaecc12008-05-26 18:41:54 +0000148}
149
150finish_test