AryaWu/sqlite
0
1# 2008 June 182#3# The author disclaims copyright to this source code. In place of4# 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.2 2008/08/12 15:21:12 drh Exp $15 16set testdir [file dirname $argv0]17source $testdir/tester.tcl18sqlite3_reset_auto_extension19 20# This procedure constructs a new database in test.db. It fills21# this database with many small records (enough to force multiple22# rebalance operations in the btree-layer and to require a large23# page cache), verifies correct results, then returns.24#25proc build_test_db {testname pragmas} {26 catch {db close}27 forcedelete test.db test.db-journal28 sqlite3 db test.db29 db eval $pragmas30 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 } 819245 integrity_check $testname.246}47 48# Reset all of the highwater marks.49#50proc reset_highwater_marks {} {51 sqlite3_status SQLITE_STATUS_MEMORY_USED 152 sqlite3_status SQLITE_STATUS_MALLOC_SIZE 153 sqlite3_status SQLITE_STATUS_PAGECACHE_USED 154 sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 155 sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 156 sqlite3_status SQLITE_STATUS_SCRATCH_USED 157 sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 158 sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 159 sqlite3_status SQLITE_STATUS_PARSER_STACK 160}61 62# Test 1: Verify that calling sqlite3_malloc(0) returns a NULL63# pointer.64#65set highwater [sqlite3_memory_highwater 0]66do_test memsubsys2-1.1 {67 sqlite3_malloc 068} {0}69do_test memsubsys2-1.2 {70 sqlite3_memory_highwater 071} $highwater72 73 74# Test 2: Verify that the highwater mark increases after a large75# allocation.76#77# Do not run this if [sqlite3_memory_used] returns 0. This indicates78# an SQLITE_DEFAULT_MEMSTATUS=0 build.79#80if {[sqlite3_memory_used]!=0} {81 sqlite3_memory_highwater 182 set highwater [sqlite3_memory_highwater 0]83 do_test memsubsys2-2.1 {84 sqlite3_free [set x [sqlite3_malloc 100000]]85 expr {$x!="0"}86 } {1}87 do_test memsubsys2-2.2.1 {88 expr {[sqlite3_memory_highwater 0]>=[sqlite3_memory_used]+100000}89 } {1}90 do_test memsubsys2-2.2.2 {91 expr {[sqlite3_memory_highwater 0]>=$highwater+50000}92 } {1}93}94 95# Test 3: Verify that turning of memstatus disables the statistics96# tracking.97#98db close99sqlite3_shutdown100sqlite3_config_memstatus 0101sqlite3_initialize102reset_highwater_marks103set highwater [sqlite3_memory_highwater 0]104do_test memsubsys2-3.1 {105 set highwater106} {0}107do_test memsubsys2-3.2 {108 sqlite3_malloc 0109} {0}110do_test memsubsys2-3.3 {111 sqlite3_memory_highwater 0112} {0}113do_test memsubsys2-3.4 {114 sqlite3_memory_used115} {0}116do_test memsubsys2-3.5 {117 set ::allocation [sqlite3_malloc 100000]118 expr {$::allocation!="0"}119} {1}120do_test memsubsys2-3.6 {121 sqlite3_memory_highwater 0122} {0}123do_test memsubsys2-3.7 {124 sqlite3_memory_used125} {0}126do_test memsubsys2-3.8 {127 sqlite3_free $::allocation128} {}129do_test memsubsys2-3.9 {130 sqlite3_free 0131} {}132 133 134# Test 4: Verify that turning on memstatus reenables the statistics135# tracking.136#137sqlite3_shutdown138sqlite3_config_memstatus 1139sqlite3_initialize140reset_highwater_marks141set highwater [sqlite3_memory_highwater 0]142do_test memsubsys2-4.1 {143 set highwater144} {0}145do_test memsubsys2-4.2 {146 sqlite3_malloc 0147} {0}148do_test memsubsys2-4.3 {149 sqlite3_memory_highwater 0150} {0}151do_test memsubsys2-4.4 {152 sqlite3_memory_used153} {0}154do_test memsubsys2-4.5 {155 set ::allocation [sqlite3_malloc 100000]156 expr {$::allocation!="0"}157} {1}158do_test memsubsys2-4.6 {159 expr {[sqlite3_memory_highwater 0]>=100000}160} {1}161do_test memsubsys2-4.7 {162 expr {[sqlite3_memory_used]>=100000}163} {1}164do_test memsubsys2-4.8 {165 sqlite3_free $::allocation166} {}167do_test memsubsys2-4.9 {168 sqlite3_free 0169} {}170do_test memsubsys2-4.10 {171 expr {[sqlite3_memory_highwater 0]>=100000}172} {1}173do_test memsubsys2-4.11 {174 sqlite3_memory_used175} {0}176 177 178 179 180autoinstall_test_functions181finish_test182 