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