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. The
12 # focus of this script is in-memory database backend.
14 # $Id: memdb.test,v 1.19 2009/05/18 16:04:38 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
22 # In the following sequence of tests, compute the MD5 sum of the content
23 # of a table, make lots of modifications to that table, then do a rollback.
24 # Verify that after the rollback, the MD5 checksum is unchanged.
26 # These tests were browed from trans.tcl.
34 CREATE TABLE t3(x TEXT);
35 INSERT INTO t3 VALUES(randstr(10,400));
36 INSERT INTO t3 VALUES(randstr(10,400));
37 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
38 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
39 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
40 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
41 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
42 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
43 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
44 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
45 INSERT INTO t3 SELECT randstr(10,400) FROM t3;
47 SELECT count(*) FROM t3;
51 # The following procedure computes a "signature" for table "t3". If
52 # T3 changes in any way, the signature should change.
54 # This is used to test ROLLBACK. We gather a signature for t3, then
55 # make lots of changes to t3, then rollback and take another signature.
56 # The two signatures should be the same.
58 proc signature {{fn {}}} {
59 set rx [db eval {SELECT x FROM t3}]
66 # set r [db eval {SELECT count(*), md5sum(x) FROM t3}]
68 return [list [string length $rx] $rx]
71 # Do rollbacks. Make sure the signature does not change.
74 for {set i 2} {$i<=$limit} {incr i} {
75 set ::sig [signature one]
77 set cnt [lindex $::sig 0]
79 execsql {PRAGMA synchronous=FULL}
81 execsql {PRAGMA synchronous=NORMAL}
83 do_test memdb-1.$i.1-$cnt {
86 DELETE FROM t3 WHERE random()%10!=0;
87 INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
88 INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
91 set sig2 [signature two]
94 # if {$sig2!=$sig} exit
95 do_test memdb-1.$i.2-$cnt {
98 DELETE FROM t3 WHERE random()%10!=0;
99 INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
100 DELETE FROM t3 WHERE random()%10!=0;
101 INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
107 do_test memdb-1.$i.9-$cnt {
109 INSERT INTO t3 SELECT randstr(10,400) FROM t3 WHERE random()%10==0;
113 set ::pager_old_format 0
116 integrity_check memdb-2.1
120 CREATE TABLE t4(a,b,c,d);
122 INSERT INTO t4 VALUES(1,2,3,4);
128 SELECT name FROM sqlite_master WHERE type='table';
134 SELECT name FROM sqlite_master WHERE type='table';
140 SELECT name FROM sqlite_master WHERE type='table';
144 # Create tables for the first group of tests.
148 CREATE TABLE t1(a, b, c, UNIQUE(a,b));
150 SELECT c FROM t1 ORDER BY c;
154 # Six columns of configuration data as follows:
156 # i The reference number of the test
157 # conf The conflict resolution algorithm on the BEGIN statement
158 # cmd An INSERT or REPLACE command to execute against table t1
159 # t0 True if there is an error from $cmd
160 # t1 Content of "c" column of t1 assuming no error in $cmd
161 # t2 Content of "x" column of t2
163 foreach {i conf cmd t0 t1 t2} {
165 2 {} {INSERT OR IGNORE} 0 3 1
166 3 {} {INSERT OR REPLACE} 0 4 1
168 5 {} {INSERT OR FAIL} 1 {} 1
169 6 {} {INSERT OR ABORT} 1 {} 1
170 7 {} {INSERT OR ROLLBACK} 1 {} {}
173 # All tests after test 1 depend on conflict resolution. So end the
174 # loop if that is not available in this build.
175 ifcapable !conflict {if {$i>1} break}
178 if {$conf!=""} {set conf "ON CONFLICT $conf"}
179 set r0 [catch {execsql [subst {
182 INSERT INTO t1 VALUES(1,2,3);
184 INSERT INTO t2 VALUES(1);
185 $cmd INTO t1 VALUES(1,2,4);
187 catch {execsql {COMMIT}}
188 if {$r0} {set r1 {}} {set r1 [execsql {SELECT c FROM t1}]}
189 set r2 [execsql {SELECT x FROM t2}]
198 CREATE TABLE t2(a,b,c);
199 INSERT INTO t2 VALUES(1,2,1);
200 INSERT INTO t2 VALUES(2,3,2);
201 INSERT INTO t2 VALUES(3,4,1);
202 INSERT INTO t2 VALUES(4,5,4);
203 SELECT c FROM t2 ORDER BY b;
205 INSERT INTO t3 VALUES(1);
209 # Six columns of configuration data as follows:
211 # i The reference number of the test
212 # conf1 The conflict resolution algorithm on the UNIQUE constraint
213 # conf2 The conflict resolution algorithm on the BEGIN statement
214 # cmd An UPDATE command to execute against table t1
215 # t0 True if there is an error from $cmd
216 # t1 Content of "b" column of t1 assuming no error in $cmd
217 # t2 Content of "x" column of t3
219 foreach {i conf1 conf2 cmd t0 t1 t2} {
220 1 {} {} UPDATE 1 {6 7 8 9} 1
221 2 REPLACE {} UPDATE 0 {7 6 9} 1
222 3 IGNORE {} UPDATE 0 {6 7 3 9} 1
223 4 FAIL {} UPDATE 1 {6 7 3 4} 1
224 5 ABORT {} UPDATE 1 {1 2 3 4} 1
225 6 ROLLBACK {} UPDATE 1 {1 2 3 4} 0
226 7 REPLACE {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1
227 8 IGNORE {} {UPDATE OR REPLACE} 0 {7 6 9} 1
228 9 FAIL {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1
229 10 ABORT {} {UPDATE OR REPLACE} 0 {7 6 9} 1
230 11 ROLLBACK {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1
231 12 {} {} {UPDATE OR IGNORE} 0 {6 7 3 9} 1
232 13 {} {} {UPDATE OR REPLACE} 0 {7 6 9} 1
233 14 {} {} {UPDATE OR FAIL} 1 {6 7 3 4} 1
234 15 {} {} {UPDATE OR ABORT} 1 {1 2 3 4} 1
235 16 {} {} {UPDATE OR ROLLBACK} 1 {1 2 3 4} 0
237 # All tests after test 1 depend on conflict resolution. So end the
238 # loop if that is not available in this build.
239 ifcapable !conflict {
243 if {$t0} {set t1 {UNIQUE constraint failed: t1.a}}
245 if {$conf1!=""} {set conf1 "ON CONFLICT $conf1"}
246 if {$conf2!=""} {set conf2 "ON CONFLICT $conf2"}
247 set r0 [catch {execsql "
249 CREATE TABLE t1(a,b,c, UNIQUE(a) $conf1);
250 INSERT INTO t1 SELECT * FROM t2;
257 catch {execsql {COMMIT}}
258 if {!$r0} {set r1 [execsql {SELECT a FROM t1 ORDER BY b}]}
259 set r2 [execsql {SELECT x FROM t3}]
268 } {1 2 1 2 3 2 3 4 1 4 5 4}
273 SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1;
279 SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1;
286 } {1 2 1 2 3 2 3 4 1 4 5 4}
290 SELECT a FROM t2 UNION SELECT b FROM t2 ORDER BY 1;
293 } ;# ifcapable compound
296 CREATE INDEX i2 ON t2(c);
297 SELECT a FROM t2 ORDER BY c;
302 SELECT a FROM t2 ORDER BY c DESC;
308 CREATE TABLE t5(x,y);
309 INSERT INTO t5 VALUES(1,2);
315 SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1;
321 SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1;
326 CREATE TABLE t5(x PRIMARY KEY, y UNIQUE);
332 SELECT * FROM t5 ORDER BY y DESC;
339 INSERT INTO t5 VALUES(1,2);
340 INSERT INTO t5 VALUES(3,4);
341 REPLACE INTO t5 VALUES(1,4);
342 SELECT rowid,* FROM t5;
347 DELETE FROM t5 WHERE x>5;
353 DELETE FROM t5 WHERE y<3;
361 DELETE FROM t5 WHERE x>0;
366 ifcapable subquery&&vtab {
368 load_static_extension db wholenumber
371 CREATE VIRTUAL TABLE nums USING wholenumber;
372 INSERT INTO t6 SELECT value FROM nums WHERE value BETWEEN 1 AND 256;
373 SELECT count(*) FROM (SELECT DISTINCT x FROM t6);
376 for {set i 1} {$i<=256} {incr i} {
377 do_test memdb-7.2.$i {
378 execsql "DELETE FROM t6 WHERE x=\
379 (SELECT x FROM t6 ORDER BY random() LIMIT 1)"
380 execsql {SELECT count(*) FROM t6}
389 sqlite3 db {:memory:}
391 PRAGMA auto_vacuum=TRUE;
393 INSERT INTO t1 VALUES(randstr(5000,6000));
394 INSERT INTO t1 VALUES(randstr(5000,6000));
395 INSERT INTO t1 VALUES(randstr(5000,6000));
396 INSERT INTO t1 VALUES(randstr(5000,6000));
397 INSERT INTO t1 VALUES(randstr(5000,6000));
398 SELECT count(*) FROM t1;
404 SELECT count(*) FROM t1;
408 # Test that auto-vacuum works with in-memory databases.
410 ifcapable autovacuum {
416 PRAGMA auto_vacuum = full;
418 INSERT INTO t1 VALUES(randstr(1000,1000));
419 INSERT INTO t1 VALUES(randstr(1000,1000));
420 INSERT INTO t1 VALUES(randstr(1000,1000));
422 set memused [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1]
423 set pgovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 1]
424 execsql { DELETE FROM t1 }
425 set memused2 [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1]
426 expr {($memused2 + 2048 < $memused) || $pgovfl==0}
430 } ;# ifcapable memorydb