os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/memsubsys2.test
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/memsubsys2.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,173 @@
     1.4 +# 2008 June 18
     1.5 +#
     1.6 +# The author disclaims copyright to this source code.  In place of
     1.7 +# a legal notice, here is a blessing:
     1.8 +#
     1.9 +#    May you do good and not evil.
    1.10 +#    May you find forgiveness for yourself and forgive others.
    1.11 +#    May you share freely, never taking more than you give.
    1.12 +#
    1.13 +#***********************************************************************
    1.14 +#
    1.15 +# This file contains tests of the memory allocation subsystem.
    1.16 +#
    1.17 +# $Id: memsubsys2.test,v 1.2 2008/08/12 15:21:12 drh Exp $
    1.18 +
    1.19 +set testdir [file dirname $argv0]
    1.20 +source $testdir/tester.tcl
    1.21 +sqlite3_reset_auto_extension
    1.22 +
    1.23 +# This procedure constructs a new database in test.db.  It fills
    1.24 +# this database with many small records (enough to force multiple
    1.25 +# rebalance operations in the btree-layer and to require a large
    1.26 +# page cache), verifies correct results, then returns.
    1.27 +#
    1.28 +proc build_test_db {testname pragmas} {
    1.29 +  catch {db close}
    1.30 +  file delete -force test.db test.db-journal
    1.31 +  sqlite3 db test.db
    1.32 +  db eval $pragmas
    1.33 +  db eval {
    1.34 +    CREATE TABLE t1(x, y);
    1.35 +    CREATE TABLE t2(a, b);
    1.36 +    CREATE INDEX i1 ON t1(x,y);
    1.37 +    INSERT INTO t1 VALUES(1, 100);
    1.38 +    INSERT INTO t1 VALUES(2, 200);
    1.39 +  }
    1.40 +  for {set i 2} {$i<5000} {incr i $i} {
    1.41 +    db eval {INSERT INTO t2 SELECT * FROM t1}
    1.42 +    db eval {INSERT INTO t1 SELECT a+$i, a+b*100 FROM t2}
    1.43 +    db eval {DELETE FROM t2}
    1.44 +  }
    1.45 +  do_test $testname.1 {
    1.46 +    db eval {SELECT count(*) FROM t1}
    1.47 +  } 8192
    1.48 +  integrity_check $testname.2
    1.49 +}
    1.50 +
    1.51 +# Reset all of the highwater marks.
    1.52 +#
    1.53 +proc reset_highwater_marks {} {
    1.54 +  sqlite3_status SQLITE_STATUS_MEMORY_USED 1
    1.55 +  sqlite3_status SQLITE_STATUS_MALLOC_SIZE 1
    1.56 +  sqlite3_status SQLITE_STATUS_PAGECACHE_USED 1
    1.57 +  sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 1
    1.58 +  sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 1
    1.59 +  sqlite3_status SQLITE_STATUS_SCRATCH_USED 1
    1.60 +  sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 1
    1.61 +  sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 1
    1.62 +  sqlite3_status SQLITE_STATUS_PARSER_STACK 1
    1.63 +}
    1.64 +
    1.65 +# Test 1:  Verify that calling sqlite3_malloc(0) returns a NULL
    1.66 +# pointer.
    1.67 +#
    1.68 +set highwater [sqlite3_memory_highwater 0]
    1.69 +do_test memsubsys2-1.1 {
    1.70 +  sqlite3_malloc 0
    1.71 +} {0}
    1.72 +do_test memsubsys2-1.2 {
    1.73 +  sqlite3_memory_highwater 0
    1.74 +} $highwater
    1.75 +
    1.76 +
    1.77 +# Test 2:  Verify that the highwater mark increases after a large
    1.78 +# allocation.
    1.79 +#
    1.80 +sqlite3_memory_highwater 1
    1.81 +set highwater [sqlite3_memory_highwater 0]
    1.82 +do_test memsubsys2-2.1 {
    1.83 +  sqlite3_free [set x [sqlite3_malloc 100000]]
    1.84 +  expr {$x!="0"}
    1.85 +} {1}
    1.86 +do_test memsubsys2-2.2 {
    1.87 +  expr {[sqlite3_memory_highwater 0]>=[sqlite3_memory_used]+$highwater}
    1.88 +} {1}
    1.89 +
    1.90 +# Test 3: Verify that turning of memstatus disables the statistics
    1.91 +# tracking.
    1.92 +#
    1.93 +db close
    1.94 +sqlite3_shutdown
    1.95 +sqlite3_config_memstatus 0
    1.96 +sqlite3_initialize
    1.97 +reset_highwater_marks
    1.98 +set highwater [sqlite3_memory_highwater 0]
    1.99 +do_test memsubsys2-3.1 {
   1.100 +  set highwater
   1.101 +} {0}
   1.102 +do_test memsubsys2-3.2 {
   1.103 +  sqlite3_malloc 0
   1.104 +} {0}
   1.105 +do_test memsubsys2-3.3 {
   1.106 +  sqlite3_memory_highwater 0
   1.107 +} {0}
   1.108 +do_test memsubsys2-3.4 {
   1.109 +  sqlite3_memory_used
   1.110 +} {0}
   1.111 +do_test memsubsys2-3.5 {
   1.112 +  set ::allocation [sqlite3_malloc 100000]
   1.113 +  expr {$::allocation!="0"}
   1.114 +} {1}
   1.115 +do_test memsubsys2-3.6 {
   1.116 +  sqlite3_memory_highwater 0
   1.117 +} {0}
   1.118 +do_test memsubsys2-3.7 {
   1.119 +  sqlite3_memory_used
   1.120 +} {0}
   1.121 +do_test memsubsys2-3.8 {
   1.122 +  sqlite3_free $::allocation
   1.123 +} {}
   1.124 +do_test memsubsys2-3.9 {
   1.125 +  sqlite3_free 0
   1.126 +} {}
   1.127 +  
   1.128 +
   1.129 +# Test 4: Verify that turning on memstatus reenables the statistics
   1.130 +# tracking.
   1.131 +#
   1.132 +sqlite3_shutdown
   1.133 +sqlite3_config_memstatus 1
   1.134 +sqlite3_initialize
   1.135 +reset_highwater_marks
   1.136 +set highwater [sqlite3_memory_highwater 0]
   1.137 +do_test memsubsys2-4.1 {
   1.138 +  set highwater
   1.139 +} {0}
   1.140 +do_test memsubsys2-4.2 {
   1.141 +  sqlite3_malloc 0
   1.142 +} {0}
   1.143 +do_test memsubsys2-4.3 {
   1.144 +  sqlite3_memory_highwater 0
   1.145 +} {0}
   1.146 +do_test memsubsys2-4.4 {
   1.147 +  sqlite3_memory_used
   1.148 +} {0}
   1.149 +do_test memsubsys2-4.5 {
   1.150 +  set ::allocation [sqlite3_malloc 100000]
   1.151 +  expr {$::allocation!="0"}
   1.152 +} {1}
   1.153 +do_test memsubsys2-4.6 {
   1.154 +  expr {[sqlite3_memory_highwater 0]>=100000}
   1.155 +} {1}
   1.156 +do_test memsubsys2-4.7 {
   1.157 +  expr {[sqlite3_memory_used]>=100000}
   1.158 +} {1}
   1.159 +do_test memsubsys2-4.8 {
   1.160 +  sqlite3_free $::allocation
   1.161 +} {}
   1.162 +do_test memsubsys2-4.9 {
   1.163 +  sqlite3_free 0
   1.164 +} {}
   1.165 +do_test memsubsys2-4.10 {
   1.166 +  expr {[sqlite3_memory_highwater 0]>=100000}
   1.167 +} {1}
   1.168 +do_test memsubsys2-4.11 {
   1.169 +  sqlite3_memory_used
   1.170 +} {0}
   1.171 +  
   1.172 +
   1.173 +
   1.174 +
   1.175 +autoinstall_test_functions
   1.176 +finish_test