summaryrefslogtreecommitdiff
path: root/test/collate9.test
blob: 5a07773a1ce2fbf999c29b290ac52badb43bed87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#
# 2007 November 12
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is making sure that the names of collation
# sequences may be quoted using double quotes in SQL statements.
#
# $Id: collate9.test,v 1.2 2008/07/10 00:32:42 drh Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

proc reverse_sort {lhs rhs} {
  return [string compare $rhs $lhs]
}
db collate "reverse sort" reverse_sort

# This procedure executes the SQL.  Then it checks to see if the OP_Sort
# opcode was executed.  If an OP_Sort did occur, then "sort" is appended
# to the result.  If no OP_Sort happened, then "nosort" is appended.
#
# This procedure is used to check to make sure sorting is or is not
# occurring as expected.
#
proc cksort {sql} {
  set ::sqlite_sort_count 0
  set data [execsql $sql]
  if {$::sqlite_sort_count} {set x sort} {set x nosort}
  lappend data $x
  return $data
}

# Test plan:
#
#     collate9-1.* - Test collation sequences attached to table columns
#     collate9-2.* - Test collation sequences attached to expressions
#     collate9-3.* - Test collation sequences attached to an index
#     collate9-4.* - Test collation sequences as an argument to REINDEX
#

do_test collate9-1.1 {
  execsql {
    CREATE TABLE xy(x COLLATE "reverse sort", y COLLATE binary);
    INSERT INTO xy VALUES('one', 'one');
    INSERT INTO xy VALUES('two', 'two');
    INSERT INTO xy VALUES('three', 'three');
  }
} {}
do_test collate9-1.2 {
  execsql { 
    SELECT x FROM xy ORDER BY x
  }
} {two three one}
do_test collate9-1.3 {
  execsql { 
    SELECT y FROM xy ORDER BY y
  }
} {one three two}
do_test collate9-1.4 {
  cksort { 
    SELECT x FROM xy ORDER BY x
  }
} {two three one sort}
do_test collate9-1.5 {
  execsql { 
    CREATE INDEX xy_i ON xy(x)
  }
} {}
do_test collate9-1.6 {
  cksort { 
    SELECT x FROM xy ORDER BY x
  }
} {two three one nosort}

do_test collate9-2.1 {
  execsql { 
    SELECT x, x < 'seven' FROM xy ORDER BY x
  }
} {two 1 three 1 one 0}
do_test collate9-2.2 {
  execsql { 
    SELECT y, y < 'seven' FROM xy ORDER BY x
  }
} {two 0 three 0 one 1}
do_test collate9-2.3 {
  execsql { 
    SELECT y, y COLLATE "reverse sort" < 'seven' FROM xy ORDER BY x
  }
} {two 1 three 1 one 0}
do_test collate9-2.4 {
  execsql {
    SELECT y FROM xy ORDER BY y
  }
} {one three two}
do_test collate9-2.5 {
  execsql {
    SELECT y FROM xy ORDER BY y COLLATE "reverse sort"
  }
} {two three one}
do_test collate9-2.6 {
  execsql {
    SELECT y COLLATE "reverse sort" AS aaa FROM xy ORDER BY aaa
  }
} {two three one}

do_test collate9-3.1 {
  execsql {
    CREATE INDEX xy_i2 ON xy(y COLLATE "reverse sort");
  }
} {}
do_test collate9-3.2 {
  cksort { 
    SELECT y FROM xy ORDER BY y 
  }
} {one three two sort}
do_test collate9-3.3 {
  cksort { 
    SELECT y FROM xy ORDER BY y COLLATE "reverse sort"
  }
} {two three one nosort}
do_test collate9-3.4 {
  cksort { 
    SELECT y AS aaa FROM xy ORDER BY aaa
  }
} {one three two sort}
do_test collate9-3.5 {
  cksort { 
    SELECT y COLLATE "reverse sort" AS aaa FROM xy ORDER BY aaa
  }
} {two three one nosort}

ifcapable reindex {
  do_test collate9-4.1 {
    execsql {
      REINDEX "reverse sort"
    }
  } {}

  # Modify the "reverse sort" collation so that it now sorts in the same
  # order as binary.
  proc reverse_sort {lhs rhs} {
    return [string compare $lhs $rhs]
  }

  # The integrity check should now fail because the indexes created using
  # "reverse sort" are no longer in sync with the collation sequence
  # implementation.
  do_test collate9-4.2 {
    expr {"ok" eq [execsql { PRAGMA integrity_check }]}
  } {0}

  do_test collate9-4.3 {
    execsql {
      REINDEX "reverse sort"
    }
  } {}

  # Integrity check should now pass.
  do_test collate9-4.4 {
    expr {"ok" eq [execsql { PRAGMA integrity_check }]}
  } {1}

  do_test collate9-4.5 {
    cksort {
      SELECT x FROM xy ORDER BY x COLLATE "reverse sort"
    }
  } {one three two nosort}
}

finish_test