1puts {# 2008 December 11
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# This file implements regression tests for SQLite library.
12#
13# This file is automatically generated from a separate TCL script.
14# This file seeks to exercise integer boundary values.
15#
16# $Id: boundary4.tcl,v 1.3 2009/01/02 15:45:48 shane Exp $
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# Many of the boundary tests depend on a working 64-bit implementation.
22if {![working_64bit_int]} { finish_test; return }
23ifcapable !altertable     { finish_test; return }
24}
25
26expr srand(0)
27
28# Generate interesting boundary numbers
29#
30foreach 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 {$x+1}]) 1
43  set boundarynum([expr {-($x+1)}]) 1
44  set boundarynum([expr {-($x+2)}]) 1
45  set boundarynum([expr {$x+$x+1}]) 1
46  set boundarynum([expr {$x+$x+2}]) 1
47}
48set x [expr {wide(127)}]
49for {set i 127} {$i<=9} {incr i} {
50  set boundarynum($x) 1
51  set boundarynum([expr {$x+1}]) 1
52  set x [expr {wide($x*128 + 127)}]
53}
54
55# Scramble the $inlist into a random order.
56#
57proc scramble {inlist} {
58  set y {}
59  foreach x $inlist {
60    lappend y [list [expr {rand()}] $x]
61  }
62  set y [lsort $y]
63  set outlist {}
64  foreach x $y {
65    lappend outlist [lindex $x 1]
66  }
67  return $outlist
68}
69
70# A simple selection sort.  Not trying to be efficient.
71#
72proc sort {inlist} {
73  set outlist {}
74  set mn [lindex $inlist 0]
75  foreach x $inlist {
76    if {$x<$mn} {set mn $x}
77  }
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
86      }
87    }
88    if {!$valid} break
89    lappend outlist $mn
90    set mx $mn
91  }
92  return $outlist
93}
94
95# Reverse the order of a list
96#
97proc 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]
102  }
103  return $outlist
104}
105
106set nums1 [scramble [array names boundarynum]]
107set nums2 [scramble [array names boundarynum]]
108
109set tname boundary4
110puts "do_test $tname-1.1 \173"
111puts "  db eval \173"
112puts "    CREATE TABLE t1(a,x);"
113set a 0
114set all_rowid {}
115set all_a {}
116set all_x {}
117foreach r $nums1 {
118  incr a
119  set t1ra($r) $a
120  set t1ar($a) $r
121  set x [format %08x%08x [expr {wide($r)>>32}] $r]
122  set t1rx($r) $x
123  set t1xr($x) $r
124  puts "    INSERT INTO t1(oid,a,x) VALUES($r,$a,'$x');"
125  lappend all_rowid $r
126  lappend all_a $a
127  lappend all_x $x
128}
129puts "    CREATE INDEX t1i1 ON t1(a);"
130puts "    CREATE INDEX t1i2 ON t1(x);"
131puts "  \175"
132puts "\175 {}"
133
134puts "do_test $tname-1.2 \173"
135puts "  db eval \173"
136puts "    SELECT count(*) FROM t1"
137puts "  \175"
138puts "\175 {[llength $nums1]}"
139
140proc maketest {tnum sql answer} {
141  puts "do_test $::tname-$tnum \173"
142  puts "  db eval \173"
143  puts "    $sql"
144  puts "  \175"
145  puts "\175 {$answer}"
146}
147
148set ans {}
149foreach r [sort $all_rowid] {
150  lappend ans $r $t1ra($r) $t1rx($r)
151}
152maketest 1.3 {SELECT rowid, a, x FROM t1 ORDER BY +rowid} $ans
153maketest 1.4 {SELECT rowid, a, x FROM t1 ORDER BY rowid} $ans
154
155set ans {}
156foreach r [reverse [sort $all_rowid]] {
157  lappend ans $r $t1ra($r) $t1rx($r)
158}
159maketest 1.5 {SELECT rowid, a, x FROM t1 ORDER BY +rowid DESC} $ans
160maketest 1.6 {SELECT rowid, a, x FROM t1 ORDER BY rowid DESC} $ans
161
162set ans {}
163foreach a [sort $all_a] {
164  set r $t1ar($a)
165  lappend ans $r $a $t1rx($r)
166}
167maketest 1.7 {SELECT rowid, a, x FROM t1 ORDER BY +a} $ans
168maketest 1.8 {SELECT rowid, a, x FROM t1 ORDER BY a} $ans
169
170set ans {}
171foreach a [reverse [sort $all_a]] {
172  set r $t1ar($a)
173  lappend ans $r $a $t1rx($r)
174}
175maketest 1.9 {SELECT rowid, a, x FROM t1 ORDER BY +a DESC} $ans
176maketest 1.10 {SELECT rowid, a, x FROM t1 ORDER BY a DESC} $ans
177
178set ans {}
179foreach x [sort $all_x] {
180  set r $t1xr($x)
181  lappend ans $r $t1ra($r) $x
182}
183maketest 1.11 {SELECT rowid, a, x FROM t1 ORDER BY +x} $ans
184maketest 1.12 {SELECT rowid, a, x FROM t1 ORDER BY x} $ans
185
186set ans {}
187foreach x [reverse [sort $all_x]] {
188  set r $t1xr($x)
189  lappend ans $r $t1ra($r) $x
190}
191maketest 1.13 {SELECT rowid, a, x FROM t1 ORDER BY +x DESC} $ans
192maketest 1.14 {SELECT rowid, a, x FROM t1 ORDER BY x DESC} $ans
193
194maketest 2.1 {UPDATE t1 SET rowid=a, a=rowid} {}
195
196set ans {}
197foreach r [sort $all_rowid] {
198  lappend ans $r $t1ra($r) $t1rx($r)
199}
200maketest 2.3 {SELECT a, rowid, x FROM t1 ORDER BY +a} $ans
201maketest 2.4 {SELECT a, rowid, x FROM t1 ORDER BY a} $ans
202
203set ans {}
204foreach r [reverse [sort $all_rowid]] {
205  lappend ans $r $t1ra($r) $t1rx($r)
206}
207maketest 2.5 {SELECT a, rowid, x FROM t1 ORDER BY +a DESC} $ans
208maketest 2.6 {SELECT a, rowid, x FROM t1 ORDER BY a DESC} $ans
209
210set ans {}
211foreach a [sort $all_a] {
212  set r $t1ar($a)
213  lappend ans $r $a $t1rx($r)
214}
215maketest 2.7 {SELECT a, rowid, x FROM t1 ORDER BY +rowid} $ans
216maketest 2.8 {SELECT a, rowid, x FROM t1 ORDER BY rowid} $ans
217
218set ans {}
219foreach a [reverse [sort $all_a]] {
220  set r $t1ar($a)
221  lappend ans $r $a $t1rx($r)
222}
223maketest 2.9 {SELECT a, rowid, x FROM t1 ORDER BY +rowid DESC} $ans
224maketest 2.10 {SELECT a, rowid, x FROM t1 ORDER BY rowid DESC} $ans
225
226set ans {}
227foreach x [sort $all_x] {
228  set r $t1xr($x)
229  lappend ans $r $t1ra($r) $x
230}
231maketest 2.11 {SELECT a, rowid, x FROM t1 ORDER BY +x} $ans
232maketest 2.12 {SELECT a, rowid, x FROM t1 ORDER BY x} $ans
233
234set ans {}
235foreach x [reverse [sort $all_x]] {
236  set r $t1xr($x)
237  lappend ans $r $t1ra($r) $x
238}
239maketest 2.13 {SELECT a, rowid, x FROM t1 ORDER BY +x DESC} $ans
240maketest 2.14 {SELECT a, rowid, x FROM t1 ORDER BY x DESC} $ans
241
242maketest 3.1 {UPDATE t1 SET rowid=a, a=rowid} {}
243maketest 3.2 {ALTER TABLE t1 ADD COLUMN z; UPDATE t1 SET z=zeroblob(600)} {}
244
245set ans {}
246foreach r [sort $all_rowid] {
247  lappend ans $r $t1ra($r) $t1rx($r)
248}
249maketest 3.3 {SELECT rowid, a, x FROM t1 ORDER BY +rowid} $ans
250maketest 3.4 {SELECT rowid, a, x FROM t1 ORDER BY rowid} $ans
251
252set ans {}
253foreach r [reverse [sort $all_rowid]] {
254  lappend ans $r $t1ra($r) $t1rx($r)
255}
256maketest 3.5 {SELECT rowid, a, x FROM t1 ORDER BY +rowid DESC} $ans
257maketest 3.6 {SELECT rowid, a, x FROM t1 ORDER BY rowid DESC} $ans
258
259set ans {}
260foreach a [sort $all_a] {
261  set r $t1ar($a)
262  lappend ans $r $a $t1rx($r)
263}
264maketest 3.7 {SELECT rowid, a, x FROM t1 ORDER BY +a} $ans
265maketest 3.8 {SELECT rowid, a, x FROM t1 ORDER BY a} $ans
266
267set ans {}
268foreach a [reverse [sort $all_a]] {
269  set r $t1ar($a)
270  lappend ans $r $a $t1rx($r)
271}
272maketest 3.9 {SELECT rowid, a, x FROM t1 ORDER BY +a DESC} $ans
273maketest 3.10 {SELECT rowid, a, x FROM t1 ORDER BY a DESC} $ans
274
275set ans {}
276foreach x [sort $all_x] {
277  set r $t1xr($x)
278  lappend ans $r $t1ra($r) $x
279}
280maketest 3.11 {SELECT rowid, a, x FROM t1 ORDER BY +x} $ans
281maketest 3.12 {SELECT rowid, a, x FROM t1 ORDER BY x} $ans
282
283set ans {}
284foreach x [reverse [sort $all_x]] {
285  set r $t1xr($x)
286  lappend ans $r $t1ra($r) $x
287}
288maketest 3.13 {SELECT rowid, a, x FROM t1 ORDER BY +x DESC} $ans
289maketest 3.14 {SELECT rowid, a, x FROM t1 ORDER BY x DESC} $ans
290
291
292maketest 4.1 {UPDATE t1 SET rowid=a, a=rowid, x=z, z=x} {}
293
294set ans {}
295foreach r [sort $all_rowid] {
296  lappend ans $r $t1ra($r) $t1rx($r)
297}
298maketest 4.3 {SELECT a, rowid, z FROM t1 ORDER BY +a} $ans
299maketest 4.4 {SELECT a, rowid, z FROM t1 ORDER BY a} $ans
300
301set ans {}
302foreach r [reverse [sort $all_rowid]] {
303  lappend ans $r $t1ra($r) $t1rx($r)
304}
305maketest 4.5 {SELECT a, rowid, z FROM t1 ORDER BY +a DESC} $ans
306maketest 4.6 {SELECT a, rowid, z FROM t1 ORDER BY a DESC} $ans
307
308set ans {}
309foreach a [sort $all_a] {
310  set r $t1ar($a)
311  lappend ans $r $a $t1rx($r)
312}
313maketest 4.7 {SELECT a, rowid, z FROM t1 ORDER BY +rowid} $ans
314maketest 4.8 {SELECT a, rowid, z FROM t1 ORDER BY rowid} $ans
315
316set ans {}
317foreach a [reverse [sort $all_a]] {
318  set r $t1ar($a)
319  lappend ans $r $a $t1rx($r)
320}
321maketest 4.9 {SELECT a, rowid, z FROM t1 ORDER BY +rowid DESC} $ans
322maketest 4.10 {SELECT a, rowid, z FROM t1 ORDER BY rowid DESC} $ans
323
324set ans {}
325foreach x [sort $all_x] {
326  set r $t1xr($x)
327  lappend ans $r $t1ra($r) $x
328}
329maketest 4.11 {SELECT a, rowid, z FROM t1 ORDER BY +z} $ans
330maketest 4.12 {SELECT a, rowid, z FROM t1 ORDER BY z} $ans
331
332set ans {}
333foreach x [reverse [sort $all_x]] {
334  set r $t1xr($x)
335  lappend ans $r $t1ra($r) $x
336}
337maketest 4.13 {SELECT a, rowid, z FROM t1 ORDER BY +z DESC} $ans
338maketest 4.14 {SELECT a, rowid, z FROM t1 ORDER BY z DESC} $ans
339
340puts {finish_test}
341