3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements some common TCL routines used for regression
12 # testing the SQLite library
14 # $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
16 #-------------------------------------------------------------------------
17 # The commands provided by the code in this file to help with creating
18 # test cases are as follows:
20 # Commands to manipulate the db and the file-system at a high level:
26 # delete_file FILENAME
27 # drop_all_tables ?DB?
28 # drop_all_indexes ?DB?
30 # forcedelete FILENAME
32 # Test the capability of the SQLite version built into the interpreter to
33 # determine if a specific test can be run:
38 # Calulate checksums based on database contents:
44 # Commands to execute/explain SQL statements:
49 # explain_no_trace SQL
54 # Commands to run test cases:
56 # do_ioerr_test TESTNAME ARGS...
58 # integrity_check TESTNAME ?DB?
59 # verify_ex_errcode TESTNAME EXPECTED ?DB?
60 # do_test TESTNAME SCRIPT EXPECTED
61 # do_execsql_test TESTNAME SQL EXPECTED
62 # do_catchsql_test TESTNAME SQL EXPECTED
63 # do_timed_execsql_test TESTNAME SQL EXPECTED
65 # Commands providing a lower level interface to the global test counters:
67 # set_test_counter COUNTER ?VALUE?
68 # omit_test TESTNAME REASON ?APPEND?
72 # Command run at the end of each test file:
76 # Commands to help create test files that run with the "WAL" and other
77 # permutations (see file permutations.test):
80 # wal_set_journal_mode ?DB?
81 # wal_check_journal_mode TESTNAME?DB?
85 # Command to test whether or not --verbose=1 was specified on the command
86 # line (returns 0 for not-verbose, 1 for verbose and 2 for "verbose in the
92 # Only run this script once. If sourced a second time, make it a no-op
93 if {[info exists
::tester_tcl_has_run]} return
95 # Set the precision of FP arithmatic used by the interpreter. And
96 # configure SQLite to take database file locks on the page that begins
97 # 64KB into the database file instead of the one 1GB in. This means
98 # the code that handles that special case can be tested without creating
99 # very large database files.
102 sqlite3_test_control_pending_byte
0x0010000
105 # If the pager codec is available, create a wrapper for the [sqlite3]
106 # command that appends "-key {xyzzy}" to the command line. i.e. this:
112 # sqlite3 db test.db -key {xyzzy}
114 if {[info command sqlite_orig
]==""} {
115 rename sqlite3 sqlite_orig
116 proc sqlite3
{args
} {
117 if {[llength $args]>=2 && [string index
[lindex $args 0] 0]!="-"} {
118 # This command is opening a new database connection.
120 if {[info exists
::G(perm
:sqlite3_args
)]} {
121 set args
[concat $args $::G(perm
:sqlite3_args
)]
123 if {[sqlite_orig
-has-codec
] && ![info exists
::do_not_use_codec]} {
124 lappend args
-key {xyzzy
}
127 set res
[uplevel 1 sqlite_orig
$args]
128 if {[info exists
::G(perm
:presql
)]} {
129 [lindex $args 0] eval $::G(perm
:presql
)
131 if {[info exists
::G(perm
:dbconfig
)]} {
132 set ::dbhandle [lindex $args 0]
133 uplevel #0 $::G(perm:dbconfig)
135 [lindex $args 0] cache size
3
138 # This command is not opening a new database connection. Pass the
139 # arguments through to the C implementation as the are.
141 uplevel 1 sqlite_orig
$args
146 proc getFileRetries
{} {
147 if {![info exists
::G(file-retries
)]} {
149 # NOTE: Return the default number of retries for [file] operations. A
150 # value of zero or less here means "disabled".
152 return [expr {$::tcl_platform(platform
) eq
"windows" ?
50 : 0}]
154 return $::G(file-retries
)
157 proc getFileRetryDelay
{} {
158 if {![info exists
::G(file-retry-delay
)]} {
160 # NOTE: Return the default number of milliseconds to wait when retrying
161 # failed [file] operations. A value of zero or less means "do not
164 return 100; # TODO: Good default?
166 return $::G(file-retry-delay
)
169 # Return the string representing the name of the current directory. On
170 # Windows, the result is "normalized" to whatever our parent command shell
171 # is using to prevent case-mismatch issues.
174 if {$::tcl_platform(platform
) eq
"windows"} {
176 # NOTE: Cannot use [file normalize] here because it would alter the
177 # case of the result to what Tcl considers canonical, which would
178 # defeat the purpose of this procedure.
180 if {[info exists
::env(ComSpec
)]} {
181 set comSpec
$::env(ComSpec
)
183 # NOTE: Hard-code the typical default value.
184 set comSpec
{C
:\Windows
\system32
\cmd.exe
}
186 return [string map
[list \\ /] \
187 [string trim
[exec -- $comSpec /c CD
]]]
193 # Copy file $from into $to. This is used because some versions of
194 # TCL for windows (notably the 8.4.1 binary package shipped with the
195 # current mingw release) have a broken "file copy" command.
197 proc copy_file
{from to
} {
198 do_copy_file false
$from $to
201 proc forcecopy
{from to
} {
202 do_copy_file true
$from $to
205 proc do_copy_file
{force from to
} {
206 set nRetry
[getFileRetries
] ;# Maximum number of retries.
207 set nDelay
[getFileRetryDelay
] ;# Delay in ms before retrying.
209 # On windows, sometimes even a [file copy -force] can fail. The cause is
210 # usually "tag-alongs" - programs like anti-virus software, automatic backup
211 # tools and various explorer extensions that keep a file open a little longer
212 # than we expect, causing the delete to fail.
214 # The solution is to wait a short amount of time before retrying the copy.
217 for {set i
0} {$i<$nRetry} {incr i
} {
220 file copy
-force $from $to
226 if {$nDelay > 0} { after $nDelay }
228 if {$rc} { error $msg }
231 file copy
-force $from $to
238 # Check if a file name is relative
240 proc is_relative_file
{ file } {
241 return [expr {[file pathtype
$file] != "absolute"}]
244 # If the VFS supports using the current directory, returns [pwd];
245 # otherwise, it returns only the provided suffix string (which is
248 proc test_pwd
{ args
} {
249 if {[llength $args] > 0} {
250 set suffix1
[lindex $args 0]
251 if {[llength $args] > 1} {
252 set suffix2
[lindex $args 1]
257 set suffix1
""; set suffix2
""
260 return "[get_pwd]$suffix1"
266 # Delete a file or directory
268 proc delete_file
{args
} {
269 do_delete_file false
{*}$args
272 proc forcedelete
{args
} {
273 do_delete_file true
{*}$args
276 proc do_delete_file
{force args
} {
277 set nRetry
[getFileRetries
] ;# Maximum number of retries.
278 set nDelay
[getFileRetryDelay
] ;# Delay in ms before retrying.
280 foreach filename $args {
281 # On windows, sometimes even a [file delete -force] can fail just after
282 # a file is closed. The cause is usually "tag-alongs" - programs like
283 # anti-virus software, automatic backup tools and various explorer
284 # extensions that keep a file open a little longer than we expect, causing
285 # the delete to fail.
287 # The solution is to wait a short amount of time before retrying the
291 for {set i
0} {$i<$nRetry} {incr i
} {
294 file delete
-force $filename
296 file delete
$filename
300 if {$nDelay > 0} { after $nDelay }
302 if {$rc} { error $msg }
305 file delete
-force $filename
307 file delete
$filename
313 if {$::tcl_platform(platform
) eq
"windows"} {
314 proc do_remove_win32_dir
{args
} {
315 set nRetry
[getFileRetries
] ;# Maximum number of retries.
316 set nDelay
[getFileRetryDelay
] ;# Delay in ms before retrying.
318 foreach dirName
$args {
319 # On windows, sometimes even a [remove_win32_dir] can fail just after
320 # a directory is emptied. The cause is usually "tag-alongs" - programs
321 # like anti-virus software, automatic backup tools and various explorer
322 # extensions that keep a file open a little longer than we expect,
323 # causing the delete to fail.
325 # The solution is to wait a short amount of time before retrying the
329 for {set i
0} {$i < $nRetry} {incr i
} {
331 remove_win32_dir
$dirName
334 if {$nDelay > 0} { after $nDelay }
336 if {$rc} { error $msg }
338 remove_win32_dir
$dirName
343 proc do_delete_win32_file
{args
} {
344 set nRetry
[getFileRetries
] ;# Maximum number of retries.
345 set nDelay
[getFileRetryDelay
] ;# Delay in ms before retrying.
347 foreach fileName
$args {
348 # On windows, sometimes even a [delete_win32_file] can fail just after
349 # a file is closed. The cause is usually "tag-alongs" - programs like
350 # anti-virus software, automatic backup tools and various explorer
351 # extensions that keep a file open a little longer than we expect,
352 # causing the delete to fail.
354 # The solution is to wait a short amount of time before retrying the
358 for {set i
0} {$i < $nRetry} {incr i
} {
360 delete_win32_file
$fileName
363 if {$nDelay > 0} { after $nDelay }
365 if {$rc} { error $msg }
367 delete_win32_file
$fileName
373 proc execpresql
{handle args
} {
374 trace remove execution
$handle enter
[list execpresql
$handle]
375 if {[info exists
::G(perm
:presql
)]} {
376 $handle eval $::G(perm
:presql
)
380 # This command should be called after loading tester.tcl from within
381 # all test scripts that are incompatible with encryption codecs.
383 proc do_not_use_codec
{} {
384 set ::do_not_use_codec 1
387 unset -nocomplain do_not_use_codec
389 # Return true if the "reserved_bytes" integer on database files is non-zero.
391 proc nonzero_reserved_bytes
{} {
392 return [sqlite3
-has-codec
]
395 # Print a HELP message and exit
397 proc print_help_and_quit
{} {
399 --pause Wait
for user input before continuing
400 --soft-heap
-limit
=N Set the soft-heap-limit to N
401 --hard-heap
-limit
=N Set the hard-heap-limit to N
402 --maxerror=N Quit
after N errors
403 --verbose=(0|
1) Control the amount of output. Default '
1'
404 --output=FILE
set --verbose=2 and output to FILE. Implies
-q
405 -q Shorthand
for --verbose=0
411 # The following block only runs the first time this file is sourced. It
412 # does not run in slave interpreters (since the ::cmdlinearg array is
413 # populated before the test script is run in slave interpreters).
415 if {[info exists cmdlinearg
]==0} {
417 # Parse any options specified in the $argv array. This script accepts the
421 # --soft-heap-limit=NN
422 # --hard-heap-limit=NN
429 # --file-retry-delay=N
430 # --start=[$permutation:]$testfile
435 # --testdir=$dir Run tests in subdirectory $dir
438 set cmdlinearg
(soft-heap-limit
) 0
439 set cmdlinearg
(hard-heap-limit
) 0
440 set cmdlinearg
(maxerror
) 1000
441 set cmdlinearg
(malloctrace
) 0
442 set cmdlinearg
(backtrace
) 10
443 set cmdlinearg
(binarylog
) 0
444 set cmdlinearg
(soak
) 0
445 set cmdlinearg
(file-retries
) 0
446 set cmdlinearg
(file-retry-delay
) 0
447 set cmdlinearg
(start
) ""
448 set cmdlinearg
(match
) ""
449 set cmdlinearg
(verbose
) ""
450 set cmdlinearg
(output
) ""
451 set cmdlinearg
(testdir
) "testdir"
455 switch -regexp -- $a {
457 # Wait for user input before continuing. This is to give the user an
458 # opportunity to connect profiling tools to the process.
459 puts -nonewline "Press RETURN to begin..."
463 {^
-+soft-heap-limit
=.
+$} {
464 foreach {dummy cmdlinearg
(soft-heap-limit
)} [split $a =] break
466 {^
-+hard-heap-limit
=.
+$} {
467 foreach {dummy cmdlinearg
(hard-heap-limit
)} [split $a =] break
470 foreach {dummy cmdlinearg
(maxerror
)} [split $a =] break
472 {^
-+malloctrace
=.
+$} {
473 foreach {dummy cmdlinearg
(malloctrace
)} [split $a =] break
474 if {$cmdlinearg(malloctrace
)} {
475 if {0==$::sqlite_options(memdebug
)} {
476 set err
"Error: --malloctrace=1 requires an SQLITE_MEMDEBUG build"
480 sqlite3_memdebug_log start
484 foreach {dummy cmdlinearg
(backtrace
)} [split $a =] break
485 sqlite3_memdebug_backtrace
$cmdlinearg(backtrace
)
488 foreach {dummy cmdlinearg
(binarylog
)} [split $a =] break
489 set cmdlinearg
(binarylog
) [file normalize
$cmdlinearg(binarylog
)]
492 foreach {dummy cmdlinearg
(soak
)} [split $a =] break
493 set ::G(issoak
) $cmdlinearg(soak
)
495 {^
-+file-retries
=.
+$} {
496 foreach {dummy cmdlinearg
(file-retries
)} [split $a =] break
497 set ::G(file-retries
) $cmdlinearg(file-retries
)
499 {^
-+file-retry-delay
=.
+$} {
500 foreach {dummy cmdlinearg
(file-retry-delay
)} [split $a =] break
501 set ::G(file-retry-delay
) $cmdlinearg(file-retry-delay
)
504 foreach {dummy cmdlinearg
(start
)} [split $a =] break
506 set ::G(start
:file) $cmdlinearg(start
)
507 if {[regexp {(.
*):(.
*)} $cmdlinearg(start
) -> s.perm s.
file]} {
508 set ::G(start
:permutation
) ${s.perm
}
509 set ::G(start
:file) ${s.
file}
511 if {$::G(start
:file) == ""} {unset ::G(start
:file)}
514 foreach {dummy cmdlinearg
(match
)} [split $a =] break
516 set ::G(match
) $cmdlinearg(match
)
517 if {$::G(match
) == ""} {unset ::G(match
)}
521 foreach {dummy cmdlinearg
(output
)} [split $a =] break
522 set cmdlinearg
(output
) [file normalize
$cmdlinearg(output
)]
523 if {$cmdlinearg(verbose
)==""} {
524 set cmdlinearg
(verbose
) 2
528 foreach {dummy cmdlinearg
(verbose
)} [split $a =] break
529 if {$cmdlinearg(verbose
)=="file"} {
530 set cmdlinearg
(verbose
) 2
531 } elseif
{[string is boolean
-strict $cmdlinearg(verbose
)]==0} {
532 error "option --verbose= must be set to a boolean or to \"file\""
536 foreach {dummy cmdlinearg
(testdir
)} [split $a =] break
542 set cmdlinearg
(output
) test-out.txt
543 set cmdlinearg
(verbose
) 2
547 if {[file tail
$a]==$a} {
550 lappend leftover
[file normalize
$a]
556 set testdir
[file normalize
$testdir]
557 set cmdlinearg
(TESTFIXTURE_HOME
) [pwd]
558 set cmdlinearg
(INFO_SCRIPT
) [file normalize
[info script
]]
559 set argv0
[file normalize
$argv0]
560 if {$cmdlinearg(testdir
)!=""} {
561 file mkdir
$cmdlinearg(testdir
)
562 cd $cmdlinearg(testdir
)
566 # Install the malloc layer used to inject OOM errors. And the 'automatic'
567 # extensions. This only needs to be done once for the process.
570 install_malloc_faultsim
1
572 autoinstall_test_functions
574 # If the --binarylog option was specified, create the logging VFS. This
575 # call installs the new VFS as the default for all SQLite connections.
577 if {$cmdlinearg(binarylog
)} {
578 vfslog new binarylog
{} vfslog.bin
581 # Set the backtrace depth, if malloc tracing is enabled.
583 if {$cmdlinearg(malloctrace
)} {
584 sqlite3_memdebug_backtrace
$cmdlinearg(backtrace
)
587 if {$cmdlinearg(output
)!=""} {
588 puts "Copying output to file $cmdlinearg(output)"
589 set ::G(output_fd
) [open $cmdlinearg(output
) w
]
590 fconfigure $::G(output_fd
) -buffering line
593 if {$cmdlinearg(verbose
)==""} {
594 set cmdlinearg
(verbose
) 1
597 if {[info commands vdbe_coverage
]!=""} {
602 # Update the soft-heap-limit each time this script is run. In that
603 # way if an individual test file changes the soft-heap-limit, it
604 # will be reset at the start of the next test file.
606 sqlite3_soft_heap_limit64
$cmdlinearg(soft-heap-limit
)
607 sqlite3_hard_heap_limit64
$cmdlinearg(hard-heap-limit
)
609 # Create a test database
614 forcedelete test.db-journal
615 forcedelete test.db-wal
617 set ::DB [sqlite3_connection_pointer db
]
618 if {[info exists
::SETUP_SQL]} {
624 # Abort early if this script has been run before.
626 if {[info exists TC
(count
)]} return
628 # Make sure memory statistics are enabled.
630 sqlite3_config_memstatus
1
632 # Initialize the test counters and set up commands to access them.
633 # Or, if this is a slave interpreter, set up aliases to write the
634 # counters in the parent interpreter.
636 if {0==[info exists
::SLAVE]} {
639 set TC
(fail_list
) [list]
640 set TC
(omit_list
) [list]
641 set TC
(warn_list
) [list]
643 proc set_test_counter
{counter args
} {
644 if {[llength $args]} {
645 set ::TC($counter) [lindex $args 0]
651 # Record the fact that a sequence of tests were omitted.
653 proc omit_test
{name reason
{append 1}} {
654 set omitList
[set_test_counter omit_list
]
656 lappend omitList
[list $name $reason]
658 set_test_counter omit_list
$omitList
661 # Record the fact that a test failed.
663 proc fail_test
{name
} {
664 set f
[set_test_counter fail_list
]
666 set_test_counter fail_list
$f
667 set_test_counter errors
[expr [set_test_counter errors
] + 1]
669 set nFail
[set_test_counter errors
]
670 if {$nFail>=$::cmdlinearg(maxerror
)} {
671 output2
"*** Giving up..."
676 # Remember a warning message to be displayed at the conclusion of all testing
678 proc warning
{msg
{append 1}} {
679 output2
"Warning: $msg"
680 set warnList
[set_test_counter warn_list
]
682 lappend warnList
$msg
684 set_test_counter warn_list
$warnList
688 # Increment the number of tests run
691 set_test_counter count
[expr [set_test_counter count
] + 1]
694 # Return true if --verbose=1 was specified on the command line. Otherwise,
698 return $::cmdlinearg(verbose
)
701 # Use the following commands instead of [puts] for test output within
702 # this file. Test scripts can still use regular [puts], which is directed
703 # to stdout and, if one is open, the --output file.
705 # output1: output that should be printed if --verbose=1 was specified.
706 # output2: output that should be printed unconditionally.
707 # output2_if_no_verbose: output that should be printed only if --verbose=0.
709 proc output1
{args
} {
712 uplevel output2
$args
714 uplevel puts [lrange $args 0 end-1
] $::G(output_fd
) [lrange $args end end
]
717 proc output2
{args
} {
718 set nArg
[llength $args]
721 proc output2_if_no_verbose
{args
} {
724 uplevel output2
$args
726 uplevel puts [lrange $args 0 end-1
] stdout
[lrange $args end end
]
730 # Override the [puts] command so that if no channel is explicitly
731 # specified the string is written to both stdout and to the file
732 # specified by "--output=", if any.
734 proc puts_override
{args
} {
735 set nArg
[llength $args]
736 if {$nArg==1 ||
($nArg==2 && [string first
[lindex $args 0] -nonewline]==0)} {
737 uplevel puts_original
$args
738 if {[info exists
::G(output_fd
)]} {
739 uplevel puts [lrange $args 0 end-1
] $::G(output_fd
) [lrange $args end end
]
742 # A channel was explicitly specified.
743 uplevel puts_original
$args
746 rename puts puts_original
747 proc puts {args
} { uplevel puts_override
$args }
750 # Invoke the do_test procedure to run a single test
752 # The $expected parameter is the expected result. The result is the return
753 # value from the last TCL command in $cmd.
755 # Normally, $expected must match exactly. But if $expected is of the form
756 # "/regexp/" then regular expression matching is used. If $expected is
757 # "~/regexp/" then the regular expression must NOT match. If $expected is
758 # of the form "#/value-list/" then each term in value-list must be numeric
759 # and must approximately match the corresponding numeric term in $result.
760 # Values must match within 10%. Or if the $expected term is A..B then the
761 # $result term must be in between A and B.
763 proc do_test
{name cmd expected
} {
764 global argv cmdlinearg
768 sqlite3_memdebug_settitle
$name
770 # if {[llength $argv]==0} {
774 # foreach pattern $argv {
775 # if {[string match $pattern $name]} {
782 if {[info exists
::G(perm
:prefix
)]} {
783 set name
"$::G(perm:prefix)$name"
787 output1
-nonewline $name...
790 if {![info exists
::G(match
)] ||
[string match
$::G(match
) $name]} {
791 if {[catch {uplevel #0 "$cmd;\n"} result]} {
792 output2_if_no_verbose
-nonewline $name...
793 output2
"\nError: $result"
796 if {[permutation
]=="maindbname"} {
797 set result
[string map
[list [string tolower ICECUBE
] main
] $result]
799 if {[regexp {^
[~
#]?/.*/$} $expected]} {
800 # "expected" is of the form "/PATTERN/" then the result if correct if
801 # regular expression PATTERN matches the result. "~/PATTERN/" means
802 # the regular expression must not match.
803 if {[string index
$expected 0]=="~"} {
804 set re
[string range
$expected 2 end-1
]
805 if {[string index
$re 0]=="*"} {
806 # If the regular expression begins with * then treat it as a glob instead
807 set ok
[string match
$re $result]
809 set re
[string map
{# {[-0-9.]+}} $re]
810 set ok
[regexp $re $result]
813 } elseif
{[string index
$expected 0]=="#"} {
814 # Numeric range value comparison. Each term of the $result is matched
815 # against one term of $expect. Both $result and $expected terms must be
816 # numeric. The values must match within 10%. Or if $expected is of the
817 # form A..B then the $result term must be between A and B.
818 set e2
[string range
$expected 2 end-1
]
819 foreach i
$result j
$e2 {
820 if {[regexp {^
(-?
\d
+)\.
\.
(-?
\d
)$} $j all A B
]} {
821 set ok
[expr {$i+0>=$A && $i+0<=$B}]
823 set ok
[expr {$i+0>=0.9*$j && $i+0<=1.1*$j}]
827 if {$ok && [llength $result]!=[llength $e2]} {set ok
0}
829 set re
[string range
$expected 1 end-1
]
830 if {[string index
$re 0]=="*"} {
831 # If the regular expression begins with * then treat it as a glob instead
832 set ok
[string match
$re $result]
834 set re
[string map
{# {[-0-9.]+}} $re]
835 set ok
[regexp $re $result]
838 } elseif
{[regexp {^~?
\*.
*\*$} $expected]} {
839 # "expected" is of the form "*GLOB*" then the result if correct if
840 # glob pattern GLOB matches the result. "~/GLOB/" means
841 # the glob must not match.
842 if {[string index
$expected 0]=="~"} {
843 set e
[string range
$expected 1 end
]
844 set ok
[expr {![string match
$e $result]}]
846 set ok
[string match
$expected $result]
849 set ok
[expr {[string compare
$result $expected]==0}]
852 # if {![info exists ::testprefix] || $::testprefix eq ""} {
853 # error "no test prefix"
856 output2
"! $name expected: \[$expected\]\n! $name got: \[$result\]"
864 omit_test
$name "pattern mismatch" 0
871 for {set i
0} {$i < [string length
$s]} {incr i
} {
872 if {$i > 0} {append r
" "}
873 append r
[format %02X
[scan [string index
$s $i] %c
]]
878 proc catchcmd
{db
{cmd
""}} {
880 set out
[open cmds.txt w
]
883 set line
"exec $CLI $db < cmds.txt"
884 set rc
[catch { eval $line } msg
]
888 proc catchcmdex
{db
{cmd
""}} {
890 set out
[open cmds.txt w
]
891 fconfigure $out -encoding binary -translation binary
892 puts -nonewline $out $cmd
894 set line
"exec -keepnewline -- $CLI $db < cmds.txt"
895 set chans
[list stdin stdout stderr
]
896 foreach chan
$chans {
898 set modes
($chan) [fconfigure $chan]
899 fconfigure $chan -encoding binary -translation binary -buffering none
902 set rc
[catch { eval $line } msg
]
903 foreach chan
$chans {
905 eval fconfigure [list $chan] $modes($chan)
908 # puts [dumpbytes $msg]
912 proc filepath_normalize
{p
} {
913 # test cases should be written to assume "unix"-like file paths
914 if {$::tcl_platform(platform
)!="unix"} {
915 string map
[list \\ / \{/ / .db
\} .db
] \
916 [regsub -nocase -all {[a-z
]:[/\\]+} $p {/}]
921 proc do_filepath_test
{name cmd expected
} {
922 uplevel [list do_test
$name [
923 subst -nocommands { filepath_normalize
[ $cmd ] }
924 ] [filepath_normalize
$expected]]
927 proc realnum_normalize
{r
} {
928 # different TCL versions display floating point values differently.
929 string map
{1.
#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
931 proc do_realnum_test
{name cmd expected
} {
932 uplevel [list do_test
$name [
933 subst -nocommands { realnum_normalize
[ $cmd ] }
934 ] [realnum_normalize
$expected]]
937 proc fix_testname
{varname
} {
938 upvar $varname testname
939 if {[info exists
::testprefix]
940 && [string is digit
[string range
$testname 0 0]]
942 set testname
"${::testprefix}-$testname"
946 proc normalize_list
{L
} {
948 foreach l
$L {lappend L2
$l}
952 # Run SQL and verify that the number of "vmsteps" required is greater
953 # than or less than some constant.
955 proc do_vmstep_test
{tn sql nstep
{res
{}}} {
956 uplevel [list do_execsql_test
$tn.0 $sql $res]
958 set vmstep
[db status vmstep
]
959 if {[string range
$nstep 0 0]=="+"} {
960 set body
"if {$vmstep<$nstep} {
961 error \"got $vmstep, expected more than [string range $nstep 1 end]\"
964 set body
"if {$vmstep>$nstep} {
965 error \"got $vmstep, expected less than $nstep\"
969 # set name "$tn.vmstep=$vmstep,expect=$nstep"
971 uplevel [list do_test
$name $body {}]
977 # do_execsql_test TESTNAME SQL ?RES?
978 # do_execsql_test -db DB TESTNAME SQL ?RES?
980 proc do_execsql_test
{args
} {
982 if {[lindex $args 0]=="-db"} {
983 set db
[lindex $args 1]
984 set args
[lrange $args 2 end
]
987 if {[llength $args]==2} {
988 foreach {testname sql
} $args {}
990 } elseif
{[llength $args]==3} {
991 foreach {testname sql result
} $args {}
993 # With some versions of Tcl on windows, if $result is all whitespace but
994 # contains some CR/LF characters, the [list {*}$result] below returns a
995 # copy of $result instead of a zero length string. Not clear exactly why
996 # this is. The following is a workaround.
997 if {[llength $result]==0} { set result
"" }
1000 wrong
# args: should be "do_execsql_test ?-db DB? testname sql ?result?"
1004 fix_testname testname
1008 [list "execsql {$sql} $db"] \
1009 [list [list {*}$result]]
1012 proc do_catchsql_test
{testname sql result
} {
1013 fix_testname testname
1014 uplevel do_test
[list $testname] [list "catchsql {$sql}"] [list $result]
1016 proc do_timed_execsql_test
{testname sql
{result
{}}} {
1017 fix_testname testname
1018 uplevel do_test
[list $testname] [list "execsql_timed {$sql}"]\
1019 [list [list {*}$result]]
1022 # Run an EXPLAIN QUERY PLAN $sql in database "db". Then rewrite the output
1023 # as an ASCII-art graph and return a string that is that graph.
1025 # Hexadecimal literals in the output text are converted into "xxxxxx" since those
1026 # literals are pointer values that might very from one run of the test to the
1027 # next, yet we want the output to be consistent.
1029 proc query_plan_graph
{sql
} {
1030 db
eval "EXPLAIN QUERY PLAN $sql" {
1032 lappend cx
($parent) $id
1034 set a
"\n QUERY PLAN\n"
1035 append a
[append_graph
" " dx cx
0]
1036 regsub -all { 0x
[A-F0-9
]+\y
} $a { xxxxxx
} a
1037 regsub -all {(MATERIALIZE|CO-ROUTINE|SUBQUERY
) \d
+\y
} $a {\1 xxxxxx
} a
1038 regsub -all {\((join|subquery
)-\d
+\)} $a {(\1-xxxxxx
)} a
1042 # Helper routine for [query_plan_graph SQL]:
1044 # Output rows of the graph that are children of $level.
1046 # prefix: Prepend to every output line
1048 # dxname: Name of an array variable that stores text describe
1049 # The description for $id is $dx($id)
1051 # cxname: Name of an array variable holding children of item.
1052 # Children of $id are $cx($id)
1054 # level: Render all lines that are children of $level
1056 proc append_graph
{prefix dxname cxname level
} {
1057 upvar $dxname dx
$cxname cx
1061 for {set i
0} {$i<$n} {incr i
} {
1062 set id
[lindex $x $i]
1070 append a
$prefix$p1$dx($id)\n
1071 if {[info exists cx
($id)]} {
1072 append a
[append_graph
"$prefix$p2" dx cx
$id]
1078 # Do an EXPLAIN QUERY PLAN test on input $sql with expected results $res
1080 # If $res begins with a "\s+QUERY PLAN\n" then it is assumed to be the
1081 # complete graph which must match the output of [query_plan_graph $sql]
1084 # If $res does not begin with "\s+QUERY PLAN\n" then take it is a string
1085 # that must be found somewhere in the query plan output.
1087 proc do_eqp_test
{name sql res
} {
1088 if {[regexp {^
\s
+QUERY PLAN
\n} $res]} {
1090 set query_plan
[query_plan_graph
$sql]
1092 if {[list {*}$query_plan]==[list {*}$res]} {
1093 uplevel [list do_test
$name [list set {} ok
] ok
]
1096 do_test
$name [list query_plan_graph
$sql] $res
1100 if {[string index
$res 0]!="/"} {
1103 uplevel do_execsql_test
$name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
1108 #-------------------------------------------------------------------------
1109 # Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
1111 # Where switches are:
1113 # -errorformat FMTSTRING
1119 proc do_select_tests
{prefix args
} {
1121 set testlist
[lindex $args end
]
1122 set switches
[lrange $args 0 end-1
]
1129 for {set i
0} {$i < [llength $switches]} {incr i
} {
1130 set s
[lindex $switches $i]
1131 set n
[string length
$s]
1132 if {$n>=2 && [string equal
-length $n $s "-query"]} {
1133 set tclquery
[list execsql
[lindex $switches [incr i
]]]
1134 } elseif
{$n>=2 && [string equal
-length $n $s "-tclquery"]} {
1135 set tclquery
[lindex $switches [incr i
]]
1136 } elseif
{$n>=2 && [string equal
-length $n $s "-errorformat"]} {
1137 set errfmt
[lindex $switches [incr i
]]
1138 } elseif
{$n>=2 && [string equal
-length $n $s "-repair"]} {
1139 set repair
[lindex $switches [incr i
]]
1140 } elseif
{$n>=2 && [string equal
-length $n $s "-count"]} {
1143 error "unknown switch: $s"
1147 if {$countonly && $errfmt!=""} {
1148 error "Cannot use -count and -errorformat together"
1150 set nTestlist
[llength $testlist]
1151 if {$nTestlist%3 ||
$nTestlist==0 } {
1152 error "SELECT test list contains [llength $testlist] elements"
1156 foreach {tn sql res
} $testlist {
1157 if {$tclquery != ""} {
1159 uplevel do_test
${prefix
}.
$tn [list $tclquery] [list [list {*}$res]]
1160 } elseif
{$countonly} {
1162 db
eval $sql {incr nRow
}
1163 uplevel do_test
${prefix
}.
$tn [list [list set {} $nRow]] [list $res]
1164 } elseif
{$errfmt==""} {
1165 uplevel do_execsql_test
${prefix
}.
${tn
} [list $sql] [list [list {*}$res]]
1167 set res
[list 1 [string trim
[format $errfmt {*}$res]]]
1168 uplevel do_catchsql_test
${prefix
}.
${tn
} [list $sql] [list $res]
1175 proc delete_all_data
{} {
1176 db
eval {SELECT tbl_name AS t FROM sqlite_master WHERE type
= 'table'
} {
1177 db
eval "DELETE FROM '[string map {' ''} $t]'"
1181 # Run an SQL script.
1182 # Return the number of microseconds per statement.
1184 proc speed_trial
{name numstmt units sql
} {
1185 output2
-nonewline [format {%-21.21s
} $name...
]
1187 set speed
[time {sqlite3_exec_nr db
$sql}]
1188 set tm
[lindex $speed 0]
1190 set rate
[format %20s
"many"]
1192 set rate
[format %20.5f
[expr {1000000.0*$numstmt/$tm}]]
1195 output2
[format {%12d uS
%s
%s
} $tm $rate $u2]
1197 set total_time
[expr {$total_time+$tm}]
1198 lappend ::speed_trial_times $name $tm
1200 proc speed_trial_tcl
{name numstmt units script
} {
1201 output2
-nonewline [format {%-21.21s
} $name...
]
1203 set speed
[time {eval $script}]
1204 set tm
[lindex $speed 0]
1206 set rate
[format %20s
"many"]
1208 set rate
[format %20.5f
[expr {1000000.0*$numstmt/$tm}]]
1211 output2
[format {%12d uS
%s
%s
} $tm $rate $u2]
1213 set total_time
[expr {$total_time+$tm}]
1214 lappend ::speed_trial_times $name $tm
1216 proc speed_trial_init
{name
} {
1219 set ::speed_trial_times [list]
1220 sqlite3 versdb
:memory:
1221 set vers
[versdb one
{SELECT sqlite_source_id
()}]
1223 output2
"SQLite $vers"
1225 proc speed_trial_summary
{name
} {
1227 output2
[format {%-21.21s
%12d uS TOTAL
} $name $total_time]
1230 sqlite3 versdb
:memory:
1231 set vers
[lindex [versdb one
{SELECT sqlite_source_id
()}] 0]
1233 output2
"CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
1234 foreach {test us
} $::speed_trial_times {
1235 output2
"INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
1240 # Clear out left-over configuration setup from the end of a test
1242 proc finish_test_precleanup
{} {
1246 catch {unregister_devsim
}
1247 catch {unregister_jt_vfs
}
1248 catch {unregister_demovfs
}
1251 # Run this routine last
1253 proc finish_test
{} {
1255 finish_test_precleanup
1256 if {[llength $argv]>0} {
1257 # If additional test scripts are specified on the command-line,
1258 # run them also, before quitting.
1259 proc finish_test
{} {
1260 finish_test_precleanup
1263 foreach extra
$argv {
1264 puts "Running \"$extra\""
1265 db_delete_and_reopen
1266 uplevel #0 source $extra
1270 if {0==[info exists
::SLAVE]} { finalize_testing
}
1272 proc finalize_testing
{} {
1273 global sqlite_open_file_count
1275 set omitList
[set_test_counter omit_list
]
1283 # sqlite3_clear_tsd_memdebug
1285 sqlite3_reset_auto_extension
1287 sqlite3_soft_heap_limit64
0
1288 sqlite3_hard_heap_limit64
0
1289 set nTest
[incr_ntest
]
1290 set nErr
[set_test_counter errors
]
1293 if {[file readable known-problems.txt
]} {
1294 set fd
[open known-problems.txt
]
1295 set content
[read $fd]
1297 foreach x
$content {set known_error
($x) 1}
1298 foreach x
[set_test_counter fail_list
] {
1299 if {[info exists known_error
($x)]} {incr nKnown
}
1303 output2
"[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
1304 out of $nTest tests"
1307 if {[catch {exec hostname
} hname
]==0} {set cpuinfo
[string trim
$hname]}
1308 append cpuinfo
" $::tcl_platform(os)"
1309 append cpuinfo
" [expr {$::tcl_platform(pointerSize)*8}]-bit"
1310 append cpuinfo
" [string map {E -e} $::tcl_platform(byteOrder)]"
1311 output2
"SQLite [sqlite3 -sourceid]"
1312 output2
"$nErr errors out of $nTest tests on $cpuinfo"
1314 if {$nErr>$nKnown} {
1315 output2
-nonewline "!Failures on these tests:"
1316 foreach x
[set_test_counter fail_list
] {
1317 if {![info exists known_error
($x)]} {output2
-nonewline " $x"}
1321 foreach warning
[set_test_counter warn_list
] {
1322 output2
"Warning: $warning"
1325 if {[llength $omitList]>0} {
1326 output2
"Omitted test cases:"
1328 foreach {rec
} [lsort $omitList] {
1329 if {$rec==$prec} continue
1331 output2
[format {.
%-12s %s
} [lindex $rec 0] [lindex $rec 1]]
1334 if {$nErr>0 && ![working_64bit_int
]} {
1335 output2
"******************************************************************"
1336 output2
"N.B.: The version of TCL that you used to build this test harness"
1337 output2
"is defective in that it does not support 64-bit integers. Some or"
1338 output2
"all of the test failures above might be a result from this defect"
1339 output2
"in your TCL build."
1340 output2
"******************************************************************"
1342 if {$::cmdlinearg(binarylog
)} {
1343 vfslog finalize binarylog
1345 if {[info exists
::run_thread_tests_called]==0} {
1346 if {$sqlite_open_file_count} {
1347 output2
"$sqlite_open_file_count files were left open"
1351 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT
0] 1]>0 ||
1352 [sqlite3_memory_used
]>0} {
1353 output2
"Unfreed memory: [sqlite3_memory_used] bytes in\
1354 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
1356 ifcapable mem5||
(mem3
&&debug
) {
1357 output2
"Writing unfreed memory log to \"./memleak.txt\""
1358 sqlite3_memdebug_dump .
/memleak.txt
1361 output2
"All memory allocations freed - no leaks"
1363 sqlite3_memdebug_dump .
/memusage.txt
1367 output2
"Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
1368 output2
"Current memory usage: [sqlite3_memory_highwater] bytes"
1369 if {[info commands sqlite3_memdebug_malloc_count
] ne
""} {
1370 output2
"Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
1372 if {$::cmdlinearg(malloctrace
)} {
1373 output2
"Writing mallocs.tcl..."
1374 memdebug_log_sql mallocs.tcl
1375 sqlite3_memdebug_log stop
1376 sqlite3_memdebug_log clear
1377 if {[sqlite3_memory_used
]>0} {
1378 output2
"Writing leaks.tcl..."
1379 sqlite3_memdebug_log sync
1380 memdebug_log_sql leaks.tcl
1383 if {[info commands vdbe_coverage
]!=""} {
1384 vdbe_coverage_report
1386 foreach f
[glob -nocomplain test.db-
*-journal] {
1389 foreach f
[glob -nocomplain test.db-mj
*] {
1392 exit [expr {$nErr>0}]
1395 proc vdbe_coverage_report
{} {
1396 puts "Writing vdbe coverage report to vdbe_coverage.txt"
1399 if {[file exists ..
/sqlite3.c
]} {
1400 set fd
[open ..
/sqlite3.c
]
1402 while { ![eof $fd] } {
1405 if {[regexp {^
/\** Begin
file (.
*\.c
) \**/} $line -> file]} {
1406 lappend lSrc
[list $iLine $file]
1411 set fd
[open vdbe_coverage.txt w
]
1412 foreach miss
[vdbe_coverage report
] {
1413 foreach {line branch never
} $miss {}
1415 while {[llength $lSrc]>0 && [lindex $lSrc 0 0] < $line} {
1416 set nextfile
[lindex $lSrc 0 1]
1417 set lSrc
[lrange $lSrc 1 end
]
1419 if {$nextfile != ""} {
1421 puts $fd "### $nextfile ###"
1423 puts $fd "Vdbe branch $line: never $never (path $branch)"
1428 # Display memory statistics for analysis and debugging purposes.
1430 proc show_memstats
{} {
1431 set x
[sqlite3_status SQLITE_STATUS_MEMORY_USED
0]
1432 set y
[sqlite3_status SQLITE_STATUS_MALLOC_SIZE
0]
1433 set val
[format {now
%10d max
%10d max-size
%10d
} \
1434 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
1435 output1
"Memory used: $val"
1436 set x
[sqlite3_status SQLITE_STATUS_MALLOC_COUNT
0]
1437 set val
[format {now
%10d max
%10d
} [lindex $x 1] [lindex $x 2]]
1438 output1
"Allocation count: $val"
1439 set x
[sqlite3_status SQLITE_STATUS_PAGECACHE_USED
0]
1440 set y
[sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE
0]
1441 set val
[format {now
%10d max
%10d max-size
%10d
} \
1442 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
1443 output1
"Page-cache used: $val"
1444 set x
[sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW
0]
1445 set val
[format {now
%10d max
%10d
} [lindex $x 1] [lindex $x 2]]
1446 output1
"Page-cache overflow: $val"
1447 ifcapable yytrackmaxstackdepth
{
1448 set x
[sqlite3_status SQLITE_STATUS_PARSER_STACK
0]
1449 set val
[format { max
%10d
} [lindex $x 2]]
1450 output2
"Parser stack depth: $val"
1454 # A procedure to execute SQL
1456 proc execsql
{sql
{db db
}} {
1458 uplevel [list $db eval $sql]
1460 proc execsql_timed
{sql
{db db
}} {
1462 set x
[uplevel [list $db eval $sql]]
1464 set tm
[lindex $tm 0]
1465 output1
-nonewline " ([expr {$tm*0.001}]ms) "
1469 # Execute SQL and catch exceptions.
1471 proc catchsql
{sql
{db db
}} {
1473 set r
[catch [list uplevel [list $db eval $sql]] msg
]
1478 # Do an VDBE code dump on the SQL given
1480 proc explain
{sql
{db db
}} {
1482 output2
"addr opcode p1 p2 p3 p4 p5 #"
1483 output2
"---- ------------ ------ ------ ------ --------------- -- -"
1484 $db eval "explain $sql" {} {
1485 output2
[format {%-4d %-12.12s
%-6d %-6d %-6d % -17s %s
%s
} \
1486 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1491 proc explain_i
{sql
{db db
}} {
1493 output2
"addr opcode p1 p2 p3 p4 p5 #"
1494 output2
"---- ------------ ------ ------ ------ ---------------- -- -"
1497 # Set up colors for the different opcodes. Scheme is as follows:
1499 # Red: Opcodes that write to a b-tree.
1500 # Blue: Opcodes that reposition or seek a cursor.
1501 # Green: The ResultRow opcode.
1503 if { [catch {fconfigure stdout
-mode}]==0 } {
1504 set R
"\033\[31;1m" ;# Red fg
1505 set G
"\033\[32;1m" ;# Green fg
1506 set B
"\033\[34;1m" ;# Red fg
1507 set D
"\033\[39;0m" ;# Default fg
1515 Seek SeekGE SeekGT SeekLE SeekLT NotFound Last Rewind
1516 NoConflict Next Prev VNext VPrev VFilter
1517 SorterSort SorterNext NextIfOpen
1519 set color
($opcode) $B
1521 foreach opcode
{ResultRow
} {
1522 set color
($opcode) $G
1524 foreach opcode
{IdxInsert Insert Delete IdxDelete
} {
1525 set color
($opcode) $R
1529 $db eval "explain $sql" {} {
1531 set op
($addr) $opcode
1533 if {$opcode == "Goto" && ($bSeenGoto==0 ||
($p2 > $addr+10))} {
1534 set linebreak
($p2) 1
1538 if {$opcode=="Once"} {
1539 for {set i
$addr} {$i<$p2} {incr i
} {
1544 if {$opcode=="Next" ||
$opcode=="Prev"
1545 ||
$opcode=="VNext" ||
$opcode=="VPrev"
1546 ||
$opcode=="SorterNext" ||
$opcode=="NextIfOpen"
1548 for {set i
$p2} {$i<$addr} {incr i
} {
1553 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1554 for {set i
[expr $p2+1]} {$i<$addr} {incr i
} {
1559 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1560 set linebreak
([expr $addr+1]) 1
1564 $db eval "explain $sql" {} {
1565 if {[info exists linebreak
($addr)]} {
1568 set I
[string repeat
" " $x($addr)]
1570 if {[info exists star
($addr)]} {
1571 set ii
[expr $x($star($addr))]
1573 set I
[string replace
$I $ii $ii *]
1577 catch { set col
$color($opcode) }
1579 output2
[format {%-4d %s
%s
%-12.12s
%s
%-6d %-6d %-6d % -17s %s
%s
} \
1580 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment
1583 output2
"---- ------------ ------ ------ ------ ---------------- -- -"
1586 proc execsql_pp
{sql
{db db
}} {
1590 set nCol
[llength $A(*)]
1592 set aWidth
($c) [string length
$c]
1597 set n
[string length
$A($c)]
1598 if {$n > $aWidth($c)} {
1606 foreach e
[array names aWidth
] { incr nTotal
$aWidth($e) }
1607 incr nTotal
[expr ($nCol-1) * 3]
1612 lappend fmt
"% -$aWidth($c)s"
1614 set fmt
"| [join $fmt { | }] |"
1616 puts [string repeat
- $nTotal]
1617 for {set i
0} {$i < [llength $data]} {incr i
$nCol} {
1618 set vals
[lrange $data $i [expr $i+$nCol-1]]
1619 puts [format $fmt {*}$vals]
1620 if {$i==0} { puts [string repeat
- $nTotal] }
1622 puts [string repeat
- $nTotal]
1627 # Show the VDBE program for an SQL statement but omit the Trace
1628 # opcode at the beginning. This procedure can be used to prove
1629 # that different SQL statements generate exactly the same VDBE code.
1631 proc explain_no_trace
{sql
} {
1632 set tr
[db
eval "EXPLAIN $sql"]
1633 return [lrange $tr 7 end
]
1636 # Another procedure to execute SQL. This one includes the field
1637 # names in the returned list.
1639 proc execsql2
{sql
} {
1642 foreach f
$data(*) {
1643 lappend result
$f $data($f)
1649 # Use a temporary in-memory database to execute SQL statements
1651 proc memdbsql
{sql
} {
1652 sqlite3 memdb
:memory:
1653 set result
[memdb
eval $sql]
1658 # Use the non-callback API to execute multiple SQL statements
1660 proc stepsql
{dbptr sql
} {
1661 set sql
[string trim
$sql]
1663 while {[string length
$sql]>0} {
1664 if {[catch {sqlite3_prepare
$dbptr $sql -1 sqltail
} vm
]} {
1667 set sql
[string trim
$sqltail]
1668 # while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1669 # foreach v $VAL {lappend r $v}
1671 while {[sqlite3_step
$vm]=="SQLITE_ROW"} {
1672 for {set i
0} {$i<[sqlite3_data_count
$vm]} {incr i
} {
1673 lappend r
[sqlite3_column_text
$vm $i]
1676 if {[catch {sqlite3_finalize
$vm} errmsg
]} {
1677 return [list 1 $errmsg]
1683 # Do an integrity check of the entire database
1685 proc integrity_check
{name
{db db
}} {
1686 ifcapable integrityck
{
1687 do_test
$name [list execsql
{PRAGMA integrity_check
} $db] {ok
}
1691 # Check the extended error code
1693 proc verify_ex_errcode
{name expected
{db db
}} {
1694 do_test
$name [list sqlite3_extended_errcode
$db] $expected
1698 # Return true if the SQL statement passed as the second argument uses a
1699 # statement transaction.
1701 proc sql_uses_stmt
{db sql
} {
1702 set stmt
[sqlite3_prepare
$db $sql -1 dummy
]
1703 set uses
[uses_stmt_journal
$stmt]
1704 sqlite3_finalize
$stmt
1708 proc fix_ifcapable_expr
{expr} {
1711 for {set i
0} {$i < [string length
$expr]} {incr i
} {
1712 set char
[string range
$expr $i $i]
1713 set newstate
[expr {[string is alnum
$char] ||
$char eq
"_"}]
1714 if {$newstate && !$state} {
1715 append ret
{$::sqlite_options(}
1717 if {!$newstate && $state} {
1723 if {$state} {append ret
)}
1727 # Returns non-zero if the capabilities are present; zero otherwise.
1729 proc capable
{expr} {
1730 set e
[fix_ifcapable_expr
$expr]; return [expr ($e)]
1733 # Evaluate a boolean expression of capabilities. If true, execute the
1734 # code. Omit the code if false.
1736 proc ifcapable
{expr code
{else ""} {elsecode
""}} {
1737 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1738 set e2
[fix_ifcapable_expr
$expr]
1740 set c
[catch {uplevel 1 $code} r
]
1742 set c
[catch {uplevel 1 $elsecode} r
]
1747 # This proc execs a seperate process that crashes midway through executing
1748 # the SQL script $sql on database test.db.
1750 # The crash occurs during a sync() of file $crashfile. When the crash
1751 # occurs a random subset of all unsynced writes made by the process are
1752 # written into the files on disk. Argument $crashdelay indicates the
1753 # number of file syncs to wait before crashing.
1755 # The return value is a list of two elements. The first element is a
1756 # boolean, indicating whether or not the process actually crashed or
1757 # reported some other error. The second element in the returned list is the
1758 # error message. This is "child process exited abnormally" if the crash
1761 # crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
1763 proc crashsql
{args
} {
1768 set opendb
{ sqlite3 db test.db
-vfs crash
}
1773 set sql
[lindex $args end
]
1775 for {set ii
0} {$ii < [llength $args]-1} {incr ii
2} {
1776 set z
[lindex $args $ii]
1777 set n
[string length
$z]
1778 set z2
[lindex $args [expr $ii+1]]
1780 if {$n>1 && [string first
$z -delay]==0} {set crashdelay
$z2} \
1781 elseif
{$n>1 && [string first
$z -opendb]==0} {set opendb
$z2} \
1782 elseif
{$n>1 && [string first
$z -seed]==0} {set prngseed
$z2} \
1783 elseif
{$n>1 && [string first
$z -file]==0} {set crashfile
$z2} \
1784 elseif
{$n>1 && [string first
$z -tclbody]==0} {set tclbody
$z2} \
1785 elseif
{$n>1 && [string first
$z -blocksize]==0} {set blocksize
"-s $z2" } \
1786 elseif
{$n>1 && [string first
$z -characteristics]==0} {set dc
"-c {$z2}" }\
1787 elseif
{$n>1 && [string first
$z -dfltvfs]==0} {set dfltvfs
$z2 }\
1788 else { error "Unrecognized option: $z" }
1791 if {$crashfile eq
""} {
1792 error "Compulsory option -file missing"
1795 # $crashfile gets compared to the native filename in
1796 # cfSync(), which can be different then what TCL uses by
1797 # default, so here we force it to the "nativename" format.
1798 set cfile
[string map
{\\ \\\\} [file nativename
[file join [get_pwd
] $crashfile]]]
1800 set f
[open crash.tcl w
]
1801 puts $f "sqlite3_initialize ; sqlite3_shutdown"
1802 puts $f "catch { install_malloc_faultsim 1 }"
1803 puts $f "sqlite3_crash_enable 1 $dfltvfs"
1804 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
1805 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
1806 puts $f "autoinstall_test_functions"
1808 # This block sets the cache size of the main database to 10
1809 # pages. This is done in case the build is configured to omit
1810 # "PRAGMA cache_size".
1813 puts $f {db
eval {SELECT
* FROM sqlite_master
;}}
1814 puts $f {set bt
[btree_from_db db
]}
1815 puts $f {btree_set_cache_size
$bt 10}
1819 set seed
[expr {$prngseed%10007+1}]
1821 puts $f "db eval {SELECT randomblob($seed)}"
1824 if {[string length
$tclbody]>0} {
1827 if {[string length
$sql]>0} {
1834 exec [info nameofexec
] crash.tcl
>@stdout
2>@stdout
1837 # Windows/ActiveState TCL returns a slightly different
1838 # error message. We map that to the expected message
1839 # so that we don't have to change all of the test
1841 if {$::tcl_platform(platform
)=="windows"} {
1842 if {$msg=="child killed: unknown signal"} {
1843 set msg
"child process exited abnormally"
1846 if {$r && [string match
{*ERROR
: LeakSanitizer
*} $msg]} {
1847 set msg
"child process exited abnormally"
1853 # crash_on_write ?-devchar DEVCHAR? CRASHDELAY SQL
1855 proc crash_on_write
{args
} {
1857 set nArg
[llength $args]
1858 if {$nArg<2 ||
$nArg%2} {
1859 error "bad args: $args"
1861 set zSql
[lindex $args end
]
1862 set nDelay
[lindex $args end-1
]
1865 for {set ii
0} {$ii < $nArg-2} {incr ii
2} {
1866 set opt
[lindex $args $ii]
1867 switch -- [lindex $args $ii] {
1869 set devchar
[lindex $args [expr $ii+1]]
1872 default { error "unrecognized option: $opt" }
1876 set f
[open crash.tcl w
]
1877 puts $f "sqlite3_crash_on_write $nDelay"
1878 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
1879 puts $f "sqlite3 db test.db -vfs writecrash"
1880 puts $f "db eval {$zSql}"
1885 exec [info nameofexec
] crash.tcl
>@stdout
1888 # Windows/ActiveState TCL returns a slightly different
1889 # error message. We map that to the expected message
1890 # so that we don't have to change all of the test
1892 if {$::tcl_platform(platform
)=="windows"} {
1893 if {$msg=="child killed: unknown signal"} {
1894 set msg
"child process exited abnormally"
1901 proc run_ioerr_prep
{} {
1902 set ::sqlite_io_error_pending 0
1905 catch {forcedelete test.db
}
1906 catch {forcedelete test.db-journal
}
1907 catch {forcedelete test2.db
}
1908 catch {forcedelete test2.db-journal
}
1909 set ::DB [sqlite3 db test.db
; sqlite3_connection_pointer db
]
1910 sqlite3_extended_result_codes
$::DB $::ioerropts(-erc)
1911 if {[info exists
::ioerropts(-tclprep)]} {
1912 eval $::ioerropts(-tclprep)
1914 if {[info exists
::ioerropts(-sqlprep)]} {
1915 execsql
$::ioerropts(-sqlprep)
1920 # Usage: do_ioerr_test <test number> <options...>
1922 # This proc is used to implement test cases that check that IO errors
1923 # are correctly handled. The first argument, <test number>, is an integer
1924 # used to name the tests executed by this proc. Options are as follows:
1926 # -tclprep TCL script to run to prepare test.
1927 # -sqlprep SQL script to run to prepare test.
1928 # -tclbody TCL script to run with IO error simulation.
1929 # -sqlbody TCL script to run with IO error simulation.
1930 # -exclude List of 'N' values not to test.
1931 # -erc Use extended result codes
1932 # -persist Make simulated I/O errors persistent
1933 # -start Value of 'N' to begin with (default 1)
1935 # -cksum Boolean. If true, test that the database does
1936 # not change during the execution of the test case.
1938 proc do_ioerr_test
{testname args
} {
1940 set ::ioerropts(-start) 1
1941 set ::ioerropts(-cksum) 0
1942 set ::ioerropts(-erc) 0
1943 set ::ioerropts(-count) 100000000
1944 set ::ioerropts(-persist) 1
1945 set ::ioerropts(-ckrefcount) 0
1946 set ::ioerropts(-restoreprng) 1
1947 array set ::ioerropts $args
1949 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1950 # a couple of obscure IO errors that do not return them.
1951 set ::ioerropts(-erc) 0
1953 # Create a single TCL script from the TCL and SQL specified
1954 # as the body of the test.
1955 set ::ioerrorbody {}
1956 if {[info exists
::ioerropts(-tclbody)]} {
1957 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1959 if {[info exists
::ioerropts(-sqlbody)]} {
1960 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1964 if {$::ioerropts(-cksum)} {
1967 set ::goodcksum [cksum
]
1972 for {set n
$::ioerropts(-start)} {$::go} {incr n
} {
1974 incr ::ioerropts(-count) -1
1975 if {$::ioerropts(-count)<0} break
1977 # Skip this IO error if it was specified with the "-exclude" option.
1978 if {[info exists
::ioerropts(-exclude)]} {
1979 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1981 if {$::ioerropts(-restoreprng)} {
1985 # Delete the files test.db and test2.db, then execute the TCL and
1986 # SQL (in that order) to prepare for the test case.
1987 do_test
$testname.
$n.1 {
1991 # Read the 'checksum' of the database.
1992 if {$::ioerropts(-cksum)} {
1993 set ::checksum [cksum
]
1996 # Set the Nth IO error to fail.
1997 do_test
$testname.
$n.2 [subst {
1998 set ::sqlite_io_error_persist $::ioerropts(-persist)
1999 set ::sqlite_io_error_pending $n
2002 # Execute the TCL script created for the body of this test. If
2003 # at least N IO operations performed by SQLite as a result of
2004 # the script, the Nth will fail.
2005 do_test
$testname.
$n.3 {
2006 set ::sqlite_io_error_hit 0
2007 set ::sqlite_io_error_hardhit 0
2008 set r
[catch $::ioerrorbody msg
]
2010 if {[info commands db
]!=""} {
2011 set rc
[sqlite3_errcode db
]
2012 if {$::ioerropts(-erc)} {
2013 # If we are in extended result code mode, make sure all of the
2014 # IOERRs we get back really do have their extended code values.
2015 # If an extended result code is returned, the sqlite3_errcode
2016 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
2017 # where nnnn is a number
2018 if {[regexp {^SQLITE_IOERR
} $rc] && ![regexp {IOERR
\+\d
} $rc]} {
2022 # If we are not in extended result code mode, make sure no
2023 # extended error codes are returned.
2024 if {[regexp {\+\d
} $rc]} {
2029 # The test repeats as long as $::go is non-zero. $::go starts out
2030 # as 1. When a test runs to completion without hitting an I/O
2031 # error, that means there is no point in continuing with this test
2032 # case so set $::go to zero.
2034 if {$::sqlite_io_error_pending>0} {
2037 set ::sqlite_io_error_pending 0
2042 set s
[expr $::sqlite_io_error_hit==0]
2043 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
2046 set ::sqlite_io_error_hit 0
2048 # One of two things must have happened. either
2049 # 1. We never hit the IO error and the SQL returned OK
2050 # 2. An IO error was hit and the SQL failed
2052 #puts "s=$s r=$r q=$q"
2053 expr { ($s && !$r && !$q) ||
(!$s && $r && $q) }
2056 set ::sqlite_io_error_hit 0
2057 set ::sqlite_io_error_pending 0
2059 # Check that no page references were leaked. There should be
2060 # a single reference if there is still an active transaction,
2061 # or zero otherwise.
2063 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
2064 # locks are established on database files (i.e. if the error
2065 # occurs while attempting to detect a hot-journal file), then
2066 # there may 0 page references and an active transaction according
2067 # to [sqlite3_get_autocommit].
2069 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
2070 do_test
$testname.
$n.4 {
2071 set bt
[btree_from_db db
]
2073 array set stats
[btree_pager_stats
$bt]
2075 set nRef
$stats(ref
)
2076 expr {$nRef == 0 ||
([sqlite3_get_autocommit db
]==0 && $nRef == 1)}
2080 # If there is an open database handle and no open transaction,
2081 # and the pager is not running in exclusive-locking mode,
2082 # check that the pager is in "unlocked" state. Theoretically,
2083 # if a call to xUnlock() failed due to an IO error the underlying
2084 # file may still be locked.
2087 if { [info commands db
] ne
""
2088 && $::ioerropts(-ckrefcount)
2089 && [db one
{pragma locking_mode
}] eq
"normal"
2090 && [sqlite3_get_autocommit db
]
2092 do_test
$testname.
$n.5 {
2093 set bt
[btree_from_db db
]
2095 array set stats
[btree_pager_stats
$bt]
2102 # If an IO error occurred, then the checksum of the database should
2103 # be the same as before the script that caused the IO error was run.
2105 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
2106 do_test
$testname.
$n.6 {
2109 set ::DB [sqlite3 db test.db
; sqlite3_connection_pointer db
]
2110 set nowcksum
[cksum
]
2111 set res
[expr {$nowcksum==$::checksum ||
$nowcksum==$::goodcksum}]
2113 output2
"now=$nowcksum"
2114 output2
"the=$::checksum"
2115 output2
"fwd=$::goodcksum"
2121 set ::sqlite_io_error_hardhit 0
2122 set ::sqlite_io_error_pending 0
2123 if {[info exists
::ioerropts(-cleanup)]} {
2124 catch $::ioerropts(-cleanup)
2127 set ::sqlite_io_error_pending 0
2128 set ::sqlite_io_error_persist 0
2132 # Return a checksum based on the contents of the main database associated
2133 # with connection $db
2135 proc cksum
{{db db
}} {
2137 SELECT name
, type
, sql FROM sqlite_master order by name
2139 foreach tbl
[$db eval {
2140 SELECT name FROM sqlite_master WHERE type
='table' order by name
2142 append txt
[$db eval "SELECT * FROM $tbl"]\n
2144 foreach prag
{default_synchronous default_cache_size
} {
2145 append txt
$prag-[$db eval "PRAGMA $prag"]\n
2147 set cksum
[string length
$txt]-[md5
$txt]
2148 # puts $cksum-[file size test.db]
2152 # Generate a checksum based on the contents of the main and temp tables
2153 # database $db. If the checksum of two databases is the same, and the
2154 # integrity-check passes for both, the two databases are identical.
2156 proc allcksum
{{db db
}} {
2160 SELECT name FROM sqlite_master WHERE type
= 'table' UNION
2161 SELECT name FROM sqlite_temp_master WHERE type
= 'table' UNION
2162 SELECT 'sqlite_master' UNION
2163 SELECT 'sqlite_temp_master' ORDER BY
1
2167 SELECT name FROM sqlite_master WHERE type
= 'table' UNION
2168 SELECT 'sqlite_master' ORDER BY
1
2171 set tbllist
[$db eval $sql]
2173 foreach tbl
$tbllist {
2174 append txt
[$db eval "SELECT * FROM $tbl"]
2176 foreach prag
{default_cache_size
} {
2177 append txt
$prag-[$db eval "PRAGMA $prag"]\n
2183 # Generate a checksum based on the contents of a single database with
2184 # a database connection. The name of the database is $dbname.
2185 # Examples of $dbname are "temp" or "main".
2187 proc dbcksum
{db dbname
} {
2188 if {$dbname=="temp"} {
2189 set master sqlite_temp_master
2191 set master
$dbname.sqlite_master
2193 set alltab
[$db eval "SELECT name FROM $master WHERE type='table'"]
2194 set txt
[$db eval "SELECT * FROM $master"]\n
2195 foreach tab
$alltab {
2196 append txt
[$db eval "SELECT * FROM $dbname.$tab"]\n
2201 proc memdebug_log_sql
{filename} {
2203 set data
[sqlite3_memdebug_log dump
]
2204 set nFrame
[expr [llength [lindex $data 0]]-2]
2205 if {$nFrame < 0} { return "" }
2209 set tbl
"CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
2213 set nCall
[lindex $e 0]
2214 set nByte
[lindex $e 1]
2215 set lStack
[lrange $e 2 end
]
2216 append sql
"INSERT INTO ${database}.malloc VALUES"
2217 append sql
"('test', $nCall, $nByte, '$lStack');\n"
2223 set tbl2
"CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
2224 set tbl3
"CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
2228 foreach f
[array names frames
] {
2229 set addr
[format %x
$f]
2230 set cmd
"eu-addr2line --pid=$pid $addr"
2231 set line
[eval exec $cmd]
2232 append sql
"INSERT INTO ${database}.frame VALUES($f, '$line');\n"
2234 set file [lindex [split $line :] 0]
2238 foreach f
[array names files
] {
2242 set contents
[read $fd]
2245 set contents
[string map
{' ''
} $contents]
2246 append sql
"INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
2249 set escaped
"BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
2250 set escaped
[string map
[list "{" "\\{" "}" "\\}" "\\" "\\\\"] $escaped]
2252 set fd
[open $filename w
]
2253 puts $fd "set BUILTIN {"
2256 puts $fd {set BUILTIN
[string map
[list "\\{" "{" "\\}" "}" "\\\\" "\\"] $BUILTIN]}
2257 set mtv
[open $::testdir/malloctraceviewer.tcl
]
2264 # Drop all tables in database [db]
2265 proc drop_all_tables
{{db db
}} {
2266 ifcapable trigger
&&foreignkey
{
2267 set pk
[$db one
"PRAGMA foreign_keys"]
2268 $db eval "PRAGMA foreign_keys = OFF"
2270 foreach {idx name
file} [db
eval {PRAGMA database_list
}] {
2272 set master sqlite_temp_master
2274 set master
$name.sqlite_master
2276 foreach {t type
} [$db eval "
2277 SELECT name, type FROM $master
2278 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
2280 $db eval "DROP $type \"$t\""
2283 ifcapable trigger
&&foreignkey
{
2284 $db eval "PRAGMA foreign_keys = $pk"
2288 # Drop all auxiliary indexes from the main database opened by handle [db].
2290 proc drop_all_indexes
{{db db
}} {
2292 SELECT name FROM sqlite_master WHERE type
='index' AND sql LIKE 'create
%'
2294 foreach idx
$L { $db eval "DROP INDEX $idx" }
2298 #-------------------------------------------------------------------------
2299 # If a test script is executed with global variable $::G(perm:name) set to
2300 # "wal", then the tests are run in WAL mode. Otherwise, they should be run
2301 # in rollback mode. The following Tcl procs are used to make this less
2304 # wal_set_journal_mode ?DB?
2306 # If running a WAL test, execute "PRAGMA journal_mode = wal" using
2307 # connection handle DB. Otherwise, this command is a no-op.
2309 # wal_check_journal_mode TESTNAME ?DB?
2311 # If running a WAL test, execute a tests case that fails if the main
2312 # database for connection handle DB is not currently a WAL database.
2313 # Otherwise (if not running a WAL permutation) this is a no-op.
2317 # Returns true if this test should be run in WAL mode. False otherwise.
2319 proc wal_is_wal_mode
{} {
2320 expr {[permutation
] eq
"wal"}
2322 proc wal_set_journal_mode
{{db db
}} {
2323 if { [wal_is_wal_mode
] } {
2324 $db eval "PRAGMA journal_mode = WAL"
2327 proc wal_check_journal_mode
{testname
{db db
}} {
2328 if { [wal_is_wal_mode
] } {
2329 $db eval { SELECT
* FROM sqlite_master
}
2330 do_test
$testname [list $db eval "PRAGMA main.journal_mode"] {wal
}
2334 proc wal_is_capable
{} {
2335 ifcapable
!wal
{ return 0 }
2336 if {[permutation
]=="journaltest"} { return 0 }
2340 proc permutation
{} {
2342 catch {set perm
$::G(perm
:name
)}
2347 catch {set presql
$::G(perm
:presql
)}
2353 catch {set ret
$::G(isquick
)}
2357 #-------------------------------------------------------------------------
2359 proc slave_test_script
{script
} {
2361 # Create the interpreter used to run the test script.
2362 interp create tinterp
2364 # Populate some global variables that tester.tcl expects to see.
2365 foreach {var value
} [list \
2370 interp eval tinterp
[list set $var $value]
2373 # If output is being copied into a file, share the file-descriptor with
2375 if {[info exists
::G(output_fd
)]} {
2376 interp share
{} $::G(output_fd
) tinterp
2379 # The alias used to access the global test counters.
2380 tinterp alias set_test_counter set_test_counter
2382 # Set up the ::cmdlinearg array in the slave.
2383 interp eval tinterp
[list array set ::cmdlinearg [array get
::cmdlinearg]]
2385 # Set up the ::G array in the slave.
2386 interp eval tinterp
[list array set ::G [array get
::G]]
2388 # Load the various test interfaces implemented in C.
2389 load_testfixture_extensions tinterp
2391 # Run the test script.
2392 interp eval tinterp
$script
2394 # Check if the interpreter call [run_thread_tests]
2395 if { [interp eval tinterp
{info exists
::run_thread_tests_called}] } {
2396 set ::run_thread_tests_called 1
2399 # Delete the interpreter used to run the test script.
2400 interp delete tinterp
2403 proc slave_test_file
{zFile
} {
2404 set tail
[file tail
$zFile]
2406 if {[info exists
::G(start
:permutation
)]} {
2407 if {[permutation
] != $::G(start
:permutation
)} return
2408 unset ::G(start
:permutation
)
2410 if {[info exists
::G(start
:file)]} {
2411 if {$tail != $::G(start
:file) && $tail!="$::G(start:file).test"} return
2412 unset ::G(start
:file)
2415 # Remember the value of the shared-cache setting. So that it is possible
2416 # to check afterwards that it was not modified by the test script.
2418 ifcapable shared_cache
{ set scs
[sqlite3_enable_shared_cache
] }
2420 # Run the test script in a slave interpreter.
2422 unset -nocomplain ::run_thread_tests_called
2424 set ::sqlite_open_file_count 0
2425 set time [time { slave_test_script
[list source $zFile] }]
2426 set ms
[expr [lindex $time 0] / 1000]
2428 # Test that all files opened by the test script were closed. Omit this
2429 # if the test script has "thread" in its name. The open file counter
2430 # is not thread-safe.
2432 if {[info exists
::run_thread_tests_called]==0} {
2433 do_test
${tail
}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
2435 set ::sqlite_open_file_count 0
2437 # Test that the global "shared-cache" setting was not altered by
2440 ifcapable shared_cache
{
2441 set res
[expr {[sqlite3_enable_shared_cache
] == $scs}]
2442 do_test
${tail
}-sharedcachesetting [list set {} $res] 1
2445 # Add some info to the output.
2447 output2
"Time: $tail $ms ms"
2451 # Open a new connection on database test.db and execute the SQL script
2452 # supplied as an argument. Before returning, close the new conection and
2453 # restore the 4 byte fields starting at header offsets 28, 92 and 96
2454 # to the values they held before the SQL was executed. This simulates
2455 # a write by a pre-3.7.0 client.
2457 proc sql36231
{sql
} {
2458 set B
[hexio_read test.db
92 8]
2459 set A
[hexio_read test.db
28 4]
2460 sqlite3 db36231 test.db
2461 catch { db36231 func a_string a_string
}
2462 execsql
$sql db36231
2464 hexio_write test.db
28 $A
2465 hexio_write test.db
92 $B
2470 foreach f
[glob -nocomplain sv_test.db
*] { forcedelete
$f }
2471 foreach f
[glob -nocomplain test.db
*] {
2476 proc db_save_and_close
{} {
2481 proc db_restore
{} {
2482 foreach f
[glob -nocomplain test.db
*] { forcedelete
$f }
2483 foreach f2
[glob -nocomplain sv_test.db
*] {
2484 set f
[string range
$f2 3 end
]
2488 proc db_restore_and_reopen
{{dbfile test.db
}} {
2493 proc db_delete_and_reopen
{{file test.db
}} {
2495 foreach f
[glob -nocomplain test.db
*] { forcedelete
$f }
2499 # Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2500 # to configure the size of the PAGECACHE allocation using the parameters
2501 # provided to this command. Save the old PAGECACHE parameters in a global
2502 # variable so that [test_restore_config_pagecache] can restore the previous
2505 # Before returning, reopen connection [db] on file test.db.
2507 proc test_set_config_pagecache
{sz nPg
} {
2513 set ::old_pagecache_config [sqlite3_config_pagecache
$sz $nPg]
2515 autoinstall_test_functions
2519 # Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2520 # to configure the size of the PAGECACHE allocation to the size saved in
2521 # the global variable by an earlier call to [test_set_config_pagecache].
2523 # Before returning, reopen connection [db] on file test.db.
2525 proc test_restore_config_pagecache
{} {
2531 if {[info exists
::old_pagecache_config]} {
2532 eval sqlite3_config_pagecache
$::old_pagecache_config
2533 unset ::old_pagecache_config
2536 autoinstall_test_functions
2540 proc test_binary_name
{nm
} {
2541 if {$::tcl_platform(platform
)=="windows"} {
2546 file normalize
[file join $::cmdlinearg(TESTFIXTURE_HOME
) $ret]
2549 proc test_find_binary
{nm
} {
2550 set ret
[test_binary_name
$nm]
2551 if {![file executable
$ret]} {
2558 # Find the name of the 'shell' executable (e.g. "sqlite3.exe") to use for
2559 # the tests in shell*.test. If no such executable can be found, invoke
2560 # [finish_test ; return] in the callers context.
2562 proc test_find_cli
{} {
2563 set prog
[test_find_binary sqlite3
]
2564 if {$prog==""} { return -code return }
2568 # Find invocation of the 'shell' executable (e.g. "sqlite3.exe") to use
2569 # for the tests in shell*.test with optional valgrind prefix when the
2570 # environment variable SQLITE_CLI_VALGRIND_OPT is set. The set value
2571 # operates as follows:
2572 # empty or 0 => no valgrind prefix;
2573 # 1 => valgrind options for memory leak check;
2574 # other => use value as valgrind options.
2575 # If shell not found, invoke [finish_test ; return] in callers context.
2577 proc test_cli_invocation
{} {
2578 set prog
[test_find_binary sqlite3
]
2579 if {$prog==""} { return -code return }
2580 set vgrun
[expr {[permutation
]=="valgrind"}]
2581 if {$vgrun ||
[info exists
::env(SQLITE_CLI_VALGRIND_OPT
)]} {
2585 set vgo
$::env(SQLITE_CLI_VALGRIND_OPT
)
2587 if {$vgo == 0 ||
$vgo eq
""} {
2589 } elseif
{$vgo == 1} {
2590 return "valgrind --quiet --leak-check=yes $prog"
2592 return "valgrind $vgo $prog"
2599 # Find the name of the 'sqldiff' executable (e.g. "sqlite3.exe") to use for
2600 # the tests in sqldiff tests. If no such executable can be found, invoke
2601 # [finish_test ; return] in the callers context.
2603 proc test_find_sqldiff
{} {
2604 set prog
[test_find_binary sqldiff
]
2605 if {$prog==""} { return -code return }
2609 # Call sqlite3_expanded_sql() on all statements associated with database
2610 # connection $db. This sometimes finds use-after-free bugs if run with
2611 # valgrind or address-sanitizer.
2612 proc expand_all_sql
{db
} {
2614 while {[set stmt
[sqlite3_next_stmt
$db $stmt]]!=""} {
2615 sqlite3_expanded_sql
$stmt
2620 # If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
2621 # to non-zero, then set the global variable $AUTOVACUUM to 1.
2622 set AUTOVACUUM
$sqlite_options(default_autovacuum
)
2624 # Make sure the FTS enhanced query syntax is disabled.
2625 set sqlite_fts3_enable_parentheses
0
2627 # During testing, assume that all database files are well-formed. The
2628 # few test cases that deliberately corrupt database files should rescind
2629 # this setting by invoking "database_can_be_corrupt"
2631 database_never_corrupt
2632 extra_schema_checks
1
2634 source $testdir/thread_common.tcl
2635 source $testdir/malloc_common.tcl
2637 set tester_tcl_has_run
1