3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. Specfically
12 # it tests that the different storage classes (integer, real, text etc.)
15 # $Id: types.test,v 1.20 2009/06/29 06:00:37 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Tests in this file are organized roughly as follows:
22 # types-1.*.*: Test that values are stored using the expected storage
23 # classes when various forms of literals are inserted into
24 # columns with different affinities.
25 # types-1.1.*: INSERT INTO <table> VALUES(...)
26 # types-1.2.*: INSERT INTO <table> SELECT...
27 # types-1.3.*: UPDATE <table> SET...
29 # types-2.*.*: Check that values can be stored and retrieving using the
30 # various storage classes.
31 # types-2.1.*: INTEGER
35 # types-2.5.*: Records with a few different storage classes.
37 # types-3.*: Test that the '=' operator respects manifest types.
40 # Disable encryption on the database for this test.
42 set DB [sqlite3 db test.db; sqlite3_connection_pointer db]
45 # Create a table with one column for each type of affinity
48 CREATE TABLE t1(i integer, n numeric, t text, o blob);
52 # Each element of the following list represents one test case.
54 # The first value of each sub-list is an SQL literal. The following
55 # four value are the storage classes that would be used if the
56 # literal were inserted into a column with affinity INTEGER, NUMERIC, TEXT
57 # or NONE, respectively.
59 { 5.0 integer integer text real }
60 { 5.1 real real text real }
61 { 5 integer integer text integer }
62 { '5.0' integer integer text text }
63 { '5.1' real real text text }
64 { '-5.0' integer integer text text }
65 { '-5.0' integer integer text text }
66 { '5' integer integer text text }
67 { 'abc' text text text text }
68 { NULL null null null null }
71 lappend values { X'00' blob blob blob blob }
74 # This code tests that the storage classes specified above (in the $values
75 # table) are correctly assigned when values are inserted using a statement
78 # INSERT INTO <table> VALUE(<values>);
82 set lit [lindex $val 0]
83 execsql "DELETE FROM t1;"
84 execsql "INSERT INTO t1 VALUES($lit, $lit, $lit, $lit);"
85 do_test types-1.1.$tnum {
87 SELECT typeof(i), typeof(n), typeof(t), typeof(o) FROM t1;
93 # This code tests that the storage classes specified above (in the $values
94 # table) are correctly assigned when values are inserted using a statement
97 # INSERT INTO t1 SELECT ....
100 foreach val $values {
101 set lit [lindex $val 0]
102 execsql "DELETE FROM t1;"
103 execsql "INSERT INTO t1 SELECT $lit, $lit, $lit, $lit;"
104 do_test types-1.2.$tnum {
106 SELECT typeof(i), typeof(n), typeof(t), typeof(o) FROM t1;
108 } [lrange $val 1 end]
112 # This code tests that the storage classes specified above (in the $values
113 # table) are correctly assigned when values are inserted using a statement
116 # UPDATE <table> SET <column> = <value>;
119 foreach val $values {
120 set lit [lindex $val 0]
121 execsql "UPDATE t1 SET i = $lit, n = $lit, t = $lit, o = $lit;"
122 do_test types-1.3.$tnum {
124 SELECT typeof(i), typeof(n), typeof(t), typeof(o) FROM t1;
126 } [lrange $val 1 end]
134 # Open the table with root-page $rootpage at the btree
135 # level. Return a list that is the length of each record
136 # in the table, in the tables default scanning order.
137 proc record_sizes {rootpage} {
138 set bt [btree_open test.db 10]
139 btree_begin_transaction $bt
140 set c [btree_cursor $bt $rootpage 0]
143 lappend res [btree_payload_size $c]
144 if {[btree_next $c]} break
146 btree_close_cursor $c
152 # Create a table and insert some 1-byte integers. Make sure they
153 # can be read back OK. These should be 3 byte records.
154 do_test types-2.1.1 {
156 CREATE TABLE t1(a integer);
157 INSERT INTO t1 VALUES(0);
158 INSERT INTO t1 VALUES(120);
159 INSERT INTO t1 VALUES(-120);
162 do_test types-2.1.2 {
168 # Try some 2-byte integers (4 byte records)
169 do_test types-2.1.3 {
171 INSERT INTO t1 VALUES(30000);
172 INSERT INTO t1 VALUES(-30000);
175 do_test types-2.1.4 {
179 } {0 120 -120 30000 -30000}
181 # 4-byte integers (6 byte records)
182 do_test types-2.1.5 {
184 INSERT INTO t1 VALUES(2100000000);
185 INSERT INTO t1 VALUES(-2100000000);
188 do_test types-2.1.6 {
192 } {0 120 -120 30000 -30000 2100000000 -2100000000}
194 # 8-byte integers (10 byte records)
195 do_test types-2.1.7 {
197 INSERT INTO t1 VALUES(9000000*1000000*1000000);
198 INSERT INTO t1 VALUES(-9000000*1000000*1000000);
201 do_test types-2.1.8 {
205 } [list 0 120 -120 30000 -30000 2100000000 -2100000000 \
206 9000000000000000000 -9000000000000000000]
208 # Check that all the record sizes are as we expected.
209 ifcapable legacyformat {
210 do_test types-2.1.9 {
211 set root [db eval {select rootpage from sqlite_master where name = 't1'}]
213 } {3 3 3 4 4 6 6 10 10}
215 do_test types-2.1.9 {
216 set root [db eval {select rootpage from sqlite_master where name = 't1'}]
218 } {2 3 3 4 4 6 6 10 10}
221 # Insert some reals. These should be 10 byte records.
222 do_test types-2.2.1 {
224 CREATE TABLE t2(a float);
225 INSERT INTO t2 VALUES(0.0);
226 INSERT INTO t2 VALUES(12345.678);
227 INSERT INTO t2 VALUES(-12345.678);
230 do_test types-2.2.2 {
234 } {0.0 12345.678 -12345.678}
236 # Check that all the record sizes are as we expected.
237 ifcapable legacyformat {
238 do_test types-2.2.3 {
239 set root [db eval {select rootpage from sqlite_master where name = 't2'}]
243 do_test types-2.2.3 {
244 set root [db eval {select rootpage from sqlite_master where name = 't2'}]
249 # Insert a NULL. This should be a two byte record.
250 do_test types-2.3.1 {
252 CREATE TABLE t3(a nullvalue);
253 INSERT INTO t3 VALUES(NULL);
256 do_test types-2.3.2 {
258 SELECT a ISNULL FROM t3;
262 # Check that all the record sizes are as we expected.
263 do_test types-2.3.3 {
264 set root [db eval {select rootpage from sqlite_master where name = 't3'}]
268 # Insert a couple of strings.
269 do_test types-2.4.1 {
270 set string10 abcdefghij
271 set string500 [string repeat $string10 50]
272 set string500000 [string repeat $string10 50000]
275 CREATE TABLE t4(a string);
276 INSERT INTO t4 VALUES('$string10');
277 INSERT INTO t4 VALUES('$string500');
278 INSERT INTO t4 VALUES('$string500000');
281 do_test types-2.4.2 {
285 } [list $string10 $string500 $string500000]
287 # Check that all the record sizes are as we expected. This is dependant on
288 # the database encoding.
289 if { $sqlite_options(utf16)==0 || [execsql {pragma encoding}] == "UTF-8" } {
290 do_test types-2.4.3 {
291 set root [db eval {select rootpage from sqlite_master where name = 't4'}]
295 do_test types-2.4.3 {
296 set root [db eval {select rootpage from sqlite_master where name = 't4'}]
301 do_test types-2.5.1 {
307 CREATE TABLE t1(a, b, c);
310 do_test types-2.5.2 {
311 set string10 abcdefghij
312 set string500 [string repeat $string10 50]
313 set string500000 [string repeat $string10 50000]
315 execsql "INSERT INTO t1 VALUES(NULL, '$string10', 4000);"
316 execsql "INSERT INTO t1 VALUES('$string500', 4000, NULL);"
317 execsql "INSERT INTO t1 VALUES(4000, NULL, '$string500000');"
319 do_test types-2.5.3 {
323 } [list {} $string10 4000 $string500 4000 {} 4000 {} $string500000]