xref: /sqlite-3.40.0/test/sort.test (revision c023e03e)
1# 2001 September 15
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.  The
12# focus of this file is testing the CREATE TABLE statement.
13#
14# $Id: sort.test,v 1.9 2003/04/18 17:45:15 drh Exp $
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Create a bunch of data to sort against
20#
21do_test sort-1.0 {
22  set fd [open data.txt w]
23  puts $fd "1\tone\t0\tI\t3.141592653"
24  puts $fd "2\ttwo\t1\tII\t2.15"
25  puts $fd "3\tthree\t1\tIII\t4221.0"
26  puts $fd "4\tfour\t2\tIV\t-0.0013442"
27  puts $fd "5\tfive\t2\tV\t-11"
28  puts $fd "6\tsix\t2\tVI\t0.123"
29  puts $fd "7\tseven\t2\tVII\t123.0"
30  puts $fd "8\teight\t3\tVIII\t-1.6"
31  close $fd
32  execsql {
33    CREATE TABLE t1(
34       n int,
35       v varchar(10),
36       log int,
37       roman varchar(10),
38       flt real
39    );
40    COPY t1 FROM 'data.txt'
41  }
42  file delete data.txt
43  execsql {SELECT count(*) FROM t1}
44} {8}
45
46do_test sort-1.1 {
47  execsql {SELECT n FROM t1 ORDER BY n}
48} {1 2 3 4 5 6 7 8}
49do_test sort-1.1.1 {
50  execsql {SELECT n FROM t1 ORDER BY n ASC}
51} {1 2 3 4 5 6 7 8}
52do_test sort-1.1.1 {
53  execsql {SELECT ALL n FROM t1 ORDER BY n ASC}
54} {1 2 3 4 5 6 7 8}
55do_test sort-1.2 {
56  execsql {SELECT n FROM t1 ORDER BY n DESC}
57} {8 7 6 5 4 3 2 1}
58do_test sort-1.3a {
59  execsql {SELECT v FROM t1 ORDER BY v}
60} {eight five four one seven six three two}
61do_test sort-1.3b {
62  execsql {SELECT n FROM t1 ORDER BY v}
63} {8 5 4 1 7 6 3 2}
64do_test sort-1.4 {
65  execsql {SELECT n FROM t1 ORDER BY v DESC}
66} {2 3 6 7 1 4 5 8}
67do_test sort-1.5 {
68  execsql {SELECT flt FROM t1 ORDER BY flt}
69} {-11 -1.6 -0.0013442 0.123 2.15 3.141592653 123.0 4221.0}
70do_test sort-1.6 {
71  execsql {SELECT flt FROM t1 ORDER BY flt DESC}
72} {4221.0 123.0 3.141592653 2.15 0.123 -0.0013442 -1.6 -11}
73do_test sort-1.7 {
74  execsql {SELECT roman FROM t1 ORDER BY roman}
75} {I II III IV V VI VII VIII}
76do_test sort-1.8 {
77  execsql {SELECT n FROM t1 ORDER BY log, flt}
78} {1 2 3 5 4 6 7 8}
79do_test sort-1.8.1 {
80  execsql {SELECT n FROM t1 ORDER BY log asc, flt}
81} {1 2 3 5 4 6 7 8}
82do_test sort-1.8.2 {
83  execsql {SELECT n FROM t1 ORDER BY log, flt ASC}
84} {1 2 3 5 4 6 7 8}
85do_test sort-1.8.3 {
86  execsql {SELECT n FROM t1 ORDER BY log ASC, flt asc}
87} {1 2 3 5 4 6 7 8}
88do_test sort-1.9 {
89  execsql {SELECT n FROM t1 ORDER BY log, flt DESC}
90} {1 3 2 7 6 4 5 8}
91do_test sort-1.9.1 {
92  execsql {SELECT n FROM t1 ORDER BY log ASC, flt DESC}
93} {1 3 2 7 6 4 5 8}
94do_test sort-1.10 {
95  execsql {SELECT n FROM t1 ORDER BY log DESC, flt}
96} {8 5 4 6 7 2 3 1}
97do_test sort-1.11 {
98  execsql {SELECT n FROM t1 ORDER BY log DESC, flt DESC}
99} {8 7 6 4 5 3 2 1}
100
101# These tests are designed to reach some hard-to-reach places
102# inside the string comparison routines.
103#
104# (Later) The sorting behavior changed in 2.7.0.  But we will
105# keep these tests.  You can never have too many test cases!
106#
107do_test sort-2.1.1 {
108  execsql {
109    UPDATE t1 SET v='x' || -flt;
110    UPDATE t1 SET v='x-2b' where v=='x-0.123';
111    SELECT v FROM t1 ORDER BY v;
112  }
113} {x-123 x-2.15 x-2b x-3.141592653 x-4221 x0.0013442 x1.6 x11}
114do_test sort-2.1.2 {
115  execsql {
116    SELECT v FROM t1 ORDER BY substr(v,2,999);
117  }
118} {x-123 x-2.15 x-2b x-3.141592653 x-4221 x0.0013442 x1.6 x11}
119do_test sort-2.1.3 {
120  execsql {
121    SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0;
122  }
123} {x-4221 x-123 x-3.141592653 x-2.15 x-2b x0.0013442 x1.6 x11}
124do_test sort-2.1.4 {
125  execsql {
126    SELECT v FROM t1 ORDER BY substr(v,2,999) DESC;
127  }
128} {x11 x1.6 x0.0013442 x-4221 x-3.141592653 x-2b x-2.15 x-123}
129do_test sort-2.1.5 {
130  execsql {
131    SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0 DESC;
132  }
133} {x11 x1.6 x0.0013442 x-2b x-2.15 x-3.141592653 x-123 x-4221}
134
135# This is a bug fix for 2.2.4.
136# Strings are normally mapped to upper-case for a caseless comparison.
137# But this can cause problems for characters in between 'Z' and 'a'.
138#
139do_test sort-3.1 {
140  execsql {
141    CREATE TABLE t2(a,b);
142    INSERT INTO t2 VALUES('AGLIENTU',1);
143    INSERT INTO t2 VALUES('AGLIE`',2);
144    INSERT INTO t2 VALUES('AGNA',3);
145    SELECT a, b FROM t2 ORDER BY a;
146  }
147} {AGLIENTU 1 AGLIE` 2 AGNA 3}
148do_test sort-3.2 {
149  execsql {
150    SELECT a, b FROM t2 ORDER BY a DESC;
151  }
152} {AGNA 3 AGLIE` 2 AGLIENTU 1}
153do_test sort-3.3 {
154  execsql {
155    DELETE FROM t2;
156    INSERT INTO t2 VALUES('aglientu',1);
157    INSERT INTO t2 VALUES('aglie`',2);
158    INSERT INTO t2 VALUES('agna',3);
159    SELECT a, b FROM t2 ORDER BY a;
160  }
161} {aglie` 2 aglientu 1 agna 3}
162do_test sort-3.4 {
163  execsql {
164    SELECT a, b FROM t2 ORDER BY a DESC;
165  }
166} {agna 3 aglientu 1 aglie` 2}
167
168# Version 2.7.0 testing.
169#
170do_test sort-4.1 {
171  execsql {
172    INSERT INTO t1 VALUES(9,'x2.7',3,'IX',4.0e5);
173    INSERT INTO t1 VALUES(10,'x5.0e10',3,'X',-4.0e5);
174    INSERT INTO t1 VALUES(11,'x-4.0e9',3,'XI',4.1e4);
175    INSERT INTO t1 VALUES(12,'x01234567890123456789',3,'XII',-4.2e3);
176    SELECT n FROM t1 ORDER BY n;
177  }
178} {1 2 3 4 5 6 7 8 9 10 11 12}
179do_test sort-4.2 {
180  execsql {
181    SELECT n||'' FROM t1 ORDER BY 1;
182  }
183} {1 10 11 12 2 3 4 5 6 7 8 9}
184do_test sort-4.3 {
185  execsql {
186    SELECT n+0 FROM t1 ORDER BY 1;
187  }
188} {1 2 3 4 5 6 7 8 9 10 11 12}
189do_test sort-4.4 {
190  execsql {
191    SELECT n||'' FROM t1 ORDER BY 1 DESC;
192  }
193} {9 8 7 6 5 4 3 2 12 11 10 1}
194do_test sort-4.5 {
195  execsql {
196    SELECT n+0 FROM t1 ORDER BY 1 DESC;
197  }
198} {12 11 10 9 8 7 6 5 4 3 2 1}
199do_test sort-4.6 {
200  execsql {
201    SELECT v FROM t1 ORDER BY 1;
202  }
203} {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}
204do_test sort-4.7 {
205  execsql {
206    SELECT v FROM t1 ORDER BY 1 DESC;
207  }
208} {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}
209do_test sort-4.8 {
210  execsql {
211    SELECT substr(v,2,99) FROM t1 ORDER BY 1;
212  }
213} {-123 -2.15 -2b -3.141592653 -4.0e9 -4221 0.0013442 01234567890123456789 1.6 11 2.7 5.0e10}
214#do_test sort-4.9 {
215#  execsql {
216#    SELECT substr(v,2,99)+0.0 FROM t1 ORDER BY 1;
217#  }
218#} {-4000000000 -4221 -123 -3.141592653 -2.15 -2 0.0013442 1.6 2.7 11 50000000000 1.23456789012346e+18}
219
220do_test sort-5.1 {
221  execsql {
222    create table t3(a,b);
223    insert into t3 values(5,NULL);
224    insert into t3 values(6,NULL);
225    insert into t3 values(3,NULL);
226    insert into t3 values(4,'cd');
227    insert into t3 values(1,'ab');
228    insert into t3 values(2,NULL);
229    select a from t3 order by b, a;
230  }
231} {2 3 5 6 1 4}
232do_test sort-5.2 {
233  execsql {
234    select a from t3 order by b, a desc;
235  }
236} {6 5 3 2 1 4}
237do_test sort-5.3 {
238  execsql {
239    select a from t3 order by b desc, a;
240  }
241} {4 1 2 3 5 6}
242do_test sort-5.4 {
243  execsql {
244    select a from t3 order by b desc, a desc;
245  }
246} {4 1 6 5 3 2}
247
248do_test sort-6.1 {
249  execsql {
250    create index i3 on t3(b,a);
251    select a from t3 order by b, a;
252  }
253} {2 3 5 6 1 4}
254do_test sort-6.2 {
255  execsql {
256    select a from t3 order by b, a desc;
257  }
258} {6 5 3 2 1 4}
259do_test sort-6.3 {
260  execsql {
261    select a from t3 order by b desc, a;
262  }
263} {4 1 2 3 5 6}
264do_test sort-6.4 {
265  execsql {
266    select a from t3 order by b desc, a desc;
267  }
268} {4 1 6 5 3 2}
269
270do_test sort-7.1 {
271  execsql {
272    CREATE TABLE t4(
273      a INTEGER,
274      b VARCHAR(30)
275    );
276    INSERT INTO t4 VALUES(1,1);
277    INSERT INTO t4 VALUES(2,2);
278    INSERT INTO t4 VALUES(11,11);
279    INSERT INTO t4 VALUES(12,12);
280    SELECT a FROM t4 ORDER BY 1;
281  }
282} {1 2 11 12}
283do_test sort-7.2 {
284  execsql {
285    SELECT b FROM t4 ORDER BY 1
286  }
287} {1 11 12 2}
288do_test sort-7.3 {
289  execsql {
290    CREATE VIEW v4 AS SELECT * FROM t4;
291    SELECT a FROM v4 ORDER BY 1;
292  }
293} {1 2 11 12}
294do_test sort-7.4 {
295  execsql {
296    SELECT b FROM v4 ORDER BY 1;
297  }
298} {1 11 12 2}
299do_test sort-7.5 {
300  execsql {
301    SELECT a FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
302  }
303} {1 2 11 12}
304do_test sort-7.6 {
305  execsql {
306    SELECT b FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
307  }
308} {1 2 11 12}
309do_test sort-7.7 {
310  execsql {
311    SELECT a FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
312  }
313} {1 2 11 12}
314do_test sort-7.8 {
315  execsql {
316    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
317  }
318} {1 11 12 2}
319do_test sort-7.9 {
320  execsql {
321    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE numeric;
322  }
323} {1 2 11 12}
324do_test sort-7.10 {
325  execsql {
326    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE integer;
327  }
328} {1 2 11 12}
329do_test sort-7.11 {
330  execsql {
331    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE text;
332  }
333} {1 11 12 2}
334do_test sort-7.12 {
335  execsql {
336    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE blob;
337  }
338} {1 11 12 2}
339do_test sort-7.13 {
340  execsql {
341    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE clob;
342  }
343} {1 11 12 2}
344do_test sort-7.14 {
345  execsql {
346    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE varchar;
347  }
348} {1 11 12 2}
349
350# Ticket #297
351#
352do_test sort-8.1 {
353  execsql {
354    CREATE TABLE t5(a real, b text);
355    INSERT INTO t5 VALUES(100,'A1');
356    INSERT INTO t5 VALUES(100.0,'A2');
357    SELECT * FROM t5 ORDER BY a, b;
358  }
359} {100 A1 100.0 A2}
360
361finish_test
362