blob: 5a936bd9c95c069acf4e86adf6254bd2f0905d70 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements some common TCL routines used for regression
12# testing the SQLite library
13#
drhbb77b752009-04-09 01:23:49 +000014# $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
danc1a60c52010-06-07 14:28:16 +000016#-------------------------------------------------------------------------
17# The commands provided by the code in this file to help with creating
18# test cases are as follows:
drh8359d8c2008-03-29 11:00:54 +000019#
danc1a60c52010-06-07 14:28:16 +000020# Commands to manipulate the db and the file-system at a high level:
drh8359d8c2008-03-29 11:00:54 +000021#
danc1a60c52010-06-07 14:28:16 +000022# copy_file FROM TO
23# drop_all_table ?DB?
24# forcedelete FILENAME
25#
26# Test the capability of the SQLite version built into the interpreter to
27# determine if a specific test can be run:
28#
29# ifcapable EXPR
30#
31# Calulate checksums based on database contents:
32#
33# dbcksum DB DBNAME
34# allcksum ?DB?
35# cksum ?DB?
36#
37# Commands to execute/explain SQL statements:
38#
39# stepsql DB SQL
40# execsql2 SQL
41# explain_no_trace SQL
42# explain SQL ?DB?
43# catchsql SQL ?DB?
44# execsql SQL ?DB?
45#
46# Commands to run test cases:
47#
48# do_ioerr_test TESTNAME ARGS...
49# crashsql ARGS...
50# integrity_check TESTNAME ?DB?
51# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000052# do_execsql_test TESTNAME SQL EXPECTED
53# do_catchsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000054#
dan430e74c2010-06-07 17:47:26 +000055# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000056#
57# set_test_counter COUNTER ?VALUE?
58# omit_test TESTNAME REASON
59# fail_test TESTNAME
60# incr_ntest
61#
62# Command run at the end of each test file:
63#
64# finish_test
65#
dan430e74c2010-06-07 17:47:26 +000066# Commands to help create test files that run with the "WAL" and other
67# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000068#
69# wal_is_wal_mode
70# wal_set_journal_mode ?DB?
71# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000072# permutation
dancb79e512010-08-06 13:50:07 +000073# presql
danc1a60c52010-06-07 14:28:16 +000074#
drh348784e2000-05-29 20:41:49 +000075
danc1a60c52010-06-07 14:28:16 +000076# Set the precision of FP arithmatic used by the interpreter. And
77# configure SQLite to take database file locks on the page that begins
78# 64KB into the database file instead of the one 1GB in. This means
79# the code that handles that special case can be tested without creating
80# very large database files.
81#
drh92febd92004-08-20 18:34:20 +000082set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000083sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000084
danc1a60c52010-06-07 14:28:16 +000085
86# If the pager codec is available, create a wrapper for the [sqlite3]
87# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000088#
danc1a60c52010-06-07 14:28:16 +000089# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000090#
danc1a60c52010-06-07 14:28:16 +000091# becomes
drh4a50aac2007-08-23 02:47:53 +000092#
danc1a60c52010-06-07 14:28:16 +000093# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +000094#
dan430e74c2010-06-07 17:47:26 +000095if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +000096 rename sqlite3 sqlite_orig
97 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +000098 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +000099 # This command is opening a new database connection.
100 #
101 if {[info exists ::G(perm:sqlite3_args)]} {
102 set args [concat $args $::G(perm:sqlite3_args)]
103 }
dan68928b62010-06-22 13:46:43 +0000104 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000105 lappend args -key {xyzzy}
106 }
107
dan3ccd20a2010-06-09 19:01:02 +0000108 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000109 if {[info exists ::G(perm:presql)]} {
110 [lindex $args 0] eval $::G(perm:presql)
111 }
dan1ce1b4a2010-12-07 14:32:28 +0000112 if {[info exists ::G(perm:dbconfig)]} {
113 set ::dbhandle [lindex $args 0]
114 uplevel #0 $::G(perm:dbconfig)
115 }
dan3ccd20a2010-06-09 19:01:02 +0000116 set res
dan430e74c2010-06-07 17:47:26 +0000117 } else {
118 # This command is not opening a new database connection. Pass the
119 # arguments through to the C implemenation as the are.
120 #
121 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000122 }
drh9eb9e262004-02-11 02:18:05 +0000123 }
124}
125
dancb354602010-07-08 09:44:42 +0000126proc execpresql {handle args} {
127 trace remove execution $handle enter [list execpresql $handle]
128 if {[info exists ::G(perm:presql)]} {
129 $handle eval $::G(perm:presql)
130 }
131}
132
dan68928b62010-06-22 13:46:43 +0000133# This command should be called after loading tester.tcl from within
134# all test scripts that are incompatible with encryption codecs.
135#
136proc do_not_use_codec {} {
137 set ::do_not_use_codec 1
138 reset_db
139}
140
dan430e74c2010-06-07 17:47:26 +0000141# The following block only runs the first time this file is sourced. It
142# does not run in slave interpreters (since the ::cmdlinearg array is
143# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000144#
danc1a60c52010-06-07 14:28:16 +0000145if {[info exists cmdlinearg]==0} {
146
147 # Parse any options specified in the $argv array. This script accepts the
148 # following options:
149 #
150 # --pause
151 # --soft-heap-limit=NN
152 # --maxerror=NN
153 # --malloctrace=N
154 # --backtrace=N
155 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000156 # --soak=N
dan6a64d672011-04-04 15:38:16 +0000157 # --start=[$permutation:]$testfile
danc1a60c52010-06-07 14:28:16 +0000158 #
159 set cmdlinearg(soft-heap-limit) 0
160 set cmdlinearg(maxerror) 1000
161 set cmdlinearg(malloctrace) 0
162 set cmdlinearg(backtrace) 10
163 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000164 set cmdlinearg(soak) 0
dan6a64d672011-04-04 15:38:16 +0000165 set cmdlinearg(start) ""
danc1a60c52010-06-07 14:28:16 +0000166
167 set leftover [list]
168 foreach a $argv {
169 switch -regexp -- $a {
170 {^-+pause$} {
171 # Wait for user input before continuing. This is to give the user an
172 # opportunity to connect profiling tools to the process.
173 puts -nonewline "Press RETURN to begin..."
174 flush stdout
175 gets stdin
176 }
177 {^-+soft-heap-limit=.+$} {
178 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
179 }
180 {^-+maxerror=.+$} {
181 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
182 }
183 {^-+malloctrace=.+$} {
184 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
185 if {$cmdlinearg(malloctrace)} {
186 sqlite3_memdebug_log start
187 }
188 }
189 {^-+backtrace=.+$} {
190 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000191 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000192 }
danc1a60c52010-06-07 14:28:16 +0000193 {^-+binarylog=.+$} {
194 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
195 }
dan0626dfc2010-06-15 06:56:37 +0000196 {^-+soak=.+$} {
197 foreach {dummy cmdlinearg(soak)} [split $a =] break
198 set ::G(issoak) $cmdlinearg(soak)
199 }
dan6a64d672011-04-04 15:38:16 +0000200 {^-+start=.+$} {
201 foreach {dummy cmdlinearg(start)} [split $a =] break
202
203 set ::G(start:file) $cmdlinearg(start)
204 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
205 set ::G(start:permutation) ${s.perm}
206 set ::G(start:file) ${s.file}
207 }
208 if {$::G(start:file) == ""} {unset ::G(start:file)}
209 }
danc1a60c52010-06-07 14:28:16 +0000210 default {
211 lappend leftover $a
212 }
213 }
214 }
215 set argv $leftover
216
dan430e74c2010-06-07 17:47:26 +0000217 # Install the malloc layer used to inject OOM errors. And the 'automatic'
218 # extensions. This only needs to be done once for the process.
219 #
danielk1977f7b9d662008-06-23 18:49:43 +0000220 sqlite3_shutdown
221 install_malloc_faultsim 1
222 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000223 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000224
dan430e74c2010-06-07 17:47:26 +0000225 # If the --binarylog option was specified, create the logging VFS. This
226 # call installs the new VFS as the default for all SQLite connections.
227 #
danc1a60c52010-06-07 14:28:16 +0000228 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000229 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000230 }
231
232 # Set the backtrace depth, if malloc tracing is enabled.
233 #
234 if {$cmdlinearg(malloctrace)} {
235 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000236 }
danielk1977f7b9d662008-06-23 18:49:43 +0000237}
danielk197703ba3fa2009-01-09 10:49:14 +0000238
danc1a60c52010-06-07 14:28:16 +0000239# Update the soft-heap-limit each time this script is run. In that
240# way if an individual test file changes the soft-heap-limit, it
241# will be reset at the start of the next test file.
242#
243sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
244
245# Create a test database
246#
danielk197703ba3fa2009-01-09 10:49:14 +0000247proc reset_db {} {
248 catch {db close}
249 file delete -force test.db
250 file delete -force test.db-journal
dand3f8f942010-04-13 11:35:01 +0000251 file delete -force test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000252 sqlite3 db ./test.db
253 set ::DB [sqlite3_connection_pointer db]
254 if {[info exists ::SETUP_SQL]} {
255 db eval $::SETUP_SQL
256 }
drhcd61c282002-03-06 22:01:34 +0000257}
danielk197703ba3fa2009-01-09 10:49:14 +0000258reset_db
drhbec2bf42000-05-29 23:48:22 +0000259
260# Abort early if this script has been run before.
261#
danc1a60c52010-06-07 14:28:16 +0000262if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000263
drhce2198c2010-09-10 13:22:59 +0000264# Make sure memory statistics are enabled.
265#
266sqlite3_config_memstatus 1
267
danc1a60c52010-06-07 14:28:16 +0000268# Initialize the test counters and set up commands to access them.
269# Or, if this is a slave interpreter, set up aliases to write the
270# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000271#
danc1a60c52010-06-07 14:28:16 +0000272if {0==[info exists ::SLAVE]} {
273 set TC(errors) 0
274 set TC(count) 0
275 set TC(fail_list) [list]
276 set TC(omit_list) [list]
277
278 proc set_test_counter {counter args} {
279 if {[llength $args]} {
280 set ::TC($counter) [lindex $args 0]
281 }
282 set ::TC($counter)
283 }
drhb62c3352006-11-23 09:39:16 +0000284}
drh348784e2000-05-29 20:41:49 +0000285
drh521cc842008-04-15 02:36:33 +0000286# Record the fact that a sequence of tests were omitted.
287#
288proc omit_test {name reason} {
danc1a60c52010-06-07 14:28:16 +0000289 set omitList [set_test_counter omit_list]
drh521cc842008-04-15 02:36:33 +0000290 lappend omitList [list $name $reason]
danc1a60c52010-06-07 14:28:16 +0000291 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000292}
293
danc1a60c52010-06-07 14:28:16 +0000294# Record the fact that a test failed.
295#
296proc fail_test {name} {
297 set f [set_test_counter fail_list]
298 lappend f $name
299 set_test_counter fail_list $f
300 set_test_counter errors [expr [set_test_counter errors] + 1]
301
302 set nFail [set_test_counter errors]
303 if {$nFail>=$::cmdlinearg(maxerror)} {
304 puts "*** Giving up..."
305 finalize_testing
306 }
307}
308
309# Increment the number of tests run
310#
311proc incr_ntest {} {
312 set_test_counter count [expr [set_test_counter count] + 1]
313}
314
315
drh348784e2000-05-29 20:41:49 +0000316# Invoke the do_test procedure to run a single test
317#
318proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000319
320 global argv cmdlinearg
321
dan8c408002010-11-01 17:38:24 +0000322 fix_testname name
323
drh4a50aac2007-08-23 02:47:53 +0000324 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000325
dan92d516a2010-07-05 14:54:48 +0000326# if {[llength $argv]==0} {
327# set go 1
328# } else {
329# set go 0
330# foreach pattern $argv {
331# if {[string match $pattern $name]} {
332# set go 1
333# break
334# }
335# }
336# }
dan430e74c2010-06-07 17:47:26 +0000337
dand506de02010-07-03 13:59:01 +0000338 if {[info exists ::G(perm:prefix)]} {
339 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000340 }
341
danc1a60c52010-06-07 14:28:16 +0000342 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000343 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000344 flush stdout
345 if {[catch {uplevel #0 "$cmd;\n"} result]} {
346 puts "\nError: $result"
danc1a60c52010-06-07 14:28:16 +0000347 fail_test $name
drh348784e2000-05-29 20:41:49 +0000348 } elseif {[string compare $result $expected]} {
349 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
danc1a60c52010-06-07 14:28:16 +0000350 fail_test $name
drh348784e2000-05-29 20:41:49 +0000351 } else {
352 puts " Ok"
353 }
drh6558db82007-04-13 03:23:21 +0000354 flush stdout
drh348784e2000-05-29 20:41:49 +0000355}
dana3f51082010-09-30 18:43:14 +0000356
dan33f53792011-05-05 19:44:22 +0000357proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000358 # different TCL versions display floating point values differently.
359 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000360}
361proc do_realnum_test {name cmd expected} {
362 uplevel [list do_test $name [
363 subst -nocommands { realnum_normalize [ $cmd ] }
364 ] [realnum_normalize $expected]]
365}
366
dana3f51082010-09-30 18:43:14 +0000367proc fix_testname {varname} {
368 upvar $varname testname
369 if {[info exists ::testprefix]
370 && [string is digit [string range $testname 0 0]]
371 } {
372 set testname "${::testprefix}-$testname"
373 }
374}
dan153eda02010-06-21 07:45:47 +0000375
dan57f7f4b2010-10-01 19:04:37 +0000376proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000377 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000378 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000379}
380proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000381 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000382 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000383}
dan39854792010-11-15 16:12:58 +0000384proc do_eqp_test {name sql res} {
385 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
386}
dan153eda02010-06-21 07:45:47 +0000387
dan51f06982010-09-18 19:00:12 +0000388#-------------------------------------------------------------------------
389# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
390#
391# Where switches are:
392#
393# -errorformat FMTSTRING
394# -count
danaf1dcab2010-09-20 19:17:53 +0000395# -query SQL
dana16d1062010-09-28 17:37:28 +0000396# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000397# -repair TCL
dan51f06982010-09-18 19:00:12 +0000398#
399proc do_select_tests {prefix args} {
400
401 set testlist [lindex $args end]
402 set switches [lrange $args 0 end-1]
403
404 set errfmt ""
405 set countonly 0
dana16d1062010-09-28 17:37:28 +0000406 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000407 set repair ""
dan51f06982010-09-18 19:00:12 +0000408
409 for {set i 0} {$i < [llength $switches]} {incr i} {
410 set s [lindex $switches $i]
411 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000412 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000413 set tclquery [list execsql [lindex $switches [incr i]]]
414 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
415 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000416 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000417 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000418 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
419 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000420 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
421 set countonly 1
422 } else {
423 error "unknown switch: $s"
424 }
425 }
426
427 if {$countonly && $errfmt!=""} {
428 error "Cannot use -count and -errorformat together"
429 }
430 set nTestlist [llength $testlist]
431 if {$nTestlist%3 || $nTestlist==0 } {
432 error "SELECT test list contains [llength $testlist] elements"
433 }
434
danaf7626f2010-09-23 18:47:36 +0000435 eval $repair
dan51f06982010-09-18 19:00:12 +0000436 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000437 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000438 execsql $sql
dana16d1062010-09-28 17:37:28 +0000439 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
440 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000441 set nRow 0
442 db eval $sql {incr nRow}
443 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
444 } elseif {$errfmt==""} {
445 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
446 } else {
447 set res [list 1 [string trim [format $errfmt {*}$res]]]
448 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
449 }
danaf7626f2010-09-23 18:47:36 +0000450 eval $repair
dan51f06982010-09-18 19:00:12 +0000451 }
danaf7626f2010-09-23 18:47:36 +0000452
dan51f06982010-09-18 19:00:12 +0000453}
454
danb04757a2010-09-21 16:59:16 +0000455proc delete_all_data {} {
456 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
457 db eval "DELETE FROM '[string map {' ''} $t]'"
458 }
459}
drh348784e2000-05-29 20:41:49 +0000460
drhb62c3352006-11-23 09:39:16 +0000461# Run an SQL script.
462# Return the number of microseconds per statement.
463#
drh3590f152006-11-23 21:09:10 +0000464proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000465 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000466 flush stdout
467 set speed [time {sqlite3_exec_nr db $sql}]
468 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000469 if {$tm == 0} {
470 set rate [format %20s "many"]
471 } else {
472 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
473 }
drh3590f152006-11-23 21:09:10 +0000474 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000475 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000476 global total_time
477 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000478 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000479}
drh45c236d2008-03-22 01:08:00 +0000480proc speed_trial_tcl {name numstmt units script} {
481 puts -nonewline [format {%-21.21s } $name...]
482 flush stdout
483 set speed [time {eval $script}]
484 set tm [lindex $speed 0]
485 if {$tm == 0} {
486 set rate [format %20s "many"]
487 } else {
488 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
489 }
490 set u2 $units/s
491 puts [format {%12d uS %s %s} $tm $rate $u2]
492 global total_time
493 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000494 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000495}
drhdd924312007-03-31 22:34:16 +0000496proc speed_trial_init {name} {
497 global total_time
498 set total_time 0
dana975b592010-10-08 16:09:43 +0000499 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000500 sqlite3 versdb :memory:
501 set vers [versdb one {SELECT sqlite_source_id()}]
502 versdb close
503 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000504}
505proc speed_trial_summary {name} {
506 global total_time
507 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000508
509 if { 0 } {
510 sqlite3 versdb :memory:
511 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
512 versdb close
513 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
514 foreach {test us} $::speed_trial_times {
515 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
516 }
517 }
drhb62c3352006-11-23 09:39:16 +0000518}
519
drh348784e2000-05-29 20:41:49 +0000520# Run this routine last
521#
522proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000523 catch {db close}
524 catch {db2 close}
525 catch {db3 close}
526 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000527}
528proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000529 global sqlite_open_file_count
530
531 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000532
drh6e142f52000-06-08 13:36:40 +0000533 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000534 catch {db2 close}
535 catch {db3 close}
536
drh9bc54492007-10-23 14:49:59 +0000537 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000538 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000539 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000540 db close
drh984bfaa2008-03-19 16:08:53 +0000541 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000542
drh3a7fb7c2007-08-10 16:41:08 +0000543 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000544 set nTest [incr_ntest]
545 set nErr [set_test_counter errors]
546
drh348784e2000-05-29 20:41:49 +0000547 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000548 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000549 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000550 }
danielk1977ee8b7992009-03-26 17:13:06 +0000551 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000552 if {[llength $omitList]>0} {
553 puts "Omitted test cases:"
554 set prec {}
555 foreach {rec} [lsort $omitList] {
556 if {$rec==$prec} continue
557 set prec $rec
558 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
559 }
560 }
drh80788d82006-09-02 14:50:23 +0000561 if {$nErr>0 && ![working_64bit_int]} {
562 puts "******************************************************************"
563 puts "N.B.: The version of TCL that you used to build this test harness"
564 puts "is defective in that it does not support 64-bit integers. Some or"
565 puts "all of the test failures above might be a result from this defect"
566 puts "in your TCL build."
567 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000568 }
danc1a60c52010-06-07 14:28:16 +0000569 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000570 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000571 }
drh94e92032003-02-16 22:21:32 +0000572 if {$sqlite_open_file_count} {
573 puts "$sqlite_open_file_count files were left open"
574 incr nErr
575 }
drheafc43b2010-07-26 18:43:40 +0000576 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
577 [sqlite3_memory_used]>0} {
578 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
579 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000580 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000581 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000582 puts "Writing unfreed memory log to \"./memleak.txt\""
583 sqlite3_memdebug_dump ./memleak.txt
584 }
drhf3a65f72007-08-22 20:18:21 +0000585 } else {
586 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000587 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000588 sqlite3_memdebug_dump ./memusage.txt
589 }
drhf3a65f72007-08-22 20:18:21 +0000590 }
drhf7141992008-06-19 00:16:08 +0000591 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000592 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
593 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000594 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
595 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
596 }
danc1a60c52010-06-07 14:28:16 +0000597 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000598 puts "Writing mallocs.sql..."
599 memdebug_log_sql
600 sqlite3_memdebug_log stop
601 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000602
603 if {[sqlite3_memory_used]>0} {
604 puts "Writing leaks.sql..."
605 sqlite3_memdebug_log sync
606 memdebug_log_sql leaks.sql
607 }
danielk197735754ac2008-03-21 17:29:37 +0000608 }
drhd9910fe2006-10-04 11:55:49 +0000609 foreach f [glob -nocomplain test.db-*-journal] {
610 file delete -force $f
611 }
612 foreach f [glob -nocomplain test.db-mj*] {
613 file delete -force $f
614 }
drhdb25e382001-03-15 18:21:22 +0000615 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000616}
617
drhf7141992008-06-19 00:16:08 +0000618# Display memory statistics for analysis and debugging purposes.
619#
620proc show_memstats {} {
621 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000622 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
623 set val [format {now %10d max %10d max-size %10d} \
624 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000625 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000626 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
627 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
628 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000629 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000630 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
631 set val [format {now %10d max %10d max-size %10d} \
632 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000633 puts "Page-cache used: $val"
634 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
635 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
636 puts "Page-cache overflow: $val"
637 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
638 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
639 puts "Scratch memory used: $val"
640 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000641 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
642 set val [format {now %10d max %10d max-size %10d} \
643 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000644 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000645 ifcapable yytrackmaxstackdepth {
646 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
647 set val [format { max %10d} [lindex $x 2]]
648 puts "Parser stack depth: $val"
649 }
drhf7141992008-06-19 00:16:08 +0000650}
651
drh348784e2000-05-29 20:41:49 +0000652# A procedure to execute SQL
653#
drhc4a3c772001-04-04 11:48:57 +0000654proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000655 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000656 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000657}
drh3aadb2e2000-05-31 17:59:25 +0000658
drhadbca9c2001-09-27 15:11:53 +0000659# Execute SQL and catch exceptions.
660#
661proc catchsql {sql {db db}} {
662 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000663 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000664 lappend r $msg
665 return $r
666}
667
drh04096482001-11-09 22:41:44 +0000668# Do an VDBE code dump on the SQL given
669#
670proc explain {sql {db db}} {
671 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000672 puts "addr opcode p1 p2 p3 p4 p5 #"
673 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000674 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000675 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
676 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
677 ]
drh04096482001-11-09 22:41:44 +0000678 }
679}
680
drhdafc0ce2008-04-17 19:14:02 +0000681# Show the VDBE program for an SQL statement but omit the Trace
682# opcode at the beginning. This procedure can be used to prove
683# that different SQL statements generate exactly the same VDBE code.
684#
685proc explain_no_trace {sql} {
686 set tr [db eval "EXPLAIN $sql"]
687 return [lrange $tr 7 end]
688}
689
drh3aadb2e2000-05-31 17:59:25 +0000690# Another procedure to execute SQL. This one includes the field
691# names in the returned list.
692#
693proc execsql2 {sql} {
694 set result {}
695 db eval $sql data {
696 foreach f $data(*) {
697 lappend result $f $data($f)
698 }
699 }
700 return $result
701}
drh17a68932001-01-31 13:28:08 +0000702
drhdde85d92003-03-01 19:45:34 +0000703# Use the non-callback API to execute multiple SQL statements
704#
705proc stepsql {dbptr sql} {
706 set sql [string trim $sql]
707 set r 0
708 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000709 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000710 return [list 1 $vm]
711 }
712 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000713# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
714# foreach v $VAL {lappend r $v}
715# }
716 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
717 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
718 lappend r [sqlite3_column_text $vm $i]
719 }
drhdde85d92003-03-01 19:45:34 +0000720 }
danielk1977106bb232004-05-21 10:08:53 +0000721 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000722 return [list 1 $errmsg]
723 }
724 }
725 return $r
726}
727
drh17a68932001-01-31 13:28:08 +0000728# Delete a file or directory
729#
dana16d1062010-09-28 17:37:28 +0000730proc forcedelete {args} {
731 foreach filename $args {
732 # On windows, sometimes even a [file delete -force] can fail just after
733 # a file is closed. The cause is usually "tag-alongs" - programs like
734 # anti-virus software, automatic backup tools and various explorer
735 # extensions that keep a file open a little longer than we expect, causing
736 # the delete to fail.
737 #
738 # The solution is to wait a short amount of time before retrying the
739 # delete.
740 #
741 set nRetry 50 ;# Maximum number of retries.
742 set nDelay 100 ;# Delay in ms before retrying.
743 for {set i 0} {$i<$nRetry} {incr i} {
744 set rc [catch {file delete -force $filename} msg]
745 if {$rc==0} break
746 after $nDelay
747 }
748 if {$rc} { error $msg }
drh17a68932001-01-31 13:28:08 +0000749 }
750}
drh21504322002-06-25 13:16:02 +0000751
752# Do an integrity check of the entire database
753#
danielk197704103022009-02-03 16:51:24 +0000754proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000755 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000756 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000757 }
758}
759
danc6055c72011-04-28 18:46:46 +0000760
761# Return true if the SQL statement passed as the second argument uses a
762# statement transaction.
763#
764proc sql_uses_stmt {db sql} {
765 set stmt [sqlite3_prepare $db $sql -1 dummy]
766 set uses [uses_stmt_journal $stmt]
767 sqlite3_finalize $stmt
768 return $uses
769}
770
danielk1977db4e8862008-01-16 08:24:46 +0000771proc fix_ifcapable_expr {expr} {
772 set ret ""
773 set state 0
774 for {set i 0} {$i < [string length $expr]} {incr i} {
775 set char [string range $expr $i $i]
776 set newstate [expr {[string is alnum $char] || $char eq "_"}]
777 if {$newstate && !$state} {
778 append ret {$::sqlite_options(}
779 }
780 if {!$newstate && $state} {
781 append ret )
782 }
783 append ret $char
784 set state $newstate
785 }
786 if {$state} {append ret )}
787 return $ret
788}
789
drh27d258a2004-10-30 20:23:09 +0000790# Evaluate a boolean expression of capabilities. If true, execute the
791# code. Omit the code if false.
792#
danielk19773e8c37e2005-01-21 03:12:14 +0000793proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000794 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
795 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000796 if ($e2) {
797 set c [catch {uplevel 1 $code} r]
798 } else {
799 set c [catch {uplevel 1 $elsecode} r]
800 }
801 return -code $c $r
drh21504322002-06-25 13:16:02 +0000802}
danielk197745901d62004-11-10 15:27:38 +0000803
danielk1977aca790a2005-01-13 11:07:52 +0000804# This proc execs a seperate process that crashes midway through executing
805# the SQL script $sql on database test.db.
806#
807# The crash occurs during a sync() of file $crashfile. When the crash
808# occurs a random subset of all unsynced writes made by the process are
809# written into the files on disk. Argument $crashdelay indicates the
810# number of file syncs to wait before crashing.
811#
812# The return value is a list of two elements. The first element is a
813# boolean, indicating whether or not the process actually crashed or
814# reported some other error. The second element in the returned list is the
815# error message. This is "child process exited abnormally" if the crash
816# occured.
817#
danielk1977f8940ae2007-08-23 11:07:10 +0000818# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000819#
820proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +0000821
822 set blocksize ""
823 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000824 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000825 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000826 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000827 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000828 set sql [lindex $args end]
829
830 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
831 set z [lindex $args $ii]
832 set n [string length $z]
833 set z2 [lindex $args [expr $ii+1]]
834
835 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000836 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000837 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000838 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000839 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000840 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000841 else { error "Unrecognized option: $z" }
842 }
843
844 if {$crashfile eq ""} {
845 error "Compulsory option -file missing"
846 }
847
shanehf2c08822010-07-08 16:30:44 +0000848 # $crashfile gets compared to the native filename in
849 # cfSync(), which can be different then what TCL uses by
850 # default, so here we force it to the "nativename" format.
851 set cfile [string map {\\ \\\\} [file nativename [file join [pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +0000852
853 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000854 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000855 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000856 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000857 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000858
859 # This block sets the cache size of the main database to 10
860 # pages. This is done in case the build is configured to omit
861 # "PRAGMA cache_size".
862 puts $f {db eval {SELECT * FROM sqlite_master;}}
863 puts $f {set bt [btree_from_db db]}
864 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000865 if {$prngseed} {
866 set seed [expr {$prngseed%10007+1}]
867 # puts seed=$seed
868 puts $f "db eval {SELECT randomblob($seed)}"
869 }
danielk1977933bbd62007-03-17 07:22:42 +0000870
drhf8587402008-01-08 16:03:49 +0000871 if {[string length $tclbody]>0} {
872 puts $f $tclbody
873 }
874 if {[string length $sql]>0} {
875 puts $f "db eval {"
876 puts $f "$sql"
877 puts $f "}"
878 }
danielk1977aca790a2005-01-13 11:07:52 +0000879 close $f
danielk1977aca790a2005-01-13 11:07:52 +0000880 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000881 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000882 } msg]
shanehf2c08822010-07-08 16:30:44 +0000883
884 # Windows/ActiveState TCL returns a slightly different
885 # error message. We map that to the expected message
886 # so that we don't have to change all of the test
887 # cases.
888 if {$::tcl_platform(platform)=="windows"} {
889 if {$msg=="child killed: unknown signal"} {
890 set msg "child process exited abnormally"
891 }
892 }
893
danielk1977aca790a2005-01-13 11:07:52 +0000894 lappend r $msg
895}
896
danielk197732554c12005-01-22 03:39:39 +0000897# Usage: do_ioerr_test <test number> <options...>
898#
899# This proc is used to implement test cases that check that IO errors
900# are correctly handled. The first argument, <test number>, is an integer
901# used to name the tests executed by this proc. Options are as follows:
902#
903# -tclprep TCL script to run to prepare test.
904# -sqlprep SQL script to run to prepare test.
905# -tclbody TCL script to run with IO error simulation.
906# -sqlbody TCL script to run with IO error simulation.
907# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000908# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000909# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000910# -start Value of 'N' to begin with (default 1)
911#
912# -cksum Boolean. If true, test that the database does
913# not change during the execution of the test case.
914#
915proc do_ioerr_test {testname args} {
916
danielk197732554c12005-01-22 03:39:39 +0000917 set ::ioerropts(-start) 1
918 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000919 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000920 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000921 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +0000922 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +0000923 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +0000924 array set ::ioerropts $args
925
danielk197747cd39c2008-05-12 12:41:15 +0000926 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
927 # a couple of obscure IO errors that do not return them.
928 set ::ioerropts(-erc) 0
929
danielk197732554c12005-01-22 03:39:39 +0000930 set ::go 1
drh93aed5a2008-01-16 17:46:38 +0000931 #reset_prng_state
932 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +0000933 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000934 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000935 incr ::ioerropts(-count) -1
936 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000937
938 # Skip this IO error if it was specified with the "-exclude" option.
939 if {[info exists ::ioerropts(-exclude)]} {
940 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
941 }
danielk1977861f7452008-06-05 11:39:11 +0000942 if {$::ioerropts(-restoreprng)} {
943 restore_prng_state
944 }
danielk197732554c12005-01-22 03:39:39 +0000945
946 # Delete the files test.db and test2.db, then execute the TCL and
947 # SQL (in that order) to prepare for the test case.
948 do_test $testname.$n.1 {
949 set ::sqlite_io_error_pending 0
950 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +0000951 catch {db2 close}
danielk197732554c12005-01-22 03:39:39 +0000952 catch {file delete -force test.db}
953 catch {file delete -force test.db-journal}
954 catch {file delete -force test2.db}
955 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000956 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000957 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000958 if {[info exists ::ioerropts(-tclprep)]} {
959 eval $::ioerropts(-tclprep)
960 }
961 if {[info exists ::ioerropts(-sqlprep)]} {
962 execsql $::ioerropts(-sqlprep)
963 }
964 expr 0
965 } {0}
966
967 # Read the 'checksum' of the database.
968 if {$::ioerropts(-cksum)} {
969 set checksum [cksum]
970 }
drh93aed5a2008-01-16 17:46:38 +0000971
danielk197732554c12005-01-22 03:39:39 +0000972 # Set the Nth IO error to fail.
973 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000974 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000975 set ::sqlite_io_error_pending $n
976 }] $n
977
978 # Create a single TCL script from the TCL and SQL specified
979 # as the body of the test.
980 set ::ioerrorbody {}
981 if {[info exists ::ioerropts(-tclbody)]} {
982 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
983 }
984 if {[info exists ::ioerropts(-sqlbody)]} {
985 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
986 }
987
988 # Execute the TCL Script created in the above block. If
989 # there are at least N IO operations performed by SQLite as
990 # a result of the script, the Nth will fail.
991 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +0000992 set ::sqlite_io_error_hit 0
993 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +0000994 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +0000995 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +0000996 set rc [sqlite3_errcode $::DB]
997 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000998 # If we are in extended result code mode, make sure all of the
999 # IOERRs we get back really do have their extended code values.
1000 # If an extended result code is returned, the sqlite3_errcode
1001 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1002 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001003 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1004 return $rc
1005 }
1006 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001007 # If we are not in extended result code mode, make sure no
1008 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001009 if {[regexp {\+\d} $rc]} {
1010 return $rc
1011 }
1012 }
drh93aed5a2008-01-16 17:46:38 +00001013 # The test repeats as long as $::go is non-zero. $::go starts out
1014 # as 1. When a test runs to completion without hitting an I/O
1015 # error, that means there is no point in continuing with this test
1016 # case so set $::go to zero.
1017 #
1018 if {$::sqlite_io_error_pending>0} {
1019 set ::go 0
1020 set q 0
1021 set ::sqlite_io_error_pending 0
1022 } else {
1023 set q 1
1024 }
1025
drhc9ac5ca2005-11-04 22:03:30 +00001026 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001027 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1028 set r 1
1029 }
danielk1977f2fa8312006-01-24 13:09:33 +00001030 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001031
1032 # One of two things must have happened. either
1033 # 1. We never hit the IO error and the SQL returned OK
1034 # 2. An IO error was hit and the SQL failed
1035 #
dand41a29a2010-05-06 15:56:28 +00001036 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001037 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001038 } {1}
1039
danielk197780daec62008-05-12 10:57:02 +00001040 set ::sqlite_io_error_hit 0
1041 set ::sqlite_io_error_pending 0
1042
danielk197752b472a2008-05-05 16:23:55 +00001043 # Check that no page references were leaked. There should be
1044 # a single reference if there is still an active transaction,
1045 # or zero otherwise.
1046 #
danielk197781620542008-06-07 05:19:37 +00001047 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
1048 # locks are established on database files (i.e. if the error
1049 # occurs while attempting to detect a hot-journal file), then
1050 # there may 0 page references and an active transaction according
1051 # to [sqlite3_get_autocommit].
1052 #
danielk19774abd5442008-05-05 15:26:50 +00001053 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001054 do_test $testname.$n.4 {
1055 set bt [btree_from_db db]
1056 db_enter db
1057 array set stats [btree_pager_stats $bt]
1058 db_leave db
danielk197781620542008-06-07 05:19:37 +00001059 set nRef $stats(ref)
1060 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1061 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001062 }
1063
danielk197752b472a2008-05-05 16:23:55 +00001064 # If there is an open database handle and no open transaction,
1065 # and the pager is not running in exclusive-locking mode,
1066 # check that the pager is in "unlocked" state. Theoretically,
1067 # if a call to xUnlock() failed due to an IO error the underlying
1068 # file may still be locked.
1069 #
1070 ifcapable pragma {
1071 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001072 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001073 && [db one {pragma locking_mode}] eq "normal"
1074 && [sqlite3_get_autocommit db]
1075 } {
1076 do_test $testname.$n.5 {
1077 set bt [btree_from_db db]
1078 db_enter db
1079 array set stats [btree_pager_stats $bt]
1080 db_leave db
1081 set stats(state)
1082 } 0
1083 }
1084 }
1085
danielk197732554c12005-01-22 03:39:39 +00001086 # If an IO error occured, then the checksum of the database should
1087 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001088 #
drh1aa5af12008-03-07 19:51:14 +00001089 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001090 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001091 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001092 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001093 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +00001094 cksum
1095 } $checksum
1096 }
1097
drh1aa5af12008-03-07 19:51:14 +00001098 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001099 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001100 if {[info exists ::ioerropts(-cleanup)]} {
1101 catch $::ioerropts(-cleanup)
1102 }
danielk197732554c12005-01-22 03:39:39 +00001103 }
1104 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001105 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001106 unset ::ioerropts
1107}
1108
drh93aed5a2008-01-16 17:46:38 +00001109# Return a checksum based on the contents of the main database associated
1110# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001111#
1112proc cksum {{db db}} {
1113 set txt [$db eval {
1114 SELECT name, type, sql FROM sqlite_master order by name
1115 }]\n
1116 foreach tbl [$db eval {
1117 SELECT name FROM sqlite_master WHERE type='table' order by name
1118 }] {
1119 append txt [$db eval "SELECT * FROM $tbl"]\n
1120 }
1121 foreach prag {default_synchronous default_cache_size} {
1122 append txt $prag-[$db eval "PRAGMA $prag"]\n
1123 }
1124 set cksum [string length $txt]-[md5 $txt]
1125 # puts $cksum-[file size test.db]
1126 return $cksum
1127}
1128
drh93aed5a2008-01-16 17:46:38 +00001129# Generate a checksum based on the contents of the main and temp tables
1130# database $db. If the checksum of two databases is the same, and the
1131# integrity-check passes for both, the two databases are identical.
1132#
drh1db639c2008-01-17 02:36:28 +00001133proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001134 set ret [list]
1135 ifcapable tempdb {
1136 set sql {
1137 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1138 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1139 SELECT 'sqlite_master' UNION
1140 SELECT 'sqlite_temp_master' ORDER BY 1
1141 }
1142 } else {
1143 set sql {
1144 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1145 SELECT 'sqlite_master' ORDER BY 1
1146 }
1147 }
1148 set tbllist [$db eval $sql]
1149 set txt {}
1150 foreach tbl $tbllist {
1151 append txt [$db eval "SELECT * FROM $tbl"]
1152 }
1153 foreach prag {default_cache_size} {
1154 append txt $prag-[$db eval "PRAGMA $prag"]\n
1155 }
1156 # puts txt=$txt
1157 return [md5 $txt]
1158}
1159
drhdc2c4912009-02-04 22:46:47 +00001160# Generate a checksum based on the contents of a single database with
1161# a database connection. The name of the database is $dbname.
1162# Examples of $dbname are "temp" or "main".
1163#
1164proc dbcksum {db dbname} {
1165 if {$dbname=="temp"} {
1166 set master sqlite_temp_master
1167 } else {
1168 set master $dbname.sqlite_master
1169 }
1170 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1171 set txt [$db eval "SELECT * FROM $master"]\n
1172 foreach tab $alltab {
1173 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1174 }
1175 return [md5 $txt]
1176}
1177
danielk197735754ac2008-03-21 17:29:37 +00001178proc memdebug_log_sql {{filename mallocs.sql}} {
1179
danielk19776f332c12008-03-21 14:22:44 +00001180 set data [sqlite3_memdebug_log dump]
1181 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001182 if {$nFrame < 0} { return "" }
1183
danielk197735754ac2008-03-21 17:29:37 +00001184 set database temp
1185
danielk19776ab3a2e2009-02-19 14:39:25 +00001186 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001187
1188 set sql ""
1189 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001190 set nCall [lindex $e 0]
1191 set nByte [lindex $e 1]
1192 set lStack [lrange $e 2 end]
1193 append sql "INSERT INTO ${database}.malloc VALUES"
1194 append sql "('test', $nCall, $nByte, '$lStack');\n"
1195 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001196 set frames($f) 1
1197 }
1198 }
1199
1200 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001201 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001202
1203 foreach f [array names frames] {
1204 set addr [format %x $f]
1205 set cmd "addr2line -e [info nameofexec] $addr"
1206 set line [eval exec $cmd]
1207 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001208
1209 set file [lindex [split $line :] 0]
1210 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001211 }
1212
danielk197735754ac2008-03-21 17:29:37 +00001213 foreach f [array names files] {
1214 set contents ""
1215 catch {
1216 set fd [open $f]
1217 set contents [read $fd]
1218 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001219 }
danielk197735754ac2008-03-21 17:29:37 +00001220 set contents [string map {' ''} $contents]
1221 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001222 }
danielk19776f332c12008-03-21 14:22:44 +00001223
danielk197735754ac2008-03-21 17:29:37 +00001224 set fd [open $filename w]
1225 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1226 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001227}
drh93aed5a2008-01-16 17:46:38 +00001228
danielk197732554c12005-01-22 03:39:39 +00001229# Copy file $from into $to. This is used because some versions of
1230# TCL for windows (notably the 8.4.1 binary package shipped with the
1231# current mingw release) have a broken "file copy" command.
1232#
1233proc copy_file {from to} {
1234 if {$::tcl_platform(platform)=="unix"} {
1235 file copy -force $from $to
1236 } else {
1237 set f [open $from]
1238 fconfigure $f -translation binary
1239 set t [open $to w]
1240 fconfigure $t -translation binary
1241 puts -nonewline $t [read $f [file size $from]]
1242 close $t
1243 close $f
1244 }
1245}
1246
danf5894502009-10-07 18:41:19 +00001247# Drop all tables in database [db]
1248proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001249 ifcapable trigger&&foreignkey {
1250 set pk [$db one "PRAGMA foreign_keys"]
1251 $db eval "PRAGMA foreign_keys = OFF"
1252 }
drh9a6ffc82010-02-15 18:03:20 +00001253 foreach {idx name file} [db eval {PRAGMA database_list}] {
1254 if {$idx==1} {
1255 set master sqlite_temp_master
1256 } else {
1257 set master $name.sqlite_master
1258 }
1259 foreach {t type} [$db eval "
1260 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001261 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001262 "] {
dana16d1062010-09-28 17:37:28 +00001263 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001264 }
danf5894502009-10-07 18:41:19 +00001265 }
shaneh4e7b32f2009-12-17 22:12:51 +00001266 ifcapable trigger&&foreignkey {
1267 $db eval "PRAGMA foreign_keys = $pk"
1268 }
danf5894502009-10-07 18:41:19 +00001269}
1270
dan71cb5182010-04-26 12:39:03 +00001271#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001272# If a test script is executed with global variable $::G(perm:name) set to
1273# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1274# in rollback mode. The following Tcl procs are used to make this less
1275# intrusive:
dan71cb5182010-04-26 12:39:03 +00001276#
1277# wal_set_journal_mode ?DB?
1278#
1279# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1280# connection handle DB. Otherwise, this command is a no-op.
1281#
1282# wal_check_journal_mode TESTNAME ?DB?
1283#
1284# If running a WAL test, execute a tests case that fails if the main
1285# database for connection handle DB is not currently a WAL database.
1286# Otherwise (if not running a WAL permutation) this is a no-op.
1287#
1288# wal_is_wal_mode
1289#
1290# Returns true if this test should be run in WAL mode. False otherwise.
1291#
1292proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001293 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001294}
1295proc wal_set_journal_mode {{db db}} {
1296 if { [wal_is_wal_mode] } {
1297 $db eval "PRAGMA journal_mode = WAL"
1298 }
1299}
1300proc wal_check_journal_mode {testname {db db}} {
1301 if { [wal_is_wal_mode] } {
1302 $db eval { SELECT * FROM sqlite_master }
1303 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1304 }
1305}
1306
dan430e74c2010-06-07 17:47:26 +00001307proc permutation {} {
1308 set perm ""
1309 catch {set perm $::G(perm:name)}
1310 set perm
1311}
dancb79e512010-08-06 13:50:07 +00001312proc presql {} {
1313 set presql ""
1314 catch {set presql $::G(perm:presql)}
1315 set presql
1316}
dan430e74c2010-06-07 17:47:26 +00001317
danc1a60c52010-06-07 14:28:16 +00001318#-------------------------------------------------------------------------
1319#
1320proc slave_test_script {script} {
1321
1322 # Create the interpreter used to run the test script.
1323 interp create tinterp
1324
1325 # Populate some global variables that tester.tcl expects to see.
1326 foreach {var value} [list \
1327 ::argv0 $::argv0 \
1328 ::argv {} \
1329 ::SLAVE 1 \
1330 ] {
1331 interp eval tinterp [list set $var $value]
1332 }
1333
1334 # The alias used to access the global test counters.
1335 tinterp alias set_test_counter set_test_counter
1336
1337 # Set up the ::cmdlinearg array in the slave.
1338 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1339
dan430e74c2010-06-07 17:47:26 +00001340 # Set up the ::G array in the slave.
1341 interp eval tinterp [list array set ::G [array get ::G]]
1342
danc1a60c52010-06-07 14:28:16 +00001343 # Load the various test interfaces implemented in C.
1344 load_testfixture_extensions tinterp
1345
1346 # Run the test script.
1347 interp eval tinterp $script
1348
danea5542d2010-07-06 11:26:15 +00001349 # Check if the interpreter call [run_thread_tests]
1350 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1351 set ::run_thread_tests_called 1
1352 }
1353
danc1a60c52010-06-07 14:28:16 +00001354 # Delete the interpreter used to run the test script.
1355 interp delete tinterp
1356}
1357
dan430e74c2010-06-07 17:47:26 +00001358proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001359 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001360
dan6a64d672011-04-04 15:38:16 +00001361 if {[info exists ::G(start:permutation)]} {
1362 if {[permutation] != $::G(start:permutation)} return
1363 unset ::G(start:permutation)
1364 }
1365 if {[info exists ::G(start:file)]} {
1366 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1367 unset ::G(start:file)
1368 }
1369
dan92d516a2010-07-05 14:54:48 +00001370 # Remember the value of the shared-cache setting. So that it is possible
1371 # to check afterwards that it was not modified by the test script.
1372 #
1373 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001374
dan92d516a2010-07-05 14:54:48 +00001375 # Run the test script in a slave interpreter.
1376 #
danea5542d2010-07-06 11:26:15 +00001377 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001378 reset_prng_state
1379 set ::sqlite_open_file_count 0
1380 set time [time { slave_test_script [list source $zFile] }]
1381 set ms [expr [lindex $time 0] / 1000]
1382
dan92d516a2010-07-05 14:54:48 +00001383 # Test that all files opened by the test script were closed. Omit this
1384 # if the test script has "thread" in its name. The open file counter
1385 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001386 #
danea5542d2010-07-06 11:26:15 +00001387 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001388 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1389 }
dan430e74c2010-06-07 17:47:26 +00001390 set ::sqlite_open_file_count 0
1391
dan0626dfc2010-06-15 06:56:37 +00001392 # Test that the global "shared-cache" setting was not altered by
1393 # the test script.
1394 #
1395 ifcapable shared_cache {
1396 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1397 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1398 }
dan430e74c2010-06-07 17:47:26 +00001399
dan92d516a2010-07-05 14:54:48 +00001400 # Add some info to the output.
1401 #
dan0626dfc2010-06-15 06:56:37 +00001402 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001403 show_memstats
danc1a60c52010-06-07 14:28:16 +00001404}
1405
dan59257dc2010-08-04 11:34:31 +00001406# Open a new connection on database test.db and execute the SQL script
1407# supplied as an argument. Before returning, close the new conection and
1408# restore the 4 byte fields starting at header offsets 28, 92 and 96
1409# to the values they held before the SQL was executed. This simulates
1410# a write by a pre-3.7.0 client.
1411#
1412proc sql36231 {sql} {
1413 set B [hexio_read test.db 92 8]
1414 set A [hexio_read test.db 28 4]
1415 sqlite3 db36231 test.db
1416 catch { db36231 func a_string a_string }
1417 execsql $sql db36231
1418 db36231 close
1419 hexio_write test.db 28 $A
1420 hexio_write test.db 92 $B
1421 return ""
1422}
danf5894502009-10-07 18:41:19 +00001423
danccc7ff42010-11-29 12:06:45 +00001424proc db_save {} {
1425 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1426 foreach f [glob -nocomplain test.db*] {
1427 set f2 "sv_$f"
1428 file copy -force $f $f2
1429 }
1430}
1431proc db_save_and_close {} {
1432 db_save
1433 catch { db close }
1434 return ""
1435}
1436proc db_restore {} {
1437 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1438 foreach f2 [glob -nocomplain sv_test.db*] {
1439 set f [string range $f2 3 end]
1440 file copy -force $f2 $f
1441 }
1442}
1443proc db_restore_and_reopen {{dbfile test.db}} {
1444 catch { db close }
1445 db_restore
1446 sqlite3 db $dbfile
1447}
1448proc db_delete_and_reopen {{file test.db}} {
1449 catch { db close }
1450 foreach f [glob -nocomplain test.db*] { file delete -force $f }
1451 sqlite3 db $file
1452}
1453
danielk197745901d62004-11-10 15:27:38 +00001454# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1455# to non-zero, then set the global variable $AUTOVACUUM to 1.
1456set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001457
1458source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001459source $testdir/malloc_common.tcl