os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/enc.test
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/enc.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,172 @@
     1.4 +# 2002 May 24
     1.5 +#
     1.6 +# The author disclaims copyright to this source code.  In place of
     1.7 +# a legal notice, here is a blessing:
     1.8 +#
     1.9 +#    May you do good and not evil.
    1.10 +#    May you find forgiveness for yourself and forgive others.
    1.11 +#    May you share freely, never taking more than you give.
    1.12 +#
    1.13 +#***********************************************************************
    1.14 +# This file implements regression tests for SQLite library.  The focus of
    1.15 +# this file is testing the SQLite routines used for converting between the
    1.16 +# various suported unicode encodings (UTF-8, UTF-16, UTF-16le and
    1.17 +# UTF-16be).
    1.18 +#
    1.19 +# $Id: enc.test,v 1.7 2007/05/23 16:23:09 danielk1977 Exp $
    1.20 +
    1.21 +set testdir [file dirname $argv0]
    1.22 +source $testdir/tester.tcl
    1.23 +
    1.24 +# Skip this test if the build does not support multiple encodings.
    1.25 +#
    1.26 +ifcapable {!utf16} {
    1.27 +  finish_test
    1.28 +  return
    1.29 +}
    1.30 +
    1.31 +proc do_bincmp_test {testname got expect} {
    1.32 +  binary scan $expect \c* expectvals
    1.33 +  binary scan $got \c* gotvals
    1.34 +  do_test $testname [list set dummy $gotvals] $expectvals
    1.35 +}
    1.36 +
    1.37 +# $utf16 is a UTF-16 encoded string. Swap each pair of bytes around
    1.38 +# to change the byte-order of the string.
    1.39 +proc swap_byte_order {utf16} {
    1.40 +  binary scan $utf16 \c* ints
    1.41 +
    1.42 +  foreach {a b} $ints {
    1.43 +    lappend ints2 $b
    1.44 +    lappend ints2 $a
    1.45 +  }
    1.46 +
    1.47 +  return [binary format \c* $ints2]
    1.48 +}
    1.49 +
    1.50 +#
    1.51 +# Test that the SQLite routines for converting between UTF encodings
    1.52 +# produce the same results as their TCL counterparts.
    1.53 +#
    1.54 +# $testname is the prefix to be used for the test names.
    1.55 +# $str is a string to use for testing (encoded in UTF-8, as normal for TCL).
    1.56 +#
    1.57 +# The test procedure is:
    1.58 +# 1. Convert the string from UTF-8 to UTF-16le and check that the TCL and
    1.59 +#    SQLite routines produce the same results.
    1.60 +#
    1.61 +# 2. Convert the string from UTF-8 to UTF-16be and check that the TCL and
    1.62 +#    SQLite routines produce the same results.
    1.63 +#
    1.64 +# 3. Use the SQLite routines to convert the native machine order UTF-16
    1.65 +#    representation back to the original UTF-8. Check that the result
    1.66 +#    matches the original representation.
    1.67 +#
    1.68 +# 4. Add a byte-order mark to each of the UTF-16 representations and
    1.69 +#    check that the SQLite routines can convert them back to UTF-8.  For
    1.70 +#    byte-order mark info, refer to section 3.10 of the unicode standard.
    1.71 +#
    1.72 +# 5. Take the byte-order marked UTF-16 strings from step 4 and ensure
    1.73 +#    that SQLite can convert them both to native byte order UTF-16 
    1.74 +#    strings, sans BOM.
    1.75 +#
    1.76 +# Coverage:
    1.77 +#
    1.78 +# sqlite_utf8to16be (step 2)
    1.79 +# sqlite_utf8to16le (step 1)
    1.80 +# sqlite_utf16to8 (steps 3, 4)
    1.81 +# sqlite_utf16to16le (step 5)
    1.82 +# sqlite_utf16to16be (step 5)
    1.83 +#
    1.84 +proc test_conversion {testname str} {
    1.85 + 
    1.86 +  # Step 1.
    1.87 +  set utf16le_sqlite3 [test_translate $str UTF8 UTF16LE]
    1.88 +  set utf16le_tcl [encoding convertto unicode $str]
    1.89 +  append utf16le_tcl "\x00\x00"
    1.90 +  if { $::tcl_platform(byteOrder)!="littleEndian" } {
    1.91 +    set utf16le_tcl [swap_byte_order $utf16le_tcl]
    1.92 +  }
    1.93 +  do_bincmp_test $testname.1 $utf16le_sqlite3 $utf16le_tcl
    1.94 +  set utf16le $utf16le_tcl
    1.95 +
    1.96 +  # Step 2.
    1.97 +  set utf16be_sqlite3 [test_translate $str UTF8 UTF16BE]
    1.98 +  set utf16be_tcl [encoding convertto unicode $str]
    1.99 +  append utf16be_tcl "\x00\x00"
   1.100 +  if { $::tcl_platform(byteOrder)=="littleEndian" } {
   1.101 +    set utf16be_tcl [swap_byte_order $utf16be_tcl]
   1.102 +  }
   1.103 +  do_bincmp_test $testname.2 $utf16be_sqlite3 $utf16be_tcl
   1.104 +  set utf16be $utf16be_tcl
   1.105 + 
   1.106 +  # Step 3.
   1.107 +  if { $::tcl_platform(byteOrder)=="littleEndian" } {
   1.108 +    set utf16 $utf16le
   1.109 +  } else {
   1.110 +    set utf16 $utf16be
   1.111 +  }
   1.112 +  set utf8_sqlite3 [test_translate $utf16 UTF16 UTF8]
   1.113 +  do_bincmp_test $testname.3 $utf8_sqlite3 [binarize $str]
   1.114 +
   1.115 +  # Step 4 (little endian).
   1.116 +  append utf16le_bom "\xFF\xFE" $utf16le
   1.117 +  set utf8_sqlite3 [test_translate $utf16le_bom UTF16 UTF8 1]
   1.118 +  do_bincmp_test $testname.4.le $utf8_sqlite3 [binarize $str]
   1.119 +
   1.120 +  # Step 4 (big endian).
   1.121 +  append utf16be_bom "\xFE\xFF" $utf16be
   1.122 +  set utf8_sqlite3 [test_translate $utf16be_bom UTF16 UTF8]
   1.123 +  do_bincmp_test $testname.4.be $utf8_sqlite3 [binarize $str]
   1.124 +
   1.125 +  # Step 5 (little endian to little endian).
   1.126 +  set utf16_sqlite3 [test_translate $utf16le_bom UTF16LE UTF16LE]
   1.127 +  do_bincmp_test $testname.5.le.le $utf16_sqlite3 $utf16le
   1.128 +
   1.129 +  # Step 5 (big endian to big endian).
   1.130 +  set utf16_sqlite3 [test_translate $utf16be_bom UTF16 UTF16BE]
   1.131 +  do_bincmp_test $testname.5.be.be $utf16_sqlite3 $utf16be
   1.132 +
   1.133 +  # Step 5 (big endian to little endian).
   1.134 +  set utf16_sqlite3 [test_translate $utf16be_bom UTF16 UTF16LE]
   1.135 +  do_bincmp_test $testname.5.be.le $utf16_sqlite3 $utf16le
   1.136 +
   1.137 +  # Step 5 (little endian to big endian).
   1.138 +  set utf16_sqlite3 [test_translate $utf16le_bom UTF16 UTF16BE]
   1.139 +  do_bincmp_test $testname.5.le.be $utf16_sqlite3 $utf16be
   1.140 +}
   1.141 +
   1.142 +translate_selftest
   1.143 +
   1.144 +test_conversion enc-1 "hello world"
   1.145 +test_conversion enc-2 "sqlite"
   1.146 +test_conversion enc-3 ""
   1.147 +test_conversion enc-X "\u0100"
   1.148 +test_conversion enc-4 "\u1234"
   1.149 +test_conversion enc-5 "\u4321abc"
   1.150 +test_conversion enc-6 "\u4321\u1234"
   1.151 +test_conversion enc-7 [string repeat "abcde\u00EF\u00EE\uFFFCabc" 100]
   1.152 +test_conversion enc-8 [string repeat "\u007E\u007F\u0080\u0081" 100]
   1.153 +test_conversion enc-9 [string repeat "\u07FE\u07FF\u0800\u0801\uFFF0" 100]
   1.154 +test_conversion enc-10 [string repeat "\uE000" 100]
   1.155 +
   1.156 +proc test_collate {enc zLeft zRight} {
   1.157 +  return [string compare $zLeft $zRight]
   1.158 +}
   1.159 +add_test_collate $::DB 0 0 1
   1.160 +do_test enc-11.1 {
   1.161 +  execsql {
   1.162 +    CREATE TABLE ab(a COLLATE test_collate, b);
   1.163 +    INSERT INTO ab VALUES(CAST (X'C388' AS TEXT), X'888800');
   1.164 +    INSERT INTO ab VALUES(CAST (X'C0808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808080808388' AS TEXT), X'888800');
   1.165 +    CREATE INDEX ab_i ON ab(a, b);
   1.166 +  }
   1.167 +} {}
   1.168 +do_test enc-11.2 {
   1.169 +  set cp200 "\u00C8"
   1.170 +  execsql {
   1.171 +    SELECT count(*) FROM ab WHERE a = $::cp200;
   1.172 +  }
   1.173 +} {2}
   1.174 +
   1.175 +finish_test