# 2003 January 29 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this script testing the callback-free C/C++ API. # # $Id: capi3.test,v 1.5 2004/05/26 13:27:00 danielk1977 Exp $ # set testdir [file dirname $argv0] source $testdir/tester.tcl # Return the UTF-16 representation of the supplied UTF-8 string $str. # If $nt is true, append two 0x00 bytes as a nul terminator. proc utf16 {str {nt 1}} { set r [encoding convertto unicode $str] if {$nt} { append r "\x00\x00" } return $r } # Return the UTF-8 representation of the supplied UTF-16 string $str. proc utf8 {str} { # If $str ends in two 0x00 0x00 bytes, knock these off before # converting to UTF-8 using TCL. binary scan $str \c* vals if {[lindex $vals end]==0 && [lindex $vals end-1]==0} { set str [binary format \c* [lrange $vals 0 end-2]] } set r [encoding convertfrom unicode $str] return $r } # These tests complement those in capi2.test. They are organized # as follows: # # capi3-1.*: Test sqlite3_prepare # capi3-2.*: Test sqlite3_prepare16 # capi3-3.*: Test sqlite3_open # capi3-4.*: Test sqlite3_open16 # capi3-5.*: Test the various sqlite3_result_* APIs # db close set DB [sqlite db test.db] do_test capi3-1.1 { set STMT [sqlite3_prepare $DB {SELECT name FROM sqlite_master} -1 TAIL] sqlite3_finalize $STMT set TAIL } {} do_test capi3-1.2 { sqlite3_errcode $DB } {SQLITE_OK} do_test capi3-1.3 { sqlite3_errmsg $DB } {not an error} do_test capi3-1.4 { set sql {SELECT name FROM sqlite_master;SELECT 10} set STMT [sqlite3_prepare $DB $sql -1 TAIL] sqlite3_finalize $STMT set TAIL } {SELECT 10} do_test capi3-1.5 { set sql {SELECT namex FROM sqlite_master} catch { set STMT [sqlite3_prepare $DB $sql -1 TAIL] } } {1} do_test capi3-1.6 { sqlite3_errcode $DB } {SQLITE_ERROR} do_test capi3-1.7 { sqlite3_errmsg $DB } {no such column: namex} do_test capi3-2.1 { set sql16 [utf16 {SELECT name FROM sqlite_master}] set STMT [sqlite3_prepare16 $DB $sql16 -1 ::TAIL] sqlite3_finalize $STMT utf8 $::TAIL } {} do_test capi3-2.2 { set sql [utf16 {SELECT name FROM sqlite_master;SELECT 10}] set STMT [sqlite3_prepare16 $DB $sql -1 TAIL] sqlite3_finalize $STMT utf8 $TAIL } {SELECT 10} do_test capi3-2.3 { set sql [utf16 {SELECT namex FROM sqlite_master}] catch { set STMT [sqlite3_prepare16 $DB $sql -1 TAIL] } } {1} do_test capi3-2.4 { sqlite3_errcode $DB } {SQLITE_ERROR} do_test capi3-2.5 { sqlite3_errmsg $DB } {no such column: namex} # rename sqlite3_open sqlite3_open_old # proc sqlite3_open {fname options} {sqlite3_open_new $fname $options} do_test capi3-3.1 { set db2 [sqlite3_open test.db {}] sqlite3_errcode $db2 } {SQLITE_OK} # FIX ME: Should test the db handle works. do_test capi3-3.2 { sqlite3_close $db2 } {} do_test capi3-3.3 { catch { set db2 [sqlite3_open /bogus/path/test.db {}] } sqlite3_errcode $db2 } {SQLITE_CANTOPEN} do_test capi3-3.4 { sqlite3_errmsg $db2 } {unable to open database file} do_test capi3-3.4 { sqlite3_close $db2 } {} # rename sqlite3_open "" # rename sqlite3_open_old sqlite3_open do_test capi3-4.1 { set db2 [sqlite3_open16 [utf16 test.db] {}] sqlite3_errcode $db2 } {SQLITE_OK} # FIX ME: Should test the db handle works. do_test capi3-4.2 { sqlite3_close $db2 } {} do_test capi3-4.3 { catch { set db2 [sqlite3_open16 [utf16 /bogus/path/test.db] {}] } sqlite3_errcode $db2 } {SQLITE_CANTOPEN} do_test capi3-4.4 { utf8 [sqlite3_errmsg16 $db2] } {unable to open database file} do_test capi3-4.4 { sqlite3_close $db2 } {} # The tests cases capi3-5.* test work as follows: # # capi3-5.0: Prepare a statement, and check we can retrieve the number of # columns (before the statement is executed). # capi3-5.1: Check we can retrieve column names (before statement execution) # capi3-5.2: Check we can retrieve column names in UTF-16 # capi3-5.3: Step the statement. # capi3-5.4: Check the types of the values. # capi3-5.5: Check the values can be retrieved as integers. # capi3-5.6: Check the values can be retrieved as UTF-8 text. # capi3-5.7: Check the values can be retrieved as UTF-16 text. # capi3-5.8: Check that the types of the values have not been altered by # retrieving the values as text. # # Test cases capi3-5.9 - capi3-5.14 are a repeat of 3-8, with a different # row of data. # do_test capi3-5.0 { execsql { CREATE TABLE t1(a VARINT, b BLOB, c VARCHAR(16)); INSERT INTO t1 VALUES(1, 2, 3); INSERT INTO t1 VALUES('one', 'two', NULL); } set sql "SELECT * FROM t1" set STMT [sqlite3_prepare $DB $sql -1 TAIL] sqlite3_column_count $STMT } 3 do_test capi3-5.1 { set cnamelist [list] foreach i {0 1 2} {lappend cnamelist [sqlite3_column_name $STMT $i]} set cnamelist } {a b c} do_test capi3-5.2 { set cnamelist [list] foreach i {0 1 2} {lappend cnamelist [utf8 [sqlite3_column_name16 $STMT $i]]} set cnamelist } {a b c} do_test capi3-5.3 { sqlite3_step $STMT } SQLITE_ROW do_test capi3-5.4 { set types [list] foreach i {0 1 2} {lappend types [sqlite3_column_type $STMT $i]} set types } {INTEGER INTEGER TEXT} if 0 { do_test capi3-5.5 { set ints [list] foreach i {0 1 2} {lappend ints [sqlite3_column_int $STMT $i]} set ints } {1 2 3} do_test capi3-5.6 { set utf8 [list] foreach i {0 1 2} {lappend utf8 [sqlite3_column_data $STMT $i]} set utf8 } {1 2 3} } do_test capi3-5.7 { set utf8 [list] foreach i {0 1 2} {lappend utf8 [utf8 [sqlite3_column_data16 $STMT $i]]} set utf8 } {1 2 3} do_test capi3-5.8 { set types [list] foreach i {0 1 2} {lappend types [sqlite3_column_type $STMT $i]} set types } {INTEGER INTEGER TEXT} do_test capi3-5.9 { sqlite3_step $STMT } SQLITE_ROW do_test capi3-5.10 { set types [list] foreach i {0 1 2} {lappend types [sqlite3_column_type $STMT $i]} set types } {TEXT TEXT NULL} do_test capi3-5.11 { set ints [list] foreach i {0 1 2} {lappend ints [sqlite3_column_int $STMT $i]} set ints } {0 0 0} do_test capi3-5.12 { set utf8 [list] foreach i {0 1 2} {lappend utf8 [sqlite3_column_data $STMT $i]} set utf8 } {one two {}} do_test capi3-5.13 { set utf8 [list] foreach i {0 1 2} {lappend utf8 [utf8 [sqlite3_column_data16 $STMT $i]]} set utf8 } {one two {}} do_test capi3-5.14 { set types [list] foreach i {0 1 2} {lappend types [sqlite3_column_type $STMT $i]} set types } {TEXT TEXT NULL} do_test capi3-5.15 { sqlite3_step $STMT } SQLITE_DONE do_test capi3-5.99 { sqlite3_finalize $STMT } {SQLITE_OK} db close finish_test