blob: 8bdc0d1e9100bd5fd180e56d03171e017c799b30 [file] [log] [blame]
drhb3226e82007-08-13 12:39:03 +00001# 2007 Aug 13
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
12# This file tests aspects of the malloc failure while parsing
13# CREATE TABLE statements in auto_vacuum mode.
14#
drheee4c8c2008-02-18 22:24:57 +000015# $Id: mallocC.test,v 1.9 2008/02/18 22:24:58 drh Exp $
drhb3226e82007-08-13 12:39:03 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
drheee4c8c2008-02-18 22:24:57 +000019source $testdir/malloc_common.tcl
drhb3226e82007-08-13 12:39:03 +000020
21# Only run these tests if memory debugging is turned on.
22#
drheee4c8c2008-02-18 22:24:57 +000023if {!$MEMDEBUG} {
drh5a3032b2007-09-03 16:12:09 +000024 puts "Skipping mallocC tests: not compiled with -DSQLITE_MEMDEBUG..."
drhed138fb2007-08-22 22:04:37 +000025 finish_test
26 return
drhb3226e82007-08-13 12:39:03 +000027}
28
drhb3226e82007-08-13 12:39:03 +000029proc do_mallocC_test {tn args} {
30 array set ::mallocopts $args
drh93aed5a2008-01-16 17:46:38 +000031 #set sum [allcksum db]
drhb3226e82007-08-13 12:39:03 +000032
33 for {set ::n 1} {true} {incr ::n} {
34
35 # Run the SQL. Malloc number $::n is set to fail. A malloc() failure
36 # may or may not be reported.
danielk1977a1644fd2007-08-29 12:31:25 +000037 sqlite3_memdebug_fail $::n -repeat 1
drhb4035742007-08-13 12:58:18 +000038 do_test mallocC-$tn.$::n.1 {
drhb3226e82007-08-13 12:39:03 +000039 set res [catchsql [string trim $::mallocopts(-sql)]]
40 set rc [expr {
41 0==[string compare $res {1 {out of memory}}] ||
danielk1977ae72d982007-10-03 08:46:44 +000042 [db errorcode] == 3082 ||
drhb3226e82007-08-13 12:39:03 +000043 0==[lindex $res 0]
44 }]
45 if {$rc!=1} {
46 puts "Error: $res"
47 }
48 set rc
49 } {1}
50
51 # If $::n is greater than the number of malloc() calls required to
52 # execute the SQL, then this test is finished. Break out of the loop.
danielk1977a1644fd2007-08-29 12:31:25 +000053 set nFail [sqlite3_memdebug_fail -1]
drhed138fb2007-08-22 22:04:37 +000054 if {$nFail==0} {
drhb3226e82007-08-13 12:39:03 +000055 break
56 }
57
58 # Recover from the malloc failure.
59 #
60 # Update: The new malloc() failure handling means that a transaction may
61 # still be active even if a malloc() has failed. But when these tests were
62 # written this was not the case. So do a manual ROLLBACK here so that the
63 # tests pass.
drhb4035742007-08-13 12:58:18 +000064 do_test mallocC-$tn.$::n.2 {
drhb3226e82007-08-13 12:39:03 +000065 catch {
66 execsql {
67 ROLLBACK;
68 }
69 }
70 expr 0
71 } {0}
72
73 # Checksum the database.
drhb4035742007-08-13 12:58:18 +000074 #do_test mallocC-$tn.$::n.3 {
drh93aed5a2008-01-16 17:46:38 +000075 # allcksum db
drhb3226e82007-08-13 12:39:03 +000076 #} $sum
77
drhb4035742007-08-13 12:58:18 +000078 #integrity_check mallocC-$tn.$::n.4
drhb3226e82007-08-13 12:39:03 +000079 if {$::nErr>1} return
80 }
81 unset ::mallocopts
82}
83
danielk1977ae72d982007-10-03 08:46:44 +000084sqlite3_extended_result_codes db 1
85
drhb3226e82007-08-13 12:39:03 +000086execsql {
87 PRAGMA auto_vacuum=1;
88 CREATE TABLE t0(a, b, c);
89}
90do_mallocC_test 1 -sql {
91 BEGIN;
92 -- Allocate 32 new root pages. This will exercise the 'extract specific
93 -- page from the freelist' code when in auto-vacuum mode (see the
94 -- allocatePage() routine in btree.c).
95 CREATE TABLE t1(a UNIQUE, b UNIQUE, c UNIQUE);
96 CREATE TABLE t2(a UNIQUE, b UNIQUE, c UNIQUE);
97 CREATE TABLE t3(a UNIQUE, b UNIQUE, c UNIQUE);
98 CREATE TABLE t4(a UNIQUE, b UNIQUE, c UNIQUE);
99 CREATE TABLE t5(a UNIQUE, b UNIQUE, c UNIQUE);
100 CREATE TABLE t6(a UNIQUE, b UNIQUE, c UNIQUE);
101 CREATE TABLE t7(a UNIQUE, b UNIQUE, c UNIQUE);
102 CREATE TABLE t8(a UNIQUE, b UNIQUE, c UNIQUE);
103
104 ROLLBACK;
105}
106
107finish_test