sl@0
|
1 |
#
|
sl@0
|
2 |
# 2007 November 12
|
sl@0
|
3 |
#
|
sl@0
|
4 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
5 |
# a legal notice, here is a blessing:
|
sl@0
|
6 |
#
|
sl@0
|
7 |
# May you do good and not evil.
|
sl@0
|
8 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
9 |
# May you share freely, never taking more than you give.
|
sl@0
|
10 |
#
|
sl@0
|
11 |
#***********************************************************************
|
sl@0
|
12 |
# This file implements regression tests for SQLite library. The
|
sl@0
|
13 |
# focus of this script is making sure that the names of collation
|
sl@0
|
14 |
# sequences may be quoted using double quotes in SQL statements.
|
sl@0
|
15 |
#
|
sl@0
|
16 |
# $Id: collate9.test,v 1.2 2008/07/10 00:32:42 drh Exp $
|
sl@0
|
17 |
|
sl@0
|
18 |
set testdir [file dirname $argv0]
|
sl@0
|
19 |
source $testdir/tester.tcl
|
sl@0
|
20 |
|
sl@0
|
21 |
proc reverse_sort {lhs rhs} {
|
sl@0
|
22 |
return [string compare $rhs $lhs]
|
sl@0
|
23 |
}
|
sl@0
|
24 |
db collate "reverse sort" reverse_sort
|
sl@0
|
25 |
|
sl@0
|
26 |
# This procedure executes the SQL. Then it checks to see if the OP_Sort
|
sl@0
|
27 |
# opcode was executed. If an OP_Sort did occur, then "sort" is appended
|
sl@0
|
28 |
# to the result. If no OP_Sort happened, then "nosort" is appended.
|
sl@0
|
29 |
#
|
sl@0
|
30 |
# This procedure is used to check to make sure sorting is or is not
|
sl@0
|
31 |
# occurring as expected.
|
sl@0
|
32 |
#
|
sl@0
|
33 |
proc cksort {sql} {
|
sl@0
|
34 |
set ::sqlite_sort_count 0
|
sl@0
|
35 |
set data [execsql $sql]
|
sl@0
|
36 |
if {$::sqlite_sort_count} {set x sort} {set x nosort}
|
sl@0
|
37 |
lappend data $x
|
sl@0
|
38 |
return $data
|
sl@0
|
39 |
}
|
sl@0
|
40 |
|
sl@0
|
41 |
# Test plan:
|
sl@0
|
42 |
#
|
sl@0
|
43 |
# collate9-1.* - Test collation sequences attached to table columns
|
sl@0
|
44 |
# collate9-2.* - Test collation sequences attached to expressions
|
sl@0
|
45 |
# collate9-3.* - Test collation sequences attached to an index
|
sl@0
|
46 |
# collate9-4.* - Test collation sequences as an argument to REINDEX
|
sl@0
|
47 |
#
|
sl@0
|
48 |
|
sl@0
|
49 |
do_test collate9-1.1 {
|
sl@0
|
50 |
execsql {
|
sl@0
|
51 |
CREATE TABLE xy(x COLLATE "reverse sort", y COLLATE binary);
|
sl@0
|
52 |
INSERT INTO xy VALUES('one', 'one');
|
sl@0
|
53 |
INSERT INTO xy VALUES('two', 'two');
|
sl@0
|
54 |
INSERT INTO xy VALUES('three', 'three');
|
sl@0
|
55 |
}
|
sl@0
|
56 |
} {}
|
sl@0
|
57 |
do_test collate9-1.2 {
|
sl@0
|
58 |
execsql {
|
sl@0
|
59 |
SELECT x FROM xy ORDER BY x
|
sl@0
|
60 |
}
|
sl@0
|
61 |
} {two three one}
|
sl@0
|
62 |
do_test collate9-1.3 {
|
sl@0
|
63 |
execsql {
|
sl@0
|
64 |
SELECT y FROM xy ORDER BY y
|
sl@0
|
65 |
}
|
sl@0
|
66 |
} {one three two}
|
sl@0
|
67 |
do_test collate9-1.4 {
|
sl@0
|
68 |
cksort {
|
sl@0
|
69 |
SELECT x FROM xy ORDER BY x
|
sl@0
|
70 |
}
|
sl@0
|
71 |
} {two three one sort}
|
sl@0
|
72 |
do_test collate9-1.5 {
|
sl@0
|
73 |
execsql {
|
sl@0
|
74 |
CREATE INDEX xy_i ON xy(x)
|
sl@0
|
75 |
}
|
sl@0
|
76 |
} {}
|
sl@0
|
77 |
do_test collate9-1.6 {
|
sl@0
|
78 |
cksort {
|
sl@0
|
79 |
SELECT x FROM xy ORDER BY x
|
sl@0
|
80 |
}
|
sl@0
|
81 |
} {two three one nosort}
|
sl@0
|
82 |
|
sl@0
|
83 |
do_test collate9-2.1 {
|
sl@0
|
84 |
execsql {
|
sl@0
|
85 |
SELECT x, x < 'seven' FROM xy ORDER BY x
|
sl@0
|
86 |
}
|
sl@0
|
87 |
} {two 1 three 1 one 0}
|
sl@0
|
88 |
do_test collate9-2.2 {
|
sl@0
|
89 |
execsql {
|
sl@0
|
90 |
SELECT y, y < 'seven' FROM xy ORDER BY x
|
sl@0
|
91 |
}
|
sl@0
|
92 |
} {two 0 three 0 one 1}
|
sl@0
|
93 |
do_test collate9-2.3 {
|
sl@0
|
94 |
execsql {
|
sl@0
|
95 |
SELECT y, y COLLATE "reverse sort" < 'seven' FROM xy ORDER BY x
|
sl@0
|
96 |
}
|
sl@0
|
97 |
} {two 1 three 1 one 0}
|
sl@0
|
98 |
do_test collate9-2.4 {
|
sl@0
|
99 |
execsql {
|
sl@0
|
100 |
SELECT y FROM xy ORDER BY y
|
sl@0
|
101 |
}
|
sl@0
|
102 |
} {one three two}
|
sl@0
|
103 |
do_test collate9-2.5 {
|
sl@0
|
104 |
execsql {
|
sl@0
|
105 |
SELECT y FROM xy ORDER BY y COLLATE "reverse sort"
|
sl@0
|
106 |
}
|
sl@0
|
107 |
} {two three one}
|
sl@0
|
108 |
do_test collate9-2.6 {
|
sl@0
|
109 |
execsql {
|
sl@0
|
110 |
SELECT y COLLATE "reverse sort" AS aaa FROM xy ORDER BY aaa
|
sl@0
|
111 |
}
|
sl@0
|
112 |
} {two three one}
|
sl@0
|
113 |
|
sl@0
|
114 |
do_test collate9-3.1 {
|
sl@0
|
115 |
execsql {
|
sl@0
|
116 |
CREATE INDEX xy_i2 ON xy(y COLLATE "reverse sort");
|
sl@0
|
117 |
}
|
sl@0
|
118 |
} {}
|
sl@0
|
119 |
do_test collate9-3.2 {
|
sl@0
|
120 |
cksort {
|
sl@0
|
121 |
SELECT y FROM xy ORDER BY y
|
sl@0
|
122 |
}
|
sl@0
|
123 |
} {one three two sort}
|
sl@0
|
124 |
do_test collate9-3.3 {
|
sl@0
|
125 |
cksort {
|
sl@0
|
126 |
SELECT y FROM xy ORDER BY y COLLATE "reverse sort"
|
sl@0
|
127 |
}
|
sl@0
|
128 |
} {two three one nosort}
|
sl@0
|
129 |
do_test collate9-3.4 {
|
sl@0
|
130 |
cksort {
|
sl@0
|
131 |
SELECT y AS aaa FROM xy ORDER BY aaa
|
sl@0
|
132 |
}
|
sl@0
|
133 |
} {one three two sort}
|
sl@0
|
134 |
do_test collate9-3.5 {
|
sl@0
|
135 |
cksort {
|
sl@0
|
136 |
SELECT y COLLATE "reverse sort" AS aaa FROM xy ORDER BY aaa
|
sl@0
|
137 |
}
|
sl@0
|
138 |
} {two three one nosort}
|
sl@0
|
139 |
|
sl@0
|
140 |
ifcapable reindex {
|
sl@0
|
141 |
do_test collate9-4.1 {
|
sl@0
|
142 |
execsql {
|
sl@0
|
143 |
REINDEX "reverse sort"
|
sl@0
|
144 |
}
|
sl@0
|
145 |
} {}
|
sl@0
|
146 |
|
sl@0
|
147 |
# Modify the "reverse sort" collation so that it now sorts in the same
|
sl@0
|
148 |
# order as binary.
|
sl@0
|
149 |
proc reverse_sort {lhs rhs} {
|
sl@0
|
150 |
return [string compare $lhs $rhs]
|
sl@0
|
151 |
}
|
sl@0
|
152 |
|
sl@0
|
153 |
# The integrity check should now fail because the indexes created using
|
sl@0
|
154 |
# "reverse sort" are no longer in sync with the collation sequence
|
sl@0
|
155 |
# implementation.
|
sl@0
|
156 |
do_test collate9-4.2 {
|
sl@0
|
157 |
expr {"ok" eq [execsql { PRAGMA integrity_check }]}
|
sl@0
|
158 |
} {0}
|
sl@0
|
159 |
|
sl@0
|
160 |
do_test collate9-4.3 {
|
sl@0
|
161 |
execsql {
|
sl@0
|
162 |
REINDEX "reverse sort"
|
sl@0
|
163 |
}
|
sl@0
|
164 |
} {}
|
sl@0
|
165 |
|
sl@0
|
166 |
# Integrity check should now pass.
|
sl@0
|
167 |
do_test collate9-4.4 {
|
sl@0
|
168 |
expr {"ok" eq [execsql { PRAGMA integrity_check }]}
|
sl@0
|
169 |
} {1}
|
sl@0
|
170 |
|
sl@0
|
171 |
do_test collate9-4.5 {
|
sl@0
|
172 |
cksort {
|
sl@0
|
173 |
SELECT x FROM xy ORDER BY x COLLATE "reverse sort"
|
sl@0
|
174 |
}
|
sl@0
|
175 |
} {one three two nosort}
|
sl@0
|
176 |
}
|
sl@0
|
177 |
|
sl@0
|
178 |
finish_test
|