blob: 5d922e0cee1125f176f8fbafa28842766268be06 [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#
drh93aed5a2008-01-16 17:46:38 +000015# $Id: mallocC.test,v 1.8 2008/01/16 17:46:38 drh Exp $
drhb3226e82007-08-13 12:39:03 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Only run these tests if memory debugging is turned on.
21#
danielk19774152e672007-09-12 17:01:45 +000022ifcapable !memdebug||!compound {
drh5a3032b2007-09-03 16:12:09 +000023 puts "Skipping mallocC tests: not compiled with -DSQLITE_MEMDEBUG..."
drhed138fb2007-08-22 22:04:37 +000024 finish_test
25 return
drhb3226e82007-08-13 12:39:03 +000026}
27
drhb3226e82007-08-13 12:39:03 +000028proc do_mallocC_test {tn args} {
29 array set ::mallocopts $args
drh93aed5a2008-01-16 17:46:38 +000030 #set sum [allcksum db]
drhb3226e82007-08-13 12:39:03 +000031
32 for {set ::n 1} {true} {incr ::n} {
33
34 # Run the SQL. Malloc number $::n is set to fail. A malloc() failure
35 # may or may not be reported.
danielk1977a1644fd2007-08-29 12:31:25 +000036 sqlite3_memdebug_fail $::n -repeat 1
drhb4035742007-08-13 12:58:18 +000037 do_test mallocC-$tn.$::n.1 {
drhb3226e82007-08-13 12:39:03 +000038 set res [catchsql [string trim $::mallocopts(-sql)]]
39 set rc [expr {
40 0==[string compare $res {1 {out of memory}}] ||
danielk1977ae72d982007-10-03 08:46:44 +000041 [db errorcode] == 3082 ||
drhb3226e82007-08-13 12:39:03 +000042 0==[lindex $res 0]
43 }]
44 if {$rc!=1} {
45 puts "Error: $res"
46 }
47 set rc
48 } {1}
49
50 # If $::n is greater than the number of malloc() calls required to
51 # execute the SQL, then this test is finished. Break out of the loop.
danielk1977a1644fd2007-08-29 12:31:25 +000052 set nFail [sqlite3_memdebug_fail -1]
drhed138fb2007-08-22 22:04:37 +000053 if {$nFail==0} {
drhb3226e82007-08-13 12:39:03 +000054 break
55 }
56
57 # Recover from the malloc failure.
58 #
59 # Update: The new malloc() failure handling means that a transaction may
60 # still be active even if a malloc() has failed. But when these tests were
61 # written this was not the case. So do a manual ROLLBACK here so that the
62 # tests pass.
drhb4035742007-08-13 12:58:18 +000063 do_test mallocC-$tn.$::n.2 {
drhb3226e82007-08-13 12:39:03 +000064 catch {
65 execsql {
66 ROLLBACK;
67 }
68 }
69 expr 0
70 } {0}
71
72 # Checksum the database.
drhb4035742007-08-13 12:58:18 +000073 #do_test mallocC-$tn.$::n.3 {
drh93aed5a2008-01-16 17:46:38 +000074 # allcksum db
drhb3226e82007-08-13 12:39:03 +000075 #} $sum
76
drhb4035742007-08-13 12:58:18 +000077 #integrity_check mallocC-$tn.$::n.4
drhb3226e82007-08-13 12:39:03 +000078 if {$::nErr>1} return
79 }
80 unset ::mallocopts
81}
82
danielk1977ae72d982007-10-03 08:46:44 +000083sqlite3_extended_result_codes db 1
84
drhb3226e82007-08-13 12:39:03 +000085execsql {
86 PRAGMA auto_vacuum=1;
87 CREATE TABLE t0(a, b, c);
88}
89do_mallocC_test 1 -sql {
90 BEGIN;
91 -- Allocate 32 new root pages. This will exercise the 'extract specific
92 -- page from the freelist' code when in auto-vacuum mode (see the
93 -- allocatePage() routine in btree.c).
94 CREATE TABLE t1(a UNIQUE, b UNIQUE, c UNIQUE);
95 CREATE TABLE t2(a UNIQUE, b UNIQUE, c UNIQUE);
96 CREATE TABLE t3(a UNIQUE, b UNIQUE, c UNIQUE);
97 CREATE TABLE t4(a UNIQUE, b UNIQUE, c UNIQUE);
98 CREATE TABLE t5(a UNIQUE, b UNIQUE, c UNIQUE);
99 CREATE TABLE t6(a UNIQUE, b UNIQUE, c UNIQUE);
100 CREATE TABLE t7(a UNIQUE, b UNIQUE, c UNIQUE);
101 CREATE TABLE t8(a UNIQUE, b UNIQUE, c UNIQUE);
102
103 ROLLBACK;
104}
105
106finish_test