xref: /titanic_51/usr/src/lib/libsqlite/test/malloc.test (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292
2*c5c4113dSnw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*c5c4113dSnw141292
4*c5c4113dSnw141292# 2001 September 15
5*c5c4113dSnw141292#
6*c5c4113dSnw141292# The author disclaims copyright to this source code.  In place of
7*c5c4113dSnw141292# a legal notice, here is a blessing:
8*c5c4113dSnw141292#
9*c5c4113dSnw141292#    May you do good and not evil.
10*c5c4113dSnw141292#    May you find forgiveness for yourself and forgive others.
11*c5c4113dSnw141292#    May you share freely, never taking more than you give.
12*c5c4113dSnw141292#
13*c5c4113dSnw141292#***********************************************************************
14*c5c4113dSnw141292# This file attempts to check the library in an out-of-memory situation.
15*c5c4113dSnw141292# When compiled with -DMEMORY_DEBUG=1, the SQLite library accepts a special
16*c5c4113dSnw141292# command (sqlite_malloc_fail N) which causes the N-th malloc to fail.  This
17*c5c4113dSnw141292# special feature is used to see what happens in the library if a malloc
18*c5c4113dSnw141292# were to really fail due to an out-of-memory situation.
19*c5c4113dSnw141292#
20*c5c4113dSnw141292# $Id: malloc.test,v 1.6 2004/02/14 01:39:50 drh Exp $
21*c5c4113dSnw141292
22*c5c4113dSnw141292set testdir [file dirname $argv0]
23*c5c4113dSnw141292source $testdir/tester.tcl
24*c5c4113dSnw141292
25*c5c4113dSnw141292# Only run these tests if memory debugging is turned on.
26*c5c4113dSnw141292#
27*c5c4113dSnw141292if {[info command sqlite_malloc_stat]==""} {
28*c5c4113dSnw141292   puts "Skipping malloc tests: not compiled with -DMEMORY_DEBUG..."
29*c5c4113dSnw141292   finish_test
30*c5c4113dSnw141292   return
31*c5c4113dSnw141292}
32*c5c4113dSnw141292
33*c5c4113dSnw141292for {set go 1; set i 1} {$go} {incr i} {
34*c5c4113dSnw141292  do_test malloc-1.$i {
35*c5c4113dSnw141292     sqlite_malloc_fail 0
36*c5c4113dSnw141292     catch {db close}
37*c5c4113dSnw141292     catch {file delete -force test.db}
38*c5c4113dSnw141292     catch {file delete -force test.db-journal}
39*c5c4113dSnw141292     sqlite_malloc_fail $i
40*c5c4113dSnw141292     set v [catch {sqlite db test.db} msg]
41*c5c4113dSnw141292     if {$v} {
42*c5c4113dSnw141292       set msg ""
43*c5c4113dSnw141292     } else {
44*c5c4113dSnw141292       set v [catch {execsql {
45*c5c4113dSnw141292          CREATE TABLE t1(
46*c5c4113dSnw141292             a int, b float, c double, d text, e varchar(20),
47*c5c4113dSnw141292             primary key(a,b,c)
48*c5c4113dSnw141292          );
49*c5c4113dSnw141292          CREATE INDEX i1 ON t1(a,b);
50*c5c4113dSnw141292          INSERT INTO t1 VALUES(1,2.3,4.5,'hi','there');
51*c5c4113dSnw141292          INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder');
52*c5c4113dSnw141292          SELECT * FROM t1;
53*c5c4113dSnw141292          SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0;
54*c5c4113dSnw141292          DELETE FROM t1 WHERE a IN (SELECT min(a) FROM t1);
55*c5c4113dSnw141292          SELECT count(*) FROM t1;
56*c5c4113dSnw141292       }} msg]
57*c5c4113dSnw141292     }
58*c5c4113dSnw141292     set leftover [lindex [sqlite_malloc_stat] 2]
59*c5c4113dSnw141292     if {$leftover>0} {
60*c5c4113dSnw141292       if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v  Message=$msg"}
61*c5c4113dSnw141292       set ::go 0
62*c5c4113dSnw141292       set v {1 1}
63*c5c4113dSnw141292     } else {
64*c5c4113dSnw141292       set v2 [expr {$msg=="" || $msg=="out of memory"}]
65*c5c4113dSnw141292       if {!$v2} {puts "\nError message returned: $msg"}
66*c5c4113dSnw141292       lappend v $v2
67*c5c4113dSnw141292     }
68*c5c4113dSnw141292  } {1 1}
69*c5c4113dSnw141292}
70*c5c4113dSnw141292
71*c5c4113dSnw141292set fd [open ./data.tmp w]
72*c5c4113dSnw141292for {set i 1} {$i<=20} {incr i} {
73*c5c4113dSnw141292  puts $fd "$i\t[expr {$i*$i}]\t[expr {100-$i}] abcdefghijklmnopqrstuvwxyz"
74*c5c4113dSnw141292}
75*c5c4113dSnw141292close $fd
76*c5c4113dSnw141292
77*c5c4113dSnw141292for {set go 1; set i 1} {$go} {incr i} {
78*c5c4113dSnw141292  do_test malloc-2.$i {
79*c5c4113dSnw141292     sqlite_malloc_fail 0
80*c5c4113dSnw141292     catch {db close}
81*c5c4113dSnw141292     catch {file delete -force test.db}
82*c5c4113dSnw141292     catch {file delete -force test.db-journal}
83*c5c4113dSnw141292     sqlite_malloc_fail $i
84*c5c4113dSnw141292     set v [catch {sqlite db test.db} msg]
85*c5c4113dSnw141292     if {$v} {
86*c5c4113dSnw141292       set msg ""
87*c5c4113dSnw141292     } else {
88*c5c4113dSnw141292       set v [catch {execsql {
89*c5c4113dSnw141292         CREATE TABLE t1(a int, b int, c int);
90*c5c4113dSnw141292         CREATE INDEX i1 ON t1(a,b);
91*c5c4113dSnw141292         COPY t1 FROM 'data.tmp';
92*c5c4113dSnw141292         SELECT 'stuff', count(*) as 'other stuff', max(a+10) FROM t1;
93*c5c4113dSnw141292         UPDATE t1 SET b=b||b||b||b;
94*c5c4113dSnw141292         UPDATE t1 SET b=a WHERE a in (10,12,22);
95*c5c4113dSnw141292         INSERT INTO t1(c,b,a) VALUES(20,10,5);
96*c5c4113dSnw141292         INSERT INTO t1 SELECT * FROM t1
97*c5c4113dSnw141292             WHERE a IN (SELECT a FROM t1 WHERE a<10);
98*c5c4113dSnw141292         DELETE FROM t1 WHERE a>=10;
99*c5c4113dSnw141292         DROP INDEX i1;
100*c5c4113dSnw141292         DELETE FROM t1;
101*c5c4113dSnw141292       }} msg]
102*c5c4113dSnw141292     }
103*c5c4113dSnw141292     set leftover [lindex [sqlite_malloc_stat] 2]
104*c5c4113dSnw141292     if {$leftover>0} {
105*c5c4113dSnw141292       if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v  Message=$msg"}
106*c5c4113dSnw141292       set ::go 0
107*c5c4113dSnw141292       set v {1 1}
108*c5c4113dSnw141292     } else {
109*c5c4113dSnw141292       set v2 [expr {$msg=="" || $msg=="out of memory"}]
110*c5c4113dSnw141292       if {!$v2} {puts "\nError message returned: $msg"}
111*c5c4113dSnw141292       lappend v $v2
112*c5c4113dSnw141292     }
113*c5c4113dSnw141292  } {1 1}
114*c5c4113dSnw141292}
115*c5c4113dSnw141292
116*c5c4113dSnw141292set fd [open ./data.tmp w]
117*c5c4113dSnw141292for {set i 1} {$i<=10} {incr i} {
118*c5c4113dSnw141292  puts $fd "$i\t[expr {$i*$i}]\t[expr {100-$i}]"
119*c5c4113dSnw141292}
120*c5c4113dSnw141292close $fd
121*c5c4113dSnw141292
122*c5c4113dSnw141292for {set go 1; set i 1} {$go} {incr i} {
123*c5c4113dSnw141292  do_test malloc-3.$i {
124*c5c4113dSnw141292     sqlite_malloc_fail 0
125*c5c4113dSnw141292     catch {db close}
126*c5c4113dSnw141292     catch {file delete -force test.db}
127*c5c4113dSnw141292     catch {file delete -force test.db-journal}
128*c5c4113dSnw141292     sqlite_malloc_fail $i
129*c5c4113dSnw141292     set v [catch {sqlite db test.db} msg]
130*c5c4113dSnw141292     if {$v} {
131*c5c4113dSnw141292       set msg ""
132*c5c4113dSnw141292     } else {
133*c5c4113dSnw141292       set v [catch {execsql {
134*c5c4113dSnw141292         BEGIN TRANSACTION;
135*c5c4113dSnw141292         CREATE TABLE t1(a int, b int, c int);
136*c5c4113dSnw141292         CREATE INDEX i1 ON t1(a,b);
137*c5c4113dSnw141292         COPY t1 FROM 'data.tmp';
138*c5c4113dSnw141292         INSERT INTO t1(c,b,a) VALUES(20,10,5);
139*c5c4113dSnw141292         DELETE FROM t1 WHERE a>=10;
140*c5c4113dSnw141292         DROP INDEX i1;
141*c5c4113dSnw141292         DELETE FROM t1;
142*c5c4113dSnw141292         ROLLBACK;
143*c5c4113dSnw141292       }} msg]
144*c5c4113dSnw141292     }
145*c5c4113dSnw141292     set leftover [lindex [sqlite_malloc_stat] 2]
146*c5c4113dSnw141292     if {$leftover>0} {
147*c5c4113dSnw141292       if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v  Message=$msg"}
148*c5c4113dSnw141292       set ::go 0
149*c5c4113dSnw141292       set v {1 1}
150*c5c4113dSnw141292     } else {
151*c5c4113dSnw141292       set v2 [expr {$msg=="" || $msg=="out of memory"}]
152*c5c4113dSnw141292       if {!$v2} {puts "\nError message returned: $msg"}
153*c5c4113dSnw141292       lappend v $v2
154*c5c4113dSnw141292     }
155*c5c4113dSnw141292  } {1 1}
156*c5c4113dSnw141292}
157*c5c4113dSnw141292for {set go 1; set i 1} {$go} {incr i} {
158*c5c4113dSnw141292  do_test malloc-4.$i {
159*c5c4113dSnw141292     sqlite_malloc_fail 0
160*c5c4113dSnw141292     catch {db close}
161*c5c4113dSnw141292     catch {file delete -force test.db}
162*c5c4113dSnw141292     catch {file delete -force test.db-journal}
163*c5c4113dSnw141292     sqlite_malloc_fail $i
164*c5c4113dSnw141292     set v [catch {sqlite db test.db} msg]
165*c5c4113dSnw141292     if {$v} {
166*c5c4113dSnw141292       set msg ""
167*c5c4113dSnw141292     } else {
168*c5c4113dSnw141292       set v [catch {execsql {
169*c5c4113dSnw141292         BEGIN TRANSACTION;
170*c5c4113dSnw141292         CREATE TABLE t1(a int, b int, c int);
171*c5c4113dSnw141292         CREATE INDEX i1 ON t1(a,b);
172*c5c4113dSnw141292         COPY t1 FROM 'data.tmp';
173*c5c4113dSnw141292         UPDATE t1 SET b=a WHERE a in (10,12,22);
174*c5c4113dSnw141292         INSERT INTO t1 SELECT * FROM t1
175*c5c4113dSnw141292             WHERE a IN (SELECT a FROM t1 WHERE a<10);
176*c5c4113dSnw141292         DROP INDEX i1;
177*c5c4113dSnw141292         DELETE FROM t1;
178*c5c4113dSnw141292         COMMIT;
179*c5c4113dSnw141292       }} msg]
180*c5c4113dSnw141292     }
181*c5c4113dSnw141292     set leftover [lindex [sqlite_malloc_stat] 2]
182*c5c4113dSnw141292     if {$leftover>0} {
183*c5c4113dSnw141292       if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v  Message=$msg"}
184*c5c4113dSnw141292       set ::go 0
185*c5c4113dSnw141292       set v {1 1}
186*c5c4113dSnw141292     } else {
187*c5c4113dSnw141292       set v2 [expr {$msg=="" || $msg=="out of memory"}]
188*c5c4113dSnw141292       if {!$v2} {puts "\nError message returned: $msg"}
189*c5c4113dSnw141292       lappend v $v2
190*c5c4113dSnw141292     }
191*c5c4113dSnw141292  } {1 1}
192*c5c4113dSnw141292}
193*c5c4113dSnw141292for {set go 1; set i 1} {$go} {incr i} {
194*c5c4113dSnw141292  do_test malloc-5.$i {
195*c5c4113dSnw141292     sqlite_malloc_fail 0
196*c5c4113dSnw141292     catch {db close}
197*c5c4113dSnw141292     catch {file delete -force test.db}
198*c5c4113dSnw141292     catch {file delete -force test.db-journal}
199*c5c4113dSnw141292     sqlite_malloc_fail $i
200*c5c4113dSnw141292     set v [catch {sqlite db test.db} msg]
201*c5c4113dSnw141292     if {$v} {
202*c5c4113dSnw141292       set msg ""
203*c5c4113dSnw141292     } else {
204*c5c4113dSnw141292       set v [catch {execsql {
205*c5c4113dSnw141292         BEGIN TRANSACTION;
206*c5c4113dSnw141292         CREATE TABLE t1(a,b);
207*c5c4113dSnw141292         CREATE TABLE t2(x,y);
208*c5c4113dSnw141292         CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN
209*c5c4113dSnw141292           INSERT INTO t2(x,y) VALUES(new.rowid,1);
210*c5c4113dSnw141292         END;
211*c5c4113dSnw141292         INSERT INTO t1(a,b) VALUES(2,3);
212*c5c4113dSnw141292         COMMIT;
213*c5c4113dSnw141292       }} msg]
214*c5c4113dSnw141292     }
215*c5c4113dSnw141292     set leftover [lindex [sqlite_malloc_stat] 2]
216*c5c4113dSnw141292     if {$leftover>0} {
217*c5c4113dSnw141292       if {$leftover>1} {puts "\nLeftover: $leftover\nReturn=$v  Message=$msg"}
218*c5c4113dSnw141292       set ::go 0
219*c5c4113dSnw141292       set v {1 1}
220*c5c4113dSnw141292     } else {
221*c5c4113dSnw141292       set v2 [expr {$msg=="" || $msg=="out of memory"}]
222*c5c4113dSnw141292       if {!$v2} {puts "\nError message returned: $msg"}
223*c5c4113dSnw141292       lappend v $v2
224*c5c4113dSnw141292     }
225*c5c4113dSnw141292  } {1 1}
226*c5c4113dSnw141292}
227*c5c4113dSnw141292sqlite_malloc_fail 0
228*c5c4113dSnw141292finish_test
229