blob: 3c34b45d4eff3868a80ba40f0dbd39534815b8cc [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements some common TCL routines used for regression
12# testing the SQLite library
13#
drhbb77b752009-04-09 01:23:49 +000014# $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
danc1a60c52010-06-07 14:28:16 +000016#-------------------------------------------------------------------------
17# The commands provided by the code in this file to help with creating
18# test cases are as follows:
drh8359d8c2008-03-29 11:00:54 +000019#
danc1a60c52010-06-07 14:28:16 +000020# Commands to manipulate the db and the file-system at a high level:
drh8359d8c2008-03-29 11:00:54 +000021#
danc1a60c52010-06-07 14:28:16 +000022# copy_file FROM TO
mistachkinfda06be2011-08-02 00:57:34 +000023# delete_file FILENAME
dan6f343962011-07-01 18:26:40 +000024# drop_all_tables ?DB?
mistachkinfda06be2011-08-02 00:57:34 +000025# forcecopy FROM TO
danc1a60c52010-06-07 14:28:16 +000026# forcedelete FILENAME
27#
28# Test the capability of the SQLite version built into the interpreter to
29# determine if a specific test can be run:
30#
31# ifcapable EXPR
32#
33# Calulate checksums based on database contents:
34#
35# dbcksum DB DBNAME
36# allcksum ?DB?
37# cksum ?DB?
38#
39# Commands to execute/explain SQL statements:
40#
41# stepsql DB SQL
42# execsql2 SQL
43# explain_no_trace SQL
44# explain SQL ?DB?
45# catchsql SQL ?DB?
46# execsql SQL ?DB?
47#
48# Commands to run test cases:
49#
50# do_ioerr_test TESTNAME ARGS...
51# crashsql ARGS...
52# integrity_check TESTNAME ?DB?
53# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000054# do_execsql_test TESTNAME SQL EXPECTED
55# do_catchsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000056#
dan430e74c2010-06-07 17:47:26 +000057# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000058#
59# set_test_counter COUNTER ?VALUE?
60# omit_test TESTNAME REASON
61# fail_test TESTNAME
62# incr_ntest
63#
64# Command run at the end of each test file:
65#
66# finish_test
67#
dan430e74c2010-06-07 17:47:26 +000068# Commands to help create test files that run with the "WAL" and other
69# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000070#
71# wal_is_wal_mode
72# wal_set_journal_mode ?DB?
73# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000074# permutation
dancb79e512010-08-06 13:50:07 +000075# presql
danc1a60c52010-06-07 14:28:16 +000076#
drh348784e2000-05-29 20:41:49 +000077
danc1a60c52010-06-07 14:28:16 +000078# Set the precision of FP arithmatic used by the interpreter. And
79# configure SQLite to take database file locks on the page that begins
80# 64KB into the database file instead of the one 1GB in. This means
81# the code that handles that special case can be tested without creating
82# very large database files.
83#
drh92febd92004-08-20 18:34:20 +000084set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000085sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000086
danc1a60c52010-06-07 14:28:16 +000087
88# If the pager codec is available, create a wrapper for the [sqlite3]
89# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +000090#
danc1a60c52010-06-07 14:28:16 +000091# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +000092#
danc1a60c52010-06-07 14:28:16 +000093# becomes
drh4a50aac2007-08-23 02:47:53 +000094#
danc1a60c52010-06-07 14:28:16 +000095# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +000096#
dan430e74c2010-06-07 17:47:26 +000097if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +000098 rename sqlite3 sqlite_orig
99 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000100 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000101 # This command is opening a new database connection.
102 #
103 if {[info exists ::G(perm:sqlite3_args)]} {
104 set args [concat $args $::G(perm:sqlite3_args)]
105 }
dan68928b62010-06-22 13:46:43 +0000106 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000107 lappend args -key {xyzzy}
108 }
109
dan3ccd20a2010-06-09 19:01:02 +0000110 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000111 if {[info exists ::G(perm:presql)]} {
112 [lindex $args 0] eval $::G(perm:presql)
113 }
dan1ce1b4a2010-12-07 14:32:28 +0000114 if {[info exists ::G(perm:dbconfig)]} {
115 set ::dbhandle [lindex $args 0]
116 uplevel #0 $::G(perm:dbconfig)
117 }
dan3ccd20a2010-06-09 19:01:02 +0000118 set res
dan430e74c2010-06-07 17:47:26 +0000119 } else {
120 # This command is not opening a new database connection. Pass the
121 # arguments through to the C implemenation as the are.
122 #
123 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000124 }
drh9eb9e262004-02-11 02:18:05 +0000125 }
126}
127
mistachkinfda06be2011-08-02 00:57:34 +0000128proc getFileRetries {} {
129 if {![info exists ::G(file-retries)]} {
130 #
131 # NOTE: Return the default number of retries for [file] operations. A
132 # value of zero or less here means "disabled".
133 #
134 return [expr {$::tcl_platform(platform) eq "windows" ? 10 : 0}]
135 }
136 return $::G(file-retries)
137}
138
139proc getFileRetryDelay {} {
140 if {![info exists ::G(file-retry-delay)]} {
141 #
142 # NOTE: Return the default number of milliseconds to wait when retrying
143 # failed [file] operations. A value of zero or less means "do not
144 # wait".
145 #
146 return 100; # TODO: Good default?
147 }
148 return $::G(file-retry-delay)
149}
150
151# Copy file $from into $to. This is used because some versions of
152# TCL for windows (notably the 8.4.1 binary package shipped with the
153# current mingw release) have a broken "file copy" command.
154#
155proc copy_file {from to} {
156 do_copy_file false $from $to
157}
158
159proc forcecopy {from to} {
160 do_copy_file true $from $to
161}
162
163proc do_copy_file {force from to} {
164 set nRetry [getFileRetries] ;# Maximum number of retries.
165 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
166
167 # On windows, sometimes even a [file copy -force] can fail. The cause is
168 # usually "tag-alongs" - programs like anti-virus software, automatic backup
169 # tools and various explorer extensions that keep a file open a little longer
170 # than we expect, causing the delete to fail.
171 #
172 # The solution is to wait a short amount of time before retrying the copy.
173 #
174 if {$nRetry > 0} {
175 for {set i 0} {$i<$nRetry} {incr i} {
176 set rc [catch {
177 if {$force} {
178 file copy -force $from $to
179 } else {
180 file copy $from $to
181 }
182 } msg]
183 if {$rc==0} break
184 if {$nDelay > 0} { after $nDelay }
185 }
186 if {$rc} { error $msg }
187 } else {
188 if {$force} {
189 file copy -force $from $to
190 } else {
191 file copy $from $to
192 }
193 }
194}
195
196# Delete a file or directory
197#
198proc delete_file {args} {
199 do_delete_file false {*}$args
200}
201
202proc forcedelete {args} {
203 do_delete_file true {*}$args
204}
205
206proc do_delete_file {force args} {
207 set nRetry [getFileRetries] ;# Maximum number of retries.
208 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
209
210 foreach filename $args {
211 # On windows, sometimes even a [file delete -force] can fail just after
212 # a file is closed. The cause is usually "tag-alongs" - programs like
213 # anti-virus software, automatic backup tools and various explorer
214 # extensions that keep a file open a little longer than we expect, causing
215 # the delete to fail.
216 #
217 # The solution is to wait a short amount of time before retrying the
218 # delete.
219 #
220 if {$nRetry > 0} {
221 for {set i 0} {$i<$nRetry} {incr i} {
222 set rc [catch {
223 if {$force} {
224 file delete -force $filename
225 } else {
226 file delete $filename
227 }
228 } msg]
229 if {$rc==0} break
230 if {$nDelay > 0} { after $nDelay }
231 }
232 if {$rc} { error $msg }
233 } else {
234 if {$force} {
235 file delete -force $filename
236 } else {
237 file delete $filename
238 }
239 }
240 }
241}
242
dancb354602010-07-08 09:44:42 +0000243proc execpresql {handle args} {
244 trace remove execution $handle enter [list execpresql $handle]
245 if {[info exists ::G(perm:presql)]} {
246 $handle eval $::G(perm:presql)
247 }
248}
249
dan68928b62010-06-22 13:46:43 +0000250# This command should be called after loading tester.tcl from within
251# all test scripts that are incompatible with encryption codecs.
252#
253proc do_not_use_codec {} {
254 set ::do_not_use_codec 1
255 reset_db
256}
257
dan430e74c2010-06-07 17:47:26 +0000258# The following block only runs the first time this file is sourced. It
259# does not run in slave interpreters (since the ::cmdlinearg array is
260# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000261#
danc1a60c52010-06-07 14:28:16 +0000262if {[info exists cmdlinearg]==0} {
263
264 # Parse any options specified in the $argv array. This script accepts the
265 # following options:
266 #
267 # --pause
268 # --soft-heap-limit=NN
269 # --maxerror=NN
270 # --malloctrace=N
271 # --backtrace=N
272 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000273 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000274 # --file-retries=N
275 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000276 # --start=[$permutation:]$testfile
danc1a60c52010-06-07 14:28:16 +0000277 #
278 set cmdlinearg(soft-heap-limit) 0
279 set cmdlinearg(maxerror) 1000
280 set cmdlinearg(malloctrace) 0
281 set cmdlinearg(backtrace) 10
282 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000283 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000284 set cmdlinearg(file-retries) 0
285 set cmdlinearg(file-retry-delay) 0
dan6a64d672011-04-04 15:38:16 +0000286 set cmdlinearg(start) ""
danc1a60c52010-06-07 14:28:16 +0000287
288 set leftover [list]
289 foreach a $argv {
290 switch -regexp -- $a {
291 {^-+pause$} {
292 # Wait for user input before continuing. This is to give the user an
293 # opportunity to connect profiling tools to the process.
294 puts -nonewline "Press RETURN to begin..."
295 flush stdout
296 gets stdin
297 }
298 {^-+soft-heap-limit=.+$} {
299 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
300 }
301 {^-+maxerror=.+$} {
302 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
303 }
304 {^-+malloctrace=.+$} {
305 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
306 if {$cmdlinearg(malloctrace)} {
307 sqlite3_memdebug_log start
308 }
309 }
310 {^-+backtrace=.+$} {
311 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000312 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000313 }
danc1a60c52010-06-07 14:28:16 +0000314 {^-+binarylog=.+$} {
315 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
316 }
dan0626dfc2010-06-15 06:56:37 +0000317 {^-+soak=.+$} {
318 foreach {dummy cmdlinearg(soak)} [split $a =] break
319 set ::G(issoak) $cmdlinearg(soak)
320 }
mistachkinfda06be2011-08-02 00:57:34 +0000321 {^-+file-retries=.+$} {
322 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
323 set ::G(file-retries) $cmdlinearg(file-retries)
324 }
325 {^-+file-retry-delay=.+$} {
326 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
327 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
328 }
dan6a64d672011-04-04 15:38:16 +0000329 {^-+start=.+$} {
330 foreach {dummy cmdlinearg(start)} [split $a =] break
331
332 set ::G(start:file) $cmdlinearg(start)
333 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
334 set ::G(start:permutation) ${s.perm}
335 set ::G(start:file) ${s.file}
336 }
337 if {$::G(start:file) == ""} {unset ::G(start:file)}
338 }
danc1a60c52010-06-07 14:28:16 +0000339 default {
340 lappend leftover $a
341 }
342 }
343 }
344 set argv $leftover
345
dan430e74c2010-06-07 17:47:26 +0000346 # Install the malloc layer used to inject OOM errors. And the 'automatic'
347 # extensions. This only needs to be done once for the process.
348 #
danielk1977f7b9d662008-06-23 18:49:43 +0000349 sqlite3_shutdown
350 install_malloc_faultsim 1
351 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000352 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000353
dan430e74c2010-06-07 17:47:26 +0000354 # If the --binarylog option was specified, create the logging VFS. This
355 # call installs the new VFS as the default for all SQLite connections.
356 #
danc1a60c52010-06-07 14:28:16 +0000357 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000358 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000359 }
360
361 # Set the backtrace depth, if malloc tracing is enabled.
362 #
363 if {$cmdlinearg(malloctrace)} {
364 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000365 }
danielk1977f7b9d662008-06-23 18:49:43 +0000366}
danielk197703ba3fa2009-01-09 10:49:14 +0000367
danc1a60c52010-06-07 14:28:16 +0000368# Update the soft-heap-limit each time this script is run. In that
369# way if an individual test file changes the soft-heap-limit, it
370# will be reset at the start of the next test file.
371#
372sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
373
374# Create a test database
375#
danielk197703ba3fa2009-01-09 10:49:14 +0000376proc reset_db {} {
377 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000378 forcedelete test.db
379 forcedelete test.db-journal
380 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000381 sqlite3 db ./test.db
382 set ::DB [sqlite3_connection_pointer db]
383 if {[info exists ::SETUP_SQL]} {
384 db eval $::SETUP_SQL
385 }
drhcd61c282002-03-06 22:01:34 +0000386}
danielk197703ba3fa2009-01-09 10:49:14 +0000387reset_db
drhbec2bf42000-05-29 23:48:22 +0000388
389# Abort early if this script has been run before.
390#
danc1a60c52010-06-07 14:28:16 +0000391if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000392
drhce2198c2010-09-10 13:22:59 +0000393# Make sure memory statistics are enabled.
394#
395sqlite3_config_memstatus 1
396
danc1a60c52010-06-07 14:28:16 +0000397# Initialize the test counters and set up commands to access them.
398# Or, if this is a slave interpreter, set up aliases to write the
399# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000400#
danc1a60c52010-06-07 14:28:16 +0000401if {0==[info exists ::SLAVE]} {
402 set TC(errors) 0
403 set TC(count) 0
404 set TC(fail_list) [list]
405 set TC(omit_list) [list]
406
407 proc set_test_counter {counter args} {
408 if {[llength $args]} {
409 set ::TC($counter) [lindex $args 0]
410 }
411 set ::TC($counter)
412 }
drhb62c3352006-11-23 09:39:16 +0000413}
drh348784e2000-05-29 20:41:49 +0000414
drh521cc842008-04-15 02:36:33 +0000415# Record the fact that a sequence of tests were omitted.
416#
417proc omit_test {name reason} {
danc1a60c52010-06-07 14:28:16 +0000418 set omitList [set_test_counter omit_list]
drh521cc842008-04-15 02:36:33 +0000419 lappend omitList [list $name $reason]
danc1a60c52010-06-07 14:28:16 +0000420 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000421}
422
danc1a60c52010-06-07 14:28:16 +0000423# Record the fact that a test failed.
424#
425proc fail_test {name} {
426 set f [set_test_counter fail_list]
427 lappend f $name
428 set_test_counter fail_list $f
429 set_test_counter errors [expr [set_test_counter errors] + 1]
430
431 set nFail [set_test_counter errors]
432 if {$nFail>=$::cmdlinearg(maxerror)} {
433 puts "*** Giving up..."
434 finalize_testing
435 }
436}
437
438# Increment the number of tests run
439#
440proc incr_ntest {} {
441 set_test_counter count [expr [set_test_counter count] + 1]
442}
443
444
drh348784e2000-05-29 20:41:49 +0000445# Invoke the do_test procedure to run a single test
446#
447proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000448
449 global argv cmdlinearg
450
dan8c408002010-11-01 17:38:24 +0000451 fix_testname name
452
drh4a50aac2007-08-23 02:47:53 +0000453 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000454
dan92d516a2010-07-05 14:54:48 +0000455# if {[llength $argv]==0} {
456# set go 1
457# } else {
458# set go 0
459# foreach pattern $argv {
460# if {[string match $pattern $name]} {
461# set go 1
462# break
463# }
464# }
465# }
dan430e74c2010-06-07 17:47:26 +0000466
dand506de02010-07-03 13:59:01 +0000467 if {[info exists ::G(perm:prefix)]} {
468 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000469 }
470
danc1a60c52010-06-07 14:28:16 +0000471 incr_ntest
drh5edc3122001-09-13 21:53:09 +0000472 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000473 flush stdout
474 if {[catch {uplevel #0 "$cmd;\n"} result]} {
475 puts "\nError: $result"
danc1a60c52010-06-07 14:28:16 +0000476 fail_test $name
drh348784e2000-05-29 20:41:49 +0000477 } elseif {[string compare $result $expected]} {
478 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
danc1a60c52010-06-07 14:28:16 +0000479 fail_test $name
drh348784e2000-05-29 20:41:49 +0000480 } else {
481 puts " Ok"
482 }
drh6558db82007-04-13 03:23:21 +0000483 flush stdout
drh348784e2000-05-29 20:41:49 +0000484}
dana3f51082010-09-30 18:43:14 +0000485
shaneh55505172011-06-21 19:30:19 +0000486proc filepath_normalize {p} {
487 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000488 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000489 # lreverse*2 as a hack to remove any unneeded {} after the string map
490 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000491 } {
492 set p
shaneh55505172011-06-21 19:30:19 +0000493 }
494}
495proc do_filepath_test {name cmd expected} {
496 uplevel [list do_test $name [
497 subst -nocommands { filepath_normalize [ $cmd ] }
498 ] [filepath_normalize $expected]]
499}
500
dan33f53792011-05-05 19:44:22 +0000501proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000502 # different TCL versions display floating point values differently.
503 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000504}
505proc do_realnum_test {name cmd expected} {
506 uplevel [list do_test $name [
507 subst -nocommands { realnum_normalize [ $cmd ] }
508 ] [realnum_normalize $expected]]
509}
510
dana3f51082010-09-30 18:43:14 +0000511proc fix_testname {varname} {
512 upvar $varname testname
513 if {[info exists ::testprefix]
514 && [string is digit [string range $testname 0 0]]
515 } {
516 set testname "${::testprefix}-$testname"
517 }
518}
dan153eda02010-06-21 07:45:47 +0000519
dan57f7f4b2010-10-01 19:04:37 +0000520proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000521 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000522 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000523}
524proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000525 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000526 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000527}
dan39854792010-11-15 16:12:58 +0000528proc do_eqp_test {name sql res} {
529 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
530}
dan153eda02010-06-21 07:45:47 +0000531
dan51f06982010-09-18 19:00:12 +0000532#-------------------------------------------------------------------------
533# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
534#
535# Where switches are:
536#
537# -errorformat FMTSTRING
538# -count
danaf1dcab2010-09-20 19:17:53 +0000539# -query SQL
dana16d1062010-09-28 17:37:28 +0000540# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000541# -repair TCL
dan51f06982010-09-18 19:00:12 +0000542#
543proc do_select_tests {prefix args} {
544
545 set testlist [lindex $args end]
546 set switches [lrange $args 0 end-1]
547
548 set errfmt ""
549 set countonly 0
dana16d1062010-09-28 17:37:28 +0000550 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000551 set repair ""
dan51f06982010-09-18 19:00:12 +0000552
553 for {set i 0} {$i < [llength $switches]} {incr i} {
554 set s [lindex $switches $i]
555 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000556 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000557 set tclquery [list execsql [lindex $switches [incr i]]]
558 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
559 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000560 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000561 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000562 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
563 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000564 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
565 set countonly 1
566 } else {
567 error "unknown switch: $s"
568 }
569 }
570
571 if {$countonly && $errfmt!=""} {
572 error "Cannot use -count and -errorformat together"
573 }
574 set nTestlist [llength $testlist]
575 if {$nTestlist%3 || $nTestlist==0 } {
576 error "SELECT test list contains [llength $testlist] elements"
577 }
578
danaf7626f2010-09-23 18:47:36 +0000579 eval $repair
dan51f06982010-09-18 19:00:12 +0000580 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +0000581 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +0000582 execsql $sql
dana16d1062010-09-28 17:37:28 +0000583 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
584 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +0000585 set nRow 0
586 db eval $sql {incr nRow}
587 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
588 } elseif {$errfmt==""} {
589 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
590 } else {
591 set res [list 1 [string trim [format $errfmt {*}$res]]]
592 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
593 }
danaf7626f2010-09-23 18:47:36 +0000594 eval $repair
dan51f06982010-09-18 19:00:12 +0000595 }
danaf7626f2010-09-23 18:47:36 +0000596
dan51f06982010-09-18 19:00:12 +0000597}
598
danb04757a2010-09-21 16:59:16 +0000599proc delete_all_data {} {
600 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
601 db eval "DELETE FROM '[string map {' ''} $t]'"
602 }
603}
drh348784e2000-05-29 20:41:49 +0000604
drhb62c3352006-11-23 09:39:16 +0000605# Run an SQL script.
606# Return the number of microseconds per statement.
607#
drh3590f152006-11-23 21:09:10 +0000608proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000609 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000610 flush stdout
611 set speed [time {sqlite3_exec_nr db $sql}]
612 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +0000613 if {$tm == 0} {
614 set rate [format %20s "many"]
615 } else {
616 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
617 }
drh3590f152006-11-23 21:09:10 +0000618 set u2 $units/s
danielk19770ee26d92008-02-08 18:25:29 +0000619 puts [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +0000620 global total_time
621 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000622 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +0000623}
drh45c236d2008-03-22 01:08:00 +0000624proc speed_trial_tcl {name numstmt units script} {
625 puts -nonewline [format {%-21.21s } $name...]
626 flush stdout
627 set speed [time {eval $script}]
628 set tm [lindex $speed 0]
629 if {$tm == 0} {
630 set rate [format %20s "many"]
631 } else {
632 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
633 }
634 set u2 $units/s
635 puts [format {%12d uS %s %s} $tm $rate $u2]
636 global total_time
637 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +0000638 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +0000639}
drhdd924312007-03-31 22:34:16 +0000640proc speed_trial_init {name} {
641 global total_time
642 set total_time 0
dana975b592010-10-08 16:09:43 +0000643 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +0000644 sqlite3 versdb :memory:
645 set vers [versdb one {SELECT sqlite_source_id()}]
646 versdb close
647 puts "SQLite $vers"
drhdd924312007-03-31 22:34:16 +0000648}
649proc speed_trial_summary {name} {
650 global total_time
651 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +0000652
653 if { 0 } {
654 sqlite3 versdb :memory:
655 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
656 versdb close
657 puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
658 foreach {test us} $::speed_trial_times {
659 puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
660 }
661 }
drhb62c3352006-11-23 09:39:16 +0000662}
663
drh348784e2000-05-29 20:41:49 +0000664# Run this routine last
665#
666proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +0000667 catch {db close}
668 catch {db2 close}
669 catch {db3 close}
670 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +0000671}
672proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +0000673 global sqlite_open_file_count
674
675 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +0000676
drh6e142f52000-06-08 13:36:40 +0000677 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000678 catch {db2 close}
679 catch {db3 close}
680
drh9bc54492007-10-23 14:49:59 +0000681 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000682 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000683 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000684 db close
drh984bfaa2008-03-19 16:08:53 +0000685 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +0000686
drh3a7fb7c2007-08-10 16:41:08 +0000687 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +0000688 set nTest [incr_ntest]
689 set nErr [set_test_counter errors]
690
drh348784e2000-05-29 20:41:49 +0000691 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000692 if {$nErr>0} {
danc1a60c52010-06-07 14:28:16 +0000693 puts "Failures on these tests: [set_test_counter fail_list]"
drhf3a65f72007-08-22 20:18:21 +0000694 }
danielk1977ee8b7992009-03-26 17:13:06 +0000695 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +0000696 if {[llength $omitList]>0} {
697 puts "Omitted test cases:"
698 set prec {}
699 foreach {rec} [lsort $omitList] {
700 if {$rec==$prec} continue
701 set prec $rec
702 puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]]
703 }
704 }
drh80788d82006-09-02 14:50:23 +0000705 if {$nErr>0 && ![working_64bit_int]} {
706 puts "******************************************************************"
707 puts "N.B.: The version of TCL that you used to build this test harness"
708 puts "is defective in that it does not support 64-bit integers. Some or"
709 puts "all of the test failures above might be a result from this defect"
710 puts "in your TCL build."
711 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000712 }
danc1a60c52010-06-07 14:28:16 +0000713 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000714 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +0000715 }
drh94e92032003-02-16 22:21:32 +0000716 if {$sqlite_open_file_count} {
717 puts "$sqlite_open_file_count files were left open"
718 incr nErr
719 }
drheafc43b2010-07-26 18:43:40 +0000720 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
721 [sqlite3_memory_used]>0} {
722 puts "Unfreed memory: [sqlite3_memory_used] bytes in\
723 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +0000724 incr nErr
drh2d7636e2008-02-16 16:21:45 +0000725 ifcapable memdebug||mem5||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000726 puts "Writing unfreed memory log to \"./memleak.txt\""
727 sqlite3_memdebug_dump ./memleak.txt
728 }
drhf3a65f72007-08-22 20:18:21 +0000729 } else {
730 puts "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +0000731 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +0000732 sqlite3_memdebug_dump ./memusage.txt
733 }
drhf3a65f72007-08-22 20:18:21 +0000734 }
drhf7141992008-06-19 00:16:08 +0000735 show_memstats
drh91fd4d42008-01-19 20:11:25 +0000736 puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
737 puts "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +0000738 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
739 puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
740 }
danc1a60c52010-06-07 14:28:16 +0000741 if {$::cmdlinearg(malloctrace)} {
danielk197735754ac2008-03-21 17:29:37 +0000742 puts "Writing mallocs.sql..."
743 memdebug_log_sql
744 sqlite3_memdebug_log stop
745 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +0000746
747 if {[sqlite3_memory_used]>0} {
748 puts "Writing leaks.sql..."
749 sqlite3_memdebug_log sync
750 memdebug_log_sql leaks.sql
751 }
danielk197735754ac2008-03-21 17:29:37 +0000752 }
drhd9910fe2006-10-04 11:55:49 +0000753 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +0000754 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000755 }
756 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +0000757 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +0000758 }
drhdb25e382001-03-15 18:21:22 +0000759 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000760}
761
drhf7141992008-06-19 00:16:08 +0000762# Display memory statistics for analysis and debugging purposes.
763#
764proc show_memstats {} {
765 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +0000766 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
767 set val [format {now %10d max %10d max-size %10d} \
768 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000769 puts "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +0000770 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
771 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
772 puts "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +0000773 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +0000774 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
775 set val [format {now %10d max %10d max-size %10d} \
776 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000777 puts "Page-cache used: $val"
778 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
779 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
780 puts "Page-cache overflow: $val"
781 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
782 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
783 puts "Scratch memory used: $val"
784 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +0000785 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
786 set val [format {now %10d max %10d max-size %10d} \
787 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
drhf7141992008-06-19 00:16:08 +0000788 puts "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +0000789 ifcapable yytrackmaxstackdepth {
790 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
791 set val [format { max %10d} [lindex $x 2]]
792 puts "Parser stack depth: $val"
793 }
drhf7141992008-06-19 00:16:08 +0000794}
795
drh348784e2000-05-29 20:41:49 +0000796# A procedure to execute SQL
797#
drhc4a3c772001-04-04 11:48:57 +0000798proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000799 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000800 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000801}
drh3aadb2e2000-05-31 17:59:25 +0000802
drhadbca9c2001-09-27 15:11:53 +0000803# Execute SQL and catch exceptions.
804#
805proc catchsql {sql {db db}} {
806 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +0000807 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +0000808 lappend r $msg
809 return $r
810}
811
drh04096482001-11-09 22:41:44 +0000812# Do an VDBE code dump on the SQL given
813#
814proc explain {sql {db db}} {
815 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000816 puts "addr opcode p1 p2 p3 p4 p5 #"
817 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000818 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000819 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
820 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
821 ]
drh04096482001-11-09 22:41:44 +0000822 }
823}
824
drhdafc0ce2008-04-17 19:14:02 +0000825# Show the VDBE program for an SQL statement but omit the Trace
826# opcode at the beginning. This procedure can be used to prove
827# that different SQL statements generate exactly the same VDBE code.
828#
829proc explain_no_trace {sql} {
830 set tr [db eval "EXPLAIN $sql"]
831 return [lrange $tr 7 end]
832}
833
drh3aadb2e2000-05-31 17:59:25 +0000834# Another procedure to execute SQL. This one includes the field
835# names in the returned list.
836#
837proc execsql2 {sql} {
838 set result {}
839 db eval $sql data {
840 foreach f $data(*) {
841 lappend result $f $data($f)
842 }
843 }
844 return $result
845}
drh17a68932001-01-31 13:28:08 +0000846
drhdde85d92003-03-01 19:45:34 +0000847# Use the non-callback API to execute multiple SQL statements
848#
849proc stepsql {dbptr sql} {
850 set sql [string trim $sql]
851 set r 0
852 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000853 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000854 return [list 1 $vm]
855 }
856 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000857# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
858# foreach v $VAL {lappend r $v}
859# }
860 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
861 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
862 lappend r [sqlite3_column_text $vm $i]
863 }
drhdde85d92003-03-01 19:45:34 +0000864 }
danielk1977106bb232004-05-21 10:08:53 +0000865 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000866 return [list 1 $errmsg]
867 }
868 }
869 return $r
870}
871
drh21504322002-06-25 13:16:02 +0000872# Do an integrity check of the entire database
873#
danielk197704103022009-02-03 16:51:24 +0000874proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +0000875 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +0000876 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +0000877 }
878}
879
danc6055c72011-04-28 18:46:46 +0000880
881# Return true if the SQL statement passed as the second argument uses a
882# statement transaction.
883#
884proc sql_uses_stmt {db sql} {
885 set stmt [sqlite3_prepare $db $sql -1 dummy]
886 set uses [uses_stmt_journal $stmt]
887 sqlite3_finalize $stmt
888 return $uses
889}
890
danielk1977db4e8862008-01-16 08:24:46 +0000891proc fix_ifcapable_expr {expr} {
892 set ret ""
893 set state 0
894 for {set i 0} {$i < [string length $expr]} {incr i} {
895 set char [string range $expr $i $i]
896 set newstate [expr {[string is alnum $char] || $char eq "_"}]
897 if {$newstate && !$state} {
898 append ret {$::sqlite_options(}
899 }
900 if {!$newstate && $state} {
901 append ret )
902 }
903 append ret $char
904 set state $newstate
905 }
906 if {$state} {append ret )}
907 return $ret
908}
909
drh27d258a2004-10-30 20:23:09 +0000910# Evaluate a boolean expression of capabilities. If true, execute the
911# code. Omit the code if false.
912#
danielk19773e8c37e2005-01-21 03:12:14 +0000913proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +0000914 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
915 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +0000916 if ($e2) {
917 set c [catch {uplevel 1 $code} r]
918 } else {
919 set c [catch {uplevel 1 $elsecode} r]
920 }
921 return -code $c $r
drh21504322002-06-25 13:16:02 +0000922}
danielk197745901d62004-11-10 15:27:38 +0000923
danielk1977aca790a2005-01-13 11:07:52 +0000924# This proc execs a seperate process that crashes midway through executing
925# the SQL script $sql on database test.db.
926#
927# The crash occurs during a sync() of file $crashfile. When the crash
928# occurs a random subset of all unsynced writes made by the process are
929# written into the files on disk. Argument $crashdelay indicates the
930# number of file syncs to wait before crashing.
931#
932# The return value is a list of two elements. The first element is a
933# boolean, indicating whether or not the process actually crashed or
934# reported some other error. The second element in the returned list is the
935# error message. This is "child process exited abnormally" if the crash
936# occured.
937#
danielk1977f8940ae2007-08-23 11:07:10 +0000938# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000939#
940proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +0000941
942 set blocksize ""
943 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +0000944 set prngseed 0
drhf8587402008-01-08 16:03:49 +0000945 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +0000946 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000947 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000948 set sql [lindex $args end]
949
950 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
951 set z [lindex $args $ii]
952 set n [string length $z]
953 set z2 [lindex $args [expr $ii+1]]
954
955 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
drhcb1f0f62008-01-08 15:18:52 +0000956 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +0000957 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +0000958 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000959 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000960 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000961 else { error "Unrecognized option: $z" }
962 }
963
964 if {$crashfile eq ""} {
965 error "Compulsory option -file missing"
966 }
967
shanehf2c08822010-07-08 16:30:44 +0000968 # $crashfile gets compared to the native filename in
969 # cfSync(), which can be different then what TCL uses by
970 # default, so here we force it to the "nativename" format.
971 set cfile [string map {\\ \\\\} [file nativename [file join [pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +0000972
973 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000974 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000975 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +0000976 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000977 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000978
979 # This block sets the cache size of the main database to 10
980 # pages. This is done in case the build is configured to omit
981 # "PRAGMA cache_size".
982 puts $f {db eval {SELECT * FROM sqlite_master;}}
983 puts $f {set bt [btree_from_db db]}
984 puts $f {btree_set_cache_size $bt 10}
drhcb1f0f62008-01-08 15:18:52 +0000985 if {$prngseed} {
986 set seed [expr {$prngseed%10007+1}]
987 # puts seed=$seed
988 puts $f "db eval {SELECT randomblob($seed)}"
989 }
danielk1977933bbd62007-03-17 07:22:42 +0000990
drhf8587402008-01-08 16:03:49 +0000991 if {[string length $tclbody]>0} {
992 puts $f $tclbody
993 }
994 if {[string length $sql]>0} {
995 puts $f "db eval {"
996 puts $f "$sql"
997 puts $f "}"
998 }
danielk1977aca790a2005-01-13 11:07:52 +0000999 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001000 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001001 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001002 } msg]
shanehf2c08822010-07-08 16:30:44 +00001003
1004 # Windows/ActiveState TCL returns a slightly different
1005 # error message. We map that to the expected message
1006 # so that we don't have to change all of the test
1007 # cases.
1008 if {$::tcl_platform(platform)=="windows"} {
1009 if {$msg=="child killed: unknown signal"} {
1010 set msg "child process exited abnormally"
1011 }
1012 }
1013
danielk1977aca790a2005-01-13 11:07:52 +00001014 lappend r $msg
1015}
1016
danielk197732554c12005-01-22 03:39:39 +00001017# Usage: do_ioerr_test <test number> <options...>
1018#
1019# This proc is used to implement test cases that check that IO errors
1020# are correctly handled. The first argument, <test number>, is an integer
1021# used to name the tests executed by this proc. Options are as follows:
1022#
1023# -tclprep TCL script to run to prepare test.
1024# -sqlprep SQL script to run to prepare test.
1025# -tclbody TCL script to run with IO error simulation.
1026# -sqlbody TCL script to run with IO error simulation.
1027# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001028# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001029# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001030# -start Value of 'N' to begin with (default 1)
1031#
1032# -cksum Boolean. If true, test that the database does
1033# not change during the execution of the test case.
1034#
1035proc do_ioerr_test {testname args} {
1036
danielk197732554c12005-01-22 03:39:39 +00001037 set ::ioerropts(-start) 1
1038 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001039 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001040 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001041 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001042 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001043 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001044 array set ::ioerropts $args
1045
danielk197747cd39c2008-05-12 12:41:15 +00001046 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1047 # a couple of obscure IO errors that do not return them.
1048 set ::ioerropts(-erc) 0
1049
danielk197732554c12005-01-22 03:39:39 +00001050 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001051 #reset_prng_state
1052 save_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001053 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001054 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001055 incr ::ioerropts(-count) -1
1056 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +00001057
1058 # Skip this IO error if it was specified with the "-exclude" option.
1059 if {[info exists ::ioerropts(-exclude)]} {
1060 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1061 }
danielk1977861f7452008-06-05 11:39:11 +00001062 if {$::ioerropts(-restoreprng)} {
1063 restore_prng_state
1064 }
danielk197732554c12005-01-22 03:39:39 +00001065
1066 # Delete the files test.db and test2.db, then execute the TCL and
1067 # SQL (in that order) to prepare for the test case.
1068 do_test $testname.$n.1 {
1069 set ::sqlite_io_error_pending 0
1070 catch {db close}
aswiftaebf4132008-11-21 00:10:35 +00001071 catch {db2 close}
mistachkinfda06be2011-08-02 00:57:34 +00001072 catch {forcedelete test.db}
1073 catch {forcedelete test.db-journal}
1074 catch {forcedelete test2.db}
1075 catch {forcedelete test2.db-journal}
drha34c62d2006-01-06 22:11:20 +00001076 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +00001077 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +00001078 if {[info exists ::ioerropts(-tclprep)]} {
1079 eval $::ioerropts(-tclprep)
1080 }
1081 if {[info exists ::ioerropts(-sqlprep)]} {
1082 execsql $::ioerropts(-sqlprep)
1083 }
1084 expr 0
1085 } {0}
1086
1087 # Read the 'checksum' of the database.
1088 if {$::ioerropts(-cksum)} {
1089 set checksum [cksum]
1090 }
drh93aed5a2008-01-16 17:46:38 +00001091
danielk197732554c12005-01-22 03:39:39 +00001092 # Set the Nth IO error to fail.
1093 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001094 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001095 set ::sqlite_io_error_pending $n
1096 }] $n
1097
1098 # Create a single TCL script from the TCL and SQL specified
1099 # as the body of the test.
1100 set ::ioerrorbody {}
1101 if {[info exists ::ioerropts(-tclbody)]} {
1102 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1103 }
1104 if {[info exists ::ioerropts(-sqlbody)]} {
1105 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1106 }
1107
1108 # Execute the TCL Script created in the above block. If
1109 # there are at least N IO operations performed by SQLite as
1110 # a result of the script, the Nth will fail.
1111 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001112 set ::sqlite_io_error_hit 0
1113 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001114 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001115 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001116 set rc [sqlite3_errcode $::DB]
1117 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001118 # If we are in extended result code mode, make sure all of the
1119 # IOERRs we get back really do have their extended code values.
1120 # If an extended result code is returned, the sqlite3_errcode
1121 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1122 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001123 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1124 return $rc
1125 }
1126 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001127 # If we are not in extended result code mode, make sure no
1128 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001129 if {[regexp {\+\d} $rc]} {
1130 return $rc
1131 }
1132 }
drh93aed5a2008-01-16 17:46:38 +00001133 # The test repeats as long as $::go is non-zero. $::go starts out
1134 # as 1. When a test runs to completion without hitting an I/O
1135 # error, that means there is no point in continuing with this test
1136 # case so set $::go to zero.
1137 #
1138 if {$::sqlite_io_error_pending>0} {
1139 set ::go 0
1140 set q 0
1141 set ::sqlite_io_error_pending 0
1142 } else {
1143 set q 1
1144 }
1145
drhc9ac5ca2005-11-04 22:03:30 +00001146 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001147 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1148 set r 1
1149 }
danielk1977f2fa8312006-01-24 13:09:33 +00001150 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001151
1152 # One of two things must have happened. either
1153 # 1. We never hit the IO error and the SQL returned OK
1154 # 2. An IO error was hit and the SQL failed
1155 #
dand41a29a2010-05-06 15:56:28 +00001156 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001157 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001158 } {1}
1159
danielk197780daec62008-05-12 10:57:02 +00001160 set ::sqlite_io_error_hit 0
1161 set ::sqlite_io_error_pending 0
1162
danielk197752b472a2008-05-05 16:23:55 +00001163 # Check that no page references were leaked. There should be
1164 # a single reference if there is still an active transaction,
1165 # or zero otherwise.
1166 #
danielk197781620542008-06-07 05:19:37 +00001167 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
1168 # locks are established on database files (i.e. if the error
1169 # occurs while attempting to detect a hot-journal file), then
1170 # there may 0 page references and an active transaction according
1171 # to [sqlite3_get_autocommit].
1172 #
danielk19774abd5442008-05-05 15:26:50 +00001173 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001174 do_test $testname.$n.4 {
1175 set bt [btree_from_db db]
1176 db_enter db
1177 array set stats [btree_pager_stats $bt]
1178 db_leave db
danielk197781620542008-06-07 05:19:37 +00001179 set nRef $stats(ref)
1180 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1181 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001182 }
1183
danielk197752b472a2008-05-05 16:23:55 +00001184 # If there is an open database handle and no open transaction,
1185 # and the pager is not running in exclusive-locking mode,
1186 # check that the pager is in "unlocked" state. Theoretically,
1187 # if a call to xUnlock() failed due to an IO error the underlying
1188 # file may still be locked.
1189 #
1190 ifcapable pragma {
1191 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001192 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001193 && [db one {pragma locking_mode}] eq "normal"
1194 && [sqlite3_get_autocommit db]
1195 } {
1196 do_test $testname.$n.5 {
1197 set bt [btree_from_db db]
1198 db_enter db
1199 array set stats [btree_pager_stats $bt]
1200 db_leave db
1201 set stats(state)
1202 } 0
1203 }
1204 }
1205
danielk197732554c12005-01-22 03:39:39 +00001206 # If an IO error occured, then the checksum of the database should
1207 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001208 #
drh1aa5af12008-03-07 19:51:14 +00001209 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001210 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001211 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001212 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001213 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +00001214 cksum
1215 } $checksum
1216 }
1217
drh1aa5af12008-03-07 19:51:14 +00001218 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001219 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001220 if {[info exists ::ioerropts(-cleanup)]} {
1221 catch $::ioerropts(-cleanup)
1222 }
danielk197732554c12005-01-22 03:39:39 +00001223 }
1224 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001225 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001226 unset ::ioerropts
1227}
1228
drh93aed5a2008-01-16 17:46:38 +00001229# Return a checksum based on the contents of the main database associated
1230# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001231#
1232proc cksum {{db db}} {
1233 set txt [$db eval {
1234 SELECT name, type, sql FROM sqlite_master order by name
1235 }]\n
1236 foreach tbl [$db eval {
1237 SELECT name FROM sqlite_master WHERE type='table' order by name
1238 }] {
1239 append txt [$db eval "SELECT * FROM $tbl"]\n
1240 }
1241 foreach prag {default_synchronous default_cache_size} {
1242 append txt $prag-[$db eval "PRAGMA $prag"]\n
1243 }
1244 set cksum [string length $txt]-[md5 $txt]
1245 # puts $cksum-[file size test.db]
1246 return $cksum
1247}
1248
drh93aed5a2008-01-16 17:46:38 +00001249# Generate a checksum based on the contents of the main and temp tables
1250# database $db. If the checksum of two databases is the same, and the
1251# integrity-check passes for both, the two databases are identical.
1252#
drh1db639c2008-01-17 02:36:28 +00001253proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001254 set ret [list]
1255 ifcapable tempdb {
1256 set sql {
1257 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1258 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1259 SELECT 'sqlite_master' UNION
1260 SELECT 'sqlite_temp_master' ORDER BY 1
1261 }
1262 } else {
1263 set sql {
1264 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1265 SELECT 'sqlite_master' ORDER BY 1
1266 }
1267 }
1268 set tbllist [$db eval $sql]
1269 set txt {}
1270 foreach tbl $tbllist {
1271 append txt [$db eval "SELECT * FROM $tbl"]
1272 }
1273 foreach prag {default_cache_size} {
1274 append txt $prag-[$db eval "PRAGMA $prag"]\n
1275 }
1276 # puts txt=$txt
1277 return [md5 $txt]
1278}
1279
drhdc2c4912009-02-04 22:46:47 +00001280# Generate a checksum based on the contents of a single database with
1281# a database connection. The name of the database is $dbname.
1282# Examples of $dbname are "temp" or "main".
1283#
1284proc dbcksum {db dbname} {
1285 if {$dbname=="temp"} {
1286 set master sqlite_temp_master
1287 } else {
1288 set master $dbname.sqlite_master
1289 }
1290 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1291 set txt [$db eval "SELECT * FROM $master"]\n
1292 foreach tab $alltab {
1293 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1294 }
1295 return [md5 $txt]
1296}
1297
danielk197735754ac2008-03-21 17:29:37 +00001298proc memdebug_log_sql {{filename mallocs.sql}} {
1299
danielk19776f332c12008-03-21 14:22:44 +00001300 set data [sqlite3_memdebug_log dump]
1301 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001302 if {$nFrame < 0} { return "" }
1303
danielk197735754ac2008-03-21 17:29:37 +00001304 set database temp
1305
danielk19776ab3a2e2009-02-19 14:39:25 +00001306 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001307
1308 set sql ""
1309 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001310 set nCall [lindex $e 0]
1311 set nByte [lindex $e 1]
1312 set lStack [lrange $e 2 end]
1313 append sql "INSERT INTO ${database}.malloc VALUES"
1314 append sql "('test', $nCall, $nByte, '$lStack');\n"
1315 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001316 set frames($f) 1
1317 }
1318 }
1319
1320 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001321 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001322
1323 foreach f [array names frames] {
1324 set addr [format %x $f]
1325 set cmd "addr2line -e [info nameofexec] $addr"
1326 set line [eval exec $cmd]
1327 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001328
1329 set file [lindex [split $line :] 0]
1330 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001331 }
1332
danielk197735754ac2008-03-21 17:29:37 +00001333 foreach f [array names files] {
1334 set contents ""
1335 catch {
1336 set fd [open $f]
1337 set contents [read $fd]
1338 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001339 }
danielk197735754ac2008-03-21 17:29:37 +00001340 set contents [string map {' ''} $contents]
1341 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001342 }
danielk19776f332c12008-03-21 14:22:44 +00001343
danielk197735754ac2008-03-21 17:29:37 +00001344 set fd [open $filename w]
1345 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1346 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001347}
drh93aed5a2008-01-16 17:46:38 +00001348
danf5894502009-10-07 18:41:19 +00001349# Drop all tables in database [db]
1350proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001351 ifcapable trigger&&foreignkey {
1352 set pk [$db one "PRAGMA foreign_keys"]
1353 $db eval "PRAGMA foreign_keys = OFF"
1354 }
drh9a6ffc82010-02-15 18:03:20 +00001355 foreach {idx name file} [db eval {PRAGMA database_list}] {
1356 if {$idx==1} {
1357 set master sqlite_temp_master
1358 } else {
1359 set master $name.sqlite_master
1360 }
1361 foreach {t type} [$db eval "
1362 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001363 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001364 "] {
dana16d1062010-09-28 17:37:28 +00001365 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001366 }
danf5894502009-10-07 18:41:19 +00001367 }
shaneh4e7b32f2009-12-17 22:12:51 +00001368 ifcapable trigger&&foreignkey {
1369 $db eval "PRAGMA foreign_keys = $pk"
1370 }
danf5894502009-10-07 18:41:19 +00001371}
1372
dan71cb5182010-04-26 12:39:03 +00001373#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001374# If a test script is executed with global variable $::G(perm:name) set to
1375# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1376# in rollback mode. The following Tcl procs are used to make this less
1377# intrusive:
dan71cb5182010-04-26 12:39:03 +00001378#
1379# wal_set_journal_mode ?DB?
1380#
1381# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1382# connection handle DB. Otherwise, this command is a no-op.
1383#
1384# wal_check_journal_mode TESTNAME ?DB?
1385#
1386# If running a WAL test, execute a tests case that fails if the main
1387# database for connection handle DB is not currently a WAL database.
1388# Otherwise (if not running a WAL permutation) this is a no-op.
1389#
1390# wal_is_wal_mode
1391#
1392# Returns true if this test should be run in WAL mode. False otherwise.
1393#
1394proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001395 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001396}
1397proc wal_set_journal_mode {{db db}} {
1398 if { [wal_is_wal_mode] } {
1399 $db eval "PRAGMA journal_mode = WAL"
1400 }
1401}
1402proc wal_check_journal_mode {testname {db db}} {
1403 if { [wal_is_wal_mode] } {
1404 $db eval { SELECT * FROM sqlite_master }
1405 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
1406 }
1407}
1408
dan430e74c2010-06-07 17:47:26 +00001409proc permutation {} {
1410 set perm ""
1411 catch {set perm $::G(perm:name)}
1412 set perm
1413}
dancb79e512010-08-06 13:50:07 +00001414proc presql {} {
1415 set presql ""
1416 catch {set presql $::G(perm:presql)}
1417 set presql
1418}
dan430e74c2010-06-07 17:47:26 +00001419
danc1a60c52010-06-07 14:28:16 +00001420#-------------------------------------------------------------------------
1421#
1422proc slave_test_script {script} {
1423
1424 # Create the interpreter used to run the test script.
1425 interp create tinterp
1426
1427 # Populate some global variables that tester.tcl expects to see.
1428 foreach {var value} [list \
1429 ::argv0 $::argv0 \
1430 ::argv {} \
1431 ::SLAVE 1 \
1432 ] {
1433 interp eval tinterp [list set $var $value]
1434 }
1435
1436 # The alias used to access the global test counters.
1437 tinterp alias set_test_counter set_test_counter
1438
1439 # Set up the ::cmdlinearg array in the slave.
1440 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
1441
dan430e74c2010-06-07 17:47:26 +00001442 # Set up the ::G array in the slave.
1443 interp eval tinterp [list array set ::G [array get ::G]]
1444
danc1a60c52010-06-07 14:28:16 +00001445 # Load the various test interfaces implemented in C.
1446 load_testfixture_extensions tinterp
1447
1448 # Run the test script.
1449 interp eval tinterp $script
1450
danea5542d2010-07-06 11:26:15 +00001451 # Check if the interpreter call [run_thread_tests]
1452 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
1453 set ::run_thread_tests_called 1
1454 }
1455
danc1a60c52010-06-07 14:28:16 +00001456 # Delete the interpreter used to run the test script.
1457 interp delete tinterp
1458}
1459
dan430e74c2010-06-07 17:47:26 +00001460proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00001461 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00001462
dan6a64d672011-04-04 15:38:16 +00001463 if {[info exists ::G(start:permutation)]} {
1464 if {[permutation] != $::G(start:permutation)} return
1465 unset ::G(start:permutation)
1466 }
1467 if {[info exists ::G(start:file)]} {
1468 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
1469 unset ::G(start:file)
1470 }
1471
dan92d516a2010-07-05 14:54:48 +00001472 # Remember the value of the shared-cache setting. So that it is possible
1473 # to check afterwards that it was not modified by the test script.
1474 #
1475 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00001476
dan92d516a2010-07-05 14:54:48 +00001477 # Run the test script in a slave interpreter.
1478 #
danea5542d2010-07-06 11:26:15 +00001479 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00001480 reset_prng_state
1481 set ::sqlite_open_file_count 0
1482 set time [time { slave_test_script [list source $zFile] }]
1483 set ms [expr [lindex $time 0] / 1000]
1484
dan92d516a2010-07-05 14:54:48 +00001485 # Test that all files opened by the test script were closed. Omit this
1486 # if the test script has "thread" in its name. The open file counter
1487 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00001488 #
danea5542d2010-07-06 11:26:15 +00001489 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00001490 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
1491 }
dan430e74c2010-06-07 17:47:26 +00001492 set ::sqlite_open_file_count 0
1493
dan0626dfc2010-06-15 06:56:37 +00001494 # Test that the global "shared-cache" setting was not altered by
1495 # the test script.
1496 #
1497 ifcapable shared_cache {
1498 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
1499 do_test ${tail}-sharedcachesetting [list set {} $res] 1
1500 }
dan430e74c2010-06-07 17:47:26 +00001501
dan92d516a2010-07-05 14:54:48 +00001502 # Add some info to the output.
1503 #
dan0626dfc2010-06-15 06:56:37 +00001504 puts "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00001505 show_memstats
danc1a60c52010-06-07 14:28:16 +00001506}
1507
dan59257dc2010-08-04 11:34:31 +00001508# Open a new connection on database test.db and execute the SQL script
1509# supplied as an argument. Before returning, close the new conection and
1510# restore the 4 byte fields starting at header offsets 28, 92 and 96
1511# to the values they held before the SQL was executed. This simulates
1512# a write by a pre-3.7.0 client.
1513#
1514proc sql36231 {sql} {
1515 set B [hexio_read test.db 92 8]
1516 set A [hexio_read test.db 28 4]
1517 sqlite3 db36231 test.db
1518 catch { db36231 func a_string a_string }
1519 execsql $sql db36231
1520 db36231 close
1521 hexio_write test.db 28 $A
1522 hexio_write test.db 92 $B
1523 return ""
1524}
danf5894502009-10-07 18:41:19 +00001525
danccc7ff42010-11-29 12:06:45 +00001526proc db_save {} {
1527 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
1528 foreach f [glob -nocomplain test.db*] {
1529 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00001530 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00001531 }
1532}
1533proc db_save_and_close {} {
1534 db_save
1535 catch { db close }
1536 return ""
1537}
1538proc db_restore {} {
1539 foreach f [glob -nocomplain test.db*] { forcedelete $f }
1540 foreach f2 [glob -nocomplain sv_test.db*] {
1541 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00001542 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00001543 }
1544}
1545proc db_restore_and_reopen {{dbfile test.db}} {
1546 catch { db close }
1547 db_restore
1548 sqlite3 db $dbfile
1549}
1550proc db_delete_and_reopen {{file test.db}} {
1551 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00001552 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00001553 sqlite3 db $file
1554}
1555
danielk197745901d62004-11-10 15:27:38 +00001556# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
1557# to non-zero, then set the global variable $AUTOVACUUM to 1.
1558set AUTOVACUUM $sqlite_options(default_autovacuum)
danielk1977ee8b7992009-03-26 17:13:06 +00001559
1560source $testdir/thread_common.tcl
danddf80eb2010-10-25 12:47:43 +00001561source $testdir/malloc_common.tcl