2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file attempts to check the library in an out-of-memory situation.
15 # When compiled with -DMEMORY_DEBUG=1, the SQLite library accepts a special
16 # command (sqlite_malloc_fail N) which causes the N-th malloc to fail. This
17 # special feature is used to see what happens in the library if a malloc
18 # were to really fail due to an out-of-memory situation.
20 # $Id: malloc.test,v 1.6 2004/02/14 01:39:50 drh Exp $
22 set testdir [file dirname $argv0]
23 source $testdir/tester.tcl
25 # Only run these tests if memory debugging is turned on.
27 if {[info command sqlite_malloc_stat]==""} {
28 puts "Skipping malloc tests: not compiled with -DMEMORY_DEBUG..."
33 for {set go 1; set i 1} {$go} {incr i} {
37 catch {file delete -force test.db}
38 catch {file delete -force test.db-journal}
40 set v [catch {sqlite db test.db} msg]
44 set v [catch {execsql {
46 a int, b float, c double, d text, e varchar(20),
49 CREATE INDEX i1 ON t1(a,b);
50 INSERT INTO t1 VALUES(1,2.3,4.5,'hi','there');
51 INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder');
53 SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0;
54 DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1);
55 SELECT count(*) FROM t1;
58 set leftover [lindex [sqlite_malloc_stat] 2]
60 if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
64 set v2 [expr {$msg=="" || $msg=="out of memory"}]
65 if {!$v2} {puts "\nError message returned: $msg"}
71 set fd [open ./data.tmp w]
72 for {set i 1} {$i<=20} {incr i} {
73 puts $fd "$i\t[expr {$i*$i}]\t[expr {100-$i}] abcdefghijklmnopqrstuvwxyz"
77 for {set go 1; set i 1} {$go} {incr i} {
81 catch {file delete -force test.db}
82 catch {file delete -force test.db-journal}
84 set v [catch {sqlite db test.db} msg]
88 set v [catch {execsql {
89 CREATE TABLE t1(a int, b int, c int);
90 CREATE INDEX i1 ON t1(a,b);
91 COPY t1 FROM 'data.tmp';
92 SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1;
93 UPDATE t1 SET b=b||b||b||b;
94 UPDATE t1 SET b=a WHERE a in (10,12,22);
95 INSERT INTO t1(c,b,a) VALUES(20,10,5);
96 INSERT INTO t1 SELECT * FROM t1
97 WHERE a IN (SELECT a FROM t1 WHERE a<10);
98 DELETE FROM t1 WHERE a>=10;
103 set leftover [lindex [sqlite_malloc_stat] 2]
105 if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
109 set v2 [expr {$msg=="" || $msg=="out of memory"}]
110 if {!$v2} {puts "\nError message returned: $msg"}
116 set fd [open ./data.tmp w]
117 for {set i 1} {$i<=10} {incr i} {
118 puts $fd "$i\t[expr {$i*$i}]\t[expr {100-$i}]"
122 for {set go 1; set i 1} {$go} {incr i} {
123 do_test malloc-3.$i {
126 catch {file delete -force test.db}
127 catch {file delete -force test.db-journal}
128 sqlite_malloc_fail $i
129 set v [catch {sqlite db test.db} msg]
133 set v [catch {execsql {
135 CREATE TABLE t1(a int, b int, c int);
136 CREATE INDEX i1 ON t1(a,b);
137 COPY t1 FROM 'data.tmp';
138 INSERT INTO t1(c,b,a) VALUES(20,10,5);
139 DELETE FROM t1 WHERE a>=10;
145 set leftover [lindex [sqlite_malloc_stat] 2]
147 if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
151 set v2 [expr {$msg=="" || $msg=="out of memory"}]
152 if {!$v2} {puts "\nError message returned: $msg"}
157 for {set go 1; set i 1} {$go} {incr i} {
158 do_test malloc-4.$i {
161 catch {file delete -force test.db}
162 catch {file delete -force test.db-journal}
163 sqlite_malloc_fail $i
164 set v [catch {sqlite db test.db} msg]
168 set v [catch {execsql {
170 CREATE TABLE t1(a int, b int, c int);
171 CREATE INDEX i1 ON t1(a,b);
172 COPY t1 FROM 'data.tmp';
173 UPDATE t1 SET b=a WHERE a in (10,12,22);
174 INSERT INTO t1 SELECT * FROM t1
175 WHERE a IN (SELECT a FROM t1 WHERE a<10);
181 set leftover [lindex [sqlite_malloc_stat] 2]
183 if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
187 set v2 [expr {$msg=="" || $msg=="out of memory"}]
188 if {!$v2} {puts "\nError message returned: $msg"}
193 for {set go 1; set i 1} {$go} {incr i} {
194 do_test malloc-5.$i {
197 catch {file delete -force test.db}
198 catch {file delete -force test.db-journal}
199 sqlite_malloc_fail $i
200 set v [catch {sqlite db test.db} msg]
204 set v [catch {execsql {
206 CREATE TABLE t1(a,b);
207 CREATE TABLE t2(x,y);
208 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
209 INSERT INTO t2(x,y) VALUES(new.rowid,1);
211 INSERT INTO t1(a,b) VALUES(2,3);
215 set leftover [lindex [sqlite_malloc_stat] 2]
217 if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v Message=$msg"}
221 set v2 [expr {$msg=="" || $msg=="out of memory"}]
222 if {!$v2} {puts "\nError message returned: $msg"}