drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 1 | # 2001 September 15 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 9 | # |
| 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 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 17 | # $Id: malloc.test,v 1.3 2001/09/16 00:13:28 drh Exp $ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 18 | |
| 19 | set testdir [file dirname $argv0] |
| 20 | source $testdir/tester.tcl |
| 21 | |
| 22 | # Only run these tests if memory debugging is turned on. |
| 23 | # |
| 24 | if {[info command sqlite_malloc_fail]==""} { |
| 25 | puts "Skipping malloc tests: not compiled with -DMEMORY_DEBUG..." |
| 26 | finish_test |
| 27 | return |
| 28 | } |
| 29 | |
| 30 | for {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; |
drh | d400728 | 2001-04-12 23:21:58 +0000 | [diff] [blame] | 44 | SELECT avg(b) FROM t1 GROUP BY a HAVING b>20.0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 45 | 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 | } |
drh | d400728 | 2001-04-12 23:21:58 +0000 | [diff] [blame] | 56 | |
| 57 | set fd [open ./data.tmp w] |
| 58 | for {set i 1} {$i<=40} {incr i} { |
| 59 | puts $fd "$i\t[expr {$i*$i}]\t[expr {100-$i}]" |
| 60 | } |
| 61 | close $fd |
| 62 | |
| 63 | for {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 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 85 | sqlite_malloc_fail 0 |
| 86 | finish_test |