xref: /sqlite-3.40.0/test/memsubsys2.test (revision 8a42cbd3)
1# 2008 June 18
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 contains tests of the memory allocation subsystem.
13#
14# $Id: memsubsys2.test,v 1.1 2008/07/10 18:13:43 drh Exp $
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18sqlite3_reset_auto_extension
19
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.
24#
25proc build_test_db {testname pragmas} {
26  catch {db close}
27  file delete -force test.db test.db-journal
28  sqlite3 db test.db
29  db eval $pragmas
30  db eval {
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);
36  }
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}
41  }
42  do_test $testname.1 {
43    db eval {SELECT count(*) FROM t1}
44  } 8192
45  integrity_check $testname.2
46}
47
48# Test 1:  Verify that calling sqlite3_malloc(0) returns a NULL
49# pointer.
50#
51set highwater [sqlite3_memory_highwater 0]
52do_test memsubsys2-1.1 {
53  sqlite3_malloc 0
54} {0}
55do_test memsubsys2-1.2 {
56  sqlite3_memory_highwater 0
57} $highwater
58
59
60# Test 2:  Verify that the highwater mark increases after a large
61# allocation.
62#
63sqlite3_memory_highwater 1
64set highwater [sqlite3_memory_highwater 0]
65do_test memsubsys2-2.1 {
66  sqlite3_free [set x [sqlite3_malloc 100000]]
67  expr {$x!="0"}
68} {1}
69do_test memsubsys2-2.2 {
70  expr {[sqlite3_memory_highwater 0]>=[sqlite3_memory_used]+$highwater}
71} {1}
72
73# Test 3: Verify that turning of memstatus disables the statistics
74# tracking.
75#
76db close
77sqlite3_shutdown
78sqlite3_config_memstatus 0
79sqlite3_initialize
80set highwater [sqlite3_memory_highwater 0]
81do_test memsubsys2-3.1 {
82  set highwater
83} {0}
84do_test memsubsys2-3.2 {
85  sqlite3_malloc 0
86} {0}
87do_test memsubsys2-3.3 {
88  sqlite3_memory_highwater 0
89} {0}
90do_test memsubsys2-3.4 {
91  sqlite3_memory_used
92} {0}
93do_test memsubsys2-3.5 {
94  set ::allocation [sqlite3_malloc 100000]
95  expr {$::allocation!="0"}
96} {1}
97do_test memsubsys2-3.6 {
98  sqlite3_memory_highwater 0
99} {0}
100do_test memsubsys2-3.7 {
101  sqlite3_memory_used
102} {0}
103do_test memsubsys2-3.8 {
104  sqlite3_free $::allocation
105} {}
106do_test memsubsys2-3.9 {
107  sqlite3_free 0
108} {}
109
110
111# Test 4: Verify that turning on memstatus reenables the statistics
112# tracking.
113#
114sqlite3_shutdown
115sqlite3_config_memstatus 1
116sqlite3_initialize
117set highwater [sqlite3_memory_highwater 0]
118do_test memsubsys2-4.1 {
119  set highwater
120} {0}
121do_test memsubsys2-4.2 {
122  sqlite3_malloc 0
123} {0}
124do_test memsubsys2-4.3 {
125  sqlite3_memory_highwater 0
126} {0}
127do_test memsubsys2-4.4 {
128  sqlite3_memory_used
129} {0}
130do_test memsubsys2-4.5 {
131  set ::allocation [sqlite3_malloc 100000]
132  expr {$::allocation!="0"}
133} {1}
134do_test memsubsys2-4.6 {
135  expr {[sqlite3_memory_highwater 0]>=100000}
136} {1}
137do_test memsubsys2-4.7 {
138  expr {[sqlite3_memory_used]>=100000}
139} {1}
140do_test memsubsys2-4.8 {
141  sqlite3_free $::allocation
142} {}
143do_test memsubsys2-4.9 {
144  sqlite3_free 0
145} {}
146do_test memsubsys2-4.10 {
147  expr {[sqlite3_memory_highwater 0]>=100000}
148} {1}
149do_test memsubsys2-4.11 {
150  sqlite3_memory_used
151} {0}
152
153
154
155
156autoinstall_test_functions
157finish_test
158