os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/collate1.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
#
sl@0
     2
# 2001 September 15
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 page cache subsystem.
sl@0
    14
#
sl@0
    15
# $Id: collate1.test,v 1.5 2007/02/01 23:02:46 drh Exp $
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
sl@0
    20
#
sl@0
    21
# Tests are roughly organised as follows:
sl@0
    22
#
sl@0
    23
# collate1-1.* - Single-field ORDER BY with an explicit COLLATE clause.
sl@0
    24
# collate1-2.* - Multi-field ORDER BY with an explicit COLLATE clause.
sl@0
    25
# collate1-3.* - ORDER BY using a default collation type. Also that an 
sl@0
    26
#                explict collate type overrides a default collate type.
sl@0
    27
# collate1-4.* - ORDER BY using a data type.
sl@0
    28
#
sl@0
    29
sl@0
    30
#
sl@0
    31
# Collation type 'HEX'. If an argument can be interpreted as a hexadecimal
sl@0
    32
# number, then it is converted to one before the comparison is performed. 
sl@0
    33
# Numbers are less than other strings. If neither argument is a number, 
sl@0
    34
# [string compare] is used.
sl@0
    35
#
sl@0
    36
db collate HEX hex_collate
sl@0
    37
proc hex_collate {lhs rhs} {
sl@0
    38
  set lhs_ishex [regexp {^(0x|)[1234567890abcdefABCDEF]+$} $lhs]
sl@0
    39
  set rhs_ishex [regexp {^(0x|)[1234567890abcdefABCDEF]+$} $rhs]
sl@0
    40
  if {$lhs_ishex && $rhs_ishex} { 
sl@0
    41
    set lhsx [scan $lhs %x]
sl@0
    42
    set rhsx [scan $rhs %x]
sl@0
    43
    if {$lhs < $rhs} {return -1}
sl@0
    44
    if {$lhs == $rhs} {return 0}
sl@0
    45
    if {$lhs > $rhs} {return 1}
sl@0
    46
  }
sl@0
    47
  if {$lhs_ishex} {
sl@0
    48
    return -1;
sl@0
    49
  }
sl@0
    50
  if {$rhs_ishex} {
sl@0
    51
    return 1;
sl@0
    52
  }
sl@0
    53
  return [string compare $lhs $rhs]
sl@0
    54
}
sl@0
    55
db function hex {format 0x%X}
sl@0
    56
sl@0
    57
# Mimic the SQLite 2 collation type NUMERIC.
sl@0
    58
db collate numeric numeric_collate
sl@0
    59
proc numeric_collate {lhs rhs} {
sl@0
    60
  if {$lhs == $rhs} {return 0} 
sl@0
    61
  return [expr ($lhs>$rhs)?1:-1]
sl@0
    62
}
sl@0
    63
sl@0
    64
do_test collate1-1.0 {
sl@0
    65
  execsql {
sl@0
    66
    CREATE TABLE collate1t1(c1, c2);
sl@0
    67
    INSERT INTO collate1t1 VALUES(45, hex(45));
sl@0
    68
    INSERT INTO collate1t1 VALUES(NULL, NULL);
sl@0
    69
    INSERT INTO collate1t1 VALUES(281, hex(281));
sl@0
    70
  }
sl@0
    71
} {}
sl@0
    72
do_test collate1-1.1 {
sl@0
    73
  execsql {
sl@0
    74
    SELECT c2 FROM collate1t1 ORDER BY 1;
sl@0
    75
  }
sl@0
    76
} {{} 0x119 0x2D}
sl@0
    77
do_test collate1-1.2 {
sl@0
    78
  execsql {
sl@0
    79
    SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex;
sl@0
    80
  }
sl@0
    81
} {{} 0x2D 0x119}
sl@0
    82
do_test collate1-1.3 {
sl@0
    83
  execsql {
sl@0
    84
    SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex DESC;
sl@0
    85
  }
sl@0
    86
} {0x119 0x2D {}}
sl@0
    87
do_test collate1-1.4 {
sl@0
    88
  execsql {
sl@0
    89
   SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex ASC;
sl@0
    90
  }
sl@0
    91
} {{} 0x2D 0x119}
sl@0
    92
do_test collate1-1.5 {
sl@0
    93
  execsql {
sl@0
    94
    SELECT c2 COLLATE hex FROM collate1t1 ORDER BY 1
sl@0
    95
  }
sl@0
    96
} {{} 0x2D 0x119}
sl@0
    97
do_test collate1-1.6 {
sl@0
    98
  execsql {
sl@0
    99
    SELECT c2 COLLATE hex FROM collate1t1 ORDER BY 1 ASC
sl@0
   100
  }
sl@0
   101
} {{} 0x2D 0x119}
sl@0
   102
do_test collate1-1.7 {
sl@0
   103
  execsql {
sl@0
   104
    SELECT c2 COLLATE hex FROM collate1t1 ORDER BY 1 DESC
sl@0
   105
  }
sl@0
   106
} {0x119 0x2D {}}
sl@0
   107
do_test collate1-1.99 {
sl@0
   108
  execsql {
sl@0
   109
    DROP TABLE collate1t1;
sl@0
   110
  }
sl@0
   111
} {}
sl@0
   112
sl@0
   113
do_test collate1-2.0 {
sl@0
   114
  execsql {
sl@0
   115
    CREATE TABLE collate1t1(c1, c2);
sl@0
   116
    INSERT INTO collate1t1 VALUES('5', '0x11');
sl@0
   117
    INSERT INTO collate1t1 VALUES('5', '0xA');
sl@0
   118
    INSERT INTO collate1t1 VALUES(NULL, NULL);
sl@0
   119
    INSERT INTO collate1t1 VALUES('7', '0xA');
sl@0
   120
    INSERT INTO collate1t1 VALUES('11', '0x11');
sl@0
   121
    INSERT INTO collate1t1 VALUES('11', '0x101');
sl@0
   122
  }
sl@0
   123
} {}
sl@0
   124
do_test collate1-2.2 {
sl@0
   125
  execsql {
sl@0
   126
    SELECT c1, c2 FROM collate1t1 ORDER BY 1 COLLATE numeric, 2 COLLATE hex;
sl@0
   127
  }
sl@0
   128
} {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
sl@0
   129
do_test collate1-2.3 {
sl@0
   130
  execsql {
sl@0
   131
    SELECT c1, c2 FROM collate1t1 ORDER BY 1 COLLATE binary, 2 COLLATE hex;
sl@0
   132
  }
sl@0
   133
} {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA}
sl@0
   134
do_test collate1-2.4 {
sl@0
   135
  execsql {
sl@0
   136
    SELECT c1, c2 FROM collate1t1 ORDER BY 1 COLLATE binary DESC, 2 COLLATE hex;
sl@0
   137
  }
sl@0
   138
} {7 0xA 5 0xA 5 0x11 11 0x11 11 0x101 {} {}}
sl@0
   139
do_test collate1-2.5 {
sl@0
   140
  execsql {
sl@0
   141
    SELECT c1, c2 FROM collate1t1 
sl@0
   142
        ORDER BY 1 COLLATE binary DESC, 2 COLLATE hex DESC;
sl@0
   143
  }
sl@0
   144
} {7 0xA 5 0x11 5 0xA 11 0x101 11 0x11 {} {}}
sl@0
   145
do_test collate1-2.6 {
sl@0
   146
  execsql {
sl@0
   147
    SELECT c1, c2 FROM collate1t1 
sl@0
   148
        ORDER BY 1 COLLATE binary ASC, 2 COLLATE hex ASC;
sl@0
   149
  }
sl@0
   150
} {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA}
sl@0
   151
do_test collate1-2.12.1 {
sl@0
   152
  execsql {
sl@0
   153
    SELECT c1 COLLATE numeric, c2 FROM collate1t1 
sl@0
   154
     ORDER BY 1, 2 COLLATE hex;
sl@0
   155
  }
sl@0
   156
} {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
sl@0
   157
do_test collate1-2.12.2 {
sl@0
   158
  execsql {
sl@0
   159
    SELECT c1 COLLATE hex, c2 FROM collate1t1 
sl@0
   160
     ORDER BY 1 COLLATE numeric, 2 COLLATE hex;
sl@0
   161
  }
sl@0
   162
} {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
sl@0
   163
do_test collate1-2.12.3 {
sl@0
   164
  execsql {
sl@0
   165
    SELECT c1, c2 COLLATE hex FROM collate1t1 
sl@0
   166
     ORDER BY 1 COLLATE numeric, 2;
sl@0
   167
  }
sl@0
   168
} {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
sl@0
   169
do_test collate1-2.12.4 {
sl@0
   170
  execsql {
sl@0
   171
    SELECT c1 COLLATE numeric, c2 COLLATE hex
sl@0
   172
      FROM collate1t1 
sl@0
   173
     ORDER BY 1, 2;
sl@0
   174
  }
sl@0
   175
} {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
sl@0
   176
do_test collate1-2.13 {
sl@0
   177
  execsql {
sl@0
   178
    SELECT c1 COLLATE binary, c2 COLLATE hex
sl@0
   179
      FROM collate1t1
sl@0
   180
     ORDER BY 1, 2;
sl@0
   181
  }
sl@0
   182
} {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA}
sl@0
   183
do_test collate1-2.14 {
sl@0
   184
  execsql {
sl@0
   185
    SELECT c1, c2
sl@0
   186
      FROM collate1t1 ORDER BY 1 COLLATE binary DESC, 2 COLLATE hex;
sl@0
   187
  }
sl@0
   188
} {7 0xA 5 0xA 5 0x11 11 0x11 11 0x101 {} {}}
sl@0
   189
do_test collate1-2.15 {
sl@0
   190
  execsql {
sl@0
   191
    SELECT c1 COLLATE binary, c2 COLLATE hex
sl@0
   192
      FROM collate1t1 
sl@0
   193
     ORDER BY 1 DESC, 2 DESC;
sl@0
   194
  }
sl@0
   195
} {7 0xA 5 0x11 5 0xA 11 0x101 11 0x11 {} {}}
sl@0
   196
do_test collate1-2.16 {
sl@0
   197
  execsql {
sl@0
   198
    SELECT c1 COLLATE hex, c2 COLLATE binary
sl@0
   199
      FROM collate1t1 
sl@0
   200
     ORDER BY 1 COLLATE binary ASC, 2 COLLATE hex ASC;
sl@0
   201
  }
sl@0
   202
} {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA}
sl@0
   203
do_test collate1-2.99 {
sl@0
   204
  execsql {
sl@0
   205
    DROP TABLE collate1t1;
sl@0
   206
  }
sl@0
   207
} {}
sl@0
   208
sl@0
   209
#
sl@0
   210
# These tests ensure that the default collation type for a column is used 
sl@0
   211
# by an ORDER BY clause correctly. The focus is all the different ways
sl@0
   212
# the column can be referenced. i.e. a, collate2t1.a, main.collate2t1.a etc.
sl@0
   213
#
sl@0
   214
do_test collate1-3.0 {
sl@0
   215
  execsql {
sl@0
   216
    CREATE TABLE collate1t1(a COLLATE hex, b);
sl@0
   217
    INSERT INTO collate1t1 VALUES( '0x5', 5 );
sl@0
   218
    INSERT INTO collate1t1 VALUES( '1', 1 );
sl@0
   219
    INSERT INTO collate1t1 VALUES( '0x45', 69 );
sl@0
   220
    INSERT INTO collate1t1 VALUES( NULL, NULL );
sl@0
   221
    SELECT * FROM collate1t1 ORDER BY a;
sl@0
   222
  }
sl@0
   223
} {{} {} 1 1 0x5 5 0x45 69}
sl@0
   224
sl@0
   225
do_test collate1-3.1 {
sl@0
   226
  execsql {
sl@0
   227
    SELECT * FROM collate1t1 ORDER BY 1;
sl@0
   228
  }
sl@0
   229
} {{} {} 1 1 0x5 5 0x45 69}
sl@0
   230
do_test collate1-3.2 {
sl@0
   231
  execsql {
sl@0
   232
    SELECT * FROM collate1t1 ORDER BY collate1t1.a;
sl@0
   233
  }
sl@0
   234
} {{} {} 1 1 0x5 5 0x45 69}
sl@0
   235
do_test collate1-3.3 {
sl@0
   236
  execsql {
sl@0
   237
    SELECT * FROM collate1t1 ORDER BY main.collate1t1.a;
sl@0
   238
  }
sl@0
   239
} {{} {} 1 1 0x5 5 0x45 69}
sl@0
   240
do_test collate1-3.4 {
sl@0
   241
  execsql {
sl@0
   242
    SELECT a as c1, b as c2 FROM collate1t1 ORDER BY c1;
sl@0
   243
  }
sl@0
   244
} {{} {} 1 1 0x5 5 0x45 69}
sl@0
   245
do_test collate1-3.5 {
sl@0
   246
  execsql {
sl@0
   247
    SELECT a as c1, b as c2 FROM collate1t1 ORDER BY c1 COLLATE binary;
sl@0
   248
  }
sl@0
   249
} {{} {} 0x45 69 0x5 5 1 1}
sl@0
   250
do_test collate1-3.5.1 {
sl@0
   251
  execsql {
sl@0
   252
    SELECT a COLLATE binary as c1, b as c2
sl@0
   253
      FROM collate1t1 ORDER BY c1;
sl@0
   254
  }
sl@0
   255
} {{} {} 0x45 69 0x5 5 1 1}
sl@0
   256
do_test collate1-3.6 {
sl@0
   257
  execsql {
sl@0
   258
    DROP TABLE collate1t1;
sl@0
   259
  }
sl@0
   260
} {}
sl@0
   261
sl@0
   262
# Update for SQLite version 3. The collate1-4.* test cases were written
sl@0
   263
# before manifest types were introduced. The following test cases still
sl@0
   264
# work, due to the 'affinity' mechanism, but they don't prove anything
sl@0
   265
# about collation sequences.
sl@0
   266
#
sl@0
   267
do_test collate1-4.0 {
sl@0
   268
  execsql {
sl@0
   269
    CREATE TABLE collate1t1(c1 numeric, c2 text);
sl@0
   270
    INSERT INTO collate1t1 VALUES(1, 1);
sl@0
   271
    INSERT INTO collate1t1 VALUES(12, 12);
sl@0
   272
    INSERT INTO collate1t1 VALUES(NULL, NULL);
sl@0
   273
    INSERT INTO collate1t1 VALUES(101, 101);
sl@0
   274
  }
sl@0
   275
} {}
sl@0
   276
do_test collate1-4.1 {
sl@0
   277
  execsql {
sl@0
   278
    SELECT c1 FROM collate1t1 ORDER BY 1;
sl@0
   279
  }
sl@0
   280
} {{} 1 12 101}
sl@0
   281
do_test collate1-4.2 {
sl@0
   282
  execsql {
sl@0
   283
    SELECT c2 FROM collate1t1 ORDER BY 1;
sl@0
   284
  }
sl@0
   285
} {{} 1 101 12}
sl@0
   286
do_test collate1-4.3 {
sl@0
   287
  execsql {
sl@0
   288
    SELECT c2+0 FROM collate1t1 ORDER BY 1;
sl@0
   289
  }
sl@0
   290
} {{} 1 12 101}
sl@0
   291
do_test collate1-4.4 {
sl@0
   292
  execsql {
sl@0
   293
    SELECT c1||'' FROM collate1t1 ORDER BY 1;
sl@0
   294
  }
sl@0
   295
} {{} 1 101 12}
sl@0
   296
do_test collate1-4.4.1 {
sl@0
   297
  execsql {
sl@0
   298
    SELECT (c1||'') COLLATE numeric FROM collate1t1 ORDER BY 1;
sl@0
   299
  }
sl@0
   300
} {{} 1 12 101}
sl@0
   301
do_test collate1-4.5 {
sl@0
   302
  execsql {
sl@0
   303
    DROP TABLE collate1t1;
sl@0
   304
  }
sl@0
   305
} {}
sl@0
   306
sl@0
   307
finish_test