xref: /sqlite-3.40.0/test/capi3e.test (revision 13acedf2)
1# 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
20# Do not use a codec for tests in this file, as the database file is
21# manipulated directly using tcl scripts (using the [hexio_write] command).
22#
23do_not_use_codec
24
25# Return the UTF-16 representation of the supplied UTF-8 string $str.
26# If $nt is true, append two 0x00 bytes as a nul terminator.
27proc utf16 {str {nt 1}} {
28  set r [encoding convertto unicode $str]
29  if {$nt} {
30    append r "\x00\x00"
31  }
32  return $r
33}
34
35# Return the UTF-8 representation of the supplied UTF-16 string $str.
36proc utf8 {str} {
37  # If $str ends in two 0x00 0x00 bytes, knock these off before
38  # converting to UTF-8 using TCL.
39  binary scan $str \c* vals
40  if {[lindex $vals end]==0 && [lindex $vals end-1]==0} {
41    set str [binary format \c* [lrange $vals 0 end-2]]
42  }
43
44  set r [encoding convertfrom unicode $str]
45  return $r
46}
47
48# These tests complement those in capi2.test. They are organized
49# as follows:
50#
51# capi3e-1.*: Test sqlite3_open with various UTF8 filenames
52# capi3e-2.*: Test sqlite3_open16 with various UTF8 filenames
53# capi3e-3.*: Test ATTACH with various UTF8 filenames
54
55db close
56
57# here's the list of file names we're testing
58set names {t 1 t. 1. t.d 1.d t-1 1-1 t.db �.db �.db �.db �.db �.db}
59
60set i 0
61foreach name $names {
62  incr i
63  do_test capi3e-1.1.$i {
64    set db2 [sqlite3_open $name {}]
65    sqlite3_errcode $db2
66  } {SQLITE_OK}
67  do_test capi3e-1.2.$i {
68    sqlite3_close $db2
69  } {SQLITE_OK}
70  do_test capi3e-1.3.$i {
71    file isfile $name
72  } {1}
73}
74
75set i 0
76foreach name $names {
77  incr i
78  do_test capi3e-2.1.$i {
79    set db2 [sqlite3_open16 [utf16 $name] {}]
80    sqlite3_errcode $db2
81  } {SQLITE_OK}
82  do_test capi3e-2.2.$i {
83    sqlite3_close $db2
84  } {SQLITE_OK}
85  do_test capi3e-2.3.$i {
86    file isfile $name
87  } {1}
88}
89
90ifcapable attach {
91  do_test capi3e-3.1 {
92    sqlite3 db2 base.db
93  } {}
94  set i 0
95  foreach name $names {
96    incr i
97    do_test capi3e-3.2.$i {
98      db2 eval "ATTACH DATABASE '$name' AS db$i;"
99    } {}
100    do_test capi3e-3.3.$i {
101      db2 eval "DETACH DATABASE db$i;"
102    } {}
103  }
104  do_test capi3e-3.4 {
105    db2 close
106  } {}
107}
108
109# clean up
110forcedelete base.db
111foreach name $names {
112  forcedelete $name
113}
114
115finish_test
116