1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/blob.test Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,147 @@
1.4 +# 2001 September 15
1.5 +#
1.6 +# The author disclaims copyright to this source code. In place of
1.7 +# a legal notice, here is a blessing:
1.8 +#
1.9 +# May you do good and not evil.
1.10 +# May you find forgiveness for yourself and forgive others.
1.11 +# May you share freely, never taking more than you give.
1.12 +#
1.13 +#***********************************************************************
1.14 +# This file implements regression tests for SQLite library.
1.15 +#
1.16 +# $Id: blob.test,v 1.7 2008/04/04 15:12:22 drh Exp $
1.17 +
1.18 +set testdir [file dirname $argv0]
1.19 +source $testdir/tester.tcl
1.20 +
1.21 +ifcapable {!bloblit} {
1.22 + finish_test
1.23 + return
1.24 +}
1.25 +
1.26 +proc bin_to_hex {blob} {
1.27 + set bytes {}
1.28 + binary scan $blob \c* bytes
1.29 + set bytes2 [list]
1.30 + foreach b $bytes {lappend bytes2 [format %02X [expr $b & 0xFF]]}
1.31 + join $bytes2 {}
1.32 +}
1.33 +
1.34 +# Simplest possible case. Specify a blob literal
1.35 +do_test blob-1.0 {
1.36 + set blob [execsql {SELECT X'01020304';}]
1.37 + bin_to_hex [lindex $blob 0]
1.38 +} {01020304}
1.39 +do_test blob-1.1 {
1.40 + set blob [execsql {SELECT x'ABCDEF';}]
1.41 + bin_to_hex [lindex $blob 0]
1.42 +} {ABCDEF}
1.43 +do_test blob-1.2 {
1.44 + set blob [execsql {SELECT x'';}]
1.45 + bin_to_hex [lindex $blob 0]
1.46 +} {}
1.47 +do_test blob-1.3 {
1.48 + set blob [execsql {SELECT x'abcdEF12';}]
1.49 + bin_to_hex [lindex $blob 0]
1.50 +} {ABCDEF12}
1.51 +do_test blob-1.3.2 {
1.52 + set blob [execsql {SELECT x'0123456789abcdefABCDEF';}]
1.53 + bin_to_hex [lindex $blob 0]
1.54 +} {0123456789ABCDEFABCDEF}
1.55 +
1.56 +# Try some syntax errors in blob literals.
1.57 +do_test blob-1.4 {
1.58 + catchsql {SELECT X'01020k304', 100}
1.59 +} {1 {unrecognized token: "X'01020k304'"}}
1.60 +do_test blob-1.5 {
1.61 + catchsql {SELECT X'01020, 100}
1.62 +} {1 {unrecognized token: "X'01020, 100"}}
1.63 +do_test blob-1.6 {
1.64 + catchsql {SELECT X'01020 100'}
1.65 +} {1 {unrecognized token: "X'01020 100'"}}
1.66 +do_test blob-1.7 {
1.67 + catchsql {SELECT X'01001'}
1.68 +} {1 {unrecognized token: "X'01001'"}}
1.69 +do_test blob-1.8 {
1.70 + catchsql {SELECT x'012/45'}
1.71 +} {1 {unrecognized token: "x'012/45'"}}
1.72 +do_test blob-1.9 {
1.73 + catchsql {SELECT x'012:45'}
1.74 +} {1 {unrecognized token: "x'012:45'"}}
1.75 +do_test blob-1.10 {
1.76 + catchsql {SELECT x'012@45'}
1.77 +} {1 {unrecognized token: "x'012@45'"}}
1.78 +do_test blob-1.11 {
1.79 + catchsql {SELECT x'012G45'}
1.80 +} {1 {unrecognized token: "x'012G45'"}}
1.81 +do_test blob-1.12 {
1.82 + catchsql {SELECT x'012`45'}
1.83 +} {1 {unrecognized token: "x'012`45'"}}
1.84 +do_test blob-1.13 {
1.85 + catchsql {SELECT x'012g45'}
1.86 +} {1 {unrecognized token: "x'012g45'"}}
1.87 +
1.88 +
1.89 +# Insert a blob into a table and retrieve it.
1.90 +do_test blob-2.0 {
1.91 + execsql {
1.92 + CREATE TABLE t1(a BLOB, b BLOB);
1.93 + INSERT INTO t1 VALUES(X'123456', x'7890ab');
1.94 + INSERT INTO t1 VALUES(X'CDEF12', x'345678');
1.95 + }
1.96 + set blobs [execsql {SELECT * FROM t1}]
1.97 + set blobs2 [list]
1.98 + foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
1.99 + set blobs2
1.100 +} {123456 7890AB CDEF12 345678}
1.101 +
1.102 +# An index on a blob column
1.103 +do_test blob-2.1 {
1.104 + execsql {
1.105 + CREATE INDEX i1 ON t1(a);
1.106 + }
1.107 + set blobs [execsql {SELECT * FROM t1}]
1.108 + set blobs2 [list]
1.109 + foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
1.110 + set blobs2
1.111 +} {123456 7890AB CDEF12 345678}
1.112 +do_test blob-2.2 {
1.113 + set blobs [execsql {SELECT * FROM t1 where a = X'123456'}]
1.114 + set blobs2 [list]
1.115 + foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
1.116 + set blobs2
1.117 +} {123456 7890AB}
1.118 +do_test blob-2.3 {
1.119 + set blobs [execsql {SELECT * FROM t1 where a = X'CDEF12'}]
1.120 + set blobs2 [list]
1.121 + foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
1.122 + set blobs2
1.123 +} {CDEF12 345678}
1.124 +do_test blob-2.4 {
1.125 + set blobs [execsql {SELECT * FROM t1 where a = X'CD12'}]
1.126 + set blobs2 [list]
1.127 + foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
1.128 + set blobs2
1.129 +} {}
1.130 +
1.131 +# Try to bind a blob value to a prepared statement.
1.132 +do_test blob-3.0 {
1.133 + sqlite3 db2 test.db
1.134 + set DB [sqlite3_connection_pointer db2]
1.135 + set STMT [sqlite3_prepare $DB "DELETE FROM t1 WHERE a = ?" -1 DUMMY]
1.136 + sqlite3_bind_blob $STMT 1 "\x12\x34\x56" 3
1.137 + sqlite3_step $STMT
1.138 +} {SQLITE_DONE}
1.139 +do_test blob-3.1 {
1.140 + sqlite3_finalize $STMT
1.141 + db2 close
1.142 +} {}
1.143 +do_test blob-2.3 {
1.144 + set blobs [execsql {SELECT * FROM t1}]
1.145 + set blobs2 [list]
1.146 + foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
1.147 + set blobs2
1.148 +} {CDEF12 345678}
1.149 +
1.150 +finish_test