blob: b9af9322dc4d490d554ab5ac063dc614109e5405 [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#-------------------------------------------------------------------------
mistachkin1d406e02013-08-29 01:09:14 +000017# The commands provided by the code in this file to help with creating
danc1a60c52010-06-07 14:28:16 +000018# 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#
mistachkinc5484652012-03-05 22:52:33 +000022# is_relative_file
mistachkin4a41f342012-03-06 03:00:49 +000023# test_pwd
mistachkinf8a78462012-03-08 20:00:36 +000024# get_pwd
danc1a60c52010-06-07 14:28:16 +000025# copy_file FROM TO
mistachkinfda06be2011-08-02 00:57:34 +000026# delete_file FILENAME
dan6f343962011-07-01 18:26:40 +000027# drop_all_tables ?DB?
mistachkinfda06be2011-08-02 00:57:34 +000028# forcecopy FROM TO
danc1a60c52010-06-07 14:28:16 +000029# forcedelete FILENAME
30#
31# Test the capability of the SQLite version built into the interpreter to
32# determine if a specific test can be run:
33#
mistachkinc60941f2012-09-13 01:51:02 +000034# capable EXPR
danc1a60c52010-06-07 14:28:16 +000035# ifcapable EXPR
36#
37# Calulate checksums based on database contents:
38#
39# dbcksum DB DBNAME
40# allcksum ?DB?
41# cksum ?DB?
42#
43# Commands to execute/explain SQL statements:
44#
mistachkin1d406e02013-08-29 01:09:14 +000045# memdbsql SQL
danc1a60c52010-06-07 14:28:16 +000046# stepsql DB SQL
47# execsql2 SQL
48# explain_no_trace SQL
49# explain SQL ?DB?
50# catchsql SQL ?DB?
51# execsql SQL ?DB?
52#
53# Commands to run test cases:
54#
55# do_ioerr_test TESTNAME ARGS...
56# crashsql ARGS...
57# integrity_check TESTNAME ?DB?
drh433dccf2013-02-09 15:37:11 +000058# verify_ex_errcode TESTNAME EXPECTED ?DB?
danc1a60c52010-06-07 14:28:16 +000059# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000060# do_execsql_test TESTNAME SQL EXPECTED
61# do_catchsql_test TESTNAME SQL EXPECTED
drh3c2aeae2014-01-24 14:37:44 +000062# do_timed_execsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000063#
dan430e74c2010-06-07 17:47:26 +000064# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000065#
66# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000067# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000068# fail_test TESTNAME
69# incr_ntest
70#
71# Command run at the end of each test file:
72#
73# finish_test
74#
dan430e74c2010-06-07 17:47:26 +000075# Commands to help create test files that run with the "WAL" and other
76# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000077#
78# wal_is_wal_mode
79# wal_set_journal_mode ?DB?
80# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000081# permutation
dancb79e512010-08-06 13:50:07 +000082# presql
danc1a60c52010-06-07 14:28:16 +000083#
drh348784e2000-05-29 20:41:49 +000084
mistachkin1d406e02013-08-29 01:09:14 +000085# Set the precision of FP arithmatic used by the interpreter. And
danc1a60c52010-06-07 14:28:16 +000086# configure SQLite to take database file locks on the page that begins
87# 64KB into the database file instead of the one 1GB in. This means
88# the code that handles that special case can be tested without creating
89# very large database files.
90#
drh92febd92004-08-20 18:34:20 +000091set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000092sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000093
danc1a60c52010-06-07 14:28:16 +000094
mistachkin1d406e02013-08-29 01:09:14 +000095# If the pager codec is available, create a wrapper for the [sqlite3]
danc1a60c52010-06-07 14:28:16 +000096# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000097#
danc1a60c52010-06-07 14:28:16 +000098# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000099#
danc1a60c52010-06-07 14:28:16 +0000100# becomes
drh4a50aac2007-08-23 02:47:53 +0000101#
danc1a60c52010-06-07 14:28:16 +0000102# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +0000103#
dan430e74c2010-06-07 17:47:26 +0000104if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +0000105 rename sqlite3 sqlite_orig
106 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000107 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000108 # This command is opening a new database connection.
109 #
110 if {[info exists ::G(perm:sqlite3_args)]} {
111 set args [concat $args $::G(perm:sqlite3_args)]
112 }
dan68928b62010-06-22 13:46:43 +0000113 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000114 lappend args -key {xyzzy}
115 }
116
dan3ccd20a2010-06-09 19:01:02 +0000117 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000118 if {[info exists ::G(perm:presql)]} {
119 [lindex $args 0] eval $::G(perm:presql)
120 }
dan1ce1b4a2010-12-07 14:32:28 +0000121 if {[info exists ::G(perm:dbconfig)]} {
122 set ::dbhandle [lindex $args 0]
123 uplevel #0 $::G(perm:dbconfig)
124 }
dan3ccd20a2010-06-09 19:01:02 +0000125 set res
dan430e74c2010-06-07 17:47:26 +0000126 } else {
mistachkin1d406e02013-08-29 01:09:14 +0000127 # This command is not opening a new database connection. Pass the
mistachkin48864df2013-03-21 21:20:32 +0000128 # arguments through to the C implementation as the are.
dan430e74c2010-06-07 17:47:26 +0000129 #
130 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000131 }
drh9eb9e262004-02-11 02:18:05 +0000132 }
133}
134
mistachkinfda06be2011-08-02 00:57:34 +0000135proc getFileRetries {} {
136 if {![info exists ::G(file-retries)]} {
137 #
138 # NOTE: Return the default number of retries for [file] operations. A
139 # value of zero or less here means "disabled".
140 #
mistachkinc60941f2012-09-13 01:51:02 +0000141 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
mistachkinfda06be2011-08-02 00:57:34 +0000142 }
143 return $::G(file-retries)
144}
145
146proc getFileRetryDelay {} {
147 if {![info exists ::G(file-retry-delay)]} {
148 #
149 # NOTE: Return the default number of milliseconds to wait when retrying
150 # failed [file] operations. A value of zero or less means "do not
151 # wait".
152 #
153 return 100; # TODO: Good default?
154 }
155 return $::G(file-retry-delay)
156}
157
mistachkinf8a78462012-03-08 20:00:36 +0000158# Return the string representing the name of the current directory. On
159# Windows, the result is "normalized" to whatever our parent command shell
160# is using to prevent case-mismatch issues.
161#
162proc get_pwd {} {
163 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000164 #
165 # NOTE: Cannot use [file normalize] here because it would alter the
166 # case of the result to what Tcl considers canonical, which would
167 # defeat the purpose of this procedure.
168 #
169 return [string map [list \\ /] \
170 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000171 } else {
172 return [pwd]
173 }
174}
175
mistachkinfda06be2011-08-02 00:57:34 +0000176# Copy file $from into $to. This is used because some versions of
177# TCL for windows (notably the 8.4.1 binary package shipped with the
178# current mingw release) have a broken "file copy" command.
179#
180proc copy_file {from to} {
181 do_copy_file false $from $to
182}
183
184proc forcecopy {from to} {
185 do_copy_file true $from $to
186}
187
188proc do_copy_file {force from to} {
189 set nRetry [getFileRetries] ;# Maximum number of retries.
190 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
191
192 # On windows, sometimes even a [file copy -force] can fail. The cause is
193 # usually "tag-alongs" - programs like anti-virus software, automatic backup
194 # tools and various explorer extensions that keep a file open a little longer
195 # than we expect, causing the delete to fail.
196 #
197 # The solution is to wait a short amount of time before retrying the copy.
198 #
199 if {$nRetry > 0} {
200 for {set i 0} {$i<$nRetry} {incr i} {
201 set rc [catch {
202 if {$force} {
203 file copy -force $from $to
204 } else {
205 file copy $from $to
206 }
207 } msg]
208 if {$rc==0} break
209 if {$nDelay > 0} { after $nDelay }
210 }
211 if {$rc} { error $msg }
212 } else {
213 if {$force} {
214 file copy -force $from $to
215 } else {
216 file copy $from $to
217 }
218 }
219}
220
mistachkinc5484652012-03-05 22:52:33 +0000221# Check if a file name is relative
222#
223proc is_relative_file { file } {
224 return [expr {[file pathtype $file] != "absolute"}]
225}
226
mistachkin4a41f342012-03-06 03:00:49 +0000227# If the VFS supports using the current directory, returns [pwd];
228# otherwise, it returns only the provided suffix string (which is
229# empty by default).
230#
231proc test_pwd { args } {
232 if {[llength $args] > 0} {
233 set suffix1 [lindex $args 0]
234 if {[llength $args] > 1} {
235 set suffix2 [lindex $args 1]
236 } else {
237 set suffix2 $suffix1
238 }
239 } else {
240 set suffix1 ""; set suffix2 ""
241 }
242 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000243 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000244 } else {
245 return $suffix2
246 }
247}
248
mistachkinfda06be2011-08-02 00:57:34 +0000249# Delete a file or directory
250#
251proc delete_file {args} {
252 do_delete_file false {*}$args
253}
254
255proc forcedelete {args} {
256 do_delete_file true {*}$args
257}
258
259proc do_delete_file {force args} {
260 set nRetry [getFileRetries] ;# Maximum number of retries.
261 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
262
263 foreach filename $args {
264 # On windows, sometimes even a [file delete -force] can fail just after
265 # a file is closed. The cause is usually "tag-alongs" - programs like
266 # anti-virus software, automatic backup tools and various explorer
267 # extensions that keep a file open a little longer than we expect, causing
268 # the delete to fail.
269 #
270 # The solution is to wait a short amount of time before retrying the
271 # delete.
272 #
273 if {$nRetry > 0} {
274 for {set i 0} {$i<$nRetry} {incr i} {
275 set rc [catch {
276 if {$force} {
277 file delete -force $filename
278 } else {
279 file delete $filename
280 }
281 } msg]
282 if {$rc==0} break
283 if {$nDelay > 0} { after $nDelay }
284 }
285 if {$rc} { error $msg }
286 } else {
287 if {$force} {
288 file delete -force $filename
289 } else {
290 file delete $filename
291 }
292 }
293 }
294}
295
mistachkin1d406e02013-08-29 01:09:14 +0000296if {$::tcl_platform(platform) eq "windows"} {
297 proc do_remove_win32_dir {args} {
298 set nRetry [getFileRetries] ;# Maximum number of retries.
299 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
300
301 foreach dirName $args {
302 # On windows, sometimes even a [remove_win32_dir] can fail just after
303 # a directory is emptied. The cause is usually "tag-alongs" - programs
304 # like anti-virus software, automatic backup tools and various explorer
305 # extensions that keep a file open a little longer than we expect,
306 # causing the delete to fail.
307 #
308 # The solution is to wait a short amount of time before retrying the
309 # removal.
310 #
311 if {$nRetry > 0} {
312 for {set i 0} {$i < $nRetry} {incr i} {
313 set rc [catch {
314 remove_win32_dir $dirName
315 } msg]
316 if {$rc == 0} break
317 if {$nDelay > 0} { after $nDelay }
318 }
319 if {$rc} { error $msg }
320 } else {
321 remove_win32_dir $dirName
322 }
323 }
324 }
325
326 proc do_delete_win32_file {args} {
327 set nRetry [getFileRetries] ;# Maximum number of retries.
328 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
329
330 foreach fileName $args {
331 # On windows, sometimes even a [delete_win32_file] can fail just after
332 # a file is closed. The cause is usually "tag-alongs" - programs like
333 # anti-virus software, automatic backup tools and various explorer
334 # extensions that keep a file open a little longer than we expect,
335 # causing the delete to fail.
336 #
337 # The solution is to wait a short amount of time before retrying the
338 # delete.
339 #
340 if {$nRetry > 0} {
341 for {set i 0} {$i < $nRetry} {incr i} {
342 set rc [catch {
343 delete_win32_file $fileName
344 } msg]
345 if {$rc == 0} break
346 if {$nDelay > 0} { after $nDelay }
347 }
348 if {$rc} { error $msg }
349 } else {
350 delete_win32_file $fileName
351 }
352 }
353 }
354}
355
dancb354602010-07-08 09:44:42 +0000356proc execpresql {handle args} {
357 trace remove execution $handle enter [list execpresql $handle]
358 if {[info exists ::G(perm:presql)]} {
359 $handle eval $::G(perm:presql)
360 }
361}
362
dan68928b62010-06-22 13:46:43 +0000363# This command should be called after loading tester.tcl from within
364# all test scripts that are incompatible with encryption codecs.
365#
366proc do_not_use_codec {} {
367 set ::do_not_use_codec 1
368 reset_db
369}
370
dan430e74c2010-06-07 17:47:26 +0000371# The following block only runs the first time this file is sourced. It
372# does not run in slave interpreters (since the ::cmdlinearg array is
373# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000374#
danc1a60c52010-06-07 14:28:16 +0000375if {[info exists cmdlinearg]==0} {
376
mistachkin1d406e02013-08-29 01:09:14 +0000377 # Parse any options specified in the $argv array. This script accepts the
378 # following options:
danc1a60c52010-06-07 14:28:16 +0000379 #
380 # --pause
381 # --soft-heap-limit=NN
382 # --maxerror=NN
383 # --malloctrace=N
384 # --backtrace=N
385 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000386 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000387 # --file-retries=N
388 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000389 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000390 # --match=$pattern
danc1a60c52010-06-07 14:28:16 +0000391 #
392 set cmdlinearg(soft-heap-limit) 0
393 set cmdlinearg(maxerror) 1000
394 set cmdlinearg(malloctrace) 0
395 set cmdlinearg(backtrace) 10
396 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000397 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000398 set cmdlinearg(file-retries) 0
399 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000400 set cmdlinearg(start) ""
401 set cmdlinearg(match) ""
danc1a60c52010-06-07 14:28:16 +0000402
403 set leftover [list]
404 foreach a $argv {
405 switch -regexp -- $a {
406 {^-+pause$} {
mistachkin1d406e02013-08-29 01:09:14 +0000407 # Wait for user input before continuing. This is to give the user an
danc1a60c52010-06-07 14:28:16 +0000408 # opportunity to connect profiling tools to the process.
409 puts -nonewline "Press RETURN to begin..."
410 flush stdout
411 gets stdin
412 }
413 {^-+soft-heap-limit=.+$} {
414 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
415 }
416 {^-+maxerror=.+$} {
417 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
418 }
419 {^-+malloctrace=.+$} {
420 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
421 if {$cmdlinearg(malloctrace)} {
422 sqlite3_memdebug_log start
423 }
424 }
425 {^-+backtrace=.+$} {
426 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000427 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000428 }
danc1a60c52010-06-07 14:28:16 +0000429 {^-+binarylog=.+$} {
430 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
431 }
dan0626dfc2010-06-15 06:56:37 +0000432 {^-+soak=.+$} {
433 foreach {dummy cmdlinearg(soak)} [split $a =] break
434 set ::G(issoak) $cmdlinearg(soak)
435 }
mistachkinfda06be2011-08-02 00:57:34 +0000436 {^-+file-retries=.+$} {
437 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
438 set ::G(file-retries) $cmdlinearg(file-retries)
439 }
440 {^-+file-retry-delay=.+$} {
441 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
442 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
443 }
dan6a64d672011-04-04 15:38:16 +0000444 {^-+start=.+$} {
445 foreach {dummy cmdlinearg(start)} [split $a =] break
446
447 set ::G(start:file) $cmdlinearg(start)
448 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
449 set ::G(start:permutation) ${s.perm}
450 set ::G(start:file) ${s.file}
451 }
452 if {$::G(start:file) == ""} {unset ::G(start:file)}
453 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000454 {^-+match=.+$} {
455 foreach {dummy cmdlinearg(match)} [split $a =] break
456
457 set ::G(match) $cmdlinearg(match)
458 if {$::G(match) == ""} {unset ::G(match)}
459 }
danc1a60c52010-06-07 14:28:16 +0000460 default {
461 lappend leftover $a
462 }
463 }
464 }
465 set argv $leftover
466
dan430e74c2010-06-07 17:47:26 +0000467 # Install the malloc layer used to inject OOM errors. And the 'automatic'
468 # extensions. This only needs to be done once for the process.
469 #
mistachkin1d406e02013-08-29 01:09:14 +0000470 sqlite3_shutdown
471 install_malloc_faultsim 1
danielk1977f7b9d662008-06-23 18:49:43 +0000472 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000473 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000474
dan430e74c2010-06-07 17:47:26 +0000475 # If the --binarylog option was specified, create the logging VFS. This
476 # call installs the new VFS as the default for all SQLite connections.
477 #
danc1a60c52010-06-07 14:28:16 +0000478 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000479 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000480 }
481
482 # Set the backtrace depth, if malloc tracing is enabled.
483 #
484 if {$cmdlinearg(malloctrace)} {
485 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000486 }
danielk1977f7b9d662008-06-23 18:49:43 +0000487}
danielk197703ba3fa2009-01-09 10:49:14 +0000488
danc1a60c52010-06-07 14:28:16 +0000489# Update the soft-heap-limit each time this script is run. In that
490# way if an individual test file changes the soft-heap-limit, it
491# will be reset at the start of the next test file.
492#
493sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
494
495# Create a test database
496#
danielk197703ba3fa2009-01-09 10:49:14 +0000497proc reset_db {} {
498 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000499 forcedelete test.db
500 forcedelete test.db-journal
501 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000502 sqlite3 db ./test.db
503 set ::DB [sqlite3_connection_pointer db]
504 if {[info exists ::SETUP_SQL]} {
505 db eval $::SETUP_SQL
506 }
drhcd61c282002-03-06 22:01:34 +0000507}
danielk197703ba3fa2009-01-09 10:49:14 +0000508reset_db
drhbec2bf42000-05-29 23:48:22 +0000509
510# Abort early if this script has been run before.
511#
danc1a60c52010-06-07 14:28:16 +0000512if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000513
drhce2198c2010-09-10 13:22:59 +0000514# Make sure memory statistics are enabled.
515#
516sqlite3_config_memstatus 1
517
danc1a60c52010-06-07 14:28:16 +0000518# Initialize the test counters and set up commands to access them.
519# Or, if this is a slave interpreter, set up aliases to write the
520# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000521#
danc1a60c52010-06-07 14:28:16 +0000522if {0==[info exists ::SLAVE]} {
523 set TC(errors) 0
524 set TC(count) 0
525 set TC(fail_list) [list]
526 set TC(omit_list) [list]
drhcca17c32013-04-19 12:32:52 +0000527 set TC(warn_list) [list]
danc1a60c52010-06-07 14:28:16 +0000528
529 proc set_test_counter {counter args} {
530 if {[llength $args]} {
531 set ::TC($counter) [lindex $args 0]
532 }
533 set ::TC($counter)
534 }
drhb62c3352006-11-23 09:39:16 +0000535}
drh348784e2000-05-29 20:41:49 +0000536
drh521cc842008-04-15 02:36:33 +0000537# Record the fact that a sequence of tests were omitted.
538#
mistachkin6c3c1a02011-11-12 03:17:40 +0000539proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000540 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000541 if {$append} {
542 lappend omitList [list $name $reason]
543 }
danc1a60c52010-06-07 14:28:16 +0000544 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000545}
546
danc1a60c52010-06-07 14:28:16 +0000547# Record the fact that a test failed.
548#
549proc fail_test {name} {
550 set f [set_test_counter fail_list]
551 lappend f $name
552 set_test_counter fail_list $f
553 set_test_counter errors [expr [set_test_counter errors] + 1]
554
555 set nFail [set_test_counter errors]
556 if {$nFail>=$::cmdlinearg(maxerror)} {
557 puts "*** Giving up..."
558 finalize_testing
559 }
560}
561
drhcca17c32013-04-19 12:32:52 +0000562# Remember a warning message to be displayed at the conclusion of all testing
563#
564proc warning {msg {append 1}} {
565 puts "Warning: $msg"
566 set warnList [set_test_counter warn_list]
567 if {$append} {
568 lappend warnList $msg
569 }
570 set_test_counter warn_list $warnList
571}
572
573
danc1a60c52010-06-07 14:28:16 +0000574# Increment the number of tests run
575#
576proc incr_ntest {} {
577 set_test_counter count [expr [set_test_counter count] + 1]
578}
579
580
mistachkin1d406e02013-08-29 01:09:14 +0000581# Invoke the do_test procedure to run a single test
drh348784e2000-05-29 20:41:49 +0000582#
583proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000584 global argv cmdlinearg
585
dan8c408002010-11-01 17:38:24 +0000586 fix_testname name
587
drh4a50aac2007-08-23 02:47:53 +0000588 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000589
mistachkin1d406e02013-08-29 01:09:14 +0000590# if {[llength $argv]==0} {
dan92d516a2010-07-05 14:54:48 +0000591# set go 1
592# } else {
593# set go 0
594# foreach pattern $argv {
595# if {[string match $pattern $name]} {
596# set go 1
597# break
598# }
599# }
600# }
dan430e74c2010-06-07 17:47:26 +0000601
dand506de02010-07-03 13:59:01 +0000602 if {[info exists ::G(perm:prefix)]} {
603 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000604 }
605
danc1a60c52010-06-07 14:28:16 +0000606 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000607 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000608 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000609
610 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
611 if {[catch {uplevel #0 "$cmd;\n"} result]} {
612 puts "\nError: $result"
613 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000614 } else {
drh3f17aef2012-04-27 01:08:02 +0000615 if {[regexp {^~?/.*/$} $expected]} {
drh70bdcc72013-05-30 19:28:34 +0000616 # "expected" is of the form "/PATTERN/" then the result if correct if
617 # regular expression PATTERN matches the result. "~/PATTERN/" means
618 # the regular expression must not match.
drh3f17aef2012-04-27 01:08:02 +0000619 if {[string index $expected 0]=="~"} {
drhc2b23e72013-11-13 15:32:15 +0000620 set re [string range $expected 2 end-1]
621 if {[string index $re 0]=="*"} {
622 # If the regular expression begins with * then treat it as a glob instead
623 set ok [string match $re $result]
624 } else {
625 set re [string map {# {[-0-9.]+}} $re]
626 set ok [regexp $re $result]
627 }
628 set ok [expr {!$ok}]
drh3f17aef2012-04-27 01:08:02 +0000629 } else {
drhc2b23e72013-11-13 15:32:15 +0000630 set re [string range $expected 1 end-1]
631 if {[string index $re 0]=="*"} {
632 # If the regular expression begins with * then treat it as a glob instead
633 set ok [string match $re $result]
634 } else {
635 set re [string map {# {[-0-9.]+}} $re]
636 set ok [regexp $re $result]
637 }
drh3f17aef2012-04-27 01:08:02 +0000638 }
drh70bdcc72013-05-30 19:28:34 +0000639 } elseif {[regexp {^~?\*.*\*$} $expected]} {
640 # "expected" is of the form "*GLOB*" then the result if correct if
641 # glob pattern GLOB matches the result. "~/GLOB/" means
642 # the glob must not match.
643 if {[string index $expected 0]=="~"} {
644 set e [string range $expected 1 end]
645 set ok [expr {![string match $e $result]}]
646 } else {
647 set ok [string match $expected $result]
648 }
drh3f17aef2012-04-27 01:08:02 +0000649 } else {
650 set ok [expr {[string compare $result $expected]==0}]
651 }
652 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000653 # if {![info exists ::testprefix] || $::testprefix eq ""} {
654 # error "no test prefix"
655 # }
drh3f17aef2012-04-27 01:08:02 +0000656 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
657 fail_test $name
658 } else {
659 puts " Ok"
660 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000661 }
drh348784e2000-05-29 20:41:49 +0000662 } else {
mistachkin6c3c1a02011-11-12 03:17:40 +0000663 puts " Omitted"
664 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000665 }
drh6558db82007-04-13 03:23:21 +0000666 flush stdout
drh348784e2000-05-29 20:41:49 +0000667}
dana3f51082010-09-30 18:43:14 +0000668
drh8df91852012-04-24 12:46:05 +0000669proc catchcmd {db {cmd ""}} {
670 global CLI
671 set out [open cmds.txt w]
672 puts $out $cmd
673 close $out
674 set line "exec $CLI $db < cmds.txt"
675 set rc [catch { eval $line } msg]
676 list $rc $msg
677}
678
shaneh55505172011-06-21 19:30:19 +0000679proc filepath_normalize {p} {
680 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000681 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000682 # lreverse*2 as a hack to remove any unneeded {} after the string map
683 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000684 } {
685 set p
shaneh55505172011-06-21 19:30:19 +0000686 }
687}
688proc do_filepath_test {name cmd expected} {
689 uplevel [list do_test $name [
690 subst -nocommands { filepath_normalize [ $cmd ] }
691 ] [filepath_normalize $expected]]
692}
693
dan33f53792011-05-05 19:44:22 +0000694proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000695 # different TCL versions display floating point values differently.
696 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000697}
698proc do_realnum_test {name cmd expected} {
699 uplevel [list do_test $name [
700 subst -nocommands { realnum_normalize [ $cmd ] }
701 ] [realnum_normalize $expected]]
702}
703
dana3f51082010-09-30 18:43:14 +0000704proc fix_testname {varname} {
705 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000706 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000707 && [string is digit [string range $testname 0 0]]
708 } {
709 set testname "${::testprefix}-$testname"
710 }
711}
mistachkin1d406e02013-08-29 01:09:14 +0000712
dan57f7f4b2010-10-01 19:04:37 +0000713proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000714 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000715 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000716}
717proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000718 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000719 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000720}
drh3c2aeae2014-01-24 14:37:44 +0000721proc do_timed_execsql_test {testname sql {result {}}} {
722 fix_testname testname
723 uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\
724 [list [list {*}$result]]
725}
dan39854792010-11-15 16:12:58 +0000726proc do_eqp_test {name sql res} {
727 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
728}
dan153eda02010-06-21 07:45:47 +0000729
dan51f06982010-09-18 19:00:12 +0000730#-------------------------------------------------------------------------
731# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
732#
733# Where switches are:
734#
735# -errorformat FMTSTRING
736# -count
danaf1dcab2010-09-20 19:17:53 +0000737# -query SQL
dana16d1062010-09-28 17:37:28 +0000738# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000739# -repair TCL
dan51f06982010-09-18 19:00:12 +0000740#
741proc do_select_tests {prefix args} {
742
743 set testlist [lindex $args end]
744 set switches [lrange $args 0 end-1]
745
746 set errfmt ""
747 set countonly 0
dana16d1062010-09-28 17:37:28 +0000748 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000749 set repair ""
dan51f06982010-09-18 19:00:12 +0000750
751 for {set i 0} {$i < [llength $switches]} {incr i} {
752 set s [lindex $switches $i]
753 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000754 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000755 set tclquery [list execsql [lindex $switches [incr i]]]
756 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
757 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000758 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000759 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000760 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
761 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000762 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
763 set countonly 1
764 } else {
765 error "unknown switch: $s"
766 }
767 }
768
769 if {$countonly && $errfmt!=""} {
770 error "Cannot use -count and -errorformat together"
771 }
772 set nTestlist [llength $testlist]
773 if {$nTestlist%3 || $nTestlist==0 } {
774 error "SELECT test list contains [llength $testlist] elements"
775 }
776
danaf7626f2010-09-23 18:47:36 +0000777 eval $repair
dan51f06982010-09-18 19:00:12 +0000778 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000779 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000780 execsql $sql
dana16d1062010-09-28 17:37:28 +0000781 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
782 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000783 set nRow 0
784 db eval $sql {incr nRow}
785 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
786 } elseif {$errfmt==""} {
787 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
788 } else {
789 set res [list 1 [string trim [format $errfmt {*}$res]]]
790 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
791 }
danaf7626f2010-09-23 18:47:36 +0000792 eval $repair
dan51f06982010-09-18 19:00:12 +0000793 }
danaf7626f2010-09-23 18:47:36 +0000794
dan51f06982010-09-18 19:00:12 +0000795}
796
danb04757a2010-09-21 16:59:16 +0000797proc delete_all_data {} {
798 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
799 db eval "DELETE FROM '[string map {' ''} $t]'"
800 }
801}
drh348784e2000-05-29 20:41:49 +0000802
mistachkin1d406e02013-08-29 01:09:14 +0000803# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +0000804# Return the number of microseconds per statement.
805#
drh3590f152006-11-23 21:09:10 +0000806proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000807 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000808 flush stdout
809 set speed [time {sqlite3_exec_nr db $sql}]
810 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000811 if {$tm == 0} {
812 set rate [format %20s "many"]
813 } else {
814 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
815 }
drh3590f152006-11-23 21:09:10 +0000816 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000817 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000818 global total_time
819 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000820 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000821}
drh45c236d2008-03-22 01:08:00 +0000822proc speed_trial_tcl {name numstmt units script} {
823 puts -nonewline [format {%-21.21s } $name...]
824 flush stdout
825 set speed [time {eval $script}]
826 set tm [lindex $speed 0]
827 if {$tm == 0} {
828 set rate [format %20s "many"]
829 } else {
830 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
831 }
832 set u2 $units/s
833 puts [format {%12d uS %s %s} $tm $rate $u2]
834 global total_time
835 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000836 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000837}
drhdd924312007-03-31 22:34:16 +0000838proc speed_trial_init {name} {
839 global total_time
840 set total_time 0
dana975b592010-10-08 16:09:43 +0000841 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000842 sqlite3 versdb :memory:
843 set vers [versdb one {SELECT sqlite_source_id()}]
844 versdb close
845 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000846}
847proc speed_trial_summary {name} {
848 global total_time
849 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000850
851 if { 0 } {
852 sqlite3 versdb :memory:
853 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
854 versdb close
855 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
856 foreach {test us} $::speed_trial_times {
857 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
858 }
859 }
drhb62c3352006-11-23 09:39:16 +0000860}
861
drh348784e2000-05-29 20:41:49 +0000862# Run this routine last
863#
864proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000865 catch {db close}
dane8559832014-07-31 17:35:40 +0000866 catch {db1 close}
danc1a60c52010-06-07 14:28:16 +0000867 catch {db2 close}
868 catch {db3 close}
869 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000870}
871proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000872 global sqlite_open_file_count
873
874 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000875
drh6e142f52000-06-08 13:36:40 +0000876 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000877 catch {db2 close}
878 catch {db3 close}
879
drh9bc54492007-10-23 14:49:59 +0000880 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000881 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000882 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000883 db close
drh984bfaa2008-03-19 16:08:53 +0000884 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000885
drh3a7fb7c2007-08-10 16:41:08 +0000886 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000887 set nTest [incr_ntest]
888 set nErr [set_test_counter errors]
889
drhef866372013-05-22 20:49:02 +0000890 set nKnown 0
891 if {[file readable known-problems.txt]} {
892 set fd [open known-problems.txt]
893 set content [read $fd]
894 close $fd
895 foreach x $content {set known_error($x) 1}
896 foreach x [set_test_counter fail_list] {
897 if {[info exists known_error($x)]} {incr nKnown}
898 }
899 }
900 if {$nKnown>0} {
901 puts "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
902 out of $nTest tests"
903 } else {
904 puts "$nErr errors out of $nTest tests"
905 }
906 if {$nErr>$nKnown} {
907 puts -nonewline "Failures on these tests:"
908 foreach x [set_test_counter fail_list] {
909 if {![info exists known_error($x)]} {puts -nonewline " $x"}
910 }
911 puts ""
drhf3a65f72007-08-22 20:18:21 +0000912 }
drhcca17c32013-04-19 12:32:52 +0000913 foreach warning [set_test_counter warn_list] {
914 puts "Warning: $warning"
915 }
danielk1977ee8b7992009-03-26 17:13:06 +0000916 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000917 if {[llength $omitList]>0} {
918 puts "Omitted test cases:"
919 set prec {}
920 foreach {rec} [lsort $omitList] {
921 if {$rec==$prec} continue
922 set prec $rec
923 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
924 }
925 }
drh80788d82006-09-02 14:50:23 +0000926 if {$nErr>0 && ![working_64bit_int]} {
927 puts "******************************************************************"
928 puts "N.B.: The version of TCL that you used to build this test harness"
929 puts "is defective in that it does not support 64-bit integers. Some or"
930 puts "all of the test failures above might be a result from this defect"
931 puts "in your TCL build."
932 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000933 }
danc1a60c52010-06-07 14:28:16 +0000934 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000935 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000936 }
drh94e92032003-02-16 22:21:32 +0000937 if {$sqlite_open_file_count} {
938 puts "$sqlite_open_file_count files were left open"
939 incr nErr
940 }
drheafc43b2010-07-26 18:43:40 +0000941 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
942 [sqlite3_memory_used]>0} {
943 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
944 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000945 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000946 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000947 puts "Writing unfreed memory log to \"./memleak.txt\""
948 sqlite3_memdebug_dump ./memleak.txt
949 }
drhf3a65f72007-08-22 20:18:21 +0000950 } else {
951 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000952 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000953 sqlite3_memdebug_dump ./memusage.txt
954 }
drhf3a65f72007-08-22 20:18:21 +0000955 }
drhf7141992008-06-19 00:16:08 +0000956 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000957 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
958 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000959 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
960 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
961 }
danc1a60c52010-06-07 14:28:16 +0000962 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000963 puts "Writing mallocs.sql..."
964 memdebug_log_sql
965 sqlite3_memdebug_log stop
966 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000967
968 if {[sqlite3_memory_used]>0} {
969 puts "Writing leaks.sql..."
970 sqlite3_memdebug_log sync
971 memdebug_log_sql leaks.sql
972 }
danielk197735754ac2008-03-21 17:29:37 +0000973 }
drhd9910fe2006-10-04 11:55:49 +0000974 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +0000975 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000976 }
977 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +0000978 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000979 }
drhdb25e382001-03-15 18:21:22 +0000980 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000981}
982
drhf7141992008-06-19 00:16:08 +0000983# Display memory statistics for analysis and debugging purposes.
984#
985proc show_memstats {} {
986 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000987 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
988 set val [format {now %10d max %10d max-size %10d} \
989 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000990 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000991 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
992 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
993 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000994 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000995 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
996 set val [format {now %10d max %10d max-size %10d} \
997 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000998 puts "Page-cache used: $val"
999 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
1000 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
1001 puts "Page-cache overflow: $val"
1002 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
1003 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
1004 puts "Scratch memory used: $val"
1005 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +00001006 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
1007 set val [format {now %10d max %10d max-size %10d} \
1008 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +00001009 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +00001010 ifcapable yytrackmaxstackdepth {
1011 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
1012 set val [format { max %10d} [lindex $x 2]]
1013 puts "Parser stack depth: $val"
1014 }
drhf7141992008-06-19 00:16:08 +00001015}
1016
drh348784e2000-05-29 20:41:49 +00001017# A procedure to execute SQL
1018#
drhc4a3c772001-04-04 11:48:57 +00001019proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001020 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001021 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001022}
drh3c2aeae2014-01-24 14:37:44 +00001023proc execsql_timed {sql {db db}} {
1024 set tm [time {
1025 set x [uplevel [list $db eval $sql]]
1026 } 1]
1027 set tm [lindex $tm 0]
1028 puts -nonewline " ([expr {$tm*0.001}]ms) "
1029 set x
1030}
drh3aadb2e2000-05-31 17:59:25 +00001031
drhadbca9c2001-09-27 15:11:53 +00001032# Execute SQL and catch exceptions.
1033#
1034proc catchsql {sql {db db}} {
1035 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001036 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001037 lappend r $msg
1038 return $r
1039}
1040
drh04096482001-11-09 22:41:44 +00001041# Do an VDBE code dump on the SQL given
1042#
1043proc explain {sql {db db}} {
1044 puts ""
danielk1977287fb612008-01-04 19:10:28 +00001045 puts "addr opcode p1 p2 p3 p4 p5 #"
1046 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001047 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +00001048 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
1049 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1050 ]
drh04096482001-11-09 22:41:44 +00001051 }
1052}
1053
dane83267d2013-11-05 14:19:22 +00001054proc explain_i {sql {db db}} {
1055 puts ""
1056 puts "addr opcode p1 p2 p3 p4 p5 #"
1057 puts "---- ------------ ------ ------ ------ ---------------- -- -"
1058
dane83267d2013-11-05 14:19:22 +00001059
dan5c663c32013-11-06 14:52:40 +00001060 # Set up colors for the different opcodes. Scheme is as follows:
1061 #
1062 # Red: Opcodes that write to a b-tree.
1063 # Blue: Opcodes that reposition or seek a cursor.
1064 # Green: The ResultRow opcode.
1065 #
dandea63f22014-03-03 16:48:47 +00001066 if { [catch {fconfigure stdout -mode}]==0 } {
1067 set R "\033\[31;1m" ;# Red fg
1068 set G "\033\[32;1m" ;# Green fg
1069 set B "\033\[34;1m" ;# Red fg
1070 set D "\033\[39;0m" ;# Default fg
1071 } else {
1072 set R ""
1073 set G ""
1074 set B ""
1075 set D ""
1076 }
dan5c663c32013-11-06 14:52:40 +00001077 foreach opcode {
1078 Seek SeekGe SeekGt SeekLe SeekLt NotFound Last Rewind
dane3ab7292013-11-12 12:30:09 +00001079 NoConflict Next Prev VNext VPrev VFilter
drhf45f2322014-03-23 17:45:03 +00001080 SorterSort SorterNext
dan5c663c32013-11-06 14:52:40 +00001081 } {
1082 set color($opcode) $B
1083 }
1084 foreach opcode {ResultRow} {
1085 set color($opcode) $G
1086 }
1087 foreach opcode {IdxInsert Insert Delete IdxDelete} {
1088 set color($opcode) $R
1089 }
1090
dan427ebba2013-11-05 16:39:31 +00001091 set bSeenGoto 0
dane83267d2013-11-05 14:19:22 +00001092 $db eval "explain $sql" {} {
1093 set x($addr) 0
1094 set op($addr) $opcode
1095
dan427ebba2013-11-05 16:39:31 +00001096 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} {
1097 set linebreak($p2) 1
1098 set bSeenGoto 1
dane83267d2013-11-05 14:19:22 +00001099 }
1100
dane3ab7292013-11-12 12:30:09 +00001101 if {$opcode=="Next" || $opcode=="Prev"
1102 || $opcode=="VNext" || $opcode=="VPrev"
drhf45f2322014-03-23 17:45:03 +00001103 || $opcode=="SorterNext"
dane3ab7292013-11-12 12:30:09 +00001104 } {
dane83267d2013-11-05 14:19:22 +00001105 for {set i $p2} {$i<$addr} {incr i} {
1106 incr x($i) 2
1107 }
1108 }
1109
1110 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1111 for {set i [expr $p2+1]} {$i<$addr} {incr i} {
1112 incr x($i) 2
1113 }
1114 }
dan427ebba2013-11-05 16:39:31 +00001115
1116 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1117 set linebreak([expr $addr+1]) 1
1118 }
dane83267d2013-11-05 14:19:22 +00001119 }
1120
1121 $db eval "explain $sql" {} {
dan427ebba2013-11-05 16:39:31 +00001122 if {[info exists linebreak($addr)]} {
dane83267d2013-11-05 14:19:22 +00001123 puts ""
1124 }
1125 set I [string repeat " " $x($addr)]
dan5c663c32013-11-06 14:52:40 +00001126
1127 set col ""
1128 catch { set col $color($opcode) }
1129
1130 puts [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \
1131 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment
dane83267d2013-11-05 14:19:22 +00001132 ]
dane83267d2013-11-05 14:19:22 +00001133 }
1134 puts "---- ------------ ------ ------ ------ ---------------- -- -"
1135}
1136
drhdafc0ce2008-04-17 19:14:02 +00001137# Show the VDBE program for an SQL statement but omit the Trace
1138# opcode at the beginning. This procedure can be used to prove
1139# that different SQL statements generate exactly the same VDBE code.
1140#
1141proc explain_no_trace {sql} {
1142 set tr [db eval "EXPLAIN $sql"]
1143 return [lrange $tr 7 end]
1144}
1145
drh3aadb2e2000-05-31 17:59:25 +00001146# Another procedure to execute SQL. This one includes the field
1147# names in the returned list.
1148#
1149proc execsql2 {sql} {
1150 set result {}
1151 db eval $sql data {
1152 foreach f $data(*) {
1153 lappend result $f $data($f)
1154 }
1155 }
1156 return $result
1157}
drh17a68932001-01-31 13:28:08 +00001158
mistachkin1d406e02013-08-29 01:09:14 +00001159# Use a temporary in-memory database to execute SQL statements
1160#
1161proc memdbsql {sql} {
1162 sqlite3 memdb :memory:
1163 set result [memdb eval $sql]
1164 memdb close
1165 return $result
1166}
1167
drhdde85d92003-03-01 19:45:34 +00001168# Use the non-callback API to execute multiple SQL statements
1169#
1170proc stepsql {dbptr sql} {
1171 set sql [string trim $sql]
1172 set r 0
1173 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001174 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001175 return [list 1 $vm]
1176 }
1177 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001178# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1179# foreach v $VAL {lappend r $v}
1180# }
1181 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1182 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1183 lappend r [sqlite3_column_text $vm $i]
1184 }
drhdde85d92003-03-01 19:45:34 +00001185 }
danielk1977106bb232004-05-21 10:08:53 +00001186 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001187 return [list 1 $errmsg]
1188 }
1189 }
1190 return $r
1191}
1192
drh21504322002-06-25 13:16:02 +00001193# Do an integrity check of the entire database
1194#
danielk197704103022009-02-03 16:51:24 +00001195proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001196 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001197 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001198 }
1199}
1200
drh433dccf2013-02-09 15:37:11 +00001201# Check the extended error code
1202#
1203proc verify_ex_errcode {name expected {db db}} {
1204 do_test $name [list sqlite3_extended_errcode $db] $expected
1205}
1206
danc6055c72011-04-28 18:46:46 +00001207
1208# Return true if the SQL statement passed as the second argument uses a
1209# statement transaction.
1210#
1211proc sql_uses_stmt {db sql} {
1212 set stmt [sqlite3_prepare $db $sql -1 dummy]
1213 set uses [uses_stmt_journal $stmt]
1214 sqlite3_finalize $stmt
1215 return $uses
1216}
1217
danielk1977db4e8862008-01-16 08:24:46 +00001218proc fix_ifcapable_expr {expr} {
1219 set ret ""
1220 set state 0
1221 for {set i 0} {$i < [string length $expr]} {incr i} {
1222 set char [string range $expr $i $i]
1223 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1224 if {$newstate && !$state} {
1225 append ret {$::sqlite_options(}
1226 }
1227 if {!$newstate && $state} {
1228 append ret )
1229 }
1230 append ret $char
1231 set state $newstate
1232 }
1233 if {$state} {append ret )}
1234 return $ret
1235}
1236
mistachkinc60941f2012-09-13 01:51:02 +00001237# Returns non-zero if the capabilities are present; zero otherwise.
1238#
1239proc capable {expr} {
1240 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1241}
1242
drh27d258a2004-10-30 20:23:09 +00001243# Evaluate a boolean expression of capabilities. If true, execute the
1244# code. Omit the code if false.
1245#
danielk19773e8c37e2005-01-21 03:12:14 +00001246proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001247 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1248 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001249 if ($e2) {
1250 set c [catch {uplevel 1 $code} r]
1251 } else {
1252 set c [catch {uplevel 1 $elsecode} r]
1253 }
1254 return -code $c $r
drh21504322002-06-25 13:16:02 +00001255}
danielk197745901d62004-11-10 15:27:38 +00001256
danielk1977aca790a2005-01-13 11:07:52 +00001257# This proc execs a seperate process that crashes midway through executing
1258# the SQL script $sql on database test.db.
1259#
1260# The crash occurs during a sync() of file $crashfile. When the crash
1261# occurs a random subset of all unsynced writes made by the process are
1262# written into the files on disk. Argument $crashdelay indicates the
1263# number of file syncs to wait before crashing.
1264#
1265# The return value is a list of two elements. The first element is a
1266# boolean, indicating whether or not the process actually crashed or
1267# reported some other error. The second element in the returned list is the
1268# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001269# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001270#
danielk1977f8940ae2007-08-23 11:07:10 +00001271# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001272#
1273proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001274
1275 set blocksize ""
1276 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001277 set prngseed 0
dan999cd082013-12-09 20:42:03 +00001278 set opendb { sqlite3 db test.db -vfs crash }
drhf8587402008-01-08 16:03:49 +00001279 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001280 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001281 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001282 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001283
danielk197759a33f92007-03-17 10:26:59 +00001284 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1285 set z [lindex $args $ii]
1286 set n [string length $z]
1287 set z2 [lindex $args [expr $ii+1]]
1288
1289 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
dan999cd082013-12-09 20:42:03 +00001290 elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001291 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001292 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001293 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001294 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001295 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001296 else { error "Unrecognized option: $z" }
1297 }
1298
1299 if {$crashfile eq ""} {
1300 error "Compulsory option -file missing"
1301 }
1302
mistachkin1d406e02013-08-29 01:09:14 +00001303 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001304 # cfSync(), which can be different then what TCL uses by
1305 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001306 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001307
1308 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001309 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001310 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001311 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk1977933bbd62007-03-17 07:22:42 +00001312
1313 # This block sets the cache size of the main database to 10
1314 # pages. This is done in case the build is configured to omit
1315 # "PRAGMA cache_size".
danec16d982015-04-17 08:36:05 +00001316 if {$opendb!=""} {
1317 puts $f $opendb
1318 puts $f {db eval {SELECT * FROM sqlite_master;}}
1319 puts $f {set bt [btree_from_db db]}
1320 puts $f {btree_set_cache_size $bt 10}
1321 }
dan999cd082013-12-09 20:42:03 +00001322
drhcb1f0f62008-01-08 15:18:52 +00001323 if {$prngseed} {
1324 set seed [expr {$prngseed%10007+1}]
1325 # puts seed=$seed
1326 puts $f "db eval {SELECT randomblob($seed)}"
1327 }
danielk1977933bbd62007-03-17 07:22:42 +00001328
drhf8587402008-01-08 16:03:49 +00001329 if {[string length $tclbody]>0} {
1330 puts $f $tclbody
1331 }
1332 if {[string length $sql]>0} {
1333 puts $f "db eval {"
1334 puts $f "$sql"
1335 puts $f "}"
1336 }
danielk1977aca790a2005-01-13 11:07:52 +00001337 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001338 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001339 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001340 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001341
shanehf2c08822010-07-08 16:30:44 +00001342 # Windows/ActiveState TCL returns a slightly different
1343 # error message. We map that to the expected message
1344 # so that we don't have to change all of the test
1345 # cases.
1346 if {$::tcl_platform(platform)=="windows"} {
1347 if {$msg=="child killed: unknown signal"} {
1348 set msg "child process exited abnormally"
1349 }
1350 }
mistachkin1d406e02013-08-29 01:09:14 +00001351
danielk1977aca790a2005-01-13 11:07:52 +00001352 lappend r $msg
1353}
1354
danb88e24f2013-02-23 18:58:11 +00001355proc run_ioerr_prep {} {
1356 set ::sqlite_io_error_pending 0
1357 catch {db close}
1358 catch {db2 close}
1359 catch {forcedelete test.db}
1360 catch {forcedelete test.db-journal}
1361 catch {forcedelete test2.db}
1362 catch {forcedelete test2.db-journal}
1363 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1364 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1365 if {[info exists ::ioerropts(-tclprep)]} {
1366 eval $::ioerropts(-tclprep)
1367 }
1368 if {[info exists ::ioerropts(-sqlprep)]} {
1369 execsql $::ioerropts(-sqlprep)
1370 }
1371 expr 0
1372}
1373
danielk197732554c12005-01-22 03:39:39 +00001374# Usage: do_ioerr_test <test number> <options...>
1375#
1376# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001377# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001378# used to name the tests executed by this proc. Options are as follows:
1379#
1380# -tclprep TCL script to run to prepare test.
1381# -sqlprep SQL script to run to prepare test.
1382# -tclbody TCL script to run with IO error simulation.
1383# -sqlbody TCL script to run with IO error simulation.
1384# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001385# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001386# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001387# -start Value of 'N' to begin with (default 1)
1388#
1389# -cksum Boolean. If true, test that the database does
1390# not change during the execution of the test case.
1391#
1392proc do_ioerr_test {testname args} {
1393
danielk197732554c12005-01-22 03:39:39 +00001394 set ::ioerropts(-start) 1
1395 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001396 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001397 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001398 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001399 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001400 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001401 array set ::ioerropts $args
1402
danielk197747cd39c2008-05-12 12:41:15 +00001403 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1404 # a couple of obscure IO errors that do not return them.
1405 set ::ioerropts(-erc) 0
mistachkin1d406e02013-08-29 01:09:14 +00001406
danb88e24f2013-02-23 18:58:11 +00001407 # Create a single TCL script from the TCL and SQL specified
1408 # as the body of the test.
1409 set ::ioerrorbody {}
1410 if {[info exists ::ioerropts(-tclbody)]} {
1411 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1412 }
1413 if {[info exists ::ioerropts(-sqlbody)]} {
1414 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1415 }
1416
1417 save_prng_state
1418 if {$::ioerropts(-cksum)} {
1419 run_ioerr_prep
1420 eval $::ioerrorbody
1421 set ::goodcksum [cksum]
1422 }
danielk197747cd39c2008-05-12 12:41:15 +00001423
danielk197732554c12005-01-22 03:39:39 +00001424 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001425 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001426 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001427 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001428 incr ::ioerropts(-count) -1
1429 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001430
danielk197732554c12005-01-22 03:39:39 +00001431 # Skip this IO error if it was specified with the "-exclude" option.
1432 if {[info exists ::ioerropts(-exclude)]} {
1433 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1434 }
danielk1977861f7452008-06-05 11:39:11 +00001435 if {$::ioerropts(-restoreprng)} {
1436 restore_prng_state
1437 }
danielk197732554c12005-01-22 03:39:39 +00001438
mistachkin1d406e02013-08-29 01:09:14 +00001439 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001440 # SQL (in that order) to prepare for the test case.
1441 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001442 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001443 } {0}
1444
1445 # Read the 'checksum' of the database.
1446 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001447 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001448 }
drh93aed5a2008-01-16 17:46:38 +00001449
danielk197732554c12005-01-22 03:39:39 +00001450 # Set the Nth IO error to fail.
1451 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001452 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001453 set ::sqlite_io_error_pending $n
1454 }] $n
danielk197732554c12005-01-22 03:39:39 +00001455
danb88e24f2013-02-23 18:58:11 +00001456 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001457 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001458 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001459 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001460 set ::sqlite_io_error_hit 0
1461 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001462 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001463 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001464 set rc [sqlite3_errcode $::DB]
1465 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001466 # If we are in extended result code mode, make sure all of the
1467 # IOERRs we get back really do have their extended code values.
1468 # If an extended result code is returned, the sqlite3_errcode
1469 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1470 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001471 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1472 return $rc
1473 }
1474 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001475 # If we are not in extended result code mode, make sure no
1476 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001477 if {[regexp {\+\d} $rc]} {
1478 return $rc
1479 }
1480 }
drh93aed5a2008-01-16 17:46:38 +00001481 # The test repeats as long as $::go is non-zero. $::go starts out
1482 # as 1. When a test runs to completion without hitting an I/O
1483 # error, that means there is no point in continuing with this test
1484 # case so set $::go to zero.
1485 #
1486 if {$::sqlite_io_error_pending>0} {
1487 set ::go 0
1488 set q 0
1489 set ::sqlite_io_error_pending 0
1490 } else {
1491 set q 1
1492 }
1493
drhc9ac5ca2005-11-04 22:03:30 +00001494 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001495 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1496 set r 1
1497 }
danielk1977f2fa8312006-01-24 13:09:33 +00001498 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001499
1500 # One of two things must have happened. either
1501 # 1. We never hit the IO error and the SQL returned OK
1502 # 2. An IO error was hit and the SQL failed
1503 #
dand41a29a2010-05-06 15:56:28 +00001504 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001505 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001506 } {1}
1507
danielk197780daec62008-05-12 10:57:02 +00001508 set ::sqlite_io_error_hit 0
1509 set ::sqlite_io_error_pending 0
1510
mistachkin1d406e02013-08-29 01:09:14 +00001511 # Check that no page references were leaked. There should be
1512 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001513 # or zero otherwise.
1514 #
danielk197781620542008-06-07 05:19:37 +00001515 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001516 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001517 # occurs while attempting to detect a hot-journal file), then
1518 # there may 0 page references and an active transaction according
1519 # to [sqlite3_get_autocommit].
1520 #
danielk19774abd5442008-05-05 15:26:50 +00001521 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001522 do_test $testname.$n.4 {
1523 set bt [btree_from_db db]
1524 db_enter db
1525 array set stats [btree_pager_stats $bt]
1526 db_leave db
danielk197781620542008-06-07 05:19:37 +00001527 set nRef $stats(ref)
1528 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1529 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001530 }
1531
mistachkin1d406e02013-08-29 01:09:14 +00001532 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001533 # and the pager is not running in exclusive-locking mode,
1534 # check that the pager is in "unlocked" state. Theoretically,
1535 # if a call to xUnlock() failed due to an IO error the underlying
1536 # file may still be locked.
1537 #
1538 ifcapable pragma {
1539 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001540 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001541 && [db one {pragma locking_mode}] eq "normal"
1542 && [sqlite3_get_autocommit db]
1543 } {
1544 do_test $testname.$n.5 {
1545 set bt [btree_from_db db]
1546 db_enter db
1547 array set stats [btree_pager_stats $bt]
1548 db_leave db
1549 set stats(state)
1550 } 0
1551 }
1552 }
1553
mistachkin48864df2013-03-21 21:20:32 +00001554 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001555 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001556 #
drh1aa5af12008-03-07 19:51:14 +00001557 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001558 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001559 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001560 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001561 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001562 set nowcksum [cksum]
1563 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1564 if {$res==0} {
1565 puts "now=$nowcksum"
1566 puts "the=$::checksum"
1567 puts "fwd=$::goodcksum"
1568 }
1569 set res
1570 } 1
danielk197732554c12005-01-22 03:39:39 +00001571 }
1572
drh1aa5af12008-03-07 19:51:14 +00001573 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001574 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001575 if {[info exists ::ioerropts(-cleanup)]} {
1576 catch $::ioerropts(-cleanup)
1577 }
danielk197732554c12005-01-22 03:39:39 +00001578 }
1579 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001580 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001581 unset ::ioerropts
1582}
1583
drh93aed5a2008-01-16 17:46:38 +00001584# Return a checksum based on the contents of the main database associated
1585# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001586#
1587proc cksum {{db db}} {
1588 set txt [$db eval {
1589 SELECT name, type, sql FROM sqlite_master order by name
1590 }]\n
1591 foreach tbl [$db eval {
1592 SELECT name FROM sqlite_master WHERE type='table' order by name
1593 }] {
1594 append txt [$db eval "SELECT * FROM $tbl"]\n
1595 }
1596 foreach prag {default_synchronous default_cache_size} {
1597 append txt $prag-[$db eval "PRAGMA $prag"]\n
1598 }
1599 set cksum [string length $txt]-[md5 $txt]
1600 # puts $cksum-[file size test.db]
1601 return $cksum
1602}
1603
drh93aed5a2008-01-16 17:46:38 +00001604# Generate a checksum based on the contents of the main and temp tables
1605# database $db. If the checksum of two databases is the same, and the
1606# integrity-check passes for both, the two databases are identical.
1607#
drh1db639c2008-01-17 02:36:28 +00001608proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001609 set ret [list]
1610 ifcapable tempdb {
1611 set sql {
1612 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1613 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1614 SELECT 'sqlite_master' UNION
1615 SELECT 'sqlite_temp_master' ORDER BY 1
1616 }
1617 } else {
1618 set sql {
1619 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1620 SELECT 'sqlite_master' ORDER BY 1
1621 }
1622 }
1623 set tbllist [$db eval $sql]
1624 set txt {}
1625 foreach tbl $tbllist {
1626 append txt [$db eval "SELECT * FROM $tbl"]
1627 }
1628 foreach prag {default_cache_size} {
1629 append txt $prag-[$db eval "PRAGMA $prag"]\n
1630 }
1631 # puts txt=$txt
1632 return [md5 $txt]
1633}
1634
drhdc2c4912009-02-04 22:46:47 +00001635# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00001636# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00001637# Examples of $dbname are "temp" or "main".
1638#
1639proc dbcksum {db dbname} {
1640 if {$dbname=="temp"} {
1641 set master sqlite_temp_master
1642 } else {
1643 set master $dbname.sqlite_master
1644 }
1645 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1646 set txt [$db eval "SELECT * FROM $master"]\n
1647 foreach tab $alltab {
1648 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1649 }
1650 return [md5 $txt]
1651}
1652
danielk197735754ac2008-03-21 17:29:37 +00001653proc memdebug_log_sql {{filename mallocs.sql}} {
1654
danielk19776f332c12008-03-21 14:22:44 +00001655 set data [sqlite3_memdebug_log dump]
1656 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001657 if {$nFrame < 0} { return "" }
1658
danielk197735754ac2008-03-21 17:29:37 +00001659 set database temp
1660
danielk19776ab3a2e2009-02-19 14:39:25 +00001661 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001662
1663 set sql ""
1664 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001665 set nCall [lindex $e 0]
1666 set nByte [lindex $e 1]
1667 set lStack [lrange $e 2 end]
1668 append sql "INSERT INTO ${database}.malloc VALUES"
1669 append sql "('test', $nCall, $nByte, '$lStack');\n"
1670 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001671 set frames($f) 1
1672 }
1673 }
1674
1675 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001676 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001677
1678 foreach f [array names frames] {
1679 set addr [format %x $f]
1680 set cmd "addr2line -e [info nameofexec] $addr"
1681 set line [eval exec $cmd]
1682 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001683
1684 set file [lindex [split $line :] 0]
1685 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001686 }
1687
danielk197735754ac2008-03-21 17:29:37 +00001688 foreach f [array names files] {
1689 set contents ""
1690 catch {
1691 set fd [open $f]
1692 set contents [read $fd]
1693 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001694 }
danielk197735754ac2008-03-21 17:29:37 +00001695 set contents [string map {' ''} $contents]
1696 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001697 }
danielk19776f332c12008-03-21 14:22:44 +00001698
danielk197735754ac2008-03-21 17:29:37 +00001699 set fd [open $filename w]
1700 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1701 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001702}
drh93aed5a2008-01-16 17:46:38 +00001703
danf5894502009-10-07 18:41:19 +00001704# Drop all tables in database [db]
1705proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001706 ifcapable trigger&&foreignkey {
1707 set pk [$db one "PRAGMA foreign_keys"]
1708 $db eval "PRAGMA foreign_keys = OFF"
1709 }
drh9a6ffc82010-02-15 18:03:20 +00001710 foreach {idx name file} [db eval {PRAGMA database_list}] {
1711 if {$idx==1} {
1712 set master sqlite_temp_master
1713 } else {
1714 set master $name.sqlite_master
1715 }
1716 foreach {t type} [$db eval "
1717 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001718 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001719 "] {
dana16d1062010-09-28 17:37:28 +00001720 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001721 }
danf5894502009-10-07 18:41:19 +00001722 }
shaneh4e7b32f2009-12-17 22:12:51 +00001723 ifcapable trigger&&foreignkey {
1724 $db eval "PRAGMA foreign_keys = $pk"
1725 }
danf5894502009-10-07 18:41:19 +00001726}
1727
dan71cb5182010-04-26 12:39:03 +00001728#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001729# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00001730# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1731# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00001732# intrusive:
dan71cb5182010-04-26 12:39:03 +00001733#
1734# wal_set_journal_mode ?DB?
1735#
1736# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1737# connection handle DB. Otherwise, this command is a no-op.
1738#
1739# wal_check_journal_mode TESTNAME ?DB?
1740#
1741# If running a WAL test, execute a tests case that fails if the main
1742# database for connection handle DB is not currently a WAL database.
1743# Otherwise (if not running a WAL permutation) this is a no-op.
1744#
1745# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00001746#
dan71cb5182010-04-26 12:39:03 +00001747# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00001748#
dan71cb5182010-04-26 12:39:03 +00001749proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001750 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001751}
1752proc wal_set_journal_mode {{db db}} {
1753 if { [wal_is_wal_mode] } {
1754 $db eval "PRAGMA journal_mode = WAL"
1755 }
1756}
1757proc wal_check_journal_mode {testname {db db}} {
1758 if { [wal_is_wal_mode] } {
1759 $db eval { SELECT * FROM sqlite_master }
1760 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1761 }
1762}
1763
dan430e74c2010-06-07 17:47:26 +00001764proc permutation {} {
1765 set perm ""
1766 catch {set perm $::G(perm:name)}
1767 set perm
1768}
dancb79e512010-08-06 13:50:07 +00001769proc presql {} {
1770 set presql ""
1771 catch {set presql $::G(perm:presql)}
1772 set presql
1773}
dan430e74c2010-06-07 17:47:26 +00001774
danc1a60c52010-06-07 14:28:16 +00001775#-------------------------------------------------------------------------
1776#
1777proc slave_test_script {script} {
1778
1779 # Create the interpreter used to run the test script.
1780 interp create tinterp
1781
1782 # Populate some global variables that tester.tcl expects to see.
1783 foreach {var value} [list \
1784 ::argv0 $::argv0 \
1785 ::argv {} \
1786 ::SLAVE 1 \
1787 ] {
1788 interp eval tinterp [list set $var $value]
1789 }
1790
1791 # The alias used to access the global test counters.
1792 tinterp alias set_test_counter set_test_counter
1793
1794 # Set up the ::cmdlinearg array in the slave.
1795 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1796
dan430e74c2010-06-07 17:47:26 +00001797 # Set up the ::G array in the slave.
1798 interp eval tinterp [list array set ::G [array get ::G]]
1799
danc1a60c52010-06-07 14:28:16 +00001800 # Load the various test interfaces implemented in C.
1801 load_testfixture_extensions tinterp
1802
1803 # Run the test script.
1804 interp eval tinterp $script
1805
danea5542d2010-07-06 11:26:15 +00001806 # Check if the interpreter call [run_thread_tests]
1807 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1808 set ::run_thread_tests_called 1
1809 }
1810
danc1a60c52010-06-07 14:28:16 +00001811 # Delete the interpreter used to run the test script.
1812 interp delete tinterp
1813}
1814
dan430e74c2010-06-07 17:47:26 +00001815proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001816 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001817
dan6a64d672011-04-04 15:38:16 +00001818 if {[info exists ::G(start:permutation)]} {
1819 if {[permutation] != $::G(start:permutation)} return
1820 unset ::G(start:permutation)
1821 }
1822 if {[info exists ::G(start:file)]} {
1823 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1824 unset ::G(start:file)
1825 }
1826
dan92d516a2010-07-05 14:54:48 +00001827 # Remember the value of the shared-cache setting. So that it is possible
1828 # to check afterwards that it was not modified by the test script.
1829 #
1830 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001831
dan92d516a2010-07-05 14:54:48 +00001832 # Run the test script in a slave interpreter.
1833 #
danea5542d2010-07-06 11:26:15 +00001834 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001835 reset_prng_state
1836 set ::sqlite_open_file_count 0
1837 set time [time { slave_test_script [list source $zFile] }]
1838 set ms [expr [lindex $time 0] / 1000]
1839
dan92d516a2010-07-05 14:54:48 +00001840 # Test that all files opened by the test script were closed. Omit this
1841 # if the test script has "thread" in its name. The open file counter
1842 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001843 #
danea5542d2010-07-06 11:26:15 +00001844 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001845 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1846 }
dan430e74c2010-06-07 17:47:26 +00001847 set ::sqlite_open_file_count 0
1848
mistachkin1d406e02013-08-29 01:09:14 +00001849 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00001850 # the test script.
1851 #
mistachkin1d406e02013-08-29 01:09:14 +00001852 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00001853 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1854 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1855 }
dan430e74c2010-06-07 17:47:26 +00001856
dan92d516a2010-07-05 14:54:48 +00001857 # Add some info to the output.
1858 #
dan0626dfc2010-06-15 06:56:37 +00001859 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001860 show_memstats
danc1a60c52010-06-07 14:28:16 +00001861}
1862
dan59257dc2010-08-04 11:34:31 +00001863# Open a new connection on database test.db and execute the SQL script
1864# supplied as an argument. Before returning, close the new conection and
1865# restore the 4 byte fields starting at header offsets 28, 92 and 96
1866# to the values they held before the SQL was executed. This simulates
1867# a write by a pre-3.7.0 client.
1868#
1869proc sql36231 {sql} {
1870 set B [hexio_read test.db 92 8]
1871 set A [hexio_read test.db 28 4]
1872 sqlite3 db36231 test.db
1873 catch { db36231 func a_string a_string }
1874 execsql $sql db36231
1875 db36231 close
1876 hexio_write test.db 28 $A
1877 hexio_write test.db 92 $B
1878 return ""
1879}
danf5894502009-10-07 18:41:19 +00001880
danccc7ff42010-11-29 12:06:45 +00001881proc db_save {} {
1882 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1883 foreach f [glob -nocomplain test.db*] {
1884 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00001885 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00001886 }
1887}
1888proc db_save_and_close {} {
1889 db_save
1890 catch { db close }
1891 return ""
1892}
1893proc db_restore {} {
1894 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1895 foreach f2 [glob -nocomplain sv_test.db*] {
1896 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00001897 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00001898 }
1899}
1900proc db_restore_and_reopen {{dbfile test.db}} {
1901 catch { db close }
1902 db_restore
1903 sqlite3 db $dbfile
1904}
1905proc db_delete_and_reopen {{file test.db}} {
1906 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00001907 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00001908 sqlite3 db $file
1909}
1910
danielk197745901d62004-11-10 15:27:38 +00001911# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1912# to non-zero, then set the global variable $AUTOVACUUM to 1.
1913set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001914
dan64b95bb2012-05-12 05:30:29 +00001915# Make sure the FTS enhanced query syntax is disabled.
1916set sqlite_fts3_enable_parentheses 0
1917
drh09fe6142013-11-29 15:06:27 +00001918# During testing, assume that all database files are well-formed. The
1919# few test cases that deliberately corrupt database files should rescind
1920# this setting by invoking "database_can_be_corrupt"
1921#
1922database_never_corrupt
1923
danielk1977ee8b7992009-03-26 17:13:06 +00001924source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001925source $testdir/malloc_common.tcl