1 # 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 # 14 15 if {![info exists testdir]} { 16 set testdir [file join [file dirname [info script]] .. .. test] 17 } 18 source [file join [file dirname [info script]] rtree_util.tcl] 19 source $testdir/tester.tcl 20 21 ifcapable !rtree { 22 finish_test 23 return 24 } 25 26 set ::NROW 1000 27 set ::NDEL 10 28 set ::NSELECT 100 29 30 if {[info exists G(isquick)] && $G(isquick)} { 31 set ::NROW 100 32 set ::NSELECT 10 33 } 34 35 foreach 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 ,] 59 #puts [rtree_treedump db t1] 60 #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 #} 71 } 72 } 73 74 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} 83 84 do_test rtree2-$module.$nDim.3 { 85 rtree_check db t1 86 } 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} 119 } 120 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 } 148 } 149 150 finish_test 151