drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements some common TCL routines used for regression |
| 12 | # testing the SQLite library |
| 13 | # |
drh | bb77b75 | 2009-04-09 01:23:49 +0000 | [diff] [blame] | 14 | # $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 15 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 16 | #------------------------------------------------------------------------- |
| 17 | # The commands provided by the code in this file to help with creating |
| 18 | # test cases are as follows: |
drh | 8359d8c | 2008-03-29 11:00:54 +0000 | [diff] [blame] | 19 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 20 | # Commands to manipulate the db and the file-system at a high level: |
drh | 8359d8c | 2008-03-29 11:00:54 +0000 | [diff] [blame] | 21 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 22 | # copy_file FROM TO |
| 23 | # drop_all_table ?DB? |
| 24 | # forcedelete FILENAME |
| 25 | # |
| 26 | # Test the capability of the SQLite version built into the interpreter to |
| 27 | # determine if a specific test can be run: |
| 28 | # |
| 29 | # ifcapable EXPR |
| 30 | # |
| 31 | # Calulate checksums based on database contents: |
| 32 | # |
| 33 | # dbcksum DB DBNAME |
| 34 | # allcksum ?DB? |
| 35 | # cksum ?DB? |
| 36 | # |
| 37 | # Commands to execute/explain SQL statements: |
| 38 | # |
| 39 | # stepsql DB SQL |
| 40 | # execsql2 SQL |
| 41 | # explain_no_trace SQL |
| 42 | # explain SQL ?DB? |
| 43 | # catchsql SQL ?DB? |
| 44 | # execsql SQL ?DB? |
| 45 | # |
| 46 | # Commands to run test cases: |
| 47 | # |
| 48 | # do_ioerr_test TESTNAME ARGS... |
| 49 | # crashsql ARGS... |
| 50 | # integrity_check TESTNAME ?DB? |
| 51 | # do_test TESTNAME SCRIPT EXPECTED |
dan | 153eda0 | 2010-06-21 07:45:47 +0000 | [diff] [blame] | 52 | # do_execsql_test TESTNAME SQL EXPECTED |
| 53 | # do_catchsql_test TESTNAME SQL EXPECTED |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 54 | # |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 55 | # Commands providing a lower level interface to the global test counters: |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 56 | # |
| 57 | # set_test_counter COUNTER ?VALUE? |
| 58 | # omit_test TESTNAME REASON |
| 59 | # fail_test TESTNAME |
| 60 | # incr_ntest |
| 61 | # |
| 62 | # Command run at the end of each test file: |
| 63 | # |
| 64 | # finish_test |
| 65 | # |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 66 | # Commands to help create test files that run with the "WAL" and other |
| 67 | # permutations (see file permutations.test): |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 68 | # |
| 69 | # wal_is_wal_mode |
| 70 | # wal_set_journal_mode ?DB? |
| 71 | # wal_check_journal_mode TESTNAME?DB? |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 72 | # permutation |
dan | cb79e51 | 2010-08-06 13:50:07 +0000 | [diff] [blame] | 73 | # presql |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 74 | # |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 75 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 76 | # Set the precision of FP arithmatic used by the interpreter. And |
| 77 | # configure SQLite to take database file locks on the page that begins |
| 78 | # 64KB into the database file instead of the one 1GB in. This means |
| 79 | # the code that handles that special case can be tested without creating |
| 80 | # very large database files. |
| 81 | # |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 82 | set tcl_precision 15 |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 83 | sqlite3_test_control_pending_byte 0x0010000 |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 84 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 85 | |
| 86 | # If the pager codec is available, create a wrapper for the [sqlite3] |
| 87 | # command that appends "-key {xyzzy}" to the command line. i.e. this: |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 88 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 89 | # sqlite3 db test.db |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 90 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 91 | # becomes |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 92 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 93 | # sqlite3 db test.db -key {xyzzy} |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 94 | # |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 95 | if {[info command sqlite_orig]==""} { |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 96 | rename sqlite3 sqlite_orig |
| 97 | proc sqlite3 {args} { |
dan | 68928b6 | 2010-06-22 13:46:43 +0000 | [diff] [blame] | 98 | if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} { |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 99 | # This command is opening a new database connection. |
| 100 | # |
| 101 | if {[info exists ::G(perm:sqlite3_args)]} { |
| 102 | set args [concat $args $::G(perm:sqlite3_args)] |
| 103 | } |
dan | 68928b6 | 2010-06-22 13:46:43 +0000 | [diff] [blame] | 104 | if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} { |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 105 | lappend args -key {xyzzy} |
| 106 | } |
| 107 | |
dan | 3ccd20a | 2010-06-09 19:01:02 +0000 | [diff] [blame] | 108 | set res [uplevel 1 sqlite_orig $args] |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 109 | if {[info exists ::G(perm:presql)]} { |
| 110 | [lindex $args 0] eval $::G(perm:presql) |
| 111 | } |
dan | 1ce1b4a | 2010-12-07 14:32:28 +0000 | [diff] [blame] | 112 | if {[info exists ::G(perm:dbconfig)]} { |
| 113 | set ::dbhandle [lindex $args 0] |
| 114 | uplevel #0 $::G(perm:dbconfig) |
| 115 | } |
dan | 3ccd20a | 2010-06-09 19:01:02 +0000 | [diff] [blame] | 116 | set res |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 117 | } else { |
| 118 | # This command is not opening a new database connection. Pass the |
| 119 | # arguments through to the C implemenation as the are. |
| 120 | # |
| 121 | uplevel 1 sqlite_orig $args |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 122 | } |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
dan | cb35460 | 2010-07-08 09:44:42 +0000 | [diff] [blame] | 126 | proc execpresql {handle args} { |
| 127 | trace remove execution $handle enter [list execpresql $handle] |
| 128 | if {[info exists ::G(perm:presql)]} { |
| 129 | $handle eval $::G(perm:presql) |
| 130 | } |
| 131 | } |
| 132 | |
dan | 68928b6 | 2010-06-22 13:46:43 +0000 | [diff] [blame] | 133 | # This command should be called after loading tester.tcl from within |
| 134 | # all test scripts that are incompatible with encryption codecs. |
| 135 | # |
| 136 | proc do_not_use_codec {} { |
| 137 | set ::do_not_use_codec 1 |
| 138 | reset_db |
| 139 | } |
| 140 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 141 | # The following block only runs the first time this file is sourced. It |
| 142 | # does not run in slave interpreters (since the ::cmdlinearg array is |
| 143 | # populated before the test script is run in slave interpreters). |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 144 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 145 | if {[info exists cmdlinearg]==0} { |
| 146 | |
| 147 | # Parse any options specified in the $argv array. This script accepts the |
| 148 | # following options: |
| 149 | # |
| 150 | # --pause |
| 151 | # --soft-heap-limit=NN |
| 152 | # --maxerror=NN |
| 153 | # --malloctrace=N |
| 154 | # --backtrace=N |
| 155 | # --binarylog=N |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 156 | # --soak=N |
dan | 6a64d67 | 2011-04-04 15:38:16 +0000 | [diff] [blame] | 157 | # --start=[$permutation:]$testfile |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 158 | # |
| 159 | set cmdlinearg(soft-heap-limit) 0 |
| 160 | set cmdlinearg(maxerror) 1000 |
| 161 | set cmdlinearg(malloctrace) 0 |
| 162 | set cmdlinearg(backtrace) 10 |
| 163 | set cmdlinearg(binarylog) 0 |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 164 | set cmdlinearg(soak) 0 |
dan | 6a64d67 | 2011-04-04 15:38:16 +0000 | [diff] [blame] | 165 | set cmdlinearg(start) "" |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 166 | |
| 167 | set leftover [list] |
| 168 | foreach a $argv { |
| 169 | switch -regexp -- $a { |
| 170 | {^-+pause$} { |
| 171 | # Wait for user input before continuing. This is to give the user an |
| 172 | # opportunity to connect profiling tools to the process. |
| 173 | puts -nonewline "Press RETURN to begin..." |
| 174 | flush stdout |
| 175 | gets stdin |
| 176 | } |
| 177 | {^-+soft-heap-limit=.+$} { |
| 178 | foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break |
| 179 | } |
| 180 | {^-+maxerror=.+$} { |
| 181 | foreach {dummy cmdlinearg(maxerror)} [split $a =] break |
| 182 | } |
| 183 | {^-+malloctrace=.+$} { |
| 184 | foreach {dummy cmdlinearg(malloctrace)} [split $a =] break |
| 185 | if {$cmdlinearg(malloctrace)} { |
| 186 | sqlite3_memdebug_log start |
| 187 | } |
| 188 | } |
| 189 | {^-+backtrace=.+$} { |
| 190 | foreach {dummy cmdlinearg(backtrace)} [split $a =] break |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 191 | sqlite3_memdebug_backtrace $value |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 192 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 193 | {^-+binarylog=.+$} { |
| 194 | foreach {dummy cmdlinearg(binarylog)} [split $a =] break |
| 195 | } |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 196 | {^-+soak=.+$} { |
| 197 | foreach {dummy cmdlinearg(soak)} [split $a =] break |
| 198 | set ::G(issoak) $cmdlinearg(soak) |
| 199 | } |
dan | 6a64d67 | 2011-04-04 15:38:16 +0000 | [diff] [blame] | 200 | {^-+start=.+$} { |
| 201 | foreach {dummy cmdlinearg(start)} [split $a =] break |
| 202 | |
| 203 | set ::G(start:file) $cmdlinearg(start) |
| 204 | if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} { |
| 205 | set ::G(start:permutation) ${s.perm} |
| 206 | set ::G(start:file) ${s.file} |
| 207 | } |
| 208 | if {$::G(start:file) == ""} {unset ::G(start:file)} |
| 209 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 210 | default { |
| 211 | lappend leftover $a |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | set argv $leftover |
| 216 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 217 | # Install the malloc layer used to inject OOM errors. And the 'automatic' |
| 218 | # extensions. This only needs to be done once for the process. |
| 219 | # |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 220 | sqlite3_shutdown |
| 221 | install_malloc_faultsim 1 |
| 222 | sqlite3_initialize |
drh | bb77b75 | 2009-04-09 01:23:49 +0000 | [diff] [blame] | 223 | autoinstall_test_functions |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 224 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 225 | # If the --binarylog option was specified, create the logging VFS. This |
| 226 | # call installs the new VFS as the default for all SQLite connections. |
| 227 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 228 | if {$cmdlinearg(binarylog)} { |
dan | fbefb89 | 2010-05-12 19:02:35 +0000 | [diff] [blame] | 229 | vfslog new binarylog {} vfslog.bin |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | # Set the backtrace depth, if malloc tracing is enabled. |
| 233 | # |
| 234 | if {$cmdlinearg(malloctrace)} { |
| 235 | sqlite3_memdebug_backtrace $cmdlinearg(backtrace) |
danielk1977 | 185eac9 | 2008-07-12 15:55:54 +0000 | [diff] [blame] | 236 | } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 237 | } |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 238 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 239 | # Update the soft-heap-limit each time this script is run. In that |
| 240 | # way if an individual test file changes the soft-heap-limit, it |
| 241 | # will be reset at the start of the next test file. |
| 242 | # |
| 243 | sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit) |
| 244 | |
| 245 | # Create a test database |
| 246 | # |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 247 | proc reset_db {} { |
| 248 | catch {db close} |
| 249 | file delete -force test.db |
| 250 | file delete -force test.db-journal |
dan | d3f8f94 | 2010-04-13 11:35:01 +0000 | [diff] [blame] | 251 | file delete -force test.db-wal |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 252 | sqlite3 db ./test.db |
| 253 | set ::DB [sqlite3_connection_pointer db] |
| 254 | if {[info exists ::SETUP_SQL]} { |
| 255 | db eval $::SETUP_SQL |
| 256 | } |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 257 | } |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 258 | reset_db |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 259 | |
| 260 | # Abort early if this script has been run before. |
| 261 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 262 | if {[info exists TC(count)]} return |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 263 | |
drh | ce2198c | 2010-09-10 13:22:59 +0000 | [diff] [blame] | 264 | # Make sure memory statistics are enabled. |
| 265 | # |
| 266 | sqlite3_config_memstatus 1 |
| 267 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 268 | # Initialize the test counters and set up commands to access them. |
| 269 | # Or, if this is a slave interpreter, set up aliases to write the |
| 270 | # counters in the parent interpreter. |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 271 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 272 | if {0==[info exists ::SLAVE]} { |
| 273 | set TC(errors) 0 |
| 274 | set TC(count) 0 |
| 275 | set TC(fail_list) [list] |
| 276 | set TC(omit_list) [list] |
| 277 | |
| 278 | proc set_test_counter {counter args} { |
| 279 | if {[llength $args]} { |
| 280 | set ::TC($counter) [lindex $args 0] |
| 281 | } |
| 282 | set ::TC($counter) |
| 283 | } |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 284 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 285 | |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 286 | # Record the fact that a sequence of tests were omitted. |
| 287 | # |
| 288 | proc omit_test {name reason} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 289 | set omitList [set_test_counter omit_list] |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 290 | lappend omitList [list $name $reason] |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 291 | set_test_counter omit_list $omitList |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 292 | } |
| 293 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 294 | # Record the fact that a test failed. |
| 295 | # |
| 296 | proc fail_test {name} { |
| 297 | set f [set_test_counter fail_list] |
| 298 | lappend f $name |
| 299 | set_test_counter fail_list $f |
| 300 | set_test_counter errors [expr [set_test_counter errors] + 1] |
| 301 | |
| 302 | set nFail [set_test_counter errors] |
| 303 | if {$nFail>=$::cmdlinearg(maxerror)} { |
| 304 | puts "*** Giving up..." |
| 305 | finalize_testing |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | # Increment the number of tests run |
| 310 | # |
| 311 | proc incr_ntest {} { |
| 312 | set_test_counter count [expr [set_test_counter count] + 1] |
| 313 | } |
| 314 | |
| 315 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 316 | # Invoke the do_test procedure to run a single test |
| 317 | # |
| 318 | proc do_test {name cmd expected} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 319 | |
| 320 | global argv cmdlinearg |
| 321 | |
dan | 8c40800 | 2010-11-01 17:38:24 +0000 | [diff] [blame] | 322 | fix_testname name |
| 323 | |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 324 | sqlite3_memdebug_settitle $name |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 325 | |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 326 | # if {[llength $argv]==0} { |
| 327 | # set go 1 |
| 328 | # } else { |
| 329 | # set go 0 |
| 330 | # foreach pattern $argv { |
| 331 | # if {[string match $pattern $name]} { |
| 332 | # set go 1 |
| 333 | # break |
| 334 | # } |
| 335 | # } |
| 336 | # } |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 337 | |
dan | d506de0 | 2010-07-03 13:59:01 +0000 | [diff] [blame] | 338 | if {[info exists ::G(perm:prefix)]} { |
| 339 | set name "$::G(perm:prefix)$name" |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 340 | } |
| 341 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 342 | incr_ntest |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 343 | puts -nonewline $name... |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 344 | flush stdout |
| 345 | if {[catch {uplevel #0 "$cmd;\n"} result]} { |
| 346 | puts "\nError: $result" |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 347 | fail_test $name |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 348 | } elseif {[string compare $result $expected]} { |
| 349 | puts "\nExpected: \[$expected\]\n Got: \[$result\]" |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 350 | fail_test $name |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 351 | } else { |
| 352 | puts " Ok" |
| 353 | } |
drh | 6558db8 | 2007-04-13 03:23:21 +0000 | [diff] [blame] | 354 | flush stdout |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 355 | } |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 356 | |
shaneh | 5550517 | 2011-06-21 19:30:19 +0000 | [diff] [blame] | 357 | proc filepath_normalize {p} { |
| 358 | # test cases should be written to assume "unix"-like file paths |
shaneh | c489640 | 2011-06-21 19:38:16 +0000 | [diff] [blame^] | 359 | if {$::tcl_platform(platform)=="unix"} { |
shaneh | 5550517 | 2011-06-21 19:30:19 +0000 | [diff] [blame] | 360 | # lreverse*2 as a hack to remove any unneeded {} after the string map |
| 361 | lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]] |
shaneh | c489640 | 2011-06-21 19:38:16 +0000 | [diff] [blame^] | 362 | } { |
| 363 | set p |
shaneh | 5550517 | 2011-06-21 19:30:19 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | proc do_filepath_test {name cmd expected} { |
| 367 | uplevel [list do_test $name [ |
| 368 | subst -nocommands { filepath_normalize [ $cmd ] } |
| 369 | ] [filepath_normalize $expected]] |
| 370 | } |
| 371 | |
dan | 33f5379 | 2011-05-05 19:44:22 +0000 | [diff] [blame] | 372 | proc realnum_normalize {r} { |
shaneh | 4f529e8 | 2011-06-20 17:41:41 +0000 | [diff] [blame] | 373 | # different TCL versions display floating point values differently. |
| 374 | string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}] |
dan | 33f5379 | 2011-05-05 19:44:22 +0000 | [diff] [blame] | 375 | } |
| 376 | proc do_realnum_test {name cmd expected} { |
| 377 | uplevel [list do_test $name [ |
| 378 | subst -nocommands { realnum_normalize [ $cmd ] } |
| 379 | ] [realnum_normalize $expected]] |
| 380 | } |
| 381 | |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 382 | proc fix_testname {varname} { |
| 383 | upvar $varname testname |
| 384 | if {[info exists ::testprefix] |
| 385 | && [string is digit [string range $testname 0 0]] |
| 386 | } { |
| 387 | set testname "${::testprefix}-$testname" |
| 388 | } |
| 389 | } |
dan | 153eda0 | 2010-06-21 07:45:47 +0000 | [diff] [blame] | 390 | |
dan | 57f7f4b | 2010-10-01 19:04:37 +0000 | [diff] [blame] | 391 | proc do_execsql_test {testname sql {result {}}} { |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 392 | fix_testname testname |
dan | 99ebad9 | 2011-06-13 09:11:01 +0000 | [diff] [blame] | 393 | uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]] |
dan | 153eda0 | 2010-06-21 07:45:47 +0000 | [diff] [blame] | 394 | } |
| 395 | proc do_catchsql_test {testname sql result} { |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 396 | fix_testname testname |
dan | 99ebad9 | 2011-06-13 09:11:01 +0000 | [diff] [blame] | 397 | uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result] |
dan | 153eda0 | 2010-06-21 07:45:47 +0000 | [diff] [blame] | 398 | } |
dan | 3985479 | 2010-11-15 16:12:58 +0000 | [diff] [blame] | 399 | proc do_eqp_test {name sql res} { |
| 400 | uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res] |
| 401 | } |
dan | 153eda0 | 2010-06-21 07:45:47 +0000 | [diff] [blame] | 402 | |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 403 | #------------------------------------------------------------------------- |
| 404 | # Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST |
| 405 | # |
| 406 | # Where switches are: |
| 407 | # |
| 408 | # -errorformat FMTSTRING |
| 409 | # -count |
dan | af1dcab | 2010-09-20 19:17:53 +0000 | [diff] [blame] | 410 | # -query SQL |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 411 | # -tclquery TCL |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 412 | # -repair TCL |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 413 | # |
| 414 | proc do_select_tests {prefix args} { |
| 415 | |
| 416 | set testlist [lindex $args end] |
| 417 | set switches [lrange $args 0 end-1] |
| 418 | |
| 419 | set errfmt "" |
| 420 | set countonly 0 |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 421 | set tclquery "" |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 422 | set repair "" |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 423 | |
| 424 | for {set i 0} {$i < [llength $switches]} {incr i} { |
| 425 | set s [lindex $switches $i] |
| 426 | set n [string length $s] |
dan | af1dcab | 2010-09-20 19:17:53 +0000 | [diff] [blame] | 427 | if {$n>=2 && [string equal -length $n $s "-query"]} { |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 428 | set tclquery [list execsql [lindex $switches [incr i]]] |
| 429 | } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} { |
| 430 | set tclquery [lindex $switches [incr i]] |
dan | af1dcab | 2010-09-20 19:17:53 +0000 | [diff] [blame] | 431 | } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} { |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 432 | set errfmt [lindex $switches [incr i]] |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 433 | } elseif {$n>=2 && [string equal -length $n $s "-repair"]} { |
| 434 | set repair [lindex $switches [incr i]] |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 435 | } elseif {$n>=2 && [string equal -length $n $s "-count"]} { |
| 436 | set countonly 1 |
| 437 | } else { |
| 438 | error "unknown switch: $s" |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if {$countonly && $errfmt!=""} { |
| 443 | error "Cannot use -count and -errorformat together" |
| 444 | } |
| 445 | set nTestlist [llength $testlist] |
| 446 | if {$nTestlist%3 || $nTestlist==0 } { |
| 447 | error "SELECT test list contains [llength $testlist] elements" |
| 448 | } |
| 449 | |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 450 | eval $repair |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 451 | foreach {tn sql res} $testlist { |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 452 | if {$tclquery != ""} { |
dan | af1dcab | 2010-09-20 19:17:53 +0000 | [diff] [blame] | 453 | execsql $sql |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 454 | uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]] |
| 455 | } elseif {$countonly} { |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 456 | set nRow 0 |
| 457 | db eval $sql {incr nRow} |
| 458 | uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res] |
| 459 | } elseif {$errfmt==""} { |
| 460 | uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]] |
| 461 | } else { |
| 462 | set res [list 1 [string trim [format $errfmt {*}$res]]] |
| 463 | uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res] |
| 464 | } |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 465 | eval $repair |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 466 | } |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 467 | |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 468 | } |
| 469 | |
dan | b04757a | 2010-09-21 16:59:16 +0000 | [diff] [blame] | 470 | proc delete_all_data {} { |
| 471 | db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} { |
| 472 | db eval "DELETE FROM '[string map {' ''} $t]'" |
| 473 | } |
| 474 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 475 | |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 476 | # Run an SQL script. |
| 477 | # Return the number of microseconds per statement. |
| 478 | # |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 479 | proc speed_trial {name numstmt units sql} { |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 480 | puts -nonewline [format {%-21.21s } $name...] |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 481 | flush stdout |
| 482 | set speed [time {sqlite3_exec_nr db $sql}] |
| 483 | set tm [lindex $speed 0] |
danielk1977 | 0ee26d9 | 2008-02-08 18:25:29 +0000 | [diff] [blame] | 484 | if {$tm == 0} { |
| 485 | set rate [format %20s "many"] |
| 486 | } else { |
| 487 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 488 | } |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 489 | set u2 $units/s |
danielk1977 | 0ee26d9 | 2008-02-08 18:25:29 +0000 | [diff] [blame] | 490 | puts [format {%12d uS %s %s} $tm $rate $u2] |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 491 | global total_time |
| 492 | set total_time [expr {$total_time+$tm}] |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 493 | lappend ::speed_trial_times $name $tm |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 494 | } |
drh | 45c236d | 2008-03-22 01:08:00 +0000 | [diff] [blame] | 495 | proc speed_trial_tcl {name numstmt units script} { |
| 496 | puts -nonewline [format {%-21.21s } $name...] |
| 497 | flush stdout |
| 498 | set speed [time {eval $script}] |
| 499 | set tm [lindex $speed 0] |
| 500 | if {$tm == 0} { |
| 501 | set rate [format %20s "many"] |
| 502 | } else { |
| 503 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 504 | } |
| 505 | set u2 $units/s |
| 506 | puts [format {%12d uS %s %s} $tm $rate $u2] |
| 507 | global total_time |
| 508 | set total_time [expr {$total_time+$tm}] |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 509 | lappend ::speed_trial_times $name $tm |
drh | 45c236d | 2008-03-22 01:08:00 +0000 | [diff] [blame] | 510 | } |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 511 | proc speed_trial_init {name} { |
| 512 | global total_time |
| 513 | set total_time 0 |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 514 | set ::speed_trial_times [list] |
drh | bb810a9 | 2010-07-03 12:00:53 +0000 | [diff] [blame] | 515 | sqlite3 versdb :memory: |
| 516 | set vers [versdb one {SELECT sqlite_source_id()}] |
| 517 | versdb close |
| 518 | puts "SQLite $vers" |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 519 | } |
| 520 | proc speed_trial_summary {name} { |
| 521 | global total_time |
| 522 | puts [format {%-21.21s %12d uS TOTAL} $name $total_time] |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 523 | |
| 524 | if { 0 } { |
| 525 | sqlite3 versdb :memory: |
| 526 | set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0] |
| 527 | versdb close |
| 528 | puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);" |
| 529 | foreach {test us} $::speed_trial_times { |
| 530 | puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);" |
| 531 | } |
| 532 | } |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 533 | } |
| 534 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 535 | # Run this routine last |
| 536 | # |
| 537 | proc finish_test {} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 538 | catch {db close} |
| 539 | catch {db2 close} |
| 540 | catch {db3 close} |
| 541 | if {0==[info exists ::SLAVE]} { finalize_testing } |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 542 | } |
| 543 | proc finalize_testing {} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 544 | global sqlite_open_file_count |
| 545 | |
| 546 | set omitList [set_test_counter omit_list] |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 547 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 548 | catch {db close} |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 549 | catch {db2 close} |
| 550 | catch {db3 close} |
| 551 | |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 552 | vfs_unlink_test |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 553 | sqlite3 db {} |
danielk1977 | cbb8496 | 2006-01-17 16:10:13 +0000 | [diff] [blame] | 554 | # sqlite3_clear_tsd_memdebug |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 555 | db close |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 556 | sqlite3_reset_auto_extension |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 557 | |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 558 | sqlite3_soft_heap_limit 0 |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 559 | set nTest [incr_ntest] |
| 560 | set nErr [set_test_counter errors] |
| 561 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 562 | puts "$nErr errors out of $nTest tests" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 563 | if {$nErr>0} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 564 | puts "Failures on these tests: [set_test_counter fail_list]" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 565 | } |
danielk1977 | ee8b799 | 2009-03-26 17:13:06 +0000 | [diff] [blame] | 566 | run_thread_tests 1 |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 567 | if {[llength $omitList]>0} { |
| 568 | puts "Omitted test cases:" |
| 569 | set prec {} |
| 570 | foreach {rec} [lsort $omitList] { |
| 571 | if {$rec==$prec} continue |
| 572 | set prec $rec |
| 573 | puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]] |
| 574 | } |
| 575 | } |
drh | 80788d8 | 2006-09-02 14:50:23 +0000 | [diff] [blame] | 576 | if {$nErr>0 && ![working_64bit_int]} { |
| 577 | puts "******************************************************************" |
| 578 | puts "N.B.: The version of TCL that you used to build this test harness" |
| 579 | puts "is defective in that it does not support 64-bit integers. Some or" |
| 580 | puts "all of the test failures above might be a result from this defect" |
| 581 | puts "in your TCL build." |
| 582 | puts "******************************************************************" |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 583 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 584 | if {$::cmdlinearg(binarylog)} { |
dan | fbefb89 | 2010-05-12 19:02:35 +0000 | [diff] [blame] | 585 | vfslog finalize binarylog |
danielk1977 | 374177e | 2008-05-08 15:58:06 +0000 | [diff] [blame] | 586 | } |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 587 | if {$sqlite_open_file_count} { |
| 588 | puts "$sqlite_open_file_count files were left open" |
| 589 | incr nErr |
| 590 | } |
drh | eafc43b | 2010-07-26 18:43:40 +0000 | [diff] [blame] | 591 | if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 || |
| 592 | [sqlite3_memory_used]>0} { |
| 593 | puts "Unfreed memory: [sqlite3_memory_used] bytes in\ |
| 594 | [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 595 | incr nErr |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 596 | ifcapable memdebug||mem5||(mem3&&debug) { |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 597 | puts "Writing unfreed memory log to \"./memleak.txt\"" |
| 598 | sqlite3_memdebug_dump ./memleak.txt |
| 599 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 600 | } else { |
| 601 | puts "All memory allocations freed - no leaks" |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 602 | ifcapable memdebug||mem5 { |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 603 | sqlite3_memdebug_dump ./memusage.txt |
| 604 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 605 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 606 | show_memstats |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 607 | puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes" |
| 608 | puts "Current memory usage: [sqlite3_memory_highwater] bytes" |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 609 | if {[info commands sqlite3_memdebug_malloc_count] ne ""} { |
| 610 | puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls" |
| 611 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 612 | if {$::cmdlinearg(malloctrace)} { |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 613 | puts "Writing mallocs.sql..." |
| 614 | memdebug_log_sql |
| 615 | sqlite3_memdebug_log stop |
| 616 | sqlite3_memdebug_log clear |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 617 | |
| 618 | if {[sqlite3_memory_used]>0} { |
| 619 | puts "Writing leaks.sql..." |
| 620 | sqlite3_memdebug_log sync |
| 621 | memdebug_log_sql leaks.sql |
| 622 | } |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 623 | } |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame] | 624 | foreach f [glob -nocomplain test.db-*-journal] { |
| 625 | file delete -force $f |
| 626 | } |
| 627 | foreach f [glob -nocomplain test.db-mj*] { |
| 628 | file delete -force $f |
| 629 | } |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 630 | exit [expr {$nErr>0}] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 631 | } |
| 632 | |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 633 | # Display memory statistics for analysis and debugging purposes. |
| 634 | # |
| 635 | proc show_memstats {} { |
| 636 | set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 637 | set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] |
| 638 | set val [format {now %10d max %10d max-size %10d} \ |
| 639 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 640 | puts "Memory used: $val" |
drh | eafc43b | 2010-07-26 18:43:40 +0000 | [diff] [blame] | 641 | set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] |
| 642 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
| 643 | puts "Allocation count: $val" |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 644 | set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 645 | set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0] |
| 646 | set val [format {now %10d max %10d max-size %10d} \ |
| 647 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 648 | puts "Page-cache used: $val" |
| 649 | set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] |
| 650 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
| 651 | puts "Page-cache overflow: $val" |
| 652 | set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] |
| 653 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
| 654 | puts "Scratch memory used: $val" |
| 655 | set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 656 | set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0] |
| 657 | set val [format {now %10d max %10d max-size %10d} \ |
| 658 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 659 | puts "Scratch overflow: $val" |
drh | ec424a5 | 2008-07-25 15:39:03 +0000 | [diff] [blame] | 660 | ifcapable yytrackmaxstackdepth { |
| 661 | set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0] |
| 662 | set val [format { max %10d} [lindex $x 2]] |
| 663 | puts "Parser stack depth: $val" |
| 664 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 665 | } |
| 666 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 667 | # A procedure to execute SQL |
| 668 | # |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 669 | proc execsql {sql {db db}} { |
drh | acbcdc4 | 2001-01-22 00:31:53 +0000 | [diff] [blame] | 670 | # puts "SQL = $sql" |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 671 | uplevel [list $db eval $sql] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 672 | } |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 673 | |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 674 | # Execute SQL and catch exceptions. |
| 675 | # |
| 676 | proc catchsql {sql {db db}} { |
| 677 | # puts "SQL = $sql" |
dan | 81fa6dc | 2009-11-28 12:40:32 +0000 | [diff] [blame] | 678 | set r [catch [list uplevel [list $db eval $sql]] msg] |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 679 | lappend r $msg |
| 680 | return $r |
| 681 | } |
| 682 | |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 683 | # Do an VDBE code dump on the SQL given |
| 684 | # |
| 685 | proc explain {sql {db db}} { |
| 686 | puts "" |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 687 | puts "addr opcode p1 p2 p3 p4 p5 #" |
| 688 | puts "---- ------------ ------ ------ ------ --------------- -- -" |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 689 | $db eval "explain $sql" {} { |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 690 | puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \ |
| 691 | $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment |
| 692 | ] |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 696 | # Show the VDBE program for an SQL statement but omit the Trace |
| 697 | # opcode at the beginning. This procedure can be used to prove |
| 698 | # that different SQL statements generate exactly the same VDBE code. |
| 699 | # |
| 700 | proc explain_no_trace {sql} { |
| 701 | set tr [db eval "EXPLAIN $sql"] |
| 702 | return [lrange $tr 7 end] |
| 703 | } |
| 704 | |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 705 | # Another procedure to execute SQL. This one includes the field |
| 706 | # names in the returned list. |
| 707 | # |
| 708 | proc execsql2 {sql} { |
| 709 | set result {} |
| 710 | db eval $sql data { |
| 711 | foreach f $data(*) { |
| 712 | lappend result $f $data($f) |
| 713 | } |
| 714 | } |
| 715 | return $result |
| 716 | } |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 717 | |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 718 | # Use the non-callback API to execute multiple SQL statements |
| 719 | # |
| 720 | proc stepsql {dbptr sql} { |
| 721 | set sql [string trim $sql] |
| 722 | set r 0 |
| 723 | while {[string length $sql]>0} { |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 724 | if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 725 | return [list 1 $vm] |
| 726 | } |
| 727 | set sql [string trim $sqltail] |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 728 | # while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} { |
| 729 | # foreach v $VAL {lappend r $v} |
| 730 | # } |
| 731 | while {[sqlite3_step $vm]=="SQLITE_ROW"} { |
| 732 | for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} { |
| 733 | lappend r [sqlite3_column_text $vm $i] |
| 734 | } |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 735 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 736 | if {[catch {sqlite3_finalize $vm} errmsg]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 737 | return [list 1 $errmsg] |
| 738 | } |
| 739 | } |
| 740 | return $r |
| 741 | } |
| 742 | |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 743 | # Delete a file or directory |
| 744 | # |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 745 | proc forcedelete {args} { |
| 746 | foreach filename $args { |
| 747 | # On windows, sometimes even a [file delete -force] can fail just after |
| 748 | # a file is closed. The cause is usually "tag-alongs" - programs like |
| 749 | # anti-virus software, automatic backup tools and various explorer |
| 750 | # extensions that keep a file open a little longer than we expect, causing |
| 751 | # the delete to fail. |
| 752 | # |
| 753 | # The solution is to wait a short amount of time before retrying the |
| 754 | # delete. |
| 755 | # |
| 756 | set nRetry 50 ;# Maximum number of retries. |
| 757 | set nDelay 100 ;# Delay in ms before retrying. |
| 758 | for {set i 0} {$i<$nRetry} {incr i} { |
| 759 | set rc [catch {file delete -force $filename} msg] |
| 760 | if {$rc==0} break |
| 761 | after $nDelay |
| 762 | } |
| 763 | if {$rc} { error $msg } |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 766 | |
| 767 | # Do an integrity check of the entire database |
| 768 | # |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 769 | proc integrity_check {name {db db}} { |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 770 | ifcapable integrityck { |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 771 | do_test $name [list execsql {PRAGMA integrity_check} $db] {ok} |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | |
dan | c6055c7 | 2011-04-28 18:46:46 +0000 | [diff] [blame] | 775 | |
| 776 | # Return true if the SQL statement passed as the second argument uses a |
| 777 | # statement transaction. |
| 778 | # |
| 779 | proc sql_uses_stmt {db sql} { |
| 780 | set stmt [sqlite3_prepare $db $sql -1 dummy] |
| 781 | set uses [uses_stmt_journal $stmt] |
| 782 | sqlite3_finalize $stmt |
| 783 | return $uses |
| 784 | } |
| 785 | |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 786 | proc fix_ifcapable_expr {expr} { |
| 787 | set ret "" |
| 788 | set state 0 |
| 789 | for {set i 0} {$i < [string length $expr]} {incr i} { |
| 790 | set char [string range $expr $i $i] |
| 791 | set newstate [expr {[string is alnum $char] || $char eq "_"}] |
| 792 | if {$newstate && !$state} { |
| 793 | append ret {$::sqlite_options(} |
| 794 | } |
| 795 | if {!$newstate && $state} { |
| 796 | append ret ) |
| 797 | } |
| 798 | append ret $char |
| 799 | set state $newstate |
| 800 | } |
| 801 | if {$state} {append ret )} |
| 802 | return $ret |
| 803 | } |
| 804 | |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 805 | # Evaluate a boolean expression of capabilities. If true, execute the |
| 806 | # code. Omit the code if false. |
| 807 | # |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 808 | proc ifcapable {expr code {else ""} {elsecode ""}} { |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 809 | #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2 |
| 810 | set e2 [fix_ifcapable_expr $expr] |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 811 | if ($e2) { |
| 812 | set c [catch {uplevel 1 $code} r] |
| 813 | } else { |
| 814 | set c [catch {uplevel 1 $elsecode} r] |
| 815 | } |
| 816 | return -code $c $r |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 817 | } |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 818 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 819 | # This proc execs a seperate process that crashes midway through executing |
| 820 | # the SQL script $sql on database test.db. |
| 821 | # |
| 822 | # The crash occurs during a sync() of file $crashfile. When the crash |
| 823 | # occurs a random subset of all unsynced writes made by the process are |
| 824 | # written into the files on disk. Argument $crashdelay indicates the |
| 825 | # number of file syncs to wait before crashing. |
| 826 | # |
| 827 | # The return value is a list of two elements. The first element is a |
| 828 | # boolean, indicating whether or not the process actually crashed or |
| 829 | # reported some other error. The second element in the returned list is the |
| 830 | # error message. This is "child process exited abnormally" if the crash |
| 831 | # occured. |
| 832 | # |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 833 | # crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 834 | # |
| 835 | proc crashsql {args} { |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 836 | |
| 837 | set blocksize "" |
| 838 | set crashdelay 1 |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 839 | set prngseed 0 |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 840 | set tclbody {} |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 841 | set crashfile "" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 842 | set dc "" |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 843 | set sql [lindex $args end] |
| 844 | |
| 845 | for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} { |
| 846 | set z [lindex $args $ii] |
| 847 | set n [string length $z] |
| 848 | set z2 [lindex $args [expr $ii+1]] |
| 849 | |
| 850 | if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \ |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 851 | elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 852 | elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \ |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 853 | elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 854 | elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \ |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 855 | elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 856 | else { error "Unrecognized option: $z" } |
| 857 | } |
| 858 | |
| 859 | if {$crashfile eq ""} { |
| 860 | error "Compulsory option -file missing" |
| 861 | } |
| 862 | |
shaneh | f2c0882 | 2010-07-08 16:30:44 +0000 | [diff] [blame] | 863 | # $crashfile gets compared to the native filename in |
| 864 | # cfSync(), which can be different then what TCL uses by |
| 865 | # default, so here we force it to the "nativename" format. |
| 866 | set cfile [string map {\\ \\\\} [file nativename [file join [pwd] $crashfile]]] |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 867 | |
| 868 | set f [open crash.tcl w] |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 869 | puts $f "sqlite3_crash_enable 1" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 870 | puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile" |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 871 | puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte" |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 872 | puts $f "sqlite3 db test.db -vfs crash" |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 873 | |
| 874 | # This block sets the cache size of the main database to 10 |
| 875 | # pages. This is done in case the build is configured to omit |
| 876 | # "PRAGMA cache_size". |
| 877 | puts $f {db eval {SELECT * FROM sqlite_master;}} |
| 878 | puts $f {set bt [btree_from_db db]} |
| 879 | puts $f {btree_set_cache_size $bt 10} |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 880 | if {$prngseed} { |
| 881 | set seed [expr {$prngseed%10007+1}] |
| 882 | # puts seed=$seed |
| 883 | puts $f "db eval {SELECT randomblob($seed)}" |
| 884 | } |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 885 | |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 886 | if {[string length $tclbody]>0} { |
| 887 | puts $f $tclbody |
| 888 | } |
| 889 | if {[string length $sql]>0} { |
| 890 | puts $f "db eval {" |
| 891 | puts $f "$sql" |
| 892 | puts $f "}" |
| 893 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 894 | close $f |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 895 | set r [catch { |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 896 | exec [info nameofexec] crash.tcl >@stdout |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 897 | } msg] |
shaneh | f2c0882 | 2010-07-08 16:30:44 +0000 | [diff] [blame] | 898 | |
| 899 | # Windows/ActiveState TCL returns a slightly different |
| 900 | # error message. We map that to the expected message |
| 901 | # so that we don't have to change all of the test |
| 902 | # cases. |
| 903 | if {$::tcl_platform(platform)=="windows"} { |
| 904 | if {$msg=="child killed: unknown signal"} { |
| 905 | set msg "child process exited abnormally" |
| 906 | } |
| 907 | } |
| 908 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 909 | lappend r $msg |
| 910 | } |
| 911 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 912 | # Usage: do_ioerr_test <test number> <options...> |
| 913 | # |
| 914 | # This proc is used to implement test cases that check that IO errors |
| 915 | # are correctly handled. The first argument, <test number>, is an integer |
| 916 | # used to name the tests executed by this proc. Options are as follows: |
| 917 | # |
| 918 | # -tclprep TCL script to run to prepare test. |
| 919 | # -sqlprep SQL script to run to prepare test. |
| 920 | # -tclbody TCL script to run with IO error simulation. |
| 921 | # -sqlbody TCL script to run with IO error simulation. |
| 922 | # -exclude List of 'N' values not to test. |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 923 | # -erc Use extended result codes |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 924 | # -persist Make simulated I/O errors persistent |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 925 | # -start Value of 'N' to begin with (default 1) |
| 926 | # |
| 927 | # -cksum Boolean. If true, test that the database does |
| 928 | # not change during the execution of the test case. |
| 929 | # |
| 930 | proc do_ioerr_test {testname args} { |
| 931 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 932 | set ::ioerropts(-start) 1 |
| 933 | set ::ioerropts(-cksum) 0 |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 934 | set ::ioerropts(-erc) 0 |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 935 | set ::ioerropts(-count) 100000000 |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 936 | set ::ioerropts(-persist) 1 |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 937 | set ::ioerropts(-ckrefcount) 0 |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 938 | set ::ioerropts(-restoreprng) 1 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 939 | array set ::ioerropts $args |
| 940 | |
danielk1977 | 47cd39c | 2008-05-12 12:41:15 +0000 | [diff] [blame] | 941 | # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are |
| 942 | # a couple of obscure IO errors that do not return them. |
| 943 | set ::ioerropts(-erc) 0 |
| 944 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 945 | set ::go 1 |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 946 | #reset_prng_state |
| 947 | save_prng_state |
danielk1977 | ef165ce | 2009-04-06 17:50:03 +0000 | [diff] [blame] | 948 | for {set n $::ioerropts(-start)} {$::go} {incr n} { |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 949 | set ::TN $n |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 950 | incr ::ioerropts(-count) -1 |
| 951 | if {$::ioerropts(-count)<0} break |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 952 | |
| 953 | # Skip this IO error if it was specified with the "-exclude" option. |
| 954 | if {[info exists ::ioerropts(-exclude)]} { |
| 955 | if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue |
| 956 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 957 | if {$::ioerropts(-restoreprng)} { |
| 958 | restore_prng_state |
| 959 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 960 | |
| 961 | # Delete the files test.db and test2.db, then execute the TCL and |
| 962 | # SQL (in that order) to prepare for the test case. |
| 963 | do_test $testname.$n.1 { |
| 964 | set ::sqlite_io_error_pending 0 |
| 965 | catch {db close} |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 966 | catch {db2 close} |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 967 | catch {file delete -force test.db} |
| 968 | catch {file delete -force test.db-journal} |
| 969 | catch {file delete -force test2.db} |
| 970 | catch {file delete -force test2.db-journal} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 971 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 972 | sqlite3_extended_result_codes $::DB $::ioerropts(-erc) |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 973 | if {[info exists ::ioerropts(-tclprep)]} { |
| 974 | eval $::ioerropts(-tclprep) |
| 975 | } |
| 976 | if {[info exists ::ioerropts(-sqlprep)]} { |
| 977 | execsql $::ioerropts(-sqlprep) |
| 978 | } |
| 979 | expr 0 |
| 980 | } {0} |
| 981 | |
| 982 | # Read the 'checksum' of the database. |
| 983 | if {$::ioerropts(-cksum)} { |
| 984 | set checksum [cksum] |
| 985 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 986 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 987 | # Set the Nth IO error to fail. |
| 988 | do_test $testname.$n.2 [subst { |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 989 | set ::sqlite_io_error_persist $::ioerropts(-persist) |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 990 | set ::sqlite_io_error_pending $n |
| 991 | }] $n |
| 992 | |
| 993 | # Create a single TCL script from the TCL and SQL specified |
| 994 | # as the body of the test. |
| 995 | set ::ioerrorbody {} |
| 996 | if {[info exists ::ioerropts(-tclbody)]} { |
| 997 | append ::ioerrorbody "$::ioerropts(-tclbody)\n" |
| 998 | } |
| 999 | if {[info exists ::ioerropts(-sqlbody)]} { |
| 1000 | append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}" |
| 1001 | } |
| 1002 | |
| 1003 | # Execute the TCL Script created in the above block. If |
| 1004 | # there are at least N IO operations performed by SQLite as |
| 1005 | # a result of the script, the Nth will fail. |
| 1006 | do_test $testname.$n.3 { |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1007 | set ::sqlite_io_error_hit 0 |
| 1008 | set ::sqlite_io_error_hardhit 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1009 | set r [catch $::ioerrorbody msg] |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1010 | set ::errseen $r |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 1011 | set rc [sqlite3_errcode $::DB] |
| 1012 | if {$::ioerropts(-erc)} { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 1013 | # If we are in extended result code mode, make sure all of the |
| 1014 | # IOERRs we get back really do have their extended code values. |
| 1015 | # If an extended result code is returned, the sqlite3_errcode |
| 1016 | # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn |
| 1017 | # where nnnn is a number |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 1018 | if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} { |
| 1019 | return $rc |
| 1020 | } |
| 1021 | } else { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 1022 | # If we are not in extended result code mode, make sure no |
| 1023 | # extended error codes are returned. |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 1024 | if {[regexp {\+\d} $rc]} { |
| 1025 | return $rc |
| 1026 | } |
| 1027 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1028 | # The test repeats as long as $::go is non-zero. $::go starts out |
| 1029 | # as 1. When a test runs to completion without hitting an I/O |
| 1030 | # error, that means there is no point in continuing with this test |
| 1031 | # case so set $::go to zero. |
| 1032 | # |
| 1033 | if {$::sqlite_io_error_pending>0} { |
| 1034 | set ::go 0 |
| 1035 | set q 0 |
| 1036 | set ::sqlite_io_error_pending 0 |
| 1037 | } else { |
| 1038 | set q 1 |
| 1039 | } |
| 1040 | |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 1041 | set s [expr $::sqlite_io_error_hit==0] |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1042 | if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} { |
| 1043 | set r 1 |
| 1044 | } |
danielk1977 | f2fa831 | 2006-01-24 13:09:33 +0000 | [diff] [blame] | 1045 | set ::sqlite_io_error_hit 0 |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 1046 | |
| 1047 | # One of two things must have happened. either |
| 1048 | # 1. We never hit the IO error and the SQL returned OK |
| 1049 | # 2. An IO error was hit and the SQL failed |
| 1050 | # |
dan | d41a29a | 2010-05-06 15:56:28 +0000 | [diff] [blame] | 1051 | #puts "s=$s r=$r q=$q" |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1052 | expr { ($s && !$r && !$q) || (!$s && $r && $q) } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1053 | } {1} |
| 1054 | |
danielk1977 | 80daec6 | 2008-05-12 10:57:02 +0000 | [diff] [blame] | 1055 | set ::sqlite_io_error_hit 0 |
| 1056 | set ::sqlite_io_error_pending 0 |
| 1057 | |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 1058 | # Check that no page references were leaked. There should be |
| 1059 | # a single reference if there is still an active transaction, |
| 1060 | # or zero otherwise. |
| 1061 | # |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 1062 | # UPDATE: If the IO error occurs after a 'BEGIN' but before any |
| 1063 | # locks are established on database files (i.e. if the error |
| 1064 | # occurs while attempting to detect a hot-journal file), then |
| 1065 | # there may 0 page references and an active transaction according |
| 1066 | # to [sqlite3_get_autocommit]. |
| 1067 | # |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 1068 | if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} { |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 1069 | do_test $testname.$n.4 { |
| 1070 | set bt [btree_from_db db] |
| 1071 | db_enter db |
| 1072 | array set stats [btree_pager_stats $bt] |
| 1073 | db_leave db |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 1074 | set nRef $stats(ref) |
| 1075 | expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)} |
| 1076 | } {1} |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 1079 | # If there is an open database handle and no open transaction, |
| 1080 | # and the pager is not running in exclusive-locking mode, |
| 1081 | # check that the pager is in "unlocked" state. Theoretically, |
| 1082 | # if a call to xUnlock() failed due to an IO error the underlying |
| 1083 | # file may still be locked. |
| 1084 | # |
| 1085 | ifcapable pragma { |
| 1086 | if { [info commands db] ne "" |
danielk1977 | 0259fbe | 2008-05-05 17:14:53 +0000 | [diff] [blame] | 1087 | && $::ioerropts(-ckrefcount) |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 1088 | && [db one {pragma locking_mode}] eq "normal" |
| 1089 | && [sqlite3_get_autocommit db] |
| 1090 | } { |
| 1091 | do_test $testname.$n.5 { |
| 1092 | set bt [btree_from_db db] |
| 1093 | db_enter db |
| 1094 | array set stats [btree_pager_stats $bt] |
| 1095 | db_leave db |
| 1096 | set stats(state) |
| 1097 | } 0 |
| 1098 | } |
| 1099 | } |
| 1100 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1101 | # If an IO error occured, then the checksum of the database should |
| 1102 | # be the same as before the script that caused the IO error was run. |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 1103 | # |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1104 | if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} { |
danielk1977 | 0259fbe | 2008-05-05 17:14:53 +0000 | [diff] [blame] | 1105 | do_test $testname.$n.6 { |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1106 | catch {db close} |
danielk1977 | a961339 | 2008-07-08 12:07:32 +0000 | [diff] [blame] | 1107 | catch {db2 close} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 1108 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1109 | cksum |
| 1110 | } $checksum |
| 1111 | } |
| 1112 | |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1113 | set ::sqlite_io_error_hardhit 0 |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 1114 | set ::sqlite_io_error_pending 0 |
danielk1977 | 105afed | 2005-05-26 15:20:53 +0000 | [diff] [blame] | 1115 | if {[info exists ::ioerropts(-cleanup)]} { |
| 1116 | catch $::ioerropts(-cleanup) |
| 1117 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1118 | } |
| 1119 | set ::sqlite_io_error_pending 0 |
drh | 6d54da0 | 2007-03-25 19:08:46 +0000 | [diff] [blame] | 1120 | set ::sqlite_io_error_persist 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1121 | unset ::ioerropts |
| 1122 | } |
| 1123 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1124 | # Return a checksum based on the contents of the main database associated |
| 1125 | # with connection $db |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1126 | # |
| 1127 | proc cksum {{db db}} { |
| 1128 | set txt [$db eval { |
| 1129 | SELECT name, type, sql FROM sqlite_master order by name |
| 1130 | }]\n |
| 1131 | foreach tbl [$db eval { |
| 1132 | SELECT name FROM sqlite_master WHERE type='table' order by name |
| 1133 | }] { |
| 1134 | append txt [$db eval "SELECT * FROM $tbl"]\n |
| 1135 | } |
| 1136 | foreach prag {default_synchronous default_cache_size} { |
| 1137 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 1138 | } |
| 1139 | set cksum [string length $txt]-[md5 $txt] |
| 1140 | # puts $cksum-[file size test.db] |
| 1141 | return $cksum |
| 1142 | } |
| 1143 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1144 | # Generate a checksum based on the contents of the main and temp tables |
| 1145 | # database $db. If the checksum of two databases is the same, and the |
| 1146 | # integrity-check passes for both, the two databases are identical. |
| 1147 | # |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 1148 | proc allcksum {{db db}} { |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1149 | set ret [list] |
| 1150 | ifcapable tempdb { |
| 1151 | set sql { |
| 1152 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 1153 | SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION |
| 1154 | SELECT 'sqlite_master' UNION |
| 1155 | SELECT 'sqlite_temp_master' ORDER BY 1 |
| 1156 | } |
| 1157 | } else { |
| 1158 | set sql { |
| 1159 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 1160 | SELECT 'sqlite_master' ORDER BY 1 |
| 1161 | } |
| 1162 | } |
| 1163 | set tbllist [$db eval $sql] |
| 1164 | set txt {} |
| 1165 | foreach tbl $tbllist { |
| 1166 | append txt [$db eval "SELECT * FROM $tbl"] |
| 1167 | } |
| 1168 | foreach prag {default_cache_size} { |
| 1169 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 1170 | } |
| 1171 | # puts txt=$txt |
| 1172 | return [md5 $txt] |
| 1173 | } |
| 1174 | |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 1175 | # Generate a checksum based on the contents of a single database with |
| 1176 | # a database connection. The name of the database is $dbname. |
| 1177 | # Examples of $dbname are "temp" or "main". |
| 1178 | # |
| 1179 | proc dbcksum {db dbname} { |
| 1180 | if {$dbname=="temp"} { |
| 1181 | set master sqlite_temp_master |
| 1182 | } else { |
| 1183 | set master $dbname.sqlite_master |
| 1184 | } |
| 1185 | set alltab [$db eval "SELECT name FROM $master WHERE type='table'"] |
| 1186 | set txt [$db eval "SELECT * FROM $master"]\n |
| 1187 | foreach tab $alltab { |
| 1188 | append txt [$db eval "SELECT * FROM $dbname.$tab"]\n |
| 1189 | } |
| 1190 | return [md5 $txt] |
| 1191 | } |
| 1192 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1193 | proc memdebug_log_sql {{filename mallocs.sql}} { |
| 1194 | |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1195 | set data [sqlite3_memdebug_log dump] |
| 1196 | set nFrame [expr [llength [lindex $data 0]]-2] |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1197 | if {$nFrame < 0} { return "" } |
| 1198 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1199 | set database temp |
| 1200 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1201 | set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1202 | |
| 1203 | set sql "" |
| 1204 | foreach e $data { |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1205 | set nCall [lindex $e 0] |
| 1206 | set nByte [lindex $e 1] |
| 1207 | set lStack [lrange $e 2 end] |
| 1208 | append sql "INSERT INTO ${database}.malloc VALUES" |
| 1209 | append sql "('test', $nCall, $nByte, '$lStack');\n" |
| 1210 | foreach f $lStack { |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1211 | set frames($f) 1 |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n" |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1216 | set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1217 | |
| 1218 | foreach f [array names frames] { |
| 1219 | set addr [format %x $f] |
| 1220 | set cmd "addr2line -e [info nameofexec] $addr" |
| 1221 | set line [eval exec $cmd] |
| 1222 | append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n" |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1223 | |
| 1224 | set file [lindex [split $line :] 0] |
| 1225 | set files($file) 1 |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1228 | foreach f [array names files] { |
| 1229 | set contents "" |
| 1230 | catch { |
| 1231 | set fd [open $f] |
| 1232 | set contents [read $fd] |
| 1233 | close $fd |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1234 | } |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1235 | set contents [string map {' ''} $contents] |
| 1236 | append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1237 | } |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1238 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1239 | set fd [open $filename w] |
| 1240 | puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;" |
| 1241 | close $fd |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1242 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1243 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1244 | # Copy file $from into $to. This is used because some versions of |
| 1245 | # TCL for windows (notably the 8.4.1 binary package shipped with the |
| 1246 | # current mingw release) have a broken "file copy" command. |
| 1247 | # |
| 1248 | proc copy_file {from to} { |
| 1249 | if {$::tcl_platform(platform)=="unix"} { |
| 1250 | file copy -force $from $to |
| 1251 | } else { |
| 1252 | set f [open $from] |
| 1253 | fconfigure $f -translation binary |
| 1254 | set t [open $to w] |
| 1255 | fconfigure $t -translation binary |
| 1256 | puts -nonewline $t [read $f [file size $from]] |
| 1257 | close $t |
| 1258 | close $f |
| 1259 | } |
| 1260 | } |
| 1261 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1262 | # Drop all tables in database [db] |
| 1263 | proc drop_all_tables {{db db}} { |
shaneh | 4e7b32f | 2009-12-17 22:12:51 +0000 | [diff] [blame] | 1264 | ifcapable trigger&&foreignkey { |
| 1265 | set pk [$db one "PRAGMA foreign_keys"] |
| 1266 | $db eval "PRAGMA foreign_keys = OFF" |
| 1267 | } |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 1268 | foreach {idx name file} [db eval {PRAGMA database_list}] { |
| 1269 | if {$idx==1} { |
| 1270 | set master sqlite_temp_master |
| 1271 | } else { |
| 1272 | set master $name.sqlite_master |
| 1273 | } |
| 1274 | foreach {t type} [$db eval " |
| 1275 | SELECT name, type FROM $master |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 1276 | WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X' |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 1277 | "] { |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 1278 | $db eval "DROP $type \"$t\"" |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 1279 | } |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1280 | } |
shaneh | 4e7b32f | 2009-12-17 22:12:51 +0000 | [diff] [blame] | 1281 | ifcapable trigger&&foreignkey { |
| 1282 | $db eval "PRAGMA foreign_keys = $pk" |
| 1283 | } |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1286 | #------------------------------------------------------------------------- |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1287 | # If a test script is executed with global variable $::G(perm:name) set to |
| 1288 | # "wal", then the tests are run in WAL mode. Otherwise, they should be run |
| 1289 | # in rollback mode. The following Tcl procs are used to make this less |
| 1290 | # intrusive: |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1291 | # |
| 1292 | # wal_set_journal_mode ?DB? |
| 1293 | # |
| 1294 | # If running a WAL test, execute "PRAGMA journal_mode = wal" using |
| 1295 | # connection handle DB. Otherwise, this command is a no-op. |
| 1296 | # |
| 1297 | # wal_check_journal_mode TESTNAME ?DB? |
| 1298 | # |
| 1299 | # If running a WAL test, execute a tests case that fails if the main |
| 1300 | # database for connection handle DB is not currently a WAL database. |
| 1301 | # Otherwise (if not running a WAL permutation) this is a no-op. |
| 1302 | # |
| 1303 | # wal_is_wal_mode |
| 1304 | # |
| 1305 | # Returns true if this test should be run in WAL mode. False otherwise. |
| 1306 | # |
| 1307 | proc wal_is_wal_mode {} { |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1308 | expr {[permutation] eq "wal"} |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1309 | } |
| 1310 | proc wal_set_journal_mode {{db db}} { |
| 1311 | if { [wal_is_wal_mode] } { |
| 1312 | $db eval "PRAGMA journal_mode = WAL" |
| 1313 | } |
| 1314 | } |
| 1315 | proc wal_check_journal_mode {testname {db db}} { |
| 1316 | if { [wal_is_wal_mode] } { |
| 1317 | $db eval { SELECT * FROM sqlite_master } |
| 1318 | do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal} |
| 1319 | } |
| 1320 | } |
| 1321 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1322 | proc permutation {} { |
| 1323 | set perm "" |
| 1324 | catch {set perm $::G(perm:name)} |
| 1325 | set perm |
| 1326 | } |
dan | cb79e51 | 2010-08-06 13:50:07 +0000 | [diff] [blame] | 1327 | proc presql {} { |
| 1328 | set presql "" |
| 1329 | catch {set presql $::G(perm:presql)} |
| 1330 | set presql |
| 1331 | } |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1332 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1333 | #------------------------------------------------------------------------- |
| 1334 | # |
| 1335 | proc slave_test_script {script} { |
| 1336 | |
| 1337 | # Create the interpreter used to run the test script. |
| 1338 | interp create tinterp |
| 1339 | |
| 1340 | # Populate some global variables that tester.tcl expects to see. |
| 1341 | foreach {var value} [list \ |
| 1342 | ::argv0 $::argv0 \ |
| 1343 | ::argv {} \ |
| 1344 | ::SLAVE 1 \ |
| 1345 | ] { |
| 1346 | interp eval tinterp [list set $var $value] |
| 1347 | } |
| 1348 | |
| 1349 | # The alias used to access the global test counters. |
| 1350 | tinterp alias set_test_counter set_test_counter |
| 1351 | |
| 1352 | # Set up the ::cmdlinearg array in the slave. |
| 1353 | interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]] |
| 1354 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1355 | # Set up the ::G array in the slave. |
| 1356 | interp eval tinterp [list array set ::G [array get ::G]] |
| 1357 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1358 | # Load the various test interfaces implemented in C. |
| 1359 | load_testfixture_extensions tinterp |
| 1360 | |
| 1361 | # Run the test script. |
| 1362 | interp eval tinterp $script |
| 1363 | |
dan | ea5542d | 2010-07-06 11:26:15 +0000 | [diff] [blame] | 1364 | # Check if the interpreter call [run_thread_tests] |
| 1365 | if { [interp eval tinterp {info exists ::run_thread_tests_called}] } { |
| 1366 | set ::run_thread_tests_called 1 |
| 1367 | } |
| 1368 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1369 | # Delete the interpreter used to run the test script. |
| 1370 | interp delete tinterp |
| 1371 | } |
| 1372 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1373 | proc slave_test_file {zFile} { |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1374 | set tail [file tail $zFile] |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1375 | |
dan | 6a64d67 | 2011-04-04 15:38:16 +0000 | [diff] [blame] | 1376 | if {[info exists ::G(start:permutation)]} { |
| 1377 | if {[permutation] != $::G(start:permutation)} return |
| 1378 | unset ::G(start:permutation) |
| 1379 | } |
| 1380 | if {[info exists ::G(start:file)]} { |
| 1381 | if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return |
| 1382 | unset ::G(start:file) |
| 1383 | } |
| 1384 | |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1385 | # Remember the value of the shared-cache setting. So that it is possible |
| 1386 | # to check afterwards that it was not modified by the test script. |
| 1387 | # |
| 1388 | ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] } |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1389 | |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1390 | # Run the test script in a slave interpreter. |
| 1391 | # |
dan | ea5542d | 2010-07-06 11:26:15 +0000 | [diff] [blame] | 1392 | unset -nocomplain ::run_thread_tests_called |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1393 | reset_prng_state |
| 1394 | set ::sqlite_open_file_count 0 |
| 1395 | set time [time { slave_test_script [list source $zFile] }] |
| 1396 | set ms [expr [lindex $time 0] / 1000] |
| 1397 | |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1398 | # Test that all files opened by the test script were closed. Omit this |
| 1399 | # if the test script has "thread" in its name. The open file counter |
| 1400 | # is not thread-safe. |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1401 | # |
dan | ea5542d | 2010-07-06 11:26:15 +0000 | [diff] [blame] | 1402 | if {[info exists ::run_thread_tests_called]==0} { |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1403 | do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0} |
| 1404 | } |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1405 | set ::sqlite_open_file_count 0 |
| 1406 | |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1407 | # Test that the global "shared-cache" setting was not altered by |
| 1408 | # the test script. |
| 1409 | # |
| 1410 | ifcapable shared_cache { |
| 1411 | set res [expr {[sqlite3_enable_shared_cache] == $scs}] |
| 1412 | do_test ${tail}-sharedcachesetting [list set {} $res] 1 |
| 1413 | } |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1414 | |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1415 | # Add some info to the output. |
| 1416 | # |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1417 | puts "Time: $tail $ms ms" |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1418 | show_memstats |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
dan | 59257dc | 2010-08-04 11:34:31 +0000 | [diff] [blame] | 1421 | # Open a new connection on database test.db and execute the SQL script |
| 1422 | # supplied as an argument. Before returning, close the new conection and |
| 1423 | # restore the 4 byte fields starting at header offsets 28, 92 and 96 |
| 1424 | # to the values they held before the SQL was executed. This simulates |
| 1425 | # a write by a pre-3.7.0 client. |
| 1426 | # |
| 1427 | proc sql36231 {sql} { |
| 1428 | set B [hexio_read test.db 92 8] |
| 1429 | set A [hexio_read test.db 28 4] |
| 1430 | sqlite3 db36231 test.db |
| 1431 | catch { db36231 func a_string a_string } |
| 1432 | execsql $sql db36231 |
| 1433 | db36231 close |
| 1434 | hexio_write test.db 28 $A |
| 1435 | hexio_write test.db 92 $B |
| 1436 | return "" |
| 1437 | } |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1438 | |
dan | ccc7ff4 | 2010-11-29 12:06:45 +0000 | [diff] [blame] | 1439 | proc db_save {} { |
| 1440 | foreach f [glob -nocomplain sv_test.db*] { forcedelete $f } |
| 1441 | foreach f [glob -nocomplain test.db*] { |
| 1442 | set f2 "sv_$f" |
| 1443 | file copy -force $f $f2 |
| 1444 | } |
| 1445 | } |
| 1446 | proc db_save_and_close {} { |
| 1447 | db_save |
| 1448 | catch { db close } |
| 1449 | return "" |
| 1450 | } |
| 1451 | proc db_restore {} { |
| 1452 | foreach f [glob -nocomplain test.db*] { forcedelete $f } |
| 1453 | foreach f2 [glob -nocomplain sv_test.db*] { |
| 1454 | set f [string range $f2 3 end] |
| 1455 | file copy -force $f2 $f |
| 1456 | } |
| 1457 | } |
| 1458 | proc db_restore_and_reopen {{dbfile test.db}} { |
| 1459 | catch { db close } |
| 1460 | db_restore |
| 1461 | sqlite3 db $dbfile |
| 1462 | } |
| 1463 | proc db_delete_and_reopen {{file test.db}} { |
| 1464 | catch { db close } |
| 1465 | foreach f [glob -nocomplain test.db*] { file delete -force $f } |
| 1466 | sqlite3 db $file |
| 1467 | } |
| 1468 | |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 1469 | # If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set |
| 1470 | # to non-zero, then set the global variable $AUTOVACUUM to 1. |
| 1471 | set AUTOVACUUM $sqlite_options(default_autovacuum) |
danielk1977 | ee8b799 | 2009-03-26 17:13:06 +0000 | [diff] [blame] | 1472 | |
| 1473 | source $testdir/thread_common.tcl |
dan | ddf80eb | 2010-10-25 12:47:43 +0000 | [diff] [blame] | 1474 | source $testdir/malloc_common.tcl |