xref: /sqlite-3.40.0/test/mallocC.test (revision 93aed5a1)
1# 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#
15# $Id: mallocC.test,v 1.8 2008/01/16 17:46:38 drh Exp $
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Only run these tests if memory debugging is turned on.
21#
22ifcapable !memdebug||!compound {
23   puts "Skipping mallocC tests: not compiled with -DSQLITE_MEMDEBUG..."
24   finish_test
25   return
26}
27
28proc do_mallocC_test {tn args} {
29  array set ::mallocopts $args
30  #set sum [allcksum db]
31
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.
36    sqlite3_memdebug_fail $::n -repeat 1
37    do_test mallocC-$tn.$::n.1 {
38      set res [catchsql [string trim $::mallocopts(-sql)]]
39      set rc [expr {
40        0==[string compare $res {1 {out of memory}}] ||
41        [db errorcode] == 3082 ||
42        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.
52    set nFail [sqlite3_memdebug_fail -1]
53    if {$nFail==0} {
54      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.
63    do_test mallocC-$tn.$::n.2 {
64      catch {
65        execsql {
66          ROLLBACK;
67        }
68      }
69      expr 0
70    } {0}
71
72    # Checksum the database.
73    #do_test mallocC-$tn.$::n.3 {
74    #  allcksum db
75    #} $sum
76
77    #integrity_check mallocC-$tn.$::n.4
78  if {$::nErr>1} return
79  }
80  unset ::mallocopts
81}
82
83sqlite3_extended_result_codes db 1
84
85execsql {
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
107