blob: 535c8ed90fde1576360a699d02d2ba86819af4b5 [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
dan67340072011-04-16 19:23:10 +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
mistachkinf21979d2015-01-18 05:35:01 +0000669proc dumpbytes {s} {
670 set r ""
671 for {set i 0} {$i < [string length $s]} {incr i} {
672 if {$i > 0} {append r " "}
673 append r [format %02X [scan [string index $s $i] %c]]
674 }
675 return $r
676}
677
drh8df91852012-04-24 12:46:05 +0000678proc catchcmd {db {cmd ""}} {
679 global CLI
680 set out [open cmds.txt w]
681 puts $out $cmd
682 close $out
683 set line "exec $CLI $db < cmds.txt"
684 set rc [catch { eval $line } msg]
685 list $rc $msg
686}
687
mistachkinf21979d2015-01-18 05:35:01 +0000688proc catchcmdex {db {cmd ""}} {
689 global CLI
690 set out [open cmds.txt w]
691 fconfigure $out -encoding binary -translation binary
692 puts -nonewline $out $cmd
693 close $out
694 set line "exec -keepnewline -- $CLI $db < cmds.txt"
695 set chans [list stdin stdout stderr]
696 foreach chan $chans {
697 catch {
698 set modes($chan) [fconfigure $chan]
699 fconfigure $chan -encoding binary -translation binary -buffering none
700 }
701 }
702 set rc [catch { eval $line } msg]
703 foreach chan $chans {
704 catch {
705 eval fconfigure [list $chan] $modes($chan)
706 }
707 }
708 # puts [dumpbytes $msg]
709 list $rc $msg
710}
711
shaneh55505172011-06-21 19:30:19 +0000712proc filepath_normalize {p} {
713 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000714 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000715 # lreverse*2 as a hack to remove any unneeded {} after the string map
716 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000717 } {
718 set p
shaneh55505172011-06-21 19:30:19 +0000719 }
720}
721proc do_filepath_test {name cmd expected} {
722 uplevel [list do_test $name [
723 subst -nocommands { filepath_normalize [ $cmd ] }
724 ] [filepath_normalize $expected]]
725}
726
dan33f53792011-05-05 19:44:22 +0000727proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000728 # different TCL versions display floating point values differently.
729 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000730}
731proc do_realnum_test {name cmd expected} {
732 uplevel [list do_test $name [
733 subst -nocommands { realnum_normalize [ $cmd ] }
734 ] [realnum_normalize $expected]]
735}
736
dana3f51082010-09-30 18:43:14 +0000737proc fix_testname {varname} {
738 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000739 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000740 && [string is digit [string range $testname 0 0]]
741 } {
742 set testname "${::testprefix}-$testname"
743 }
744}
dan67340072011-04-16 19:23:10 +0000745
746proc normalize_list {L} {
747 set L2 [list]
748 foreach l $L {lappend L2 $l}
749 set L2
750}
drh5f9742e2013-08-29 15:08:38 +0000751
dan57f7f4b2010-10-01 19:04:37 +0000752proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000753 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000754 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000755}
756proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000757 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000758 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000759}
drh3c2aeae2014-01-24 14:37:44 +0000760proc do_timed_execsql_test {testname sql {result {}}} {
761 fix_testname testname
762 uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\
763 [list [list {*}$result]]
764}
dan39854792010-11-15 16:12:58 +0000765proc do_eqp_test {name sql res} {
766 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
767}
dan153eda02010-06-21 07:45:47 +0000768
dan51f06982010-09-18 19:00:12 +0000769#-------------------------------------------------------------------------
770# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
771#
772# Where switches are:
773#
774# -errorformat FMTSTRING
775# -count
danaf1dcab2010-09-20 19:17:53 +0000776# -query SQL
dana16d1062010-09-28 17:37:28 +0000777# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000778# -repair TCL
dan51f06982010-09-18 19:00:12 +0000779#
780proc do_select_tests {prefix args} {
781
782 set testlist [lindex $args end]
783 set switches [lrange $args 0 end-1]
784
785 set errfmt ""
786 set countonly 0
dana16d1062010-09-28 17:37:28 +0000787 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000788 set repair ""
dan51f06982010-09-18 19:00:12 +0000789
790 for {set i 0} {$i < [llength $switches]} {incr i} {
791 set s [lindex $switches $i]
792 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000793 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000794 set tclquery [list execsql [lindex $switches [incr i]]]
795 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
796 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000797 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000798 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000799 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
800 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000801 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
802 set countonly 1
803 } else {
804 error "unknown switch: $s"
805 }
806 }
807
808 if {$countonly && $errfmt!=""} {
809 error "Cannot use -count and -errorformat together"
810 }
811 set nTestlist [llength $testlist]
812 if {$nTestlist%3 || $nTestlist==0 } {
813 error "SELECT test list contains [llength $testlist] elements"
814 }
815
danaf7626f2010-09-23 18:47:36 +0000816 eval $repair
dan51f06982010-09-18 19:00:12 +0000817 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000818 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000819 execsql $sql
dana16d1062010-09-28 17:37:28 +0000820 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
821 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000822 set nRow 0
823 db eval $sql {incr nRow}
824 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
825 } elseif {$errfmt==""} {
826 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
827 } else {
828 set res [list 1 [string trim [format $errfmt {*}$res]]]
829 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
830 }
danaf7626f2010-09-23 18:47:36 +0000831 eval $repair
dan51f06982010-09-18 19:00:12 +0000832 }
danaf7626f2010-09-23 18:47:36 +0000833
dan51f06982010-09-18 19:00:12 +0000834}
835
danb04757a2010-09-21 16:59:16 +0000836proc delete_all_data {} {
837 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
838 db eval "DELETE FROM '[string map {' ''} $t]'"
839 }
840}
drh348784e2000-05-29 20:41:49 +0000841
mistachkin1d406e02013-08-29 01:09:14 +0000842# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +0000843# Return the number of microseconds per statement.
844#
drh3590f152006-11-23 21:09:10 +0000845proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000846 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000847 flush stdout
848 set speed [time {sqlite3_exec_nr db $sql}]
849 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000850 if {$tm == 0} {
851 set rate [format %20s "many"]
852 } else {
853 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
854 }
drh3590f152006-11-23 21:09:10 +0000855 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000856 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000857 global total_time
858 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000859 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000860}
drh45c236d2008-03-22 01:08:00 +0000861proc speed_trial_tcl {name numstmt units script} {
862 puts -nonewline [format {%-21.21s } $name...]
863 flush stdout
864 set speed [time {eval $script}]
865 set tm [lindex $speed 0]
866 if {$tm == 0} {
867 set rate [format %20s "many"]
868 } else {
869 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
870 }
871 set u2 $units/s
872 puts [format {%12d uS %s %s} $tm $rate $u2]
873 global total_time
874 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000875 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000876}
drhdd924312007-03-31 22:34:16 +0000877proc speed_trial_init {name} {
878 global total_time
879 set total_time 0
dana975b592010-10-08 16:09:43 +0000880 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000881 sqlite3 versdb :memory:
882 set vers [versdb one {SELECT sqlite_source_id()}]
883 versdb close
884 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000885}
886proc speed_trial_summary {name} {
887 global total_time
888 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000889
890 if { 0 } {
891 sqlite3 versdb :memory:
892 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
893 versdb close
894 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
895 foreach {test us} $::speed_trial_times {
896 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
897 }
898 }
drhb62c3352006-11-23 09:39:16 +0000899}
900
drh348784e2000-05-29 20:41:49 +0000901# Run this routine last
902#
903proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000904 catch {db close}
dane8559832014-07-31 17:35:40 +0000905 catch {db1 close}
danc1a60c52010-06-07 14:28:16 +0000906 catch {db2 close}
907 catch {db3 close}
908 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000909}
910proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000911 global sqlite_open_file_count
912
913 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000914
drh6e142f52000-06-08 13:36:40 +0000915 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000916 catch {db2 close}
917 catch {db3 close}
918
drh9bc54492007-10-23 14:49:59 +0000919 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000920 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000921 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000922 db close
drh984bfaa2008-03-19 16:08:53 +0000923 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000924
drh3a7fb7c2007-08-10 16:41:08 +0000925 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000926 set nTest [incr_ntest]
927 set nErr [set_test_counter errors]
928
drhef866372013-05-22 20:49:02 +0000929 set nKnown 0
930 if {[file readable known-problems.txt]} {
931 set fd [open known-problems.txt]
932 set content [read $fd]
933 close $fd
934 foreach x $content {set known_error($x) 1}
935 foreach x [set_test_counter fail_list] {
936 if {[info exists known_error($x)]} {incr nKnown}
937 }
938 }
939 if {$nKnown>0} {
940 puts "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
941 out of $nTest tests"
942 } else {
943 puts "$nErr errors out of $nTest tests"
944 }
945 if {$nErr>$nKnown} {
946 puts -nonewline "Failures on these tests:"
947 foreach x [set_test_counter fail_list] {
948 if {![info exists known_error($x)]} {puts -nonewline " $x"}
949 }
950 puts ""
drhf3a65f72007-08-22 20:18:21 +0000951 }
drhcca17c32013-04-19 12:32:52 +0000952 foreach warning [set_test_counter warn_list] {
953 puts "Warning: $warning"
954 }
danielk1977ee8b7992009-03-26 17:13:06 +0000955 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000956 if {[llength $omitList]>0} {
957 puts "Omitted test cases:"
958 set prec {}
959 foreach {rec} [lsort $omitList] {
960 if {$rec==$prec} continue
961 set prec $rec
962 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
963 }
964 }
drh80788d82006-09-02 14:50:23 +0000965 if {$nErr>0 && ![working_64bit_int]} {
966 puts "******************************************************************"
967 puts "N.B.: The version of TCL that you used to build this test harness"
968 puts "is defective in that it does not support 64-bit integers. Some or"
969 puts "all of the test failures above might be a result from this defect"
970 puts "in your TCL build."
971 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000972 }
danc1a60c52010-06-07 14:28:16 +0000973 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000974 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000975 }
drh94e92032003-02-16 22:21:32 +0000976 if {$sqlite_open_file_count} {
977 puts "$sqlite_open_file_count files were left open"
978 incr nErr
979 }
drheafc43b2010-07-26 18:43:40 +0000980 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
981 [sqlite3_memory_used]>0} {
982 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
983 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000984 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000985 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000986 puts "Writing unfreed memory log to \"./memleak.txt\""
987 sqlite3_memdebug_dump ./memleak.txt
988 }
drhf3a65f72007-08-22 20:18:21 +0000989 } else {
990 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000991 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000992 sqlite3_memdebug_dump ./memusage.txt
993 }
drhf3a65f72007-08-22 20:18:21 +0000994 }
drhf7141992008-06-19 00:16:08 +0000995 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000996 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
997 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000998 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
999 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
1000 }
danc1a60c52010-06-07 14:28:16 +00001001 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +00001002 puts "Writing mallocs.sql..."
1003 memdebug_log_sql
1004 sqlite3_memdebug_log stop
1005 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +00001006
1007 if {[sqlite3_memory_used]>0} {
1008 puts "Writing leaks.sql..."
1009 sqlite3_memdebug_log sync
1010 memdebug_log_sql leaks.sql
1011 }
danielk197735754ac2008-03-21 17:29:37 +00001012 }
drhd9910fe2006-10-04 11:55:49 +00001013 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +00001014 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001015 }
1016 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +00001017 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001018 }
drhdb25e382001-03-15 18:21:22 +00001019 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +00001020}
1021
drhf7141992008-06-19 00:16:08 +00001022# Display memory statistics for analysis and debugging purposes.
1023#
1024proc show_memstats {} {
1025 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +00001026 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
1027 set val [format {now %10d max %10d max-size %10d} \
1028 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +00001029 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +00001030 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
1031 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
1032 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +00001033 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +00001034 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
1035 set val [format {now %10d max %10d max-size %10d} \
1036 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +00001037 puts "Page-cache used: $val"
1038 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
1039 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
1040 puts "Page-cache overflow: $val"
1041 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
1042 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
1043 puts "Scratch memory used: $val"
1044 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +00001045 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
1046 set val [format {now %10d max %10d max-size %10d} \
1047 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +00001048 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +00001049 ifcapable yytrackmaxstackdepth {
1050 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
1051 set val [format { max %10d} [lindex $x 2]]
1052 puts "Parser stack depth: $val"
1053 }
drhf7141992008-06-19 00:16:08 +00001054}
1055
drh348784e2000-05-29 20:41:49 +00001056# A procedure to execute SQL
1057#
drhc4a3c772001-04-04 11:48:57 +00001058proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001059 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001060 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001061}
drh3c2aeae2014-01-24 14:37:44 +00001062proc execsql_timed {sql {db db}} {
1063 set tm [time {
1064 set x [uplevel [list $db eval $sql]]
1065 } 1]
1066 set tm [lindex $tm 0]
1067 puts -nonewline " ([expr {$tm*0.001}]ms) "
1068 set x
1069}
drh3aadb2e2000-05-31 17:59:25 +00001070
drhadbca9c2001-09-27 15:11:53 +00001071# Execute SQL and catch exceptions.
1072#
1073proc catchsql {sql {db db}} {
1074 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001075 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001076 lappend r $msg
1077 return $r
1078}
1079
drh04096482001-11-09 22:41:44 +00001080# Do an VDBE code dump on the SQL given
1081#
1082proc explain {sql {db db}} {
1083 puts ""
danielk1977287fb612008-01-04 19:10:28 +00001084 puts "addr opcode p1 p2 p3 p4 p5 #"
1085 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001086 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +00001087 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
1088 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1089 ]
drh04096482001-11-09 22:41:44 +00001090 }
1091}
1092
dane83267d2013-11-05 14:19:22 +00001093proc explain_i {sql {db db}} {
1094 puts ""
1095 puts "addr opcode p1 p2 p3 p4 p5 #"
1096 puts "---- ------------ ------ ------ ------ ---------------- -- -"
1097
dane83267d2013-11-05 14:19:22 +00001098
dan5c663c32013-11-06 14:52:40 +00001099 # Set up colors for the different opcodes. Scheme is as follows:
1100 #
1101 # Red: Opcodes that write to a b-tree.
1102 # Blue: Opcodes that reposition or seek a cursor.
1103 # Green: The ResultRow opcode.
1104 #
dandea63f22014-03-03 16:48:47 +00001105 if { [catch {fconfigure stdout -mode}]==0 } {
1106 set R "\033\[31;1m" ;# Red fg
1107 set G "\033\[32;1m" ;# Green fg
1108 set B "\033\[34;1m" ;# Red fg
1109 set D "\033\[39;0m" ;# Default fg
1110 } else {
1111 set R ""
1112 set G ""
1113 set B ""
1114 set D ""
1115 }
dan5c663c32013-11-06 14:52:40 +00001116 foreach opcode {
1117 Seek SeekGe SeekGt SeekLe SeekLt NotFound Last Rewind
dane3ab7292013-11-12 12:30:09 +00001118 NoConflict Next Prev VNext VPrev VFilter
drhf45f2322014-03-23 17:45:03 +00001119 SorterSort SorterNext
dan5c663c32013-11-06 14:52:40 +00001120 } {
1121 set color($opcode) $B
1122 }
1123 foreach opcode {ResultRow} {
1124 set color($opcode) $G
1125 }
1126 foreach opcode {IdxInsert Insert Delete IdxDelete} {
1127 set color($opcode) $R
1128 }
1129
dan427ebba2013-11-05 16:39:31 +00001130 set bSeenGoto 0
dane83267d2013-11-05 14:19:22 +00001131 $db eval "explain $sql" {} {
1132 set x($addr) 0
1133 set op($addr) $opcode
1134
dan427ebba2013-11-05 16:39:31 +00001135 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} {
1136 set linebreak($p2) 1
1137 set bSeenGoto 1
dane83267d2013-11-05 14:19:22 +00001138 }
1139
dane3ab7292013-11-12 12:30:09 +00001140 if {$opcode=="Next" || $opcode=="Prev"
1141 || $opcode=="VNext" || $opcode=="VPrev"
drhf45f2322014-03-23 17:45:03 +00001142 || $opcode=="SorterNext"
dane3ab7292013-11-12 12:30:09 +00001143 } {
dane83267d2013-11-05 14:19:22 +00001144 for {set i $p2} {$i<$addr} {incr i} {
1145 incr x($i) 2
1146 }
1147 }
1148
1149 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1150 for {set i [expr $p2+1]} {$i<$addr} {incr i} {
1151 incr x($i) 2
1152 }
1153 }
dan427ebba2013-11-05 16:39:31 +00001154
1155 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1156 set linebreak([expr $addr+1]) 1
1157 }
dane83267d2013-11-05 14:19:22 +00001158 }
1159
1160 $db eval "explain $sql" {} {
dan427ebba2013-11-05 16:39:31 +00001161 if {[info exists linebreak($addr)]} {
dane83267d2013-11-05 14:19:22 +00001162 puts ""
1163 }
1164 set I [string repeat " " $x($addr)]
dan5c663c32013-11-06 14:52:40 +00001165
1166 set col ""
1167 catch { set col $color($opcode) }
1168
1169 puts [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \
1170 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment
dane83267d2013-11-05 14:19:22 +00001171 ]
dane83267d2013-11-05 14:19:22 +00001172 }
1173 puts "---- ------------ ------ ------ ------ ---------------- -- -"
1174}
1175
drhdafc0ce2008-04-17 19:14:02 +00001176# Show the VDBE program for an SQL statement but omit the Trace
1177# opcode at the beginning. This procedure can be used to prove
1178# that different SQL statements generate exactly the same VDBE code.
1179#
1180proc explain_no_trace {sql} {
1181 set tr [db eval "EXPLAIN $sql"]
1182 return [lrange $tr 7 end]
1183}
1184
drh3aadb2e2000-05-31 17:59:25 +00001185# Another procedure to execute SQL. This one includes the field
1186# names in the returned list.
1187#
1188proc execsql2 {sql} {
1189 set result {}
1190 db eval $sql data {
1191 foreach f $data(*) {
1192 lappend result $f $data($f)
1193 }
1194 }
1195 return $result
1196}
drh17a68932001-01-31 13:28:08 +00001197
mistachkin1d406e02013-08-29 01:09:14 +00001198# Use a temporary in-memory database to execute SQL statements
1199#
1200proc memdbsql {sql} {
1201 sqlite3 memdb :memory:
1202 set result [memdb eval $sql]
1203 memdb close
1204 return $result
1205}
1206
drhdde85d92003-03-01 19:45:34 +00001207# Use the non-callback API to execute multiple SQL statements
1208#
1209proc stepsql {dbptr sql} {
1210 set sql [string trim $sql]
1211 set r 0
1212 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001213 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001214 return [list 1 $vm]
1215 }
1216 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001217# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1218# foreach v $VAL {lappend r $v}
1219# }
1220 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1221 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1222 lappend r [sqlite3_column_text $vm $i]
1223 }
drhdde85d92003-03-01 19:45:34 +00001224 }
danielk1977106bb232004-05-21 10:08:53 +00001225 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001226 return [list 1 $errmsg]
1227 }
1228 }
1229 return $r
1230}
1231
drh21504322002-06-25 13:16:02 +00001232# Do an integrity check of the entire database
1233#
danielk197704103022009-02-03 16:51:24 +00001234proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001235 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001236 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001237 }
1238}
1239
drh433dccf2013-02-09 15:37:11 +00001240# Check the extended error code
1241#
1242proc verify_ex_errcode {name expected {db db}} {
1243 do_test $name [list sqlite3_extended_errcode $db] $expected
1244}
1245
danc6055c72011-04-28 18:46:46 +00001246
1247# Return true if the SQL statement passed as the second argument uses a
1248# statement transaction.
1249#
1250proc sql_uses_stmt {db sql} {
1251 set stmt [sqlite3_prepare $db $sql -1 dummy]
1252 set uses [uses_stmt_journal $stmt]
1253 sqlite3_finalize $stmt
1254 return $uses
1255}
1256
danielk1977db4e8862008-01-16 08:24:46 +00001257proc fix_ifcapable_expr {expr} {
1258 set ret ""
1259 set state 0
1260 for {set i 0} {$i < [string length $expr]} {incr i} {
1261 set char [string range $expr $i $i]
1262 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1263 if {$newstate && !$state} {
1264 append ret {$::sqlite_options(}
1265 }
1266 if {!$newstate && $state} {
1267 append ret )
1268 }
1269 append ret $char
1270 set state $newstate
1271 }
1272 if {$state} {append ret )}
1273 return $ret
1274}
1275
mistachkinc60941f2012-09-13 01:51:02 +00001276# Returns non-zero if the capabilities are present; zero otherwise.
1277#
1278proc capable {expr} {
1279 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1280}
1281
drh27d258a2004-10-30 20:23:09 +00001282# Evaluate a boolean expression of capabilities. If true, execute the
1283# code. Omit the code if false.
1284#
danielk19773e8c37e2005-01-21 03:12:14 +00001285proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001286 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1287 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001288 if ($e2) {
1289 set c [catch {uplevel 1 $code} r]
1290 } else {
1291 set c [catch {uplevel 1 $elsecode} r]
1292 }
1293 return -code $c $r
drh21504322002-06-25 13:16:02 +00001294}
danielk197745901d62004-11-10 15:27:38 +00001295
danielk1977aca790a2005-01-13 11:07:52 +00001296# This proc execs a seperate process that crashes midway through executing
1297# the SQL script $sql on database test.db.
1298#
1299# The crash occurs during a sync() of file $crashfile. When the crash
1300# occurs a random subset of all unsynced writes made by the process are
1301# written into the files on disk. Argument $crashdelay indicates the
1302# number of file syncs to wait before crashing.
1303#
1304# The return value is a list of two elements. The first element is a
1305# boolean, indicating whether or not the process actually crashed or
1306# reported some other error. The second element in the returned list is the
1307# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001308# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001309#
danielk1977f8940ae2007-08-23 11:07:10 +00001310# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001311#
1312proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001313
1314 set blocksize ""
1315 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001316 set prngseed 0
dan999cd082013-12-09 20:42:03 +00001317 set opendb { sqlite3 db test.db -vfs crash }
drhf8587402008-01-08 16:03:49 +00001318 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001319 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001320 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001321 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001322
danielk197759a33f92007-03-17 10:26:59 +00001323 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1324 set z [lindex $args $ii]
1325 set n [string length $z]
1326 set z2 [lindex $args [expr $ii+1]]
1327
1328 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
dan999cd082013-12-09 20:42:03 +00001329 elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001330 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001331 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001332 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001333 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001334 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001335 else { error "Unrecognized option: $z" }
1336 }
1337
1338 if {$crashfile eq ""} {
1339 error "Compulsory option -file missing"
1340 }
1341
mistachkin1d406e02013-08-29 01:09:14 +00001342 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001343 # cfSync(), which can be different then what TCL uses by
1344 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001345 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001346
1347 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001348 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001349 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001350 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk1977933bbd62007-03-17 07:22:42 +00001351
1352 # This block sets the cache size of the main database to 10
1353 # pages. This is done in case the build is configured to omit
1354 # "PRAGMA cache_size".
danec16d982015-04-17 08:36:05 +00001355 if {$opendb!=""} {
1356 puts $f $opendb
1357 puts $f {db eval {SELECT * FROM sqlite_master;}}
1358 puts $f {set bt [btree_from_db db]}
1359 puts $f {btree_set_cache_size $bt 10}
1360 }
dan999cd082013-12-09 20:42:03 +00001361
drhcb1f0f62008-01-08 15:18:52 +00001362 if {$prngseed} {
1363 set seed [expr {$prngseed%10007+1}]
1364 # puts seed=$seed
1365 puts $f "db eval {SELECT randomblob($seed)}"
1366 }
danielk1977933bbd62007-03-17 07:22:42 +00001367
drhf8587402008-01-08 16:03:49 +00001368 if {[string length $tclbody]>0} {
1369 puts $f $tclbody
1370 }
1371 if {[string length $sql]>0} {
1372 puts $f "db eval {"
1373 puts $f "$sql"
1374 puts $f "}"
1375 }
danielk1977aca790a2005-01-13 11:07:52 +00001376 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001377 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001378 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001379 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001380
shanehf2c08822010-07-08 16:30:44 +00001381 # Windows/ActiveState TCL returns a slightly different
1382 # error message. We map that to the expected message
1383 # so that we don't have to change all of the test
1384 # cases.
1385 if {$::tcl_platform(platform)=="windows"} {
1386 if {$msg=="child killed: unknown signal"} {
1387 set msg "child process exited abnormally"
1388 }
1389 }
mistachkin1d406e02013-08-29 01:09:14 +00001390
danielk1977aca790a2005-01-13 11:07:52 +00001391 lappend r $msg
1392}
1393
danb88e24f2013-02-23 18:58:11 +00001394proc run_ioerr_prep {} {
1395 set ::sqlite_io_error_pending 0
1396 catch {db close}
1397 catch {db2 close}
1398 catch {forcedelete test.db}
1399 catch {forcedelete test.db-journal}
1400 catch {forcedelete test2.db}
1401 catch {forcedelete test2.db-journal}
1402 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1403 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1404 if {[info exists ::ioerropts(-tclprep)]} {
1405 eval $::ioerropts(-tclprep)
1406 }
1407 if {[info exists ::ioerropts(-sqlprep)]} {
1408 execsql $::ioerropts(-sqlprep)
1409 }
1410 expr 0
1411}
1412
danielk197732554c12005-01-22 03:39:39 +00001413# Usage: do_ioerr_test <test number> <options...>
1414#
1415# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001416# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001417# used to name the tests executed by this proc. Options are as follows:
1418#
1419# -tclprep TCL script to run to prepare test.
1420# -sqlprep SQL script to run to prepare test.
1421# -tclbody TCL script to run with IO error simulation.
1422# -sqlbody TCL script to run with IO error simulation.
1423# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001424# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001425# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001426# -start Value of 'N' to begin with (default 1)
1427#
1428# -cksum Boolean. If true, test that the database does
1429# not change during the execution of the test case.
1430#
1431proc do_ioerr_test {testname args} {
1432
danielk197732554c12005-01-22 03:39:39 +00001433 set ::ioerropts(-start) 1
1434 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001435 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001436 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001437 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001438 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001439 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001440 array set ::ioerropts $args
1441
danielk197747cd39c2008-05-12 12:41:15 +00001442 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1443 # a couple of obscure IO errors that do not return them.
1444 set ::ioerropts(-erc) 0
mistachkin1d406e02013-08-29 01:09:14 +00001445
danb88e24f2013-02-23 18:58:11 +00001446 # Create a single TCL script from the TCL and SQL specified
1447 # as the body of the test.
1448 set ::ioerrorbody {}
1449 if {[info exists ::ioerropts(-tclbody)]} {
1450 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1451 }
1452 if {[info exists ::ioerropts(-sqlbody)]} {
1453 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1454 }
1455
1456 save_prng_state
1457 if {$::ioerropts(-cksum)} {
1458 run_ioerr_prep
1459 eval $::ioerrorbody
1460 set ::goodcksum [cksum]
1461 }
danielk197747cd39c2008-05-12 12:41:15 +00001462
danielk197732554c12005-01-22 03:39:39 +00001463 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001464 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001465 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001466 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001467 incr ::ioerropts(-count) -1
1468 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001469
danielk197732554c12005-01-22 03:39:39 +00001470 # Skip this IO error if it was specified with the "-exclude" option.
1471 if {[info exists ::ioerropts(-exclude)]} {
1472 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1473 }
danielk1977861f7452008-06-05 11:39:11 +00001474 if {$::ioerropts(-restoreprng)} {
1475 restore_prng_state
1476 }
danielk197732554c12005-01-22 03:39:39 +00001477
mistachkin1d406e02013-08-29 01:09:14 +00001478 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001479 # SQL (in that order) to prepare for the test case.
1480 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001481 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001482 } {0}
1483
1484 # Read the 'checksum' of the database.
1485 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001486 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001487 }
drh93aed5a2008-01-16 17:46:38 +00001488
danielk197732554c12005-01-22 03:39:39 +00001489 # Set the Nth IO error to fail.
1490 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001491 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001492 set ::sqlite_io_error_pending $n
1493 }] $n
danielk197732554c12005-01-22 03:39:39 +00001494
danb88e24f2013-02-23 18:58:11 +00001495 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001496 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001497 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001498 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001499 set ::sqlite_io_error_hit 0
1500 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001501 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001502 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001503 set rc [sqlite3_errcode $::DB]
1504 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001505 # If we are in extended result code mode, make sure all of the
1506 # IOERRs we get back really do have their extended code values.
1507 # If an extended result code is returned, the sqlite3_errcode
1508 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1509 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001510 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1511 return $rc
1512 }
1513 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001514 # If we are not in extended result code mode, make sure no
1515 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001516 if {[regexp {\+\d} $rc]} {
1517 return $rc
1518 }
1519 }
drh93aed5a2008-01-16 17:46:38 +00001520 # The test repeats as long as $::go is non-zero. $::go starts out
1521 # as 1. When a test runs to completion without hitting an I/O
1522 # error, that means there is no point in continuing with this test
1523 # case so set $::go to zero.
1524 #
1525 if {$::sqlite_io_error_pending>0} {
1526 set ::go 0
1527 set q 0
1528 set ::sqlite_io_error_pending 0
1529 } else {
1530 set q 1
1531 }
1532
drhc9ac5ca2005-11-04 22:03:30 +00001533 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001534 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1535 set r 1
1536 }
danielk1977f2fa8312006-01-24 13:09:33 +00001537 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001538
1539 # One of two things must have happened. either
1540 # 1. We never hit the IO error and the SQL returned OK
1541 # 2. An IO error was hit and the SQL failed
1542 #
dand41a29a2010-05-06 15:56:28 +00001543 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001544 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001545 } {1}
1546
danielk197780daec62008-05-12 10:57:02 +00001547 set ::sqlite_io_error_hit 0
1548 set ::sqlite_io_error_pending 0
1549
mistachkin1d406e02013-08-29 01:09:14 +00001550 # Check that no page references were leaked. There should be
1551 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001552 # or zero otherwise.
1553 #
danielk197781620542008-06-07 05:19:37 +00001554 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001555 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001556 # occurs while attempting to detect a hot-journal file), then
1557 # there may 0 page references and an active transaction according
1558 # to [sqlite3_get_autocommit].
1559 #
danielk19774abd5442008-05-05 15:26:50 +00001560 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001561 do_test $testname.$n.4 {
1562 set bt [btree_from_db db]
1563 db_enter db
1564 array set stats [btree_pager_stats $bt]
1565 db_leave db
danielk197781620542008-06-07 05:19:37 +00001566 set nRef $stats(ref)
1567 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1568 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001569 }
1570
mistachkin1d406e02013-08-29 01:09:14 +00001571 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001572 # and the pager is not running in exclusive-locking mode,
1573 # check that the pager is in "unlocked" state. Theoretically,
1574 # if a call to xUnlock() failed due to an IO error the underlying
1575 # file may still be locked.
1576 #
1577 ifcapable pragma {
1578 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001579 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001580 && [db one {pragma locking_mode}] eq "normal"
1581 && [sqlite3_get_autocommit db]
1582 } {
1583 do_test $testname.$n.5 {
1584 set bt [btree_from_db db]
1585 db_enter db
1586 array set stats [btree_pager_stats $bt]
1587 db_leave db
1588 set stats(state)
1589 } 0
1590 }
1591 }
1592
mistachkin48864df2013-03-21 21:20:32 +00001593 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001594 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001595 #
drh1aa5af12008-03-07 19:51:14 +00001596 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001597 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001598 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001599 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001600 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001601 set nowcksum [cksum]
1602 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1603 if {$res==0} {
1604 puts "now=$nowcksum"
1605 puts "the=$::checksum"
1606 puts "fwd=$::goodcksum"
1607 }
1608 set res
1609 } 1
danielk197732554c12005-01-22 03:39:39 +00001610 }
1611
drh1aa5af12008-03-07 19:51:14 +00001612 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001613 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001614 if {[info exists ::ioerropts(-cleanup)]} {
1615 catch $::ioerropts(-cleanup)
1616 }
danielk197732554c12005-01-22 03:39:39 +00001617 }
1618 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001619 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001620 unset ::ioerropts
1621}
1622
drh93aed5a2008-01-16 17:46:38 +00001623# Return a checksum based on the contents of the main database associated
1624# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001625#
1626proc cksum {{db db}} {
1627 set txt [$db eval {
1628 SELECT name, type, sql FROM sqlite_master order by name
1629 }]\n
1630 foreach tbl [$db eval {
1631 SELECT name FROM sqlite_master WHERE type='table' order by name
1632 }] {
1633 append txt [$db eval "SELECT * FROM $tbl"]\n
1634 }
1635 foreach prag {default_synchronous default_cache_size} {
1636 append txt $prag-[$db eval "PRAGMA $prag"]\n
1637 }
1638 set cksum [string length $txt]-[md5 $txt]
1639 # puts $cksum-[file size test.db]
1640 return $cksum
1641}
1642
drh93aed5a2008-01-16 17:46:38 +00001643# Generate a checksum based on the contents of the main and temp tables
1644# database $db. If the checksum of two databases is the same, and the
1645# integrity-check passes for both, the two databases are identical.
1646#
drh1db639c2008-01-17 02:36:28 +00001647proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001648 set ret [list]
1649 ifcapable tempdb {
1650 set sql {
1651 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1652 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1653 SELECT 'sqlite_master' UNION
1654 SELECT 'sqlite_temp_master' ORDER BY 1
1655 }
1656 } else {
1657 set sql {
1658 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1659 SELECT 'sqlite_master' ORDER BY 1
1660 }
1661 }
1662 set tbllist [$db eval $sql]
1663 set txt {}
1664 foreach tbl $tbllist {
1665 append txt [$db eval "SELECT * FROM $tbl"]
1666 }
1667 foreach prag {default_cache_size} {
1668 append txt $prag-[$db eval "PRAGMA $prag"]\n
1669 }
1670 # puts txt=$txt
1671 return [md5 $txt]
1672}
1673
drhdc2c4912009-02-04 22:46:47 +00001674# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00001675# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00001676# Examples of $dbname are "temp" or "main".
1677#
1678proc dbcksum {db dbname} {
1679 if {$dbname=="temp"} {
1680 set master sqlite_temp_master
1681 } else {
1682 set master $dbname.sqlite_master
1683 }
1684 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1685 set txt [$db eval "SELECT * FROM $master"]\n
1686 foreach tab $alltab {
1687 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1688 }
1689 return [md5 $txt]
1690}
1691
danielk197735754ac2008-03-21 17:29:37 +00001692proc memdebug_log_sql {{filename mallocs.sql}} {
1693
danielk19776f332c12008-03-21 14:22:44 +00001694 set data [sqlite3_memdebug_log dump]
1695 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001696 if {$nFrame < 0} { return "" }
1697
danielk197735754ac2008-03-21 17:29:37 +00001698 set database temp
1699
danielk19776ab3a2e2009-02-19 14:39:25 +00001700 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001701
1702 set sql ""
1703 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001704 set nCall [lindex $e 0]
1705 set nByte [lindex $e 1]
1706 set lStack [lrange $e 2 end]
1707 append sql "INSERT INTO ${database}.malloc VALUES"
1708 append sql "('test', $nCall, $nByte, '$lStack');\n"
1709 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001710 set frames($f) 1
1711 }
1712 }
1713
1714 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001715 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001716
1717 foreach f [array names frames] {
1718 set addr [format %x $f]
1719 set cmd "addr2line -e [info nameofexec] $addr"
1720 set line [eval exec $cmd]
1721 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001722
1723 set file [lindex [split $line :] 0]
1724 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001725 }
1726
danielk197735754ac2008-03-21 17:29:37 +00001727 foreach f [array names files] {
1728 set contents ""
1729 catch {
1730 set fd [open $f]
1731 set contents [read $fd]
1732 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001733 }
danielk197735754ac2008-03-21 17:29:37 +00001734 set contents [string map {' ''} $contents]
1735 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001736 }
danielk19776f332c12008-03-21 14:22:44 +00001737
danielk197735754ac2008-03-21 17:29:37 +00001738 set fd [open $filename w]
1739 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1740 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001741}
drh93aed5a2008-01-16 17:46:38 +00001742
danf5894502009-10-07 18:41:19 +00001743# Drop all tables in database [db]
1744proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001745 ifcapable trigger&&foreignkey {
1746 set pk [$db one "PRAGMA foreign_keys"]
1747 $db eval "PRAGMA foreign_keys = OFF"
1748 }
drh9a6ffc82010-02-15 18:03:20 +00001749 foreach {idx name file} [db eval {PRAGMA database_list}] {
1750 if {$idx==1} {
1751 set master sqlite_temp_master
1752 } else {
1753 set master $name.sqlite_master
1754 }
1755 foreach {t type} [$db eval "
1756 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001757 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001758 "] {
dana16d1062010-09-28 17:37:28 +00001759 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001760 }
danf5894502009-10-07 18:41:19 +00001761 }
shaneh4e7b32f2009-12-17 22:12:51 +00001762 ifcapable trigger&&foreignkey {
1763 $db eval "PRAGMA foreign_keys = $pk"
1764 }
danf5894502009-10-07 18:41:19 +00001765}
1766
dan71cb5182010-04-26 12:39:03 +00001767#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001768# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00001769# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1770# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00001771# intrusive:
dan71cb5182010-04-26 12:39:03 +00001772#
1773# wal_set_journal_mode ?DB?
1774#
1775# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1776# connection handle DB. Otherwise, this command is a no-op.
1777#
1778# wal_check_journal_mode TESTNAME ?DB?
1779#
1780# If running a WAL test, execute a tests case that fails if the main
1781# database for connection handle DB is not currently a WAL database.
1782# Otherwise (if not running a WAL permutation) this is a no-op.
1783#
1784# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00001785#
dan71cb5182010-04-26 12:39:03 +00001786# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00001787#
dan71cb5182010-04-26 12:39:03 +00001788proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001789 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001790}
1791proc wal_set_journal_mode {{db db}} {
1792 if { [wal_is_wal_mode] } {
1793 $db eval "PRAGMA journal_mode = WAL"
1794 }
1795}
1796proc wal_check_journal_mode {testname {db db}} {
1797 if { [wal_is_wal_mode] } {
1798 $db eval { SELECT * FROM sqlite_master }
1799 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1800 }
1801}
1802
dan430e74c2010-06-07 17:47:26 +00001803proc permutation {} {
1804 set perm ""
1805 catch {set perm $::G(perm:name)}
1806 set perm
1807}
dancb79e512010-08-06 13:50:07 +00001808proc presql {} {
1809 set presql ""
1810 catch {set presql $::G(perm:presql)}
1811 set presql
1812}
dan430e74c2010-06-07 17:47:26 +00001813
danc1a60c52010-06-07 14:28:16 +00001814#-------------------------------------------------------------------------
1815#
1816proc slave_test_script {script} {
1817
1818 # Create the interpreter used to run the test script.
1819 interp create tinterp
1820
1821 # Populate some global variables that tester.tcl expects to see.
1822 foreach {var value} [list \
1823 ::argv0 $::argv0 \
1824 ::argv {} \
1825 ::SLAVE 1 \
1826 ] {
1827 interp eval tinterp [list set $var $value]
1828 }
1829
1830 # The alias used to access the global test counters.
1831 tinterp alias set_test_counter set_test_counter
1832
1833 # Set up the ::cmdlinearg array in the slave.
1834 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1835
dan430e74c2010-06-07 17:47:26 +00001836 # Set up the ::G array in the slave.
1837 interp eval tinterp [list array set ::G [array get ::G]]
1838
danc1a60c52010-06-07 14:28:16 +00001839 # Load the various test interfaces implemented in C.
1840 load_testfixture_extensions tinterp
1841
1842 # Run the test script.
1843 interp eval tinterp $script
1844
danea5542d2010-07-06 11:26:15 +00001845 # Check if the interpreter call [run_thread_tests]
1846 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1847 set ::run_thread_tests_called 1
1848 }
1849
danc1a60c52010-06-07 14:28:16 +00001850 # Delete the interpreter used to run the test script.
1851 interp delete tinterp
1852}
1853
dan430e74c2010-06-07 17:47:26 +00001854proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001855 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001856
dan6a64d672011-04-04 15:38:16 +00001857 if {[info exists ::G(start:permutation)]} {
1858 if {[permutation] != $::G(start:permutation)} return
1859 unset ::G(start:permutation)
1860 }
1861 if {[info exists ::G(start:file)]} {
1862 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1863 unset ::G(start:file)
1864 }
1865
dan92d516a2010-07-05 14:54:48 +00001866 # Remember the value of the shared-cache setting. So that it is possible
1867 # to check afterwards that it was not modified by the test script.
1868 #
1869 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001870
dan92d516a2010-07-05 14:54:48 +00001871 # Run the test script in a slave interpreter.
1872 #
danea5542d2010-07-06 11:26:15 +00001873 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001874 reset_prng_state
1875 set ::sqlite_open_file_count 0
1876 set time [time { slave_test_script [list source $zFile] }]
1877 set ms [expr [lindex $time 0] / 1000]
1878
dan92d516a2010-07-05 14:54:48 +00001879 # Test that all files opened by the test script were closed. Omit this
1880 # if the test script has "thread" in its name. The open file counter
1881 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001882 #
danea5542d2010-07-06 11:26:15 +00001883 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001884 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1885 }
dan430e74c2010-06-07 17:47:26 +00001886 set ::sqlite_open_file_count 0
1887
mistachkin1d406e02013-08-29 01:09:14 +00001888 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00001889 # the test script.
1890 #
mistachkin1d406e02013-08-29 01:09:14 +00001891 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00001892 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1893 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1894 }
dan430e74c2010-06-07 17:47:26 +00001895
dan92d516a2010-07-05 14:54:48 +00001896 # Add some info to the output.
1897 #
dan0626dfc2010-06-15 06:56:37 +00001898 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001899 show_memstats
danc1a60c52010-06-07 14:28:16 +00001900}
1901
dan59257dc2010-08-04 11:34:31 +00001902# Open a new connection on database test.db and execute the SQL script
1903# supplied as an argument. Before returning, close the new conection and
1904# restore the 4 byte fields starting at header offsets 28, 92 and 96
1905# to the values they held before the SQL was executed. This simulates
1906# a write by a pre-3.7.0 client.
1907#
1908proc sql36231 {sql} {
1909 set B [hexio_read test.db 92 8]
1910 set A [hexio_read test.db 28 4]
1911 sqlite3 db36231 test.db
1912 catch { db36231 func a_string a_string }
1913 execsql $sql db36231
1914 db36231 close
1915 hexio_write test.db 28 $A
1916 hexio_write test.db 92 $B
1917 return ""
1918}
danf5894502009-10-07 18:41:19 +00001919
danccc7ff42010-11-29 12:06:45 +00001920proc db_save {} {
1921 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1922 foreach f [glob -nocomplain test.db*] {
1923 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00001924 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00001925 }
1926}
1927proc db_save_and_close {} {
1928 db_save
1929 catch { db close }
1930 return ""
1931}
1932proc db_restore {} {
1933 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1934 foreach f2 [glob -nocomplain sv_test.db*] {
1935 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00001936 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00001937 }
1938}
1939proc db_restore_and_reopen {{dbfile test.db}} {
1940 catch { db close }
1941 db_restore
1942 sqlite3 db $dbfile
1943}
1944proc db_delete_and_reopen {{file test.db}} {
1945 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00001946 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00001947 sqlite3 db $file
1948}
1949
danielk197745901d62004-11-10 15:27:38 +00001950# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1951# to non-zero, then set the global variable $AUTOVACUUM to 1.
1952set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001953
dan64b95bb2012-05-12 05:30:29 +00001954# Make sure the FTS enhanced query syntax is disabled.
1955set sqlite_fts3_enable_parentheses 0
1956
drh09fe6142013-11-29 15:06:27 +00001957# During testing, assume that all database files are well-formed. The
1958# few test cases that deliberately corrupt database files should rescind
1959# this setting by invoking "database_can_be_corrupt"
1960#
1961database_never_corrupt
1962
danielk1977ee8b7992009-03-26 17:13:06 +00001963source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001964source $testdir/malloc_common.tcl