blob: f8c66995dfcbc4d7278defe143bb868043cc7ddd [file] [log] [blame]
drhed7c8552001-04-11 14:29:21 +00001# Copyright (c) 2001 D. Richard Hipp
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public
5# License as published by the Free Software Foundation; either
6# version 2 of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11# General Public License for more details.
12#
13# You should have received a copy of the GNU General Public
14# License along with this library; if not, write to the
15# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16# Boston, MA 02111-1307, USA.
17#
18# Author contact information:
19# drh@hwaci.com
20# http://www.hwaci.com/drh/
21#
22#***********************************************************************
23# This file attempts to check the library in an out-of-memory situation.
24# When compiled with -DMEMORY_DEBUG=1, the SQLite library accepts a special
25# command (--malloc-fail=N) which causes the N-th malloc to fail. This
26# special feature is used to see what happens in the library if a malloc
27# were to really fail due to an out-of-memory situation.
28#
drhd4007282001-04-12 23:21:58 +000029# $Id: malloc.test,v 1.2 2001/04/12 23:21:59 drh Exp $
drhed7c8552001-04-11 14:29:21 +000030
31set testdir [file dirname $argv0]
32source $testdir/tester.tcl
33
34# Only run these tests if memory debugging is turned on.
35#
36if {[info command sqlite_malloc_fail]==""} {
37 puts "Skipping malloc tests: not compiled with -DMEMORY_DEBUG..."
38 finish_test
39 return
40}
41
42for {set go 1; set i 1} {$go} {incr i} {
43 do_test malloc-1.$i {
44 sqlite_malloc_fail 0
45 catch {execsql {DROP TABLE t1}}
46 sqlite_malloc_fail $i
47 set v [catch {execsql {
48 CREATE TABLE t1(
49 a int, b float, c double, d text, e varchar(20),
50 primary key(a,b,c)
51 );
52 CREATE INDEX i1 ON t1(a,b);
53 INSERT INTO t1 VALUES(1,2.3,4.5,'hi','there');
54 INSERT INTO t1 VALUES(6,7.0,0.8,'hello','out yonder');
55 SELECT * FROM t1;
drhd4007282001-04-12 23:21:58 +000056 SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0;
drhed7c8552001-04-11 14:29:21 +000057 DELETE FROM t1 WHERE a==6;
58 SELECT count(*) FROM t1;
59 }} msg]
60 if {[lindex [sqlite_malloc_stat] 2]>0} {
61 set ::go 0
62 set v {1 1}
63 } else {
64 lappend v [expr {$msg=="" || $msg=="out of memory"}]
65 }
66 } {1 1}
67}
drhd4007282001-04-12 23:21:58 +000068
69set fd [open ./data.tmp w]
70for {set i 1} {$i<=40} {incr i} {
71 puts $fd "$i\t[expr {$i*$i}]\t[expr {100-$i}]"
72}
73close $fd
74
75for {set go 1; set i 1} {$go} {incr i} {
76 do_test malloc-2.$i {
77 sqlite_malloc_fail 0
78 catch {execsql {DROP TABLE t1}}
79 sqlite_malloc_fail $i
80 set v [catch {execsql {
81 CREATE TABLE t1(a int, b int, c int);
82 CREATE INDEX i1 ON t1(a,b);
83 COPY t1 FROM 'data.tmp';
84 SELECT 'stuff', count(*) as 'other stuff' FROM t1;
85 UPDATE t1 SET b=a WHERE a in (10,12,22);
86 DROP INDEX i1;
87 VACUUM t1;
88 }} msg]
89 if {[lindex [sqlite_malloc_stat] 2]>0} {
90 set ::go 0
91 set v {1 1}
92 } else {
93 lappend v [expr {$msg=="" || $msg=="out of memory"}]
94 }
95 } {1 1}
96}
drhed7c8552001-04-11 14:29:21 +000097sqlite_malloc_fail 0
98finish_test