xref: /sqlite-3.40.0/test/capi3e.test (revision 2953ba9e)
113acedf2Sshaneh# 2010 Novemeber 18
213acedf2Sshaneh#
313acedf2Sshaneh# The author disclaims copyright to this source code.  In place of
413acedf2Sshaneh# a legal notice, here is a blessing:
513acedf2Sshaneh#
613acedf2Sshaneh#    May you do good and not evil.
713acedf2Sshaneh#    May you find forgiveness for yourself and forgive others.
813acedf2Sshaneh#    May you share freely, never taking more than you give.
913acedf2Sshaneh#
1013acedf2Sshaneh#***********************************************************************
1113acedf2Sshaneh# This file implements regression tests for SQLite library.  The
1213acedf2Sshaneh# focus of this script testing the callback-free C/C++ API.
1313acedf2Sshaneh#
1413acedf2Sshaneh# $Id: capi3e.test,v 1.70 2009/01/09 02:49:32 drh Exp $
1513acedf2Sshaneh#
1613acedf2Sshaneh
1713acedf2Sshanehset testdir [file dirname $argv0]
1813acedf2Sshanehsource $testdir/tester.tcl
1913acedf2Sshaneh
2038777db5Sdan# Make sure the system encoding is utf-8. Otherwise, if the system encoding
2138777db5Sdan# is other than utf-8, [file isfile $x] may not refer to the same file
2238777db5Sdan# as [sqlite3 db $x].
23*2953ba9eSmistachkin#
24*2953ba9eSmistachkin# This is no longer needed here because it should be done within the test
25*2953ba9eSmistachkin# fixture executable itself, via Tcl_SetSystemEncoding.
26*2953ba9eSmistachkin#
27*2953ba9eSmistachkin# encoding system utf-8
2838777db5Sdan
2913acedf2Sshaneh# Do not use a codec for tests in this file, as the database file is
3013acedf2Sshaneh# manipulated directly using tcl scripts (using the [hexio_write] command).
3113acedf2Sshaneh#
3213acedf2Sshanehdo_not_use_codec
3313acedf2Sshaneh
3413acedf2Sshaneh# Return the UTF-16 representation of the supplied UTF-8 string $str.
3513acedf2Sshaneh# If $nt is true, append two 0x00 bytes as a nul terminator.
3613acedf2Sshanehproc utf16 {str {nt 1}} {
3713acedf2Sshaneh  set r [encoding convertto unicode $str]
3813acedf2Sshaneh  if {$nt} {
3913acedf2Sshaneh    append r "\x00\x00"
4013acedf2Sshaneh  }
4113acedf2Sshaneh  return $r
4213acedf2Sshaneh}
4313acedf2Sshaneh
4413acedf2Sshaneh# Return the UTF-8 representation of the supplied UTF-16 string $str.
4513acedf2Sshanehproc utf8 {str} {
4613acedf2Sshaneh  # If $str ends in two 0x00 0x00 bytes, knock these off before
4713acedf2Sshaneh  # converting to UTF-8 using TCL.
4813acedf2Sshaneh  binary scan $str \c* vals
4913acedf2Sshaneh  if {[lindex $vals end]==0 && [lindex $vals end-1]==0} {
5013acedf2Sshaneh    set str [binary format \c* [lrange $vals 0 end-2]]
5113acedf2Sshaneh  }
5213acedf2Sshaneh
5313acedf2Sshaneh  set r [encoding convertfrom unicode $str]
5413acedf2Sshaneh  return $r
5513acedf2Sshaneh}
5613acedf2Sshaneh
5713acedf2Sshaneh# These tests complement those in capi2.test. They are organized
5813acedf2Sshaneh# as follows:
5913acedf2Sshaneh#
6013acedf2Sshaneh# capi3e-1.*: Test sqlite3_open with various UTF8 filenames
6113acedf2Sshaneh# capi3e-2.*: Test sqlite3_open16 with various UTF8 filenames
6213acedf2Sshaneh# capi3e-3.*: Test ATTACH with various UTF8 filenames
6313acedf2Sshaneh
6413acedf2Sshanehdb close
6513acedf2Sshaneh
6613acedf2Sshaneh# here's the list of file names we're testing
675822d6feSdrhset names {t 1 t. 1. t.d 1.d t-1 1-1 t.db ä.db ë.db ö.db ü.db ÿ.db}
6813acedf2Sshaneh
6913acedf2Sshanehset i 0
7013acedf2Sshanehforeach name $names {
7113acedf2Sshaneh  incr i
7213acedf2Sshaneh  do_test capi3e-1.1.$i {
7313acedf2Sshaneh    set db2 [sqlite3_open $name {}]
7413acedf2Sshaneh    sqlite3_errcode $db2
7513acedf2Sshaneh  } {SQLITE_OK}
7613acedf2Sshaneh  do_test capi3e-1.2.$i {
7713acedf2Sshaneh    sqlite3_close $db2
7813acedf2Sshaneh  } {SQLITE_OK}
7913acedf2Sshaneh  do_test capi3e-1.3.$i {
8013acedf2Sshaneh    file isfile $name
8113acedf2Sshaneh  } {1}
8213acedf2Sshaneh}
8313acedf2Sshaneh
8454215638Sshanehifcapable {utf16} {
8513acedf2Sshaneh  set i 0
8613acedf2Sshaneh  foreach name $names {
8713acedf2Sshaneh    incr i
8813acedf2Sshaneh    do_test capi3e-2.1.$i {
8913acedf2Sshaneh      set db2 [sqlite3_open16 [utf16 $name] {}]
9013acedf2Sshaneh      sqlite3_errcode $db2
9113acedf2Sshaneh    } {SQLITE_OK}
9213acedf2Sshaneh    do_test capi3e-2.2.$i {
9313acedf2Sshaneh      sqlite3_close $db2
9413acedf2Sshaneh    } {SQLITE_OK}
9513acedf2Sshaneh    do_test capi3e-2.3.$i {
9613acedf2Sshaneh      file isfile $name
9713acedf2Sshaneh    } {1}
9813acedf2Sshaneh  }
9954215638Sshaneh}
10013acedf2Sshaneh
10113acedf2Sshanehifcapable attach {
10213acedf2Sshaneh  do_test capi3e-3.1 {
10313acedf2Sshaneh    sqlite3 db2 base.db
10413acedf2Sshaneh  } {}
10513acedf2Sshaneh  set i 0
10613acedf2Sshaneh  foreach name $names {
10713acedf2Sshaneh    incr i
10813acedf2Sshaneh    do_test capi3e-3.2.$i {
10913acedf2Sshaneh      db2 eval "ATTACH DATABASE '$name' AS db$i;"
11013acedf2Sshaneh    } {}
11113acedf2Sshaneh    do_test capi3e-3.3.$i {
11213acedf2Sshaneh      db2 eval "DETACH DATABASE db$i;"
11313acedf2Sshaneh    } {}
11413acedf2Sshaneh  }
11513acedf2Sshaneh  do_test capi3e-3.4 {
11613acedf2Sshaneh    db2 close
11713acedf2Sshaneh  } {}
11813acedf2Sshaneh}
11913acedf2Sshaneh
12013acedf2Sshaneh# clean up
12113acedf2Sshanehforcedelete base.db
12213acedf2Sshanehforeach name $names {
12313acedf2Sshaneh  forcedelete $name
12413acedf2Sshaneh}
12513acedf2Sshaneh
12613acedf2Sshanehfinish_test
127