os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/enc2.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
# 2002 May 24
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The focus of
sl@0
    12
# this file is testing the SQLite routines used for converting between the
sl@0
    13
# various suported unicode encodings (UTF-8, UTF-16, UTF-16le and
sl@0
    14
# UTF-16be).
sl@0
    15
#
sl@0
    16
# $Id: enc2.test,v 1.29 2007/10/09 08:29:32 danielk1977 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
# If UTF16 support is disabled, ignore the tests in this file
sl@0
    22
#
sl@0
    23
ifcapable {!utf16} {
sl@0
    24
  finish_test
sl@0
    25
  return
sl@0
    26
}
sl@0
    27
sl@0
    28
# The rough organisation of tests in this file is:
sl@0
    29
#
sl@0
    30
# enc2.1.*: Simple tests with a UTF-8 db.
sl@0
    31
# enc2.2.*: Simple tests with a UTF-16LE db.
sl@0
    32
# enc2.3.*: Simple tests with a UTF-16BE db.
sl@0
    33
# enc2.4.*: Test that attached databases must have the same text encoding
sl@0
    34
#           as the main database.
sl@0
    35
# enc2.5.*: Test the behaviour of the library when a collation sequence is
sl@0
    36
#           not available for the most desirable text encoding.
sl@0
    37
# enc2.6.*: Similar test for user functions.
sl@0
    38
# enc2.7.*: Test that the VerifyCookie opcode protects against assuming the
sl@0
    39
#           wrong text encoding for the database.
sl@0
    40
# enc2.8.*: Test sqlite3_complete16()
sl@0
    41
#
sl@0
    42
sl@0
    43
db close
sl@0
    44
sl@0
    45
# Return the UTF-8 representation of the supplied UTF-16 string $str. 
sl@0
    46
proc utf8 {str} {
sl@0
    47
  # If $str ends in two 0x00 0x00 bytes, knock these off before
sl@0
    48
  # converting to UTF-8 using TCL.
sl@0
    49
  binary scan $str \c* vals
sl@0
    50
  if {[lindex $vals end]==0 && [lindex $vals end-1]==0} {
sl@0
    51
    set str [binary format \c* [lrange $vals 0 end-2]]
sl@0
    52
  }
sl@0
    53
sl@0
    54
  set r [encoding convertfrom unicode $str]
sl@0
    55
  return $r
sl@0
    56
}
sl@0
    57
sl@0
    58
#
sl@0
    59
# This proc contains all the tests in this file. It is run
sl@0
    60
# three times. Each time the file 'test.db' contains a database
sl@0
    61
# with the following contents:
sl@0
    62
set dbcontents {
sl@0
    63
  CREATE TABLE t1(a PRIMARY KEY, b, c);
sl@0
    64
  INSERT INTO t1 VALUES('one', 'I', 1);
sl@0
    65
}
sl@0
    66
# This proc tests that we can open and manipulate the test.db 
sl@0
    67
# database, and that it is possible to retreive values in
sl@0
    68
# various text encodings.
sl@0
    69
#
sl@0
    70
proc run_test_script {t enc} {
sl@0
    71
sl@0
    72
# Open the database and pull out a (the) row.
sl@0
    73
do_test $t.1 {
sl@0
    74
  sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
    75
  execsql {SELECT * FROM t1}
sl@0
    76
} {one I 1}
sl@0
    77
sl@0
    78
# Insert some data
sl@0
    79
do_test $t.2 {
sl@0
    80
  execsql {INSERT INTO t1 VALUES('two', 'II', 2);}
sl@0
    81
  execsql {SELECT * FROM t1}
sl@0
    82
} {one I 1 two II 2}
sl@0
    83
sl@0
    84
# Insert some data 
sl@0
    85
do_test $t.3 {
sl@0
    86
  execsql {
sl@0
    87
    INSERT INTO t1 VALUES('three','III',3);
sl@0
    88
    INSERT INTO t1 VALUES('four','IV',4);
sl@0
    89
    INSERT INTO t1 VALUES('five','V',5);
sl@0
    90
  }
sl@0
    91
  execsql {SELECT * FROM t1}
sl@0
    92
} {one I 1 two II 2 three III 3 four IV 4 five V 5}
sl@0
    93
sl@0
    94
# Use the index
sl@0
    95
do_test $t.4 {
sl@0
    96
  execsql {
sl@0
    97
    SELECT * FROM t1 WHERE a = 'one';
sl@0
    98
  }
sl@0
    99
} {one I 1}
sl@0
   100
do_test $t.5 {
sl@0
   101
  execsql {
sl@0
   102
    SELECT * FROM t1 WHERE a = 'four';
sl@0
   103
  }
sl@0
   104
} {four IV 4}
sl@0
   105
ifcapable subquery {
sl@0
   106
  do_test $t.6 {
sl@0
   107
    execsql {
sl@0
   108
      SELECT * FROM t1 WHERE a IN ('one', 'two');
sl@0
   109
    }
sl@0
   110
  } {one I 1 two II 2}
sl@0
   111
}
sl@0
   112
sl@0
   113
# Now check that we can retrieve data in both UTF-16 and UTF-8
sl@0
   114
do_test $t.7 {
sl@0
   115
  set STMT [sqlite3_prepare $DB "SELECT a FROM t1 WHERE c>3;" -1 TAIL]
sl@0
   116
  sqlite3_step $STMT
sl@0
   117
  sqlite3_column_text $STMT 0
sl@0
   118
} {four}
sl@0
   119
sl@0
   120
do_test $t.8 {
sl@0
   121
  sqlite3_step $STMT
sl@0
   122
  utf8 [sqlite3_column_text16 $STMT 0]
sl@0
   123
} {five}
sl@0
   124
sl@0
   125
do_test $t.9 {
sl@0
   126
  sqlite3_finalize $STMT
sl@0
   127
} SQLITE_OK
sl@0
   128
sl@0
   129
ifcapable vacuum {
sl@0
   130
  execsql VACUUM
sl@0
   131
}
sl@0
   132
sl@0
   133
do_test $t.10 {
sl@0
   134
  db eval {PRAGMA encoding}
sl@0
   135
} $enc
sl@0
   136
sl@0
   137
}
sl@0
   138
sl@0
   139
# The three unicode encodings understood by SQLite.
sl@0
   140
set encodings [list UTF-8 UTF-16le UTF-16be]
sl@0
   141
sl@0
   142
set sqlite_os_trace 0
sl@0
   143
set i 1
sl@0
   144
foreach enc $encodings {
sl@0
   145
  file delete -force test.db
sl@0
   146
  sqlite3 db test.db
sl@0
   147
  db eval "PRAGMA encoding = \"$enc\""
sl@0
   148
  execsql $dbcontents
sl@0
   149
  do_test enc2-$i.0.1 {
sl@0
   150
    db eval {PRAGMA encoding}
sl@0
   151
  } $enc
sl@0
   152
  do_test enc2-$i.0.2 {
sl@0
   153
    db eval {PRAGMA encoding=UTF8}
sl@0
   154
    db eval {PRAGMA encoding}
sl@0
   155
  } $enc
sl@0
   156
  do_test enc2-$i.0.3 {
sl@0
   157
    db eval {PRAGMA encoding=UTF16le}
sl@0
   158
    db eval {PRAGMA encoding}
sl@0
   159
  } $enc
sl@0
   160
  do_test enc2-$i.0.4 {
sl@0
   161
    db eval {PRAGMA encoding=UTF16be}
sl@0
   162
    db eval {PRAGMA encoding}
sl@0
   163
  } $enc
sl@0
   164
sl@0
   165
  db close
sl@0
   166
  run_test_script enc2-$i $enc
sl@0
   167
  db close
sl@0
   168
  incr i
sl@0
   169
}
sl@0
   170
sl@0
   171
# Test that it is an error to try to attach a database with a different
sl@0
   172
# encoding to the main database.
sl@0
   173
ifcapable attach {
sl@0
   174
  do_test enc2-4.1 {
sl@0
   175
    file delete -force test.db
sl@0
   176
    sqlite3 db test.db
sl@0
   177
    db eval "PRAGMA encoding = 'UTF-8'"
sl@0
   178
    db eval "CREATE TABLE abc(a, b, c);"
sl@0
   179
  } {}
sl@0
   180
  do_test enc2-4.2 {
sl@0
   181
    file delete -force test2.db
sl@0
   182
    sqlite3 db2 test2.db
sl@0
   183
    db2 eval "PRAGMA encoding = 'UTF-16'"
sl@0
   184
    db2 eval "CREATE TABLE abc(a, b, c);"
sl@0
   185
  } {}
sl@0
   186
  do_test enc2-4.3 {
sl@0
   187
    catchsql {
sl@0
   188
      ATTACH 'test2.db' as aux;
sl@0
   189
    }
sl@0
   190
  } {1 {attached databases must use the same text encoding as main database}}
sl@0
   191
  db2 close
sl@0
   192
  db close
sl@0
   193
}
sl@0
   194
sl@0
   195
# The following tests - enc2-5.* - test that SQLite selects the correct
sl@0
   196
# collation sequence when more than one is available.
sl@0
   197
sl@0
   198
set ::values [list one two three four five]
sl@0
   199
set ::test_collate_enc INVALID
sl@0
   200
proc test_collate {enc lhs rhs} {
sl@0
   201
  set ::test_collate_enc $enc
sl@0
   202
  set l [lsearch -exact $::values $lhs]
sl@0
   203
  set r [lsearch -exact $::values $rhs]
sl@0
   204
  set res [expr $l - $r]
sl@0
   205
  # puts "enc=$enc lhs=$lhs/$l rhs=$rhs/$r res=$res"
sl@0
   206
  return $res
sl@0
   207
}
sl@0
   208
sl@0
   209
file delete -force test.db
sl@0
   210
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   211
do_test enc2-5.0 {
sl@0
   212
  execsql {
sl@0
   213
    CREATE TABLE t5(a);
sl@0
   214
    INSERT INTO t5 VALUES('one');
sl@0
   215
    INSERT INTO t5 VALUES('two');
sl@0
   216
    INSERT INTO t5 VALUES('five');
sl@0
   217
    INSERT INTO t5 VALUES('three');
sl@0
   218
    INSERT INTO t5 VALUES('four');
sl@0
   219
  }
sl@0
   220
} {}
sl@0
   221
do_test enc2-5.1 {
sl@0
   222
  add_test_collate $DB 1 1 1
sl@0
   223
  set res [execsql {SELECT * FROM t5 ORDER BY 1 COLLATE test_collate;}]
sl@0
   224
  lappend res $::test_collate_enc
sl@0
   225
} {one two three four five UTF-8}
sl@0
   226
do_test enc2-5.2 {
sl@0
   227
  add_test_collate $DB 0 1 0
sl@0
   228
  set res [execsql {SELECT * FROM t5 ORDER BY 1 COLLATE test_collate}]
sl@0
   229
  lappend res $::test_collate_enc
sl@0
   230
} {one two three four five UTF-16LE}
sl@0
   231
do_test enc2-5.3 {
sl@0
   232
  add_test_collate $DB 0 0 1
sl@0
   233
  set res [execsql {SELECT * FROM t5 ORDER BY 1 COLLATE test_collate}]
sl@0
   234
  lappend res $::test_collate_enc
sl@0
   235
} {one two three four five UTF-16BE}
sl@0
   236
sl@0
   237
db close
sl@0
   238
file delete -force test.db
sl@0
   239
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   240
execsql {pragma encoding = 'UTF-16LE'}
sl@0
   241
do_test enc2-5.4 {
sl@0
   242
  execsql {
sl@0
   243
    CREATE TABLE t5(a);
sl@0
   244
    INSERT INTO t5 VALUES('one');
sl@0
   245
    INSERT INTO t5 VALUES('two');
sl@0
   246
    INSERT INTO t5 VALUES('five');
sl@0
   247
    INSERT INTO t5 VALUES('three');
sl@0
   248
    INSERT INTO t5 VALUES('four');
sl@0
   249
  }
sl@0
   250
} {}
sl@0
   251
do_test enc2-5.5 {
sl@0
   252
  add_test_collate $DB 1 1 1
sl@0
   253
  set res [execsql {SELECT * FROM t5 ORDER BY 1 COLLATE test_collate}]
sl@0
   254
  lappend res $::test_collate_enc
sl@0
   255
} {one two three four five UTF-16LE}
sl@0
   256
do_test enc2-5.6 {
sl@0
   257
  add_test_collate $DB 1 0 1
sl@0
   258
  set res [execsql {SELECT * FROM t5 ORDER BY 1 COLLATE test_collate}]
sl@0
   259
  lappend res $::test_collate_enc
sl@0
   260
} {one two three four five UTF-16BE}
sl@0
   261
do_test enc2-5.7 {
sl@0
   262
  add_test_collate $DB 1 0 0
sl@0
   263
  set res [execsql {SELECT * FROM t5 ORDER BY 1 COLLATE test_collate}]
sl@0
   264
  lappend res $::test_collate_enc
sl@0
   265
} {one two three four five UTF-8}
sl@0
   266
sl@0
   267
db close
sl@0
   268
file delete -force test.db
sl@0
   269
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   270
execsql {pragma encoding = 'UTF-16BE'}
sl@0
   271
do_test enc2-5.8 {
sl@0
   272
  execsql {
sl@0
   273
    CREATE TABLE t5(a);
sl@0
   274
    INSERT INTO t5 VALUES('one');
sl@0
   275
    INSERT INTO t5 VALUES('two');
sl@0
   276
    INSERT INTO t5 VALUES('five');
sl@0
   277
    INSERT INTO t5 VALUES('three');
sl@0
   278
    INSERT INTO t5 VALUES('four');
sl@0
   279
  }
sl@0
   280
} {}
sl@0
   281
do_test enc2-5.9 {
sl@0
   282
  add_test_collate $DB 1 1 1
sl@0
   283
  set res [execsql {SELECT * FROM t5 ORDER BY 1 COLLATE test_collate}]
sl@0
   284
  lappend res $::test_collate_enc
sl@0
   285
} {one two three four five UTF-16BE}
sl@0
   286
do_test enc2-5.10 {
sl@0
   287
  add_test_collate $DB 1 1 0
sl@0
   288
  set res [execsql {SELECT * FROM t5 ORDER BY 1 COLLATE test_collate}]
sl@0
   289
  lappend res $::test_collate_enc
sl@0
   290
} {one two three four five UTF-16LE}
sl@0
   291
do_test enc2-5.11 {
sl@0
   292
  add_test_collate $DB 1 0 0
sl@0
   293
  set res [execsql {SELECT * FROM t5 ORDER BY 1 COLLATE test_collate}]
sl@0
   294
  lappend res $::test_collate_enc
sl@0
   295
} {one two three four five UTF-8}
sl@0
   296
sl@0
   297
# Also test that a UTF-16 collation factory works.
sl@0
   298
do_test enc2-5-12 {
sl@0
   299
  add_test_collate $DB 0 0 0
sl@0
   300
  catchsql {
sl@0
   301
    SELECT * FROM t5 ORDER BY 1 COLLATE test_collate
sl@0
   302
  }
sl@0
   303
} {1 {no such collation sequence: test_collate}}
sl@0
   304
do_test enc2-5.13 {
sl@0
   305
  add_test_collate_needed $DB 
sl@0
   306
  set res [execsql {SELECT * FROM t5 ORDER BY 1 COLLATE test_collate; }]
sl@0
   307
  lappend res $::test_collate_enc
sl@0
   308
} {one two three four five UTF-16BE}
sl@0
   309
do_test enc2-5.14 {
sl@0
   310
  set ::sqlite_last_needed_collation
sl@0
   311
} test_collate
sl@0
   312
sl@0
   313
db close
sl@0
   314
file delete -force test.db
sl@0
   315
sl@0
   316
do_test enc2-5.15 {
sl@0
   317
  sqlite3 db test.db; set ::DB [sqlite3_connection_pointer db]
sl@0
   318
  add_test_collate_needed $::DB
sl@0
   319
  set ::sqlite_last_needed_collation
sl@0
   320
} {}
sl@0
   321
do_test enc2-5.16 {
sl@0
   322
  execsql {CREATE TABLE t1(a varchar collate test_collate);}
sl@0
   323
} {}
sl@0
   324
do_test enc2-5.17 {
sl@0
   325
  set ::sqlite_last_needed_collation
sl@0
   326
} {test_collate}
sl@0
   327
sl@0
   328
# The following tests - enc2-6.* - test that SQLite selects the correct
sl@0
   329
# user function when more than one is available.
sl@0
   330
sl@0
   331
proc test_function {enc arg} {
sl@0
   332
  return "$enc $arg"
sl@0
   333
}
sl@0
   334
sl@0
   335
db close
sl@0
   336
file delete -force test.db
sl@0
   337
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   338
execsql {pragma encoding = 'UTF-8'}
sl@0
   339
do_test enc2-6.0 {
sl@0
   340
  execsql {
sl@0
   341
    CREATE TABLE t5(a);
sl@0
   342
    INSERT INTO t5 VALUES('one');
sl@0
   343
  }
sl@0
   344
} {}
sl@0
   345
do_test enc2-6.1 {
sl@0
   346
  add_test_function $DB 1 1 1
sl@0
   347
  execsql {
sl@0
   348
    SELECT test_function('sqlite')
sl@0
   349
  }
sl@0
   350
} {{UTF-8 sqlite}}
sl@0
   351
db close
sl@0
   352
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   353
do_test enc2-6.2 {
sl@0
   354
  add_test_function $DB 0 1 0
sl@0
   355
  execsql {
sl@0
   356
    SELECT test_function('sqlite')
sl@0
   357
  }
sl@0
   358
} {{UTF-16LE sqlite}}
sl@0
   359
db close
sl@0
   360
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   361
do_test enc2-6.3 {
sl@0
   362
  add_test_function $DB 0 0 1
sl@0
   363
  execsql {
sl@0
   364
    SELECT test_function('sqlite')
sl@0
   365
  }
sl@0
   366
} {{UTF-16BE sqlite}}
sl@0
   367
sl@0
   368
db close
sl@0
   369
file delete -force test.db
sl@0
   370
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   371
execsql {pragma encoding = 'UTF-16LE'}
sl@0
   372
do_test enc2-6.3 {
sl@0
   373
  execsql {
sl@0
   374
    CREATE TABLE t5(a);
sl@0
   375
    INSERT INTO t5 VALUES('sqlite');
sl@0
   376
  }
sl@0
   377
} {}
sl@0
   378
do_test enc2-6.4 {
sl@0
   379
  add_test_function $DB 1 1 1
sl@0
   380
  execsql {
sl@0
   381
    SELECT test_function('sqlite')
sl@0
   382
  }
sl@0
   383
} {{UTF-16LE sqlite}}
sl@0
   384
db close
sl@0
   385
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   386
do_test enc2-6.5 {
sl@0
   387
  add_test_function $DB 0 1 0
sl@0
   388
  execsql {
sl@0
   389
    SELECT test_function('sqlite')
sl@0
   390
  }
sl@0
   391
} {{UTF-16LE sqlite}}
sl@0
   392
db close
sl@0
   393
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   394
do_test enc2-6.6 {
sl@0
   395
  add_test_function $DB 0 0 1
sl@0
   396
  execsql {
sl@0
   397
    SELECT test_function('sqlite')
sl@0
   398
  }
sl@0
   399
} {{UTF-16BE sqlite}}
sl@0
   400
sl@0
   401
db close
sl@0
   402
file delete -force test.db
sl@0
   403
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   404
execsql {pragma encoding = 'UTF-16BE'}
sl@0
   405
do_test enc2-6.7 {
sl@0
   406
  execsql {
sl@0
   407
    CREATE TABLE t5(a);
sl@0
   408
    INSERT INTO t5 VALUES('sqlite');
sl@0
   409
  }
sl@0
   410
} {}
sl@0
   411
do_test enc2-6.8 {
sl@0
   412
  add_test_function $DB 1 1 1
sl@0
   413
  execsql {
sl@0
   414
    SELECT test_function('sqlite')
sl@0
   415
  }
sl@0
   416
} {{UTF-16BE sqlite}}
sl@0
   417
db close
sl@0
   418
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   419
do_test enc2-6.9 {
sl@0
   420
  add_test_function $DB 0 1 0
sl@0
   421
  execsql {
sl@0
   422
    SELECT test_function('sqlite')
sl@0
   423
  }
sl@0
   424
} {{UTF-16LE sqlite}}
sl@0
   425
db close
sl@0
   426
sqlite3 db test.db; set DB [sqlite3_connection_pointer db]
sl@0
   427
do_test enc2-6.10 {
sl@0
   428
  add_test_function $DB 0 0 1
sl@0
   429
  execsql {
sl@0
   430
    SELECT test_function('sqlite')
sl@0
   431
  }
sl@0
   432
} {{UTF-16BE sqlite}}
sl@0
   433
sl@0
   434
sl@0
   435
db close
sl@0
   436
file delete -force test.db
sl@0
   437
sl@0
   438
# The following tests - enc2-7.* - function as follows:
sl@0
   439
#
sl@0
   440
# 1: Open an empty database file assuming UTF-16 encoding.
sl@0
   441
# 2: Open the same database with a different handle assuming UTF-8. Create
sl@0
   442
#    a table using this handle.
sl@0
   443
# 3: Read the sqlite_master table from the first handle. 
sl@0
   444
# 4: Ensure the first handle recognises the database encoding is UTF-8.
sl@0
   445
#
sl@0
   446
do_test enc2-7.1 {
sl@0
   447
  sqlite3 db test.db
sl@0
   448
  execsql {
sl@0
   449
    PRAGMA encoding = 'UTF-16';
sl@0
   450
    SELECT * FROM sqlite_master;
sl@0
   451
  }
sl@0
   452
} {}
sl@0
   453
do_test enc2-7.2 {
sl@0
   454
  set enc [execsql {
sl@0
   455
    PRAGMA encoding;
sl@0
   456
  }]
sl@0
   457
  string range $enc 0 end-2 ;# Chop off the "le" or "be"
sl@0
   458
} {UTF-16}
sl@0
   459
do_test enc2-7.3 {
sl@0
   460
  sqlite3 db2 test.db
sl@0
   461
  execsql {
sl@0
   462
    PRAGMA encoding = 'UTF-8';
sl@0
   463
    CREATE TABLE abc(a, b, c);
sl@0
   464
  } db2
sl@0
   465
} {}
sl@0
   466
do_test enc2-7.4 {
sl@0
   467
  execsql {
sl@0
   468
    SELECT * FROM sqlite_master;
sl@0
   469
  }
sl@0
   470
} "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}"
sl@0
   471
do_test enc2-7.5 {
sl@0
   472
  execsql {
sl@0
   473
    PRAGMA encoding;
sl@0
   474
  }
sl@0
   475
} {UTF-8}
sl@0
   476
sl@0
   477
db close
sl@0
   478
db2 close
sl@0
   479
sl@0
   480
proc utf16 {utf8} {
sl@0
   481
  set utf16 [encoding convertto unicode $utf8]
sl@0
   482
  append utf16 "\x00\x00"
sl@0
   483
  return $utf16
sl@0
   484
}
sl@0
   485
ifcapable {complete} {
sl@0
   486
  do_test enc2-8.1 {
sl@0
   487
    sqlite3_complete16 [utf16 "SELECT * FROM t1;"]
sl@0
   488
  } {1}
sl@0
   489
  do_test enc2-8.2 {
sl@0
   490
    sqlite3_complete16 [utf16 "SELECT * FROM"]
sl@0
   491
  } {0}
sl@0
   492
}
sl@0
   493
sl@0
   494
# Test that the encoding of an empty database may still be set after the
sl@0
   495
# (empty) schema has been initialized.
sl@0
   496
file delete -force test.db
sl@0
   497
do_test enc2-9.1 {
sl@0
   498
  sqlite3 db test.db
sl@0
   499
  execsql {
sl@0
   500
    PRAGMA encoding = 'UTF-8';
sl@0
   501
    PRAGMA encoding;
sl@0
   502
  }
sl@0
   503
} {UTF-8}
sl@0
   504
do_test enc2-9.2 {
sl@0
   505
  sqlite3 db test.db
sl@0
   506
  execsql {
sl@0
   507
    PRAGMA encoding = 'UTF-16le';
sl@0
   508
    PRAGMA encoding;
sl@0
   509
  }
sl@0
   510
} {UTF-16le}
sl@0
   511
do_test enc2-9.3 {
sl@0
   512
  sqlite3 db test.db
sl@0
   513
  execsql {
sl@0
   514
    SELECT * FROM sqlite_master;
sl@0
   515
    PRAGMA encoding = 'UTF-8';
sl@0
   516
    PRAGMA encoding;
sl@0
   517
  }
sl@0
   518
} {UTF-8}
sl@0
   519
do_test enc2-9.4 {
sl@0
   520
  sqlite3 db test.db
sl@0
   521
  execsql {
sl@0
   522
    PRAGMA encoding = 'UTF-16le';
sl@0
   523
    CREATE TABLE abc(a, b, c);
sl@0
   524
    PRAGMA encoding;
sl@0
   525
  }
sl@0
   526
} {UTF-16le}
sl@0
   527
do_test enc2-9.5 {
sl@0
   528
  sqlite3 db test.db
sl@0
   529
  execsql {
sl@0
   530
    PRAGMA encoding = 'UTF-8';
sl@0
   531
    PRAGMA encoding;
sl@0
   532
  }
sl@0
   533
} {UTF-16le}
sl@0
   534
sl@0
   535
# Ticket #1987.
sl@0
   536
# Disallow encoding changes once the encoding has been set.
sl@0
   537
#
sl@0
   538
do_test enc2-10.1 {
sl@0
   539
  db close
sl@0
   540
  file delete -force test.db test.db-journal
sl@0
   541
  sqlite3 db test.db
sl@0
   542
  db eval {
sl@0
   543
    PRAGMA encoding=UTF16;
sl@0
   544
    CREATE TABLE t1(a);
sl@0
   545
    PRAGMA encoding=UTF8;
sl@0
   546
    CREATE TABLE t2(b);
sl@0
   547
  }
sl@0
   548
  db close
sl@0
   549
  sqlite3 db test.db
sl@0
   550
  db eval {
sl@0
   551
    SELECT name FROM sqlite_master
sl@0
   552
  }
sl@0
   553
} {t1 t2}
sl@0
   554
sl@0
   555
finish_test