sl@0
|
1 |
# 2008 June 18
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
4 |
# a legal notice, here is a blessing:
|
sl@0
|
5 |
#
|
sl@0
|
6 |
# May you do good and not evil.
|
sl@0
|
7 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
8 |
# May you share freely, never taking more than you give.
|
sl@0
|
9 |
#
|
sl@0
|
10 |
#***********************************************************************
|
sl@0
|
11 |
#
|
sl@0
|
12 |
# This file contains tests of the memory allocation subsystem.
|
sl@0
|
13 |
#
|
sl@0
|
14 |
# $Id: memsubsys2.test,v 1.2 2008/08/12 15:21:12 drh Exp $
|
sl@0
|
15 |
|
sl@0
|
16 |
set testdir [file dirname $argv0]
|
sl@0
|
17 |
source $testdir/tester.tcl
|
sl@0
|
18 |
sqlite3_reset_auto_extension
|
sl@0
|
19 |
|
sl@0
|
20 |
# This procedure constructs a new database in test.db. It fills
|
sl@0
|
21 |
# this database with many small records (enough to force multiple
|
sl@0
|
22 |
# rebalance operations in the btree-layer and to require a large
|
sl@0
|
23 |
# page cache), verifies correct results, then returns.
|
sl@0
|
24 |
#
|
sl@0
|
25 |
proc build_test_db {testname pragmas} {
|
sl@0
|
26 |
catch {db close}
|
sl@0
|
27 |
file delete -force test.db test.db-journal
|
sl@0
|
28 |
sqlite3 db test.db
|
sl@0
|
29 |
db eval $pragmas
|
sl@0
|
30 |
db eval {
|
sl@0
|
31 |
CREATE TABLE t1(x, y);
|
sl@0
|
32 |
CREATE TABLE t2(a, b);
|
sl@0
|
33 |
CREATE INDEX i1 ON t1(x,y);
|
sl@0
|
34 |
INSERT INTO t1 VALUES(1, 100);
|
sl@0
|
35 |
INSERT INTO t1 VALUES(2, 200);
|
sl@0
|
36 |
}
|
sl@0
|
37 |
for {set i 2} {$i<5000} {incr i $i} {
|
sl@0
|
38 |
db eval {INSERT INTO t2 SELECT * FROM t1}
|
sl@0
|
39 |
db eval {INSERT INTO t1 SELECT a+$i, a+b*100 FROM t2}
|
sl@0
|
40 |
db eval {DELETE FROM t2}
|
sl@0
|
41 |
}
|
sl@0
|
42 |
do_test $testname.1 {
|
sl@0
|
43 |
db eval {SELECT count(*) FROM t1}
|
sl@0
|
44 |
} 8192
|
sl@0
|
45 |
integrity_check $testname.2
|
sl@0
|
46 |
}
|
sl@0
|
47 |
|
sl@0
|
48 |
# Reset all of the highwater marks.
|
sl@0
|
49 |
#
|
sl@0
|
50 |
proc reset_highwater_marks {} {
|
sl@0
|
51 |
sqlite3_status SQLITE_STATUS_MEMORY_USED 1
|
sl@0
|
52 |
sqlite3_status SQLITE_STATUS_MALLOC_SIZE 1
|
sl@0
|
53 |
sqlite3_status SQLITE_STATUS_PAGECACHE_USED 1
|
sl@0
|
54 |
sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 1
|
sl@0
|
55 |
sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 1
|
sl@0
|
56 |
sqlite3_status SQLITE_STATUS_SCRATCH_USED 1
|
sl@0
|
57 |
sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 1
|
sl@0
|
58 |
sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 1
|
sl@0
|
59 |
sqlite3_status SQLITE_STATUS_PARSER_STACK 1
|
sl@0
|
60 |
}
|
sl@0
|
61 |
|
sl@0
|
62 |
# Test 1: Verify that calling sqlite3_malloc(0) returns a NULL
|
sl@0
|
63 |
# pointer.
|
sl@0
|
64 |
#
|
sl@0
|
65 |
set highwater [sqlite3_memory_highwater 0]
|
sl@0
|
66 |
do_test memsubsys2-1.1 {
|
sl@0
|
67 |
sqlite3_malloc 0
|
sl@0
|
68 |
} {0}
|
sl@0
|
69 |
do_test memsubsys2-1.2 {
|
sl@0
|
70 |
sqlite3_memory_highwater 0
|
sl@0
|
71 |
} $highwater
|
sl@0
|
72 |
|
sl@0
|
73 |
|
sl@0
|
74 |
# Test 2: Verify that the highwater mark increases after a large
|
sl@0
|
75 |
# allocation.
|
sl@0
|
76 |
#
|
sl@0
|
77 |
sqlite3_memory_highwater 1
|
sl@0
|
78 |
set highwater [sqlite3_memory_highwater 0]
|
sl@0
|
79 |
do_test memsubsys2-2.1 {
|
sl@0
|
80 |
sqlite3_free [set x [sqlite3_malloc 100000]]
|
sl@0
|
81 |
expr {$x!="0"}
|
sl@0
|
82 |
} {1}
|
sl@0
|
83 |
do_test memsubsys2-2.2 {
|
sl@0
|
84 |
expr {[sqlite3_memory_highwater 0]>=[sqlite3_memory_used]+$highwater}
|
sl@0
|
85 |
} {1}
|
sl@0
|
86 |
|
sl@0
|
87 |
# Test 3: Verify that turning of memstatus disables the statistics
|
sl@0
|
88 |
# tracking.
|
sl@0
|
89 |
#
|
sl@0
|
90 |
db close
|
sl@0
|
91 |
sqlite3_shutdown
|
sl@0
|
92 |
sqlite3_config_memstatus 0
|
sl@0
|
93 |
sqlite3_initialize
|
sl@0
|
94 |
reset_highwater_marks
|
sl@0
|
95 |
set highwater [sqlite3_memory_highwater 0]
|
sl@0
|
96 |
do_test memsubsys2-3.1 {
|
sl@0
|
97 |
set highwater
|
sl@0
|
98 |
} {0}
|
sl@0
|
99 |
do_test memsubsys2-3.2 {
|
sl@0
|
100 |
sqlite3_malloc 0
|
sl@0
|
101 |
} {0}
|
sl@0
|
102 |
do_test memsubsys2-3.3 {
|
sl@0
|
103 |
sqlite3_memory_highwater 0
|
sl@0
|
104 |
} {0}
|
sl@0
|
105 |
do_test memsubsys2-3.4 {
|
sl@0
|
106 |
sqlite3_memory_used
|
sl@0
|
107 |
} {0}
|
sl@0
|
108 |
do_test memsubsys2-3.5 {
|
sl@0
|
109 |
set ::allocation [sqlite3_malloc 100000]
|
sl@0
|
110 |
expr {$::allocation!="0"}
|
sl@0
|
111 |
} {1}
|
sl@0
|
112 |
do_test memsubsys2-3.6 {
|
sl@0
|
113 |
sqlite3_memory_highwater 0
|
sl@0
|
114 |
} {0}
|
sl@0
|
115 |
do_test memsubsys2-3.7 {
|
sl@0
|
116 |
sqlite3_memory_used
|
sl@0
|
117 |
} {0}
|
sl@0
|
118 |
do_test memsubsys2-3.8 {
|
sl@0
|
119 |
sqlite3_free $::allocation
|
sl@0
|
120 |
} {}
|
sl@0
|
121 |
do_test memsubsys2-3.9 {
|
sl@0
|
122 |
sqlite3_free 0
|
sl@0
|
123 |
} {}
|
sl@0
|
124 |
|
sl@0
|
125 |
|
sl@0
|
126 |
# Test 4: Verify that turning on memstatus reenables the statistics
|
sl@0
|
127 |
# tracking.
|
sl@0
|
128 |
#
|
sl@0
|
129 |
sqlite3_shutdown
|
sl@0
|
130 |
sqlite3_config_memstatus 1
|
sl@0
|
131 |
sqlite3_initialize
|
sl@0
|
132 |
reset_highwater_marks
|
sl@0
|
133 |
set highwater [sqlite3_memory_highwater 0]
|
sl@0
|
134 |
do_test memsubsys2-4.1 {
|
sl@0
|
135 |
set highwater
|
sl@0
|
136 |
} {0}
|
sl@0
|
137 |
do_test memsubsys2-4.2 {
|
sl@0
|
138 |
sqlite3_malloc 0
|
sl@0
|
139 |
} {0}
|
sl@0
|
140 |
do_test memsubsys2-4.3 {
|
sl@0
|
141 |
sqlite3_memory_highwater 0
|
sl@0
|
142 |
} {0}
|
sl@0
|
143 |
do_test memsubsys2-4.4 {
|
sl@0
|
144 |
sqlite3_memory_used
|
sl@0
|
145 |
} {0}
|
sl@0
|
146 |
do_test memsubsys2-4.5 {
|
sl@0
|
147 |
set ::allocation [sqlite3_malloc 100000]
|
sl@0
|
148 |
expr {$::allocation!="0"}
|
sl@0
|
149 |
} {1}
|
sl@0
|
150 |
do_test memsubsys2-4.6 {
|
sl@0
|
151 |
expr {[sqlite3_memory_highwater 0]>=100000}
|
sl@0
|
152 |
} {1}
|
sl@0
|
153 |
do_test memsubsys2-4.7 {
|
sl@0
|
154 |
expr {[sqlite3_memory_used]>=100000}
|
sl@0
|
155 |
} {1}
|
sl@0
|
156 |
do_test memsubsys2-4.8 {
|
sl@0
|
157 |
sqlite3_free $::allocation
|
sl@0
|
158 |
} {}
|
sl@0
|
159 |
do_test memsubsys2-4.9 {
|
sl@0
|
160 |
sqlite3_free 0
|
sl@0
|
161 |
} {}
|
sl@0
|
162 |
do_test memsubsys2-4.10 {
|
sl@0
|
163 |
expr {[sqlite3_memory_highwater 0]>=100000}
|
sl@0
|
164 |
} {1}
|
sl@0
|
165 |
do_test memsubsys2-4.11 {
|
sl@0
|
166 |
sqlite3_memory_used
|
sl@0
|
167 |
} {0}
|
sl@0
|
168 |
|
sl@0
|
169 |
|
sl@0
|
170 |
|
sl@0
|
171 |
|
sl@0
|
172 |
autoinstall_test_functions
|
sl@0
|
173 |
finish_test
|