blob: 5e9ea888a6b54ab67d9ad0883f7d47ba0e6e3c19 [file] [log] [blame]
dan58ca31c2011-09-22 14:41:16 +00001# 2011 September 20
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#
drh9ad3ee42012-03-24 20:06:14 +000012# Tests for the sqlite3_db_status() function
dan58ca31c2011-09-22 14:41:16 +000013#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17
18set ::testprefix dbstatus2
19
20do_execsql_test 1.0 {
21 PRAGMA page_size = 1024;
22 PRAGMA auto_vacuum = 0;
23
24 CREATE TABLE t1(a PRIMARY KEY, b);
25 INSERT INTO t1 VALUES(1, randomblob(600));
26 INSERT INTO t1 VALUES(2, randomblob(600));
27 INSERT INTO t1 VALUES(3, randomblob(600));
28}
29
30proc db_hit_miss {db {reset 0}} {
31 set nHit [sqlite3_db_status $db CACHE_HIT $reset]
32 set nMiss [sqlite3_db_status $db CACHE_MISS $reset]
33 list $nHit $nMiss
34}
35
drh9ad3ee42012-03-24 20:06:14 +000036proc db_write {db {reset 0}} {
37 sqlite3_db_status $db CACHE_WRITE $reset
38}
39
drhffc78a42018-03-14 14:53:50 +000040proc db_spill {db {reset 0}} {
41 sqlite3_db_status $db CACHE_SPILL $reset
42}
43
dan58ca31c2011-09-22 14:41:16 +000044do_test 1.1 {
45 db close
46 sqlite3 db test.db
drh9b4c59f2013-04-15 17:03:42 +000047 execsql { PRAGMA mmap_size = 0 }
dan58ca31c2011-09-22 14:41:16 +000048 expr {[file size test.db] / 1024}
49} 6
50
51do_test 1.2 {
52 execsql { SELECT b FROM t1 WHERE a=2 }
53 db_hit_miss db
54} {{0 2 0} {0 4 0}}
55
56do_test 1.3 {
57 execsql { SELECT b FROM t1 WHERE a=2 }
58 db_hit_miss db
59} {{0 6 0} {0 4 0}}
60
61do_test 1.4 {
62 execsql { SELECT b FROM t1 WHERE a=2 }
63 db_hit_miss db
64} {{0 10 0} {0 4 0}}
65
66do_test 1.5 {
67 db_hit_miss db 1
68} {{0 10 0} {0 4 0}}
69
70do_test 1.6 {
71 db_hit_miss db 0
72} {{0 0 0} {0 0 0}}
73
74do_test 1.7 {
75 set fd [db incrblob main t1 b 1]
76 fconfigure $fd -translation binary
77 set len [string length [read $fd]]
78 close $fd
79 set len
80} 600
81do_test 1.8 { sqlite3_db_status db CACHE_HIT 0 } {0 2 0}
82do_test 1.9 { sqlite3_db_status db CACHE_MISS 0 } {0 1 0}
83
drh9ad3ee42012-03-24 20:06:14 +000084do_test 2.1 { db_write db } {0 0 0}
85do_test 2.2 {
86 execsql { INSERT INTO t1 VALUES(4, randomblob(600)) }
87 db_write db
88} {0 4 0}
89do_test 2.3 { db_write db 1 } {0 4 0}
90do_test 2.4 { db_write db 0 } {0 0 0}
91do_test 2.5 { db_write db 1 } {0 0 0}
92
dan05accd22016-04-27 18:54:49 +000093if {[wal_is_capable]} {
mistachkinc60941f2012-09-13 01:51:02 +000094 do_test 2.6 {
95 execsql { PRAGMA journal_mode = WAL }
96 db_write db 1
97 } {0 1 0}
98}
drh9ad3ee42012-03-24 20:06:14 +000099do_test 2.7 {
100 execsql { INSERT INTO t1 VALUES(5, randomblob(600)) }
101 db_write db
102} {0 4 0}
103do_test 2.8 { db_write db 1 } {0 4 0}
104do_test 2.9 { db_write db 0 } {0 0 0}
drhffc78a42018-03-14 14:53:50 +0000105
106do_test 3.0 { db_spill db 1 } {0 0 0}
107do_test 3.1 { db_spill db 0 } {0 0 0}
108do_execsql_test 3.2 {
109 PRAGMA journal_mode=DELETE;
110 PRAGMA cache_size=3;
111 UPDATE t1 SET b=randomblob(1000);
112} {delete}
mistachkin2d44d552018-03-16 23:53:47 +0000113do_test 3.3 { db_spill db 0 } {0 8 0}
dan58ca31c2011-09-22 14:41:16 +0000114
115finish_test