blob: 3e478e79040eff9d9df47dbd9a65d87a458cc914 [file] [log] [blame]
shaneh13acedf2010-11-18 15:44:59 +00001# 2010 Novemeber 18
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
12# focus of this script testing the callback-free C/C++ API.
13#
14# $Id: capi3e.test,v 1.70 2009/01/09 02:49:32 drh Exp $
15#
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
dan38777db2010-11-18 16:58:48 +000020# Make sure the system encoding is utf-8. Otherwise, if the system encoding
21# is other than utf-8, [file isfile $x] may not refer to the same file
22# as [sqlite3 db $x].
mistachkin2953ba92014-02-14 00:25:03 +000023#
24# This is no longer needed here because it should be done within the test
25# fixture executable itself, via Tcl_SetSystemEncoding.
26#
27# encoding system utf-8
dan38777db2010-11-18 16:58:48 +000028
shaneh13acedf2010-11-18 15:44:59 +000029# Do not use a codec for tests in this file, as the database file is
30# manipulated directly using tcl scripts (using the [hexio_write] command).
31#
32do_not_use_codec
33
34# Return the UTF-16 representation of the supplied UTF-8 string $str.
35# If $nt is true, append two 0x00 bytes as a nul terminator.
36proc utf16 {str {nt 1}} {
37 set r [encoding convertto unicode $str]
38 if {$nt} {
39 append r "\x00\x00"
40 }
41 return $r
42}
43
44# Return the UTF-8 representation of the supplied UTF-16 string $str.
45proc utf8 {str} {
46 # If $str ends in two 0x00 0x00 bytes, knock these off before
47 # converting to UTF-8 using TCL.
48 binary scan $str \c* vals
49 if {[lindex $vals end]==0 && [lindex $vals end-1]==0} {
50 set str [binary format \c* [lrange $vals 0 end-2]]
51 }
52
53 set r [encoding convertfrom unicode $str]
54 return $r
55}
56
57# These tests complement those in capi2.test. They are organized
58# as follows:
59#
60# capi3e-1.*: Test sqlite3_open with various UTF8 filenames
61# capi3e-2.*: Test sqlite3_open16 with various UTF8 filenames
62# capi3e-3.*: Test ATTACH with various UTF8 filenames
63
64db close
65
66# here's the list of file names we're testing
drh5822d6f2013-06-10 23:30:09 +000067set names {t 1 t. 1. t.d 1.d t-1 1-1 t.db ä.db ë.db ö.db ü.db ÿ.db}
shaneh13acedf2010-11-18 15:44:59 +000068
69set i 0
70foreach name $names {
71 incr i
72 do_test capi3e-1.1.$i {
73 set db2 [sqlite3_open $name {}]
74 sqlite3_errcode $db2
75 } {SQLITE_OK}
76 do_test capi3e-1.2.$i {
77 sqlite3_close $db2
78 } {SQLITE_OK}
79 do_test capi3e-1.3.$i {
80 file isfile $name
81 } {1}
82}
83
shaneh54215632011-02-14 03:49:40 +000084ifcapable {utf16} {
85 set i 0
86 foreach name $names {
87 incr i
88 do_test capi3e-2.1.$i {
89 set db2 [sqlite3_open16 [utf16 $name] {}]
90 sqlite3_errcode $db2
91 } {SQLITE_OK}
92 do_test capi3e-2.2.$i {
93 sqlite3_close $db2
94 } {SQLITE_OK}
95 do_test capi3e-2.3.$i {
96 file isfile $name
97 } {1}
98 }
shaneh13acedf2010-11-18 15:44:59 +000099}
100
shaneh54215632011-02-14 03:49:40 +0000101ifcapable attach {
shaneh13acedf2010-11-18 15:44:59 +0000102 do_test capi3e-3.1 {
103 sqlite3 db2 base.db
104 } {}
105 set i 0
106 foreach name $names {
107 incr i
108 do_test capi3e-3.2.$i {
109 db2 eval "ATTACH DATABASE '$name' AS db$i;"
110 } {}
111 do_test capi3e-3.3.$i {
112 db2 eval "DETACH DATABASE db$i;"
113 } {}
114 }
115 do_test capi3e-3.4 {
116 db2 close
117 } {}
118}
119
120# clean up
121forcedelete base.db
122foreach name $names {
123 forcedelete $name
124}
125
126finish_test