blob: bc008e0b7e9938b8954166e3fca708c997257eef [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drhed7c8552001-04-11 14:29:21 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drhed7c8552001-04-11 14:29:21 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drhed7c8552001-04-11 14:29:21 +00009#
10#***********************************************************************
11# This file attempts to check the library in an out-of-memory situation.
12# When compiled with -DMEMORY_DEBUG=1, the SQLite library accepts a special
13# command (--malloc-fail=N) which causes the N-th malloc to fail. This
14# special feature is used to see what happens in the library if a malloc
15# were to really fail due to an out-of-memory situation.
16#
drhb19a2bc2001-09-16 00:13:26 +000017# $Id: malloc.test,v 1.3 2001/09/16 00:13:28 drh Exp $
drhed7c8552001-04-11 14:29:21 +000018
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22# Only run these tests if memory debugging is turned on.
23#
24if {[info command sqlite_malloc_fail]==""} {
25 puts "Skipping malloc tests: not compiled with -DMEMORY_DEBUG..."
26 finish_test
27 return
28}
29
30for {set go 1; set i 1} {$go} {incr i} {
31 do_test malloc-1.$i {
32 sqlite_malloc_fail 0
33 catch {execsql {DROP TABLE t1}}
34 sqlite_malloc_fail $i
35 set v [catch {execsql {
36 CREATE TABLE t1(
37 a int, b float, c double, d text, e varchar(20),
38 primary key(a,b,c)
39 );
40 CREATE INDEX i1 ON t1(a,b);
41 INSERT INTO t1 VALUES(1,2.3,4.5,'hi','there');
42 INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder');
43 SELECT * FROM t1;
drhd4007282001-04-12 23:21:58 +000044 SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0;
drhed7c8552001-04-11 14:29:21 +000045 DELETE FROM t1 WHERE a==6;
46 SELECT count(*) FROM t1;
47 }} msg]
48 if {[lindex [sqlite_malloc_stat] 2]>0} {
49 set ::go 0
50 set v {1 1}
51 } else {
52 lappend v [expr {$msg=="" || $msg=="out of memory"}]
53 }
54 } {1 1}
55}
drhd4007282001-04-12 23:21:58 +000056
57set fd [open ./data.tmp w]
58for {set i 1} {$i<=40} {incr i} {
59 puts $fd "$i\t[expr {$i*$i}]\t[expr {100-$i}]"
60}
61close $fd
62
63for {set go 1; set i 1} {$go} {incr i} {
64 do_test malloc-2.$i {
65 sqlite_malloc_fail 0
66 catch {execsql {DROP TABLE t1}}
67 sqlite_malloc_fail $i
68 set v [catch {execsql {
69 CREATE TABLE t1(a int, b int, c int);
70 CREATE INDEX i1 ON t1(a,b);
71 COPY t1 FROM 'data.tmp';
72 SELECT 'stuff', count(*) as 'other stuff' FROM t1;
73 UPDATE t1 SET b=a WHERE a in (10,12,22);
74 DROP INDEX i1;
75 VACUUM t1;
76 }} msg]
77 if {[lindex [sqlite_malloc_stat] 2]>0} {
78 set ::go 0
79 set v {1 1}
80 } else {
81 lappend v [expr {$msg=="" || $msg=="out of memory"}]
82 }
83 } {1 1}
84}
drhed7c8552001-04-11 14:29:21 +000085sqlite_malloc_fail 0
86finish_test