1# 2# 2001 September 15 3# 4# The author disclaims copyright to this source code. In place of 5# a legal notice, here is a blessing: 6# 7# May you do good and not evil. 8# May you find forgiveness for yourself and forgive others. 9# May you share freely, never taking more than you give. 10# 11#*********************************************************************** 12# This file implements regression tests for SQLite library. The 13# focus of this script is page cache subsystem. 14# 15# $Id: collate1.test,v 1.3 2004/08/20 18:34:20 drh Exp $ 16 17set testdir [file dirname $argv0] 18source $testdir/tester.tcl 19 20# 21# Tests are roughly organised as follows: 22# 23# collate1-1.* - Single-field ORDER BY with an explicit COLLATE clause. 24# collate1-2.* - Multi-field ORDER BY with an explicit COLLATE clause. 25# collate1-3.* - ORDER BY using a default collation type. Also that an 26# explict collate type overrides a default collate type. 27# collate1-4.* - ORDER BY using a data type. 28# 29 30# 31# Collation type 'HEX'. If an argument can be interpreted as a hexadecimal 32# number, then it is converted to one before the comparison is performed. 33# Numbers are less than other strings. If neither argument is a number, 34# [string compare] is used. 35# 36db collate HEX hex_collate 37proc hex_collate {lhs rhs} { 38 set lhs_ishex [regexp {^(0x|)[1234567890abcdefABCDEF]+$} $lhs] 39 set rhs_ishex [regexp {^(0x|)[1234567890abcdefABCDEF]+$} $rhs] 40 if {$lhs_ishex && $rhs_ishex} { 41 set lhsx [scan $lhs %x] 42 set rhsx [scan $rhs %x] 43 if {$lhs < $rhs} {return -1} 44 if {$lhs == $rhs} {return 0} 45 if {$lhs > $rhs} {return 1} 46 } 47 if {$lhs_ishex} { 48 return -1; 49 } 50 if {$rhs_ishex} { 51 return 1; 52 } 53 return [string compare $lhs $rhs] 54} 55db function hex {format 0x%X} 56 57# Mimic the SQLite 2 collation type NUMERIC. 58db collate numeric numeric_collate 59proc numeric_collate {lhs rhs} { 60 if {$lhs == $rhs} {return 0} 61 return [expr ($lhs>$rhs)?1:-1] 62} 63 64do_test collate1-1.0 { 65 execsql { 66 CREATE TABLE collate1t1(c1, c2); 67 INSERT INTO collate1t1 VALUES(45, hex(45)); 68 INSERT INTO collate1t1 VALUES(NULL, NULL); 69 INSERT INTO collate1t1 VALUES(281, hex(281)); 70 } 71} {} 72do_test collate1-1.1 { 73 execsql { 74 SELECT c2 FROM collate1t1 ORDER BY 1; 75 } 76} {{} 0x119 0x2D} 77do_test collate1-1.2 { 78 execsql { 79 SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex; 80 } 81} {{} 0x2D 0x119} 82do_test collate1-1.3 { 83 execsql { 84 SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex DESC; 85 } 86} {0x119 0x2D {}} 87do_test collate1-1.4 { 88 execsql { 89 SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex ASC; 90 } 91} {{} 0x2D 0x119} 92do_test collate1-1.5 { 93 execsql { 94 DROP TABLE collate1t1; 95 } 96} {} 97 98do_test collate1-2.0 { 99 execsql { 100 CREATE TABLE collate1t1(c1, c2); 101 INSERT INTO collate1t1 VALUES('5', '0x11'); 102 INSERT INTO collate1t1 VALUES('5', '0xA'); 103 INSERT INTO collate1t1 VALUES(NULL, NULL); 104 INSERT INTO collate1t1 VALUES('7', '0xA'); 105 INSERT INTO collate1t1 VALUES('11', '0x11'); 106 INSERT INTO collate1t1 VALUES('11', '0x101'); 107 } 108} {} 109do_test collate1-2.2 { 110 execsql { 111 SELECT c1, c2 FROM collate1t1 ORDER BY 1 COLLATE numeric, 2 COLLATE hex; 112 } 113} {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101} 114do_test collate1-2.3 { 115 execsql { 116 SELECT c1, c2 FROM collate1t1 ORDER BY 1 COLLATE binary, 2 COLLATE hex; 117 } 118} {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA} 119do_test collate1-2.4 { 120 execsql { 121 SELECT c1, c2 FROM collate1t1 ORDER BY 1 COLLATE binary DESC, 2 COLLATE hex; 122 } 123} {7 0xA 5 0xA 5 0x11 11 0x11 11 0x101 {} {}} 124do_test collate1-2.5 { 125 execsql { 126 SELECT c1, c2 FROM collate1t1 127 ORDER BY 1 COLLATE binary DESC, 2 COLLATE hex DESC; 128 } 129} {7 0xA 5 0x11 5 0xA 11 0x101 11 0x11 {} {}} 130do_test collate1-2.6 { 131 execsql { 132 SELECT c1, c2 FROM collate1t1 133 ORDER BY 1 COLLATE binary ASC, 2 COLLATE hex ASC; 134 } 135} {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA} 136do_test collate1-2.7 { 137 execsql { 138 DROP TABLE collate1t1; 139 } 140} {} 141 142# 143# These tests ensure that the default collation type for a column is used 144# by an ORDER BY clause correctly. The focus is all the different ways 145# the column can be referenced. i.e. a, collate2t1.a, main.collate2t1.a etc. 146# 147do_test collate1-3.0 { 148 execsql { 149 CREATE TABLE collate1t1(a COLLATE hex, b); 150 INSERT INTO collate1t1 VALUES( '0x5', 5 ); 151 INSERT INTO collate1t1 VALUES( '1', 1 ); 152 INSERT INTO collate1t1 VALUES( '0x45', 69 ); 153 INSERT INTO collate1t1 VALUES( NULL, NULL ); 154 SELECT * FROM collate1t1 ORDER BY a; 155 } 156} {{} {} 1 1 0x5 5 0x45 69} 157 158do_test collate1-3.1 { 159 execsql { 160 SELECT * FROM collate1t1 ORDER BY 1; 161 } 162} {{} {} 1 1 0x5 5 0x45 69} 163do_test collate1-3.2 { 164 execsql { 165 SELECT * FROM collate1t1 ORDER BY collate1t1.a; 166 } 167} {{} {} 1 1 0x5 5 0x45 69} 168do_test collate1-3.3 { 169 execsql { 170 SELECT * FROM collate1t1 ORDER BY main.collate1t1.a; 171 } 172} {{} {} 1 1 0x5 5 0x45 69} 173do_test collate1-3.4 { 174 execsql { 175 SELECT a as c1, b as c2 FROM collate1t1 ORDER BY c1; 176 } 177} {{} {} 1 1 0x5 5 0x45 69} 178do_test collate1-3.5 { 179 execsql { 180 SELECT a as c1, b as c2 FROM collate1t1 ORDER BY c1 COLLATE binary; 181 } 182} {{} {} 0x45 69 0x5 5 1 1} 183do_test collate1-3.6 { 184 execsql { 185 DROP TABLE collate1t1; 186 } 187} {} 188 189# Update for SQLite version 3. The collate1-4.* test cases were written 190# before manifest types were introduced. The following test cases still 191# work, due to the 'affinity' mechanism, but they don't prove anything 192# about collation sequences. 193# 194do_test collate1-4.0 { 195 execsql { 196 CREATE TABLE collate1t1(c1 numeric, c2 text); 197 INSERT INTO collate1t1 VALUES(1, 1); 198 INSERT INTO collate1t1 VALUES(12, 12); 199 INSERT INTO collate1t1 VALUES(NULL, NULL); 200 INSERT INTO collate1t1 VALUES(101, 101); 201 } 202} {} 203do_test collate1-4.1 { 204 execsql { 205 SELECT c1 FROM collate1t1 ORDER BY 1; 206 } 207} {{} 1 12 101} 208do_test collate1-4.2 { 209 execsql { 210 SELECT c2 FROM collate1t1 ORDER BY 1; 211 } 212} {{} 1 101 12} 213do_test collate1-4.3 { 214 execsql { 215 SELECT c2+0 FROM collate1t1 ORDER BY 1; 216 } 217} {{} 1.0 12.0 101.0} 218do_test collate1-4.4 { 219 execsql { 220 SELECT c1||'' FROM collate1t1 ORDER BY 1; 221 } 222} {{} 1 101 12} 223do_test collate1-4.5 { 224 execsql { 225 DROP TABLE collate1t1; 226 } 227} {} 228 229finish_test 230