3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # This file contains tests of the memory allocation subsystem.
14 # $Id: memsubsys2.test,v 1.2 2008/08/12 15:21:12 drh Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 sqlite3_reset_auto_extension
20 # This procedure constructs a new database in test.db. It fills
21 # this database with many small records (enough to force multiple
22 # rebalance operations in the btree-layer and to require a large
23 # page cache), verifies correct results, then returns.
25 proc build_test_db {testname pragmas} {
27 forcedelete test.db test.db-journal
31 CREATE TABLE t1(x, y);
32 CREATE TABLE t2(a, b);
33 CREATE INDEX i1 ON t1(x,y);
34 INSERT INTO t1 VALUES(1, 100);
35 INSERT INTO t1 VALUES(2, 200);
37 for {set i 2} {$i<5000} {incr i $i} {
38 db eval {INSERT INTO t2 SELECT * FROM t1}
39 db eval {INSERT INTO t1 SELECT a+$i, a+b*100 FROM t2}
40 db eval {DELETE FROM t2}
43 db eval {SELECT count(*) FROM t1}
45 integrity_check $testname.2
48 # Reset all of the highwater marks.
50 proc reset_highwater_marks {} {
51 sqlite3_status SQLITE_STATUS_MEMORY_USED 1
52 sqlite3_status SQLITE_STATUS_MALLOC_SIZE 1
53 sqlite3_status SQLITE_STATUS_PAGECACHE_USED 1
54 sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 1
55 sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 1
56 sqlite3_status SQLITE_STATUS_SCRATCH_USED 1
57 sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 1
58 sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 1
59 sqlite3_status SQLITE_STATUS_PARSER_STACK 1
62 # Test 1: Verify that calling sqlite3_malloc(0) returns a NULL
65 set highwater [sqlite3_memory_highwater 0]
66 do_test memsubsys2-1.1 {
69 do_test memsubsys2-1.2 {
70 sqlite3_memory_highwater 0
74 # Test 2: Verify that the highwater mark increases after a large
77 # Do not run this if [sqlite3_memory_used] returns 0. This indicates
78 # an SQLITE_DEFAULT_MEMSTATUS=0 build.
80 if {[sqlite3_memory_used]!=0} {
81 sqlite3_memory_highwater 1
82 set highwater [sqlite3_memory_highwater 0]
83 do_test memsubsys2-2.1 {
84 sqlite3_free [set x [sqlite3_malloc 100000]]
87 do_test memsubsys2-2.2.1 {
88 expr {[sqlite3_memory_highwater 0]>=[sqlite3_memory_used]+100000}
90 do_test memsubsys2-2.2.2 {
91 expr {[sqlite3_memory_highwater 0]>=$highwater+50000}
95 # Test 3: Verify that turning of memstatus disables the statistics
100 sqlite3_config_memstatus 0
102 reset_highwater_marks
103 set highwater [sqlite3_memory_highwater 0]
104 do_test memsubsys2-3.1 {
107 do_test memsubsys2-3.2 {
110 do_test memsubsys2-3.3 {
111 sqlite3_memory_highwater 0
113 do_test memsubsys2-3.4 {
116 do_test memsubsys2-3.5 {
117 set ::allocation [sqlite3_malloc 100000]
118 expr {$::allocation!="0"}
120 do_test memsubsys2-3.6 {
121 sqlite3_memory_highwater 0
123 do_test memsubsys2-3.7 {
126 do_test memsubsys2-3.8 {
127 sqlite3_free $::allocation
129 do_test memsubsys2-3.9 {
134 # Test 4: Verify that turning on memstatus reenables the statistics
138 sqlite3_config_memstatus 1
140 reset_highwater_marks
141 set highwater [sqlite3_memory_highwater 0]
142 do_test memsubsys2-4.1 {
145 do_test memsubsys2-4.2 {
148 do_test memsubsys2-4.3 {
149 sqlite3_memory_highwater 0
151 do_test memsubsys2-4.4 {
154 do_test memsubsys2-4.5 {
155 set ::allocation [sqlite3_malloc 100000]
156 expr {$::allocation!="0"}
158 do_test memsubsys2-4.6 {
159 expr {[sqlite3_memory_highwater 0]>=100000}
161 do_test memsubsys2-4.7 {
162 expr {[sqlite3_memory_used]>=100000}
164 do_test memsubsys2-4.8 {
165 sqlite3_free $::allocation
167 do_test memsubsys2-4.9 {
170 do_test memsubsys2-4.10 {
171 expr {[sqlite3_memory_highwater 0]>=100000}
173 do_test memsubsys2-4.11 {
180 autoinstall_test_functions