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