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