Merge branch 'prerelease' of ssh://git.zetetic.net/sqlcipher into prerelease
[sqlcipher.git] / test / boundary3.tcl
blob5fc26d6113f087f50cf783244c41764417173a23
1 puts {# 2008 December 11
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 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file is automatically generated from a separate TCL script.
14 # This file seeks to exercise integer boundary values.
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Many of the boundary tests depend on a working 64-bit implementation.
21 if {![working_64bit_int]} { finish_test; return }
24 expr srand(0)
26 # Generate interesting boundary numbers
28 foreach x {
31 0x7f
32 0x7fff
33 0x7fffff
34 0x7fffffff
35 0x7fffffffff
36 0x7fffffffffff
37 0x7fffffffffffff
38 0x7fffffffffffffff
39 } {
40 set x [expr {wide($x)}]
41 set boundarynum($x) 1
42 set boundarynum([expr {wide($x+1)}]) 1
43 set boundarynum([expr {wide(-($x+1))}]) 1
44 set boundarynum([expr {wide(-($x+2))}]) 1
45 set boundarynum([expr {wide($x+$x+1)}]) 1
46 set boundarynum([expr {wide($x+$x+2)}]) 1
48 set x [expr {wide(127)}]
49 for {set i 1} {$i<=9} {incr i} {
50 set boundarynum($x) 1
51 set boundarynum([expr {wide($x+1)}]) 1
52 set x [expr {wide($x*128 + 127)}]
55 # Scramble the $inlist into a random order.
57 proc scramble {inlist} {
58 set y {}
59 foreach x $inlist {
60 lappend y [list [expr {rand()}] $x]
62 set y [lsort $y]
63 set outlist {}
64 foreach x $y {
65 lappend outlist [lindex $x 1]
67 return $outlist
70 # A simple selection sort. Not trying to be efficient.
72 proc sort {inlist} {
73 set outlist {}
74 set mn [lindex $inlist 0]
75 foreach x $inlist {
76 if {$x<$mn} {set mn $x}
78 set outlist $mn
79 set mx $mn
80 while {1} {
81 set valid 0
82 foreach x $inlist {
83 if {$x>$mx && (!$valid || $mn>$x)} {
84 set mn $x
85 set valid 1
88 if {!$valid} break
89 lappend outlist $mn
90 set mx $mn
92 return $outlist
95 # Reverse the order of a list
97 proc reverse {inlist} {
98 set i [llength $inlist]
99 set outlist {}
100 for {incr i -1} {$i>=0} {incr i -1} {
101 lappend outlist [lindex $inlist $i]
103 return $outlist
106 set nums1 [scramble [array names boundarynum]]
107 set nums2 [scramble [array names boundarynum]]
109 set tname boundary3
110 puts "do_test $tname-1.1 \173"
111 puts " db eval \173"
112 puts " CREATE TABLE t1(a,x);"
113 set a 0
114 foreach r $nums1 {
115 incr a
116 set t1ra($r) $a
117 set t1ar($a) $r
118 set x [format %016x [expr {wide($r)}]]
119 set t1rx($r) $x
120 set t1xr($x) $r
121 puts " INSERT INTO t1(oid,a,x) VALUES($r,$a,'$x');"
123 puts " CREATE INDEX t1i1 ON t1(a);"
124 puts " CREATE INDEX t1i2 ON t1(x);"
125 puts " \175"
126 puts "\175 {}"
128 puts "do_test $tname-1.2 \173"
129 puts " db eval \173"
130 puts " SELECT count(*) FROM t1"
131 puts " \175"
132 puts "\175 {64}"
134 puts "do_test $tname-1.3 \173"
135 puts " db eval \173"
136 puts " CREATE TABLE t2(r,a);"
137 puts " INSERT INTO t2 SELECT rowid, a FROM t1;"
138 puts " CREATE INDEX t2i1 ON t2(r);"
139 puts " CREATE INDEX t2i2 ON t2(a);"
140 puts " INSERT INTO t2 VALUES(9.22337303685477580800e+18,65);"
141 set t1ra(9.22337303685477580800e+18) 65
142 set t1ar(65) 9.22337303685477580800e+18)
143 puts " INSERT INTO t2 VALUES(-9.22337303685477580800e+18,66);"
144 set t1ra(-9.22337303685477580800e+18) 66
145 set t1ar(66) -9.22337303685477580800e+18)
146 puts " SELECT count(*) FROM t2;"
147 puts " \175"
148 puts "\175 {66}"
150 set nums3 $nums2
151 lappend nums3 9.22337303685477580800e+18
152 lappend nums3 -9.22337303685477580800e+18
154 set i 0
155 foreach r $nums3 {
156 incr i
158 set r5 $r.5
159 set r0 $r.0
160 if {abs($r)<0x7FFFFFFFFFFFFFFF || $r==-9223372036854775808} {
161 set x $t1rx($r)
162 set a $t1ra($r)
163 puts "do_test $tname-2.$i.1 \173"
164 puts " db eval \173"
165 puts " SELECT t1.* FROM t1, t2 WHERE t1.rowid=$r AND t2.a=t1.a"
166 puts " \175"
167 puts "\175 {$a $x}"
168 puts "do_test $tname-2.$i.2 \173"
169 puts " db eval \173"
170 puts " SELECT t2.* FROM t1 JOIN t2 USING(a) WHERE x='$x'"
171 puts " \175"
172 puts "\175 {$r $a}"
173 puts "do_test $tname-2.$i.3 \173"
174 puts " db eval \173"
175 puts " SELECT t1.rowid, x FROM t1 JOIN t2 ON t2.r=t1.rowid WHERE t2.a=$a"
176 puts " \175"
177 puts "\175 {$r $x}"
180 foreach op {> >= < <=} subno {gt ge lt le} {
182 ################################################################ 2.x.y.1
183 set rset {}
184 set aset {}
185 foreach rx $nums2 {
186 if "\$rx $op \$r" {
187 lappend rset $rx
188 lappend aset $t1ra($rx)
191 puts "do_test $tname-2.$i.$subno.1 \173"
192 puts " db eval \173"
193 puts " SELECT t2.a FROM t1 JOIN t2 USING(a)"
194 puts " WHERE t1.rowid $op $r ORDER BY t2.a"
195 puts " \175"
196 puts "\175 {[sort $aset]}"
198 ################################################################ 2.x.y.2
199 puts "do_test $tname-2.$i.$subno.2 \173"
200 puts " db eval \173"
201 puts " SELECT t2.a FROM t2 NATURAL JOIN t1"
202 puts " WHERE t1.rowid $op $r ORDER BY t1.a DESC"
203 puts " \175"
204 puts "\175 {[reverse [sort $aset]]}"
207 ################################################################ 2.x.y.3
208 set ax $t1ra($r)
209 set aset {}
210 foreach rx [sort $rset] {
211 lappend aset $t1ra($rx)
213 puts "do_test $tname-2.$i.$subno.3 \173"
214 puts " db eval \173"
215 puts " SELECT t1.a FROM t1 JOIN t2 ON t1.rowid $op t2.r"
216 puts " WHERE t2.a=$ax"
217 puts " ORDER BY t1.rowid"
218 puts " \175"
219 puts "\175 {$aset}"
221 ################################################################ 2.x.y.4
222 set aset {}
223 foreach rx [reverse [sort $rset]] {
224 lappend aset $t1ra($rx)
226 puts "do_test $tname-2.$i.$subno.4 \173"
227 puts " db eval \173"
228 puts " SELECT t1.a FROM t1 JOIN t2 ON t1.rowid $op t2.r"
229 puts " WHERE t2.a=$ax"
230 puts " ORDER BY t1.rowid DESC"
231 puts " \175"
232 puts "\175 {$aset}"
234 ################################################################ 2.x.y.5
235 set aset {}
236 set xset {}
237 foreach rx $rset {
238 lappend xset $t1rx($rx)
240 foreach x [sort $xset] {
241 set rx $t1xr($x)
242 lappend aset $t1ra($rx)
244 puts "do_test $tname-2.$i.$subno.5 \173"
245 puts " db eval \173"
246 puts " SELECT t1.a FROM t1 JOIN t2 ON t1.rowid $op t2.r"
247 puts " WHERE t2.a=$ax"
248 puts " ORDER BY x"
249 puts " \175"
250 puts "\175 {$aset}"
252 ################################################################ 2.x.y.10
253 if {[string length $r5]>15} continue
254 set rset {}
255 set aset {}
256 foreach rx $nums2 {
257 if "\$rx $op \$r0" {
258 lappend rset $rx
261 foreach rx [sort $rset] {
262 lappend aset $t1ra($rx)
264 puts "do_test $tname-2.$i.$subno.10 \173"
265 puts " db eval \173"
266 puts " SELECT t1.a FROM t1 JOIN t2 ON t1.rowid $op CAST(t2.r AS real)"
267 puts " WHERE t2.a=$ax"
268 puts " ORDER BY t1.rowid"
269 puts " \175"
270 puts "\175 {$aset}"
272 ################################################################ 2.x.y.11
273 set aset {}
274 foreach rx [reverse [sort $rset]] {
275 lappend aset $t1ra($rx)
277 puts "do_test $tname-2.$i.$subno.11 \173"
278 puts " db eval \173"
279 puts " SELECT t1.a FROM t1 JOIN t2 ON t1.rowid $op CAST(t2.r AS real)"
280 puts " WHERE t2.a=$ax"
281 puts " ORDER BY t1.rowid DESC"
282 puts " \175"
283 puts "\175 {$aset}"
289 puts {finish_test}