xref: /sqlite-3.40.0/test/blob.test (revision ef5ecb41)
1# 2001 September 15
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.
12#
13# $Id: blob.test,v 1.1 2004/05/27 13:55:27 danielk1977 Exp $
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17
18proc bin_to_hex {blob} {
19  set bytes {}
20  binary scan $blob \c* bytes
21  set bytes2 [list]
22  foreach b $bytes {lappend bytes2 [format %02X [expr $b & 0xFF]]}
23  join $bytes2 {}
24}
25
26# Simplest possible case. Specify a blob literal
27do_test blob-1.0 {
28  set blob [execsql {SELECT X'01020304'}]
29  bin_to_hex [lindex $blob 0]
30} {01020304}
31do_test blob-1.1 {
32  set blob [execsql {SELECT x'ABCDEF'}]
33  bin_to_hex [lindex $blob 0]
34} {ABCDEF}
35do_test blob-1.2 {
36  set blob [execsql {SELECT x''}]
37  bin_to_hex [lindex $blob 0]
38} {}
39do_test blob-1.3 {
40  set blob [execsql {SELECT x'abcdEF12'}]
41  bin_to_hex [lindex $blob 0]
42} {ABCDEF12}
43
44# Try some syntax errors in blob literals.
45do_test blob-1.4 {
46  catchsql {SELECT X'01020k304', 100}
47} {1 {unrecognized token: "X'01020"}}
48do_test blob-1.5 {
49  catchsql {SELECT X'01020, 100}
50} {1 {unrecognized token: "X'01020"}}
51do_test blob-1.6 {
52  catchsql {SELECT X'01020 100'}
53} {1 {unrecognized token: "X'01020"}}
54do_test blob-1.7 {
55  catchsql {SELECT X'01001'}
56} {1 {unrecognized token: "X'01001'"}}
57
58# Insert a blob into a table and retrieve it.
59do_test blob-2.0 {
60  execsql {
61    CREATE TABLE t1(a BLOB, b BLOB);
62    INSERT INTO t1 VALUES(X'123456', x'7890ab');
63    INSERT INTO t1 VALUES(X'CDEF12', x'345678');
64  }
65  set blobs [execsql {SELECT * FROM t1}]
66  set blobs2 [list]
67  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
68  set blobs2
69} {123456 7890AB CDEF12 345678}
70
71# An index on a blob column
72do_test blob-2.1 {
73  execsql {
74    CREATE INDEX i1 ON t1(a);
75  }
76  set blobs [execsql {SELECT * FROM t1}]
77  set blobs2 [list]
78  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
79  set blobs2
80} {123456 7890AB CDEF12 345678}
81do_test blob-2.2 {
82  set blobs [execsql {SELECT * FROM t1 where a = X'123456'}]
83  set blobs2 [list]
84  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
85  set blobs2
86} {123456 7890AB}
87do_test blob-2.3 {
88  set blobs [execsql {SELECT * FROM t1 where a = X'CDEF12'}]
89  set blobs2 [list]
90  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
91  set blobs2
92} {CDEF12 345678}
93do_test blob-2.4 {
94  set blobs [execsql {SELECT * FROM t1 where a = X'CD12'}]
95  set blobs2 [list]
96  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
97  set blobs2
98} {}
99
100# Try to bind a blob value to a prepared statement.
101do_test blob-3.0 {
102  set DB [sqlite db2 test.db]
103  set STMT [sqlite3_prepare $DB "DELETE FROM t1 WHERE a = ?" -1 DUMMY]
104  sqlite3_bind_blob $STMT 1 "\x12\x34\x56" 3
105  sqlite3_step $STMT
106} {SQLITE_DONE}
107do_test blob-3.1 {
108  sqlite3_finalize $STMT
109  db2 close
110} {}
111do_test blob-2.3 {
112  set blobs [execsql {SELECT * FROM t1}]
113  set blobs2 [list]
114  foreach b $blobs {lappend blobs2 [bin_to_hex $b]}
115  set blobs2
116} {CDEF12 345678}
117
118finish_test
119
120
121