2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing the CREATE TABLE statement.
17 # $Id: sort.test,v 1.9 2003/04/18 17:45:15 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Create a bunch of data to sort against
25 set fd [open data.txt w]
26 puts $fd "1\tone\t0\tI\t3.141592653"
27 puts $fd "2\ttwo\t1\tII\t2.15"
28 puts $fd "3\tthree\t1\tIII\t4221.0"
29 puts $fd "4\tfour\t2\tIV\t-0.0013442"
30 puts $fd "5\tfive\t2\tV\t-11"
31 puts $fd "6\tsix\t2\tVI\t0.123"
32 puts $fd "7\tseven\t2\tVII\t123.0"
33 puts $fd "8\teight\t3\tVIII\t-1.6"
43 COPY t1 FROM 'data.txt'
46 execsql {SELECT count(*) FROM t1}
50 execsql {SELECT n FROM t1 ORDER BY n}
53 execsql {SELECT n FROM t1 ORDER BY n ASC}
56 execsql {SELECT ALL n FROM t1 ORDER BY n ASC}
59 execsql {SELECT n FROM t1 ORDER BY n DESC}
62 execsql {SELECT v FROM t1 ORDER BY v}
63 } {eight five four one seven six three two}
65 execsql {SELECT n FROM t1 ORDER BY v}
68 execsql {SELECT n FROM t1 ORDER BY v DESC}
71 execsql {SELECT flt FROM t1 ORDER BY flt}
72 } {-11 -1.6 -0.0013442 0.123 2.15 3.141592653 123.0 4221.0}
74 execsql {SELECT flt FROM t1 ORDER BY flt DESC}
75 } {4221.0 123.0 3.141592653 2.15 0.123 -0.0013442 -1.6 -11}
77 execsql {SELECT roman FROM t1 ORDER BY roman}
78 } {I II III IV V VI VII VIII}
80 execsql {SELECT n FROM t1 ORDER BY log, flt}
83 execsql {SELECT n FROM t1 ORDER BY log asc, flt}
86 execsql {SELECT n FROM t1 ORDER BY log, flt ASC}
89 execsql {SELECT n FROM t1 ORDER BY log ASC, flt asc}
92 execsql {SELECT n FROM t1 ORDER BY log, flt DESC}
95 execsql {SELECT n FROM t1 ORDER BY log ASC, flt DESC}
98 execsql {SELECT n FROM t1 ORDER BY log DESC, flt}
101 execsql {SELECT n FROM t1 ORDER BY log DESC, flt DESC}
104 # These tests are designed to reach some hard-to-reach places
105 # inside the string comparison routines.
107 # (Later) The sorting behavior changed in 2.7.0. But we will
108 # keep these tests. You can never have too many test cases!
112 UPDATE t1 SET v='x' || -flt;
113 UPDATE t1 SET v='x-2b' where v=='x-0.123';
114 SELECT v FROM t1 ORDER BY v;
116 } {x-123 x-2.15 x-2b x-3.141592653 x-4221 x0.0013442 x1.6 x11}
119 SELECT v FROM t1 ORDER BY substr(v,2,999);
121 } {x-123 x-2.15 x-2b x-3.141592653 x-4221 x0.0013442 x1.6 x11}
124 SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0;
126 } {x-4221 x-123 x-3.141592653 x-2.15 x-2b x0.0013442 x1.6 x11}
129 SELECT v FROM t1 ORDER BY substr(v,2,999) DESC;
131 } {x11 x1.6 x0.0013442 x-4221 x-3.141592653 x-2b x-2.15 x-123}
134 SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0 DESC;
136 } {x11 x1.6 x0.0013442 x-2b x-2.15 x-3.141592653 x-123 x-4221}
138 # This is a bug fix for 2.2.4.
139 # Strings are normally mapped to upper-case for a caseless comparison.
140 # But this can cause problems for characters in between 'Z' and 'a'.
144 CREATE TABLE t2(a,b);
145 INSERT INTO t2 VALUES('AGLIENTU',1);
146 INSERT INTO t2 VALUES('AGLIE`',2);
147 INSERT INTO t2 VALUES('AGNA',3);
148 SELECT a, b FROM t2 ORDER BY a;
150 } {AGLIENTU 1 AGLIE` 2 AGNA 3}
153 SELECT a, b FROM t2 ORDER BY a DESC;
155 } {AGNA 3 AGLIE` 2 AGLIENTU 1}
159 INSERT INTO t2 VALUES('aglientu',1);
160 INSERT INTO t2 VALUES('aglie`',2);
161 INSERT INTO t2 VALUES('agna',3);
162 SELECT a, b FROM t2 ORDER BY a;
164 } {aglie` 2 aglientu 1 agna 3}
167 SELECT a, b FROM t2 ORDER BY a DESC;
169 } {agna 3 aglientu 1 aglie` 2}
171 # Version 2.7.0 testing.
175 INSERT INTO t1 VALUES(9,'x2.7',3,'IX',4.0e5);
176 INSERT INTO t1 VALUES(10,'x5.0e10',3,'X',-4.0e5);
177 INSERT INTO t1 VALUES(11,'x-4.0e9',3,'XI',4.1e4);
178 INSERT INTO t1 VALUES(12,'x01234567890123456789',3,'XII',-4.2e3);
179 SELECT n FROM t1 ORDER BY n;
181 } {1 2 3 4 5 6 7 8 9 10 11 12}
184 SELECT n||'' FROM t1 ORDER BY 1;
186 } {1 10 11 12 2 3 4 5 6 7 8 9}
189 SELECT n+0 FROM t1 ORDER BY 1;
191 } {1 2 3 4 5 6 7 8 9 10 11 12}
194 SELECT n||'' FROM t1 ORDER BY 1 DESC;
196 } {9 8 7 6 5 4 3 2 12 11 10 1}
199 SELECT n+0 FROM t1 ORDER BY 1 DESC;
201 } {12 11 10 9 8 7 6 5 4 3 2 1}
204 SELECT v FROM t1 ORDER BY 1;
206 } {x-123 x-2.15 x-2b x-3.141592653 x-4.0e9 x-4221 x0.0013442 x01234567890123456789 x1.6 x11 x2.7 x5.0e10}
209 SELECT v FROM t1 ORDER BY 1 DESC;
211 } {x5.0e10 x2.7 x11 x1.6 x01234567890123456789 x0.0013442 x-4221 x-4.0e9 x-3.141592653 x-2b x-2.15 x-123}
214 SELECT substr(v,2,99) FROM t1 ORDER BY 1;
216 } {-123 -2.15 -2b -3.141592653 -4.0e9 -4221 0.0013442 01234567890123456789 1.6 11 2.7 5.0e10}
219 # SELECT substr(v,2,99)+0.0 FROM t1 ORDER BY 1;
221 #} {-4000000000 -4221 -123 -3.141592653 -2.15 -2 0.0013442 1.6 2.7 11 50000000000 1.23456789012346e+18}
225 create table t3(a,b);
226 insert into t3 values(5,NULL);
227 insert into t3 values(6,NULL);
228 insert into t3 values(3,NULL);
229 insert into t3 values(4,'cd');
230 insert into t3 values(1,'ab');
231 insert into t3 values(2,NULL);
232 select a from t3 order by b, a;
237 select a from t3 order by b, a desc;
242 select a from t3 order by b desc, a;
247 select a from t3 order by b desc, a desc;
253 create index i3 on t3(b,a);
254 select a from t3 order by b, a;
259 select a from t3 order by b, a desc;
264 select a from t3 order by b desc, a;
269 select a from t3 order by b desc, a desc;
279 INSERT INTO t4 VALUES(1,1);
280 INSERT INTO t4 VALUES(2,2);
281 INSERT INTO t4 VALUES(11,11);
282 INSERT INTO t4 VALUES(12,12);
283 SELECT a FROM t4 ORDER BY 1;
288 SELECT b FROM t4 ORDER BY 1
293 CREATE VIEW v4 AS SELECT * FROM t4;
294 SELECT a FROM v4 ORDER BY 1;
299 SELECT b FROM v4 ORDER BY 1;
304 SELECT a FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
309 SELECT b FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
314 SELECT a FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
319 SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
324 SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE numeric;
329 SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE integer;
334 SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE text;
339 SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE blob;
344 SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE clob;
349 SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE varchar;
357 CREATE TABLE t5(a real, b text);
358 INSERT INTO t5 VALUES(100,'A1');
359 INSERT INTO t5 VALUES(100.0,'A2');
360 SELECT * FROM t5 ORDER BY a, b;