xref: /sqlite-3.40.0/test/fallocate.test (revision 502019c8)
1# 2010 July 28
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
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15
16file_control_chunksize_test db main [expr 1024*1024]
17
18do_test fallocate-1.1 {
19  execsql {
20    PRAGMA page_size = 1024;
21    PRAGMA auto_vacuum = 1;
22    CREATE TABLE t1(a, b);
23  }
24  file size test.db
25} [expr 1*1024*1024]
26
27do_test fallocate-1.2 {
28  execsql { INSERT INTO t1 VALUES(1, zeroblob(1024*900)) }
29  file size test.db
30} [expr 1*1024*1024]
31
32do_test fallocate-1.3 {
33  execsql { INSERT INTO t1 VALUES(2, zeroblob(1024*900)) }
34  file size test.db
35} [expr 2*1024*1024]
36
37do_test fallocate-1.4 {
38  execsql { DELETE FROM t1 WHERE a = 1 }
39  file size test.db
40} [expr 1*1024*1024]
41
42do_test fallocate-1.5 {
43  execsql { DELETE FROM t1 WHERE a = 2 }
44  file size test.db
45} [expr 1*1024*1024]
46
47do_test fallocate-1.6 {
48  execsql { PRAGMA freelist_count }
49} {0}
50
51# Start a write-transaction and read the "database file size" field from
52# the journal file. This field should be set to the number of pages in
53# the database file based on the size of the file on disk, not the actual
54# logical size of the database within the file.
55#
56# We need to check this to verify that if in the unlikely event a rollback
57# causes a database file to grow, the database grows to its previous size
58# on disk, not to the minimum size required to hold the database image.
59#
60do_test fallocate-1.7 {
61  execsql { BEGIN; INSERT INTO t1 VALUES(1, 2); }
62  hexio_get_int [hexio_read test.db-journal 16 4]
63} {1024}
64do_test fallocate-1.8 { execsql { COMMIT } } {}
65
66
67#-------------------------------------------------------------------------
68# The following tests - fallocate-2.* - test that things work in WAL
69# mode as well.
70#
71db close
72file delete -force test.db
73sqlite3 db test.db
74file_control_chunksize_test db main [expr 32*1024]
75
76do_test fallocate-2.1 {
77  execsql {
78    PRAGMA page_size = 1024;
79    PRAGMA journal_mode = WAL;
80    CREATE TABLE t1(a, b);
81  }
82  file size test.db
83} [expr 32*1024]
84
85do_test fallocate-2.2 {
86  execsql { INSERT INTO t1 VALUES(1, zeroblob(35*1024)) }
87  execsql { PRAGMA wal_checkpoint }
88  file size test.db
89} [expr 64*1024]
90
91do_test fallocate-2.3 {
92  execsql { DELETE FROM t1 }
93  execsql { VACUUM }
94  file size test.db
95} [expr 64*1024]
96
97do_test fallocate-2.4 {
98  execsql { PRAGMA wal_checkpoint }
99  file size test.db
100} [expr 32*1024]
101
102do_test fallocate-2.5 {
103  execsql {
104    INSERT INTO t1 VALUES(2, randomblob(35*1024));
105    PRAGMA wal_checkpoint;
106    INSERT INTO t1 VALUES(3, randomblob(128));
107    DELETE FROM t1 WHERE a = 2;
108    VACUUM;
109  }
110  file size test.db
111} [expr 64*1024]
112
113do_test fallocate-2.6 {
114  sqlite3 db2 test.db
115  execsql { BEGIN ; SELECT count(a) FROM t1 } db2
116  execsql {
117    INSERT INTO t1 VALUES(4, randomblob(128));
118    PRAGMA wal_checkpoint;
119  }
120  file size test.db
121} [expr 64*1024]
122
123do_test fallocate-2.7 {
124  execsql { SELECT count(b) FROM t1 } db2
125} {1}
126
127do_test fallocate-2.8 {
128  execsql { COMMIT } db2
129  execsql { PRAGMA wal_checkpoint }
130  file size test.db
131} [expr 32*1024]
132
133
134finish_test
135
136