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 | #------------------------------------------------------------------------- |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 17 | # The commands provided by the code in this file to help with creating |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 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 | # |
mistachkin | c548465 | 2012-03-05 22:52:33 +0000 | [diff] [blame] | 22 | # is_relative_file |
mistachkin | 4a41f34 | 2012-03-06 03:00:49 +0000 | [diff] [blame] | 23 | # test_pwd |
mistachkin | f8a7846 | 2012-03-08 20:00:36 +0000 | [diff] [blame] | 24 | # get_pwd |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 25 | # copy_file FROM TO |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 26 | # delete_file FILENAME |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 27 | # drop_all_tables ?DB? |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 28 | # forcecopy FROM TO |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 29 | # forcedelete FILENAME |
| 30 | # |
| 31 | # Test the capability of the SQLite version built into the interpreter to |
| 32 | # determine if a specific test can be run: |
| 33 | # |
mistachkin | c60941f | 2012-09-13 01:51:02 +0000 | [diff] [blame] | 34 | # capable EXPR |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 35 | # ifcapable EXPR |
| 36 | # |
| 37 | # Calulate checksums based on database contents: |
| 38 | # |
| 39 | # dbcksum DB DBNAME |
| 40 | # allcksum ?DB? |
| 41 | # cksum ?DB? |
| 42 | # |
| 43 | # Commands to execute/explain SQL statements: |
| 44 | # |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 45 | # memdbsql SQL |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 46 | # stepsql DB SQL |
| 47 | # execsql2 SQL |
| 48 | # explain_no_trace SQL |
| 49 | # explain SQL ?DB? |
| 50 | # catchsql SQL ?DB? |
| 51 | # execsql SQL ?DB? |
| 52 | # |
| 53 | # Commands to run test cases: |
| 54 | # |
| 55 | # do_ioerr_test TESTNAME ARGS... |
| 56 | # crashsql ARGS... |
| 57 | # integrity_check TESTNAME ?DB? |
drh | 433dccf | 2013-02-09 15:37:11 +0000 | [diff] [blame] | 58 | # verify_ex_errcode TESTNAME EXPECTED ?DB? |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 59 | # do_test TESTNAME SCRIPT EXPECTED |
dan | 153eda0 | 2010-06-21 07:45:47 +0000 | [diff] [blame] | 60 | # do_execsql_test TESTNAME SQL EXPECTED |
| 61 | # do_catchsql_test TESTNAME SQL EXPECTED |
drh | 3c2aeae | 2014-01-24 14:37:44 +0000 | [diff] [blame] | 62 | # do_timed_execsql_test TESTNAME SQL EXPECTED |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 63 | # |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 64 | # Commands providing a lower level interface to the global test counters: |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 65 | # |
| 66 | # set_test_counter COUNTER ?VALUE? |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 67 | # omit_test TESTNAME REASON ?APPEND? |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 68 | # fail_test TESTNAME |
| 69 | # incr_ntest |
| 70 | # |
| 71 | # Command run at the end of each test file: |
| 72 | # |
| 73 | # finish_test |
| 74 | # |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 75 | # Commands to help create test files that run with the "WAL" and other |
| 76 | # permutations (see file permutations.test): |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 77 | # |
| 78 | # wal_is_wal_mode |
| 79 | # wal_set_journal_mode ?DB? |
| 80 | # wal_check_journal_mode TESTNAME?DB? |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 81 | # permutation |
dan | cb79e51 | 2010-08-06 13:50:07 +0000 | [diff] [blame] | 82 | # presql |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 83 | # |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 84 | # Command to test whether or not --verbose=1 was specified on the command |
| 85 | # line (returns 0 for not-verbose, 1 for verbose and 2 for "verbose in the |
| 86 | # output file only"). |
| 87 | # |
| 88 | # verbose |
| 89 | # |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 90 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 91 | # Set the precision of FP arithmatic used by the interpreter. And |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 92 | # configure SQLite to take database file locks on the page that begins |
| 93 | # 64KB into the database file instead of the one 1GB in. This means |
| 94 | # the code that handles that special case can be tested without creating |
| 95 | # very large database files. |
| 96 | # |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 97 | set tcl_precision 15 |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 98 | sqlite3_test_control_pending_byte 0x0010000 |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 99 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 100 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 101 | # If the pager codec is available, create a wrapper for the [sqlite3] |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 102 | # command that appends "-key {xyzzy}" to the command line. i.e. this: |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 103 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 104 | # sqlite3 db test.db |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 105 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 106 | # becomes |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 107 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 108 | # sqlite3 db test.db -key {xyzzy} |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 109 | # |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 110 | if {[info command sqlite_orig]==""} { |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 111 | rename sqlite3 sqlite_orig |
| 112 | proc sqlite3 {args} { |
dan | 68928b6 | 2010-06-22 13:46:43 +0000 | [diff] [blame] | 113 | if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} { |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 114 | # This command is opening a new database connection. |
| 115 | # |
| 116 | if {[info exists ::G(perm:sqlite3_args)]} { |
| 117 | set args [concat $args $::G(perm:sqlite3_args)] |
| 118 | } |
dan | 68928b6 | 2010-06-22 13:46:43 +0000 | [diff] [blame] | 119 | if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} { |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 120 | lappend args -key {xyzzy} |
| 121 | } |
| 122 | |
dan | 3ccd20a | 2010-06-09 19:01:02 +0000 | [diff] [blame] | 123 | set res [uplevel 1 sqlite_orig $args] |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 124 | if {[info exists ::G(perm:presql)]} { |
| 125 | [lindex $args 0] eval $::G(perm:presql) |
| 126 | } |
dan | 1ce1b4a | 2010-12-07 14:32:28 +0000 | [diff] [blame] | 127 | if {[info exists ::G(perm:dbconfig)]} { |
| 128 | set ::dbhandle [lindex $args 0] |
| 129 | uplevel #0 $::G(perm:dbconfig) |
| 130 | } |
dan | 3ccd20a | 2010-06-09 19:01:02 +0000 | [diff] [blame] | 131 | set res |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 132 | } else { |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 133 | # This command is not opening a new database connection. Pass the |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 134 | # arguments through to the C implementation as the are. |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 135 | # |
| 136 | uplevel 1 sqlite_orig $args |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 137 | } |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 141 | proc getFileRetries {} { |
| 142 | if {![info exists ::G(file-retries)]} { |
| 143 | # |
| 144 | # NOTE: Return the default number of retries for [file] operations. A |
| 145 | # value of zero or less here means "disabled". |
| 146 | # |
mistachkin | c60941f | 2012-09-13 01:51:02 +0000 | [diff] [blame] | 147 | return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}] |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 148 | } |
| 149 | return $::G(file-retries) |
| 150 | } |
| 151 | |
| 152 | proc getFileRetryDelay {} { |
| 153 | if {![info exists ::G(file-retry-delay)]} { |
| 154 | # |
| 155 | # NOTE: Return the default number of milliseconds to wait when retrying |
| 156 | # failed [file] operations. A value of zero or less means "do not |
| 157 | # wait". |
| 158 | # |
| 159 | return 100; # TODO: Good default? |
| 160 | } |
| 161 | return $::G(file-retry-delay) |
| 162 | } |
| 163 | |
mistachkin | f8a7846 | 2012-03-08 20:00:36 +0000 | [diff] [blame] | 164 | # Return the string representing the name of the current directory. On |
| 165 | # Windows, the result is "normalized" to whatever our parent command shell |
| 166 | # is using to prevent case-mismatch issues. |
| 167 | # |
| 168 | proc get_pwd {} { |
| 169 | if {$::tcl_platform(platform) eq "windows"} { |
mistachkin | 533b8f6 | 2012-03-08 20:28:31 +0000 | [diff] [blame] | 170 | # |
| 171 | # NOTE: Cannot use [file normalize] here because it would alter the |
| 172 | # case of the result to what Tcl considers canonical, which would |
| 173 | # defeat the purpose of this procedure. |
| 174 | # |
| 175 | return [string map [list \\ /] \ |
| 176 | [string trim [exec -- $::env(ComSpec) /c echo %CD%]]] |
mistachkin | f8a7846 | 2012-03-08 20:00:36 +0000 | [diff] [blame] | 177 | } else { |
| 178 | return [pwd] |
| 179 | } |
| 180 | } |
| 181 | |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 182 | # Copy file $from into $to. This is used because some versions of |
| 183 | # TCL for windows (notably the 8.4.1 binary package shipped with the |
| 184 | # current mingw release) have a broken "file copy" command. |
| 185 | # |
| 186 | proc copy_file {from to} { |
| 187 | do_copy_file false $from $to |
| 188 | } |
| 189 | |
| 190 | proc forcecopy {from to} { |
| 191 | do_copy_file true $from $to |
| 192 | } |
| 193 | |
| 194 | proc do_copy_file {force from to} { |
| 195 | set nRetry [getFileRetries] ;# Maximum number of retries. |
| 196 | set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. |
| 197 | |
| 198 | # On windows, sometimes even a [file copy -force] can fail. The cause is |
| 199 | # usually "tag-alongs" - programs like anti-virus software, automatic backup |
| 200 | # tools and various explorer extensions that keep a file open a little longer |
| 201 | # than we expect, causing the delete to fail. |
| 202 | # |
| 203 | # The solution is to wait a short amount of time before retrying the copy. |
| 204 | # |
| 205 | if {$nRetry > 0} { |
| 206 | for {set i 0} {$i<$nRetry} {incr i} { |
| 207 | set rc [catch { |
| 208 | if {$force} { |
| 209 | file copy -force $from $to |
| 210 | } else { |
| 211 | file copy $from $to |
| 212 | } |
| 213 | } msg] |
| 214 | if {$rc==0} break |
| 215 | if {$nDelay > 0} { after $nDelay } |
| 216 | } |
| 217 | if {$rc} { error $msg } |
| 218 | } else { |
| 219 | if {$force} { |
| 220 | file copy -force $from $to |
| 221 | } else { |
| 222 | file copy $from $to |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
mistachkin | c548465 | 2012-03-05 22:52:33 +0000 | [diff] [blame] | 227 | # Check if a file name is relative |
| 228 | # |
| 229 | proc is_relative_file { file } { |
| 230 | return [expr {[file pathtype $file] != "absolute"}] |
| 231 | } |
| 232 | |
mistachkin | 4a41f34 | 2012-03-06 03:00:49 +0000 | [diff] [blame] | 233 | # If the VFS supports using the current directory, returns [pwd]; |
| 234 | # otherwise, it returns only the provided suffix string (which is |
| 235 | # empty by default). |
| 236 | # |
| 237 | proc test_pwd { args } { |
| 238 | if {[llength $args] > 0} { |
| 239 | set suffix1 [lindex $args 0] |
| 240 | if {[llength $args] > 1} { |
| 241 | set suffix2 [lindex $args 1] |
| 242 | } else { |
| 243 | set suffix2 $suffix1 |
| 244 | } |
| 245 | } else { |
| 246 | set suffix1 ""; set suffix2 "" |
| 247 | } |
| 248 | ifcapable curdir { |
mistachkin | 6aa18c9 | 2012-03-08 20:22:42 +0000 | [diff] [blame] | 249 | return "[get_pwd]$suffix1" |
mistachkin | 4a41f34 | 2012-03-06 03:00:49 +0000 | [diff] [blame] | 250 | } else { |
| 251 | return $suffix2 |
| 252 | } |
| 253 | } |
| 254 | |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 255 | # Delete a file or directory |
| 256 | # |
| 257 | proc delete_file {args} { |
| 258 | do_delete_file false {*}$args |
| 259 | } |
| 260 | |
| 261 | proc forcedelete {args} { |
| 262 | do_delete_file true {*}$args |
| 263 | } |
| 264 | |
| 265 | proc do_delete_file {force args} { |
| 266 | set nRetry [getFileRetries] ;# Maximum number of retries. |
| 267 | set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. |
| 268 | |
| 269 | foreach filename $args { |
| 270 | # On windows, sometimes even a [file delete -force] can fail just after |
| 271 | # a file is closed. The cause is usually "tag-alongs" - programs like |
| 272 | # anti-virus software, automatic backup tools and various explorer |
| 273 | # extensions that keep a file open a little longer than we expect, causing |
| 274 | # the delete to fail. |
| 275 | # |
| 276 | # The solution is to wait a short amount of time before retrying the |
| 277 | # delete. |
| 278 | # |
| 279 | if {$nRetry > 0} { |
| 280 | for {set i 0} {$i<$nRetry} {incr i} { |
| 281 | set rc [catch { |
| 282 | if {$force} { |
| 283 | file delete -force $filename |
| 284 | } else { |
| 285 | file delete $filename |
| 286 | } |
| 287 | } msg] |
| 288 | if {$rc==0} break |
| 289 | if {$nDelay > 0} { after $nDelay } |
| 290 | } |
| 291 | if {$rc} { error $msg } |
| 292 | } else { |
| 293 | if {$force} { |
| 294 | file delete -force $filename |
| 295 | } else { |
| 296 | file delete $filename |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 302 | if {$::tcl_platform(platform) eq "windows"} { |
| 303 | proc do_remove_win32_dir {args} { |
| 304 | set nRetry [getFileRetries] ;# Maximum number of retries. |
| 305 | set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. |
| 306 | |
| 307 | foreach dirName $args { |
| 308 | # On windows, sometimes even a [remove_win32_dir] can fail just after |
| 309 | # a directory is emptied. The cause is usually "tag-alongs" - programs |
| 310 | # like anti-virus software, automatic backup tools and various explorer |
| 311 | # extensions that keep a file open a little longer than we expect, |
| 312 | # causing the delete to fail. |
| 313 | # |
| 314 | # The solution is to wait a short amount of time before retrying the |
| 315 | # removal. |
| 316 | # |
| 317 | if {$nRetry > 0} { |
| 318 | for {set i 0} {$i < $nRetry} {incr i} { |
| 319 | set rc [catch { |
| 320 | remove_win32_dir $dirName |
| 321 | } msg] |
| 322 | if {$rc == 0} break |
| 323 | if {$nDelay > 0} { after $nDelay } |
| 324 | } |
| 325 | if {$rc} { error $msg } |
| 326 | } else { |
| 327 | remove_win32_dir $dirName |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | proc do_delete_win32_file {args} { |
| 333 | set nRetry [getFileRetries] ;# Maximum number of retries. |
| 334 | set nDelay [getFileRetryDelay] ;# Delay in ms before retrying. |
| 335 | |
| 336 | foreach fileName $args { |
| 337 | # On windows, sometimes even a [delete_win32_file] can fail just after |
| 338 | # a file is closed. The cause is usually "tag-alongs" - programs like |
| 339 | # anti-virus software, automatic backup tools and various explorer |
| 340 | # extensions that keep a file open a little longer than we expect, |
| 341 | # causing the delete to fail. |
| 342 | # |
| 343 | # The solution is to wait a short amount of time before retrying the |
| 344 | # delete. |
| 345 | # |
| 346 | if {$nRetry > 0} { |
| 347 | for {set i 0} {$i < $nRetry} {incr i} { |
| 348 | set rc [catch { |
| 349 | delete_win32_file $fileName |
| 350 | } msg] |
| 351 | if {$rc == 0} break |
| 352 | if {$nDelay > 0} { after $nDelay } |
| 353 | } |
| 354 | if {$rc} { error $msg } |
| 355 | } else { |
| 356 | delete_win32_file $fileName |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
dan | cb35460 | 2010-07-08 09:44:42 +0000 | [diff] [blame] | 362 | proc execpresql {handle args} { |
| 363 | trace remove execution $handle enter [list execpresql $handle] |
| 364 | if {[info exists ::G(perm:presql)]} { |
| 365 | $handle eval $::G(perm:presql) |
| 366 | } |
| 367 | } |
| 368 | |
dan | 68928b6 | 2010-06-22 13:46:43 +0000 | [diff] [blame] | 369 | # This command should be called after loading tester.tcl from within |
| 370 | # all test scripts that are incompatible with encryption codecs. |
| 371 | # |
| 372 | proc do_not_use_codec {} { |
| 373 | set ::do_not_use_codec 1 |
| 374 | reset_db |
| 375 | } |
| 376 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 377 | # The following block only runs the first time this file is sourced. It |
| 378 | # does not run in slave interpreters (since the ::cmdlinearg array is |
| 379 | # populated before the test script is run in slave interpreters). |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 380 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 381 | if {[info exists cmdlinearg]==0} { |
| 382 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 383 | # Parse any options specified in the $argv array. This script accepts the |
| 384 | # following options: |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 385 | # |
| 386 | # --pause |
| 387 | # --soft-heap-limit=NN |
| 388 | # --maxerror=NN |
| 389 | # --malloctrace=N |
| 390 | # --backtrace=N |
| 391 | # --binarylog=N |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 392 | # --soak=N |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 393 | # --file-retries=N |
| 394 | # --file-retry-delay=N |
dan | 6a64d67 | 2011-04-04 15:38:16 +0000 | [diff] [blame] | 395 | # --start=[$permutation:]$testfile |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 396 | # --match=$pattern |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 397 | # --verbose=$val |
| 398 | # --output=$filename |
| 399 | # --help |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 400 | # |
| 401 | set cmdlinearg(soft-heap-limit) 0 |
| 402 | set cmdlinearg(maxerror) 1000 |
| 403 | set cmdlinearg(malloctrace) 0 |
| 404 | set cmdlinearg(backtrace) 10 |
| 405 | set cmdlinearg(binarylog) 0 |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 406 | set cmdlinearg(soak) 0 |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 407 | set cmdlinearg(file-retries) 0 |
| 408 | set cmdlinearg(file-retry-delay) 0 |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 409 | set cmdlinearg(start) "" |
| 410 | set cmdlinearg(match) "" |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 411 | set cmdlinearg(verbose) "" |
| 412 | set cmdlinearg(output) "" |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 413 | |
| 414 | set leftover [list] |
| 415 | foreach a $argv { |
| 416 | switch -regexp -- $a { |
| 417 | {^-+pause$} { |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 418 | # Wait for user input before continuing. This is to give the user an |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 419 | # opportunity to connect profiling tools to the process. |
| 420 | puts -nonewline "Press RETURN to begin..." |
| 421 | flush stdout |
| 422 | gets stdin |
| 423 | } |
| 424 | {^-+soft-heap-limit=.+$} { |
| 425 | foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break |
| 426 | } |
| 427 | {^-+maxerror=.+$} { |
| 428 | foreach {dummy cmdlinearg(maxerror)} [split $a =] break |
| 429 | } |
| 430 | {^-+malloctrace=.+$} { |
| 431 | foreach {dummy cmdlinearg(malloctrace)} [split $a =] break |
| 432 | if {$cmdlinearg(malloctrace)} { |
| 433 | sqlite3_memdebug_log start |
| 434 | } |
| 435 | } |
| 436 | {^-+backtrace=.+$} { |
| 437 | foreach {dummy cmdlinearg(backtrace)} [split $a =] break |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 438 | sqlite3_memdebug_backtrace $value |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 439 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 440 | {^-+binarylog=.+$} { |
| 441 | foreach {dummy cmdlinearg(binarylog)} [split $a =] break |
| 442 | } |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 443 | {^-+soak=.+$} { |
| 444 | foreach {dummy cmdlinearg(soak)} [split $a =] break |
| 445 | set ::G(issoak) $cmdlinearg(soak) |
| 446 | } |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 447 | {^-+file-retries=.+$} { |
| 448 | foreach {dummy cmdlinearg(file-retries)} [split $a =] break |
| 449 | set ::G(file-retries) $cmdlinearg(file-retries) |
| 450 | } |
| 451 | {^-+file-retry-delay=.+$} { |
| 452 | foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break |
| 453 | set ::G(file-retry-delay) $cmdlinearg(file-retry-delay) |
| 454 | } |
dan | 6a64d67 | 2011-04-04 15:38:16 +0000 | [diff] [blame] | 455 | {^-+start=.+$} { |
| 456 | foreach {dummy cmdlinearg(start)} [split $a =] break |
| 457 | |
| 458 | set ::G(start:file) $cmdlinearg(start) |
| 459 | if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} { |
| 460 | set ::G(start:permutation) ${s.perm} |
| 461 | set ::G(start:file) ${s.file} |
| 462 | } |
| 463 | if {$::G(start:file) == ""} {unset ::G(start:file)} |
| 464 | } |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 465 | {^-+match=.+$} { |
| 466 | foreach {dummy cmdlinearg(match)} [split $a =] break |
| 467 | |
| 468 | set ::G(match) $cmdlinearg(match) |
| 469 | if {$::G(match) == ""} {unset ::G(match)} |
| 470 | } |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 471 | |
| 472 | {^-+output=.+$} { |
| 473 | foreach {dummy cmdlinearg(output)} [split $a =] break |
| 474 | if {$cmdlinearg(verbose)==""} { |
| 475 | set cmdlinearg(verbose) 2 |
| 476 | } |
| 477 | } |
| 478 | {^-+verbose=.+$} { |
| 479 | foreach {dummy cmdlinearg(verbose)} [split $a =] break |
| 480 | if {$cmdlinearg(verbose)=="file"} { |
| 481 | set cmdlinearg(verbose) 2 |
| 482 | } elseif {[string is boolean -strict $cmdlinearg(verbose)]==0} { |
| 483 | error "option --verbose= must be set to a boolean or to \"file\"" |
| 484 | } |
| 485 | } |
| 486 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 487 | default { |
| 488 | lappend leftover $a |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | set argv $leftover |
| 493 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 494 | # Install the malloc layer used to inject OOM errors. And the 'automatic' |
| 495 | # extensions. This only needs to be done once for the process. |
| 496 | # |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 497 | sqlite3_shutdown |
| 498 | install_malloc_faultsim 1 |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 499 | sqlite3_initialize |
drh | bb77b75 | 2009-04-09 01:23:49 +0000 | [diff] [blame] | 500 | autoinstall_test_functions |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 501 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 502 | # If the --binarylog option was specified, create the logging VFS. This |
| 503 | # call installs the new VFS as the default for all SQLite connections. |
| 504 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 505 | if {$cmdlinearg(binarylog)} { |
dan | fbefb89 | 2010-05-12 19:02:35 +0000 | [diff] [blame] | 506 | vfslog new binarylog {} vfslog.bin |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | # Set the backtrace depth, if malloc tracing is enabled. |
| 510 | # |
| 511 | if {$cmdlinearg(malloctrace)} { |
| 512 | sqlite3_memdebug_backtrace $cmdlinearg(backtrace) |
danielk1977 | 185eac9 | 2008-07-12 15:55:54 +0000 | [diff] [blame] | 513 | } |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 514 | |
| 515 | if {$cmdlinearg(output)!=""} { |
| 516 | puts "Copying output to file $cmdlinearg(output)" |
| 517 | set ::G(output_fd) [open $cmdlinearg(output) w] |
| 518 | fconfigure $::G(output_fd) -buffering line |
| 519 | } |
| 520 | |
| 521 | if {$cmdlinearg(verbose)==""} { |
| 522 | set cmdlinearg(verbose) 1 |
| 523 | } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 524 | } |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 525 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 526 | # Update the soft-heap-limit each time this script is run. In that |
| 527 | # way if an individual test file changes the soft-heap-limit, it |
| 528 | # will be reset at the start of the next test file. |
| 529 | # |
| 530 | sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit) |
| 531 | |
| 532 | # Create a test database |
| 533 | # |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 534 | proc reset_db {} { |
| 535 | catch {db close} |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 536 | forcedelete test.db |
| 537 | forcedelete test.db-journal |
| 538 | forcedelete test.db-wal |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 539 | sqlite3 db ./test.db |
| 540 | set ::DB [sqlite3_connection_pointer db] |
| 541 | if {[info exists ::SETUP_SQL]} { |
| 542 | db eval $::SETUP_SQL |
| 543 | } |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 544 | } |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 545 | reset_db |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 546 | |
| 547 | # Abort early if this script has been run before. |
| 548 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 549 | if {[info exists TC(count)]} return |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 550 | |
drh | ce2198c | 2010-09-10 13:22:59 +0000 | [diff] [blame] | 551 | # Make sure memory statistics are enabled. |
| 552 | # |
| 553 | sqlite3_config_memstatus 1 |
| 554 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 555 | # Initialize the test counters and set up commands to access them. |
| 556 | # Or, if this is a slave interpreter, set up aliases to write the |
| 557 | # counters in the parent interpreter. |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 558 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 559 | if {0==[info exists ::SLAVE]} { |
| 560 | set TC(errors) 0 |
| 561 | set TC(count) 0 |
| 562 | set TC(fail_list) [list] |
| 563 | set TC(omit_list) [list] |
drh | cca17c3 | 2013-04-19 12:32:52 +0000 | [diff] [blame] | 564 | set TC(warn_list) [list] |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 565 | |
| 566 | proc set_test_counter {counter args} { |
| 567 | if {[llength $args]} { |
| 568 | set ::TC($counter) [lindex $args 0] |
| 569 | } |
| 570 | set ::TC($counter) |
| 571 | } |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 572 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 573 | |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 574 | # Record the fact that a sequence of tests were omitted. |
| 575 | # |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 576 | proc omit_test {name reason {append 1}} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 577 | set omitList [set_test_counter omit_list] |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 578 | if {$append} { |
| 579 | lappend omitList [list $name $reason] |
| 580 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 581 | set_test_counter omit_list $omitList |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 582 | } |
| 583 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 584 | # Record the fact that a test failed. |
| 585 | # |
| 586 | proc fail_test {name} { |
| 587 | set f [set_test_counter fail_list] |
| 588 | lappend f $name |
| 589 | set_test_counter fail_list $f |
| 590 | set_test_counter errors [expr [set_test_counter errors] + 1] |
| 591 | |
| 592 | set nFail [set_test_counter errors] |
| 593 | if {$nFail>=$::cmdlinearg(maxerror)} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 594 | output2 "*** Giving up..." |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 595 | finalize_testing |
| 596 | } |
| 597 | } |
| 598 | |
drh | cca17c3 | 2013-04-19 12:32:52 +0000 | [diff] [blame] | 599 | # Remember a warning message to be displayed at the conclusion of all testing |
| 600 | # |
| 601 | proc warning {msg {append 1}} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 602 | output2 "Warning: $msg" |
drh | cca17c3 | 2013-04-19 12:32:52 +0000 | [diff] [blame] | 603 | set warnList [set_test_counter warn_list] |
| 604 | if {$append} { |
| 605 | lappend warnList $msg |
| 606 | } |
| 607 | set_test_counter warn_list $warnList |
| 608 | } |
| 609 | |
| 610 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 611 | # Increment the number of tests run |
| 612 | # |
| 613 | proc incr_ntest {} { |
| 614 | set_test_counter count [expr [set_test_counter count] + 1] |
| 615 | } |
| 616 | |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 617 | # Return true if --verbose=1 was specified on the command line. Otherwise, |
| 618 | # return false. |
| 619 | # |
| 620 | proc verbose {} { |
| 621 | return $::cmdlinearg(verbose) |
| 622 | } |
| 623 | |
| 624 | # Use the following commands instead of [puts] for test output within |
| 625 | # this file. Test scripts can still use regular [puts], which is directed |
| 626 | # to stdout and, if one is open, the --output file. |
| 627 | # |
| 628 | # output1: output that should be printed if --verbose=1 was specified. |
| 629 | # output2: output that should be printed unconditionally. |
| 630 | # output2_if_no_verbose: output that should be printed only if --verbose=0. |
| 631 | # |
| 632 | proc output1 {args} { |
| 633 | set v [verbose] |
| 634 | if {$v==1} { |
| 635 | uplevel output2 $args |
| 636 | } elseif {$v==2} { |
| 637 | uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end] |
| 638 | } |
| 639 | } |
| 640 | proc output2 {args} { |
| 641 | set nArg [llength $args] |
| 642 | uplevel puts $args |
| 643 | } |
| 644 | proc output2_if_no_verbose {args} { |
| 645 | set v [verbose] |
| 646 | if {$v==0} { |
| 647 | uplevel output2 $args |
| 648 | } elseif {$v==2} { |
| 649 | uplevel puts [lrange $args 0 end-1] stdout [lrange $args end end] |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | # Override the [puts] command so that if no channel is explicitly |
| 654 | # specified the string is written to both stdout and to the file |
| 655 | # specified by "--output=", if any. |
| 656 | # |
| 657 | proc puts_override {args} { |
| 658 | set nArg [llength $args] |
| 659 | if {$nArg==1 || ($nArg==2 && [string first [lindex $args 0] -nonewline]==0)} { |
| 660 | uplevel puts_original $args |
| 661 | if {[info exists ::G(output_fd)]} { |
| 662 | uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end] |
| 663 | } |
| 664 | } else { |
| 665 | # A channel was explicitly specified. |
| 666 | uplevel puts_original $args |
| 667 | } |
| 668 | } |
| 669 | rename puts puts_original |
| 670 | proc puts {args} { uplevel puts_override $args } |
| 671 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 672 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 673 | # Invoke the do_test procedure to run a single test |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 674 | # |
| 675 | proc do_test {name cmd expected} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 676 | global argv cmdlinearg |
| 677 | |
dan | 8c40800 | 2010-11-01 17:38:24 +0000 | [diff] [blame] | 678 | fix_testname name |
| 679 | |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 680 | sqlite3_memdebug_settitle $name |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 681 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 682 | # if {[llength $argv]==0} { |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 683 | # set go 1 |
| 684 | # } else { |
| 685 | # set go 0 |
| 686 | # foreach pattern $argv { |
| 687 | # if {[string match $pattern $name]} { |
| 688 | # set go 1 |
| 689 | # break |
| 690 | # } |
| 691 | # } |
| 692 | # } |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 693 | |
dan | d506de0 | 2010-07-03 13:59:01 +0000 | [diff] [blame] | 694 | if {[info exists ::G(perm:prefix)]} { |
| 695 | set name "$::G(perm:prefix)$name" |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 696 | } |
| 697 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 698 | incr_ntest |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 699 | output1 -nonewline $name... |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 700 | flush stdout |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 701 | |
| 702 | if {![info exists ::G(match)] || [string match $::G(match) $name]} { |
| 703 | if {[catch {uplevel #0 "$cmd;\n"} result]} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 704 | output2_if_no_verbose -nonewline $name... |
| 705 | output2 "\nError: $result" |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 706 | fail_test $name |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 707 | } else { |
drh | 3f17aef | 2012-04-27 01:08:02 +0000 | [diff] [blame] | 708 | if {[regexp {^~?/.*/$} $expected]} { |
drh | 70bdcc7 | 2013-05-30 19:28:34 +0000 | [diff] [blame] | 709 | # "expected" is of the form "/PATTERN/" then the result if correct if |
| 710 | # regular expression PATTERN matches the result. "~/PATTERN/" means |
| 711 | # the regular expression must not match. |
drh | 3f17aef | 2012-04-27 01:08:02 +0000 | [diff] [blame] | 712 | if {[string index $expected 0]=="~"} { |
drh | c2b23e7 | 2013-11-13 15:32:15 +0000 | [diff] [blame] | 713 | set re [string range $expected 2 end-1] |
| 714 | if {[string index $re 0]=="*"} { |
| 715 | # If the regular expression begins with * then treat it as a glob instead |
| 716 | set ok [string match $re $result] |
| 717 | } else { |
| 718 | set re [string map {# {[-0-9.]+}} $re] |
| 719 | set ok [regexp $re $result] |
| 720 | } |
| 721 | set ok [expr {!$ok}] |
drh | 3f17aef | 2012-04-27 01:08:02 +0000 | [diff] [blame] | 722 | } else { |
drh | c2b23e7 | 2013-11-13 15:32:15 +0000 | [diff] [blame] | 723 | set re [string range $expected 1 end-1] |
| 724 | if {[string index $re 0]=="*"} { |
| 725 | # If the regular expression begins with * then treat it as a glob instead |
| 726 | set ok [string match $re $result] |
| 727 | } else { |
| 728 | set re [string map {# {[-0-9.]+}} $re] |
| 729 | set ok [regexp $re $result] |
| 730 | } |
drh | 3f17aef | 2012-04-27 01:08:02 +0000 | [diff] [blame] | 731 | } |
drh | 70bdcc7 | 2013-05-30 19:28:34 +0000 | [diff] [blame] | 732 | } elseif {[regexp {^~?\*.*\*$} $expected]} { |
| 733 | # "expected" is of the form "*GLOB*" then the result if correct if |
| 734 | # glob pattern GLOB matches the result. "~/GLOB/" means |
| 735 | # the glob must not match. |
| 736 | if {[string index $expected 0]=="~"} { |
| 737 | set e [string range $expected 1 end] |
| 738 | set ok [expr {![string match $e $result]}] |
| 739 | } else { |
| 740 | set ok [string match $expected $result] |
| 741 | } |
drh | 3f17aef | 2012-04-27 01:08:02 +0000 | [diff] [blame] | 742 | } else { |
| 743 | set ok [expr {[string compare $result $expected]==0}] |
| 744 | } |
| 745 | if {!$ok} { |
mistachkin | c60941f | 2012-09-13 01:51:02 +0000 | [diff] [blame] | 746 | # if {![info exists ::testprefix] || $::testprefix eq ""} { |
| 747 | # error "no test prefix" |
| 748 | # } |
drh | d66b2e0 | 2015-11-12 21:42:40 +0000 | [diff] [blame] | 749 | output1 "" |
drh | 061d35c | 2015-11-13 00:03:14 +0000 | [diff] [blame] | 750 | output2 "! $name expected: \[$expected\]\n! $name got: \[$result\]" |
drh | 3f17aef | 2012-04-27 01:08:02 +0000 | [diff] [blame] | 751 | fail_test $name |
| 752 | } else { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 753 | output1 " Ok" |
drh | 3f17aef | 2012-04-27 01:08:02 +0000 | [diff] [blame] | 754 | } |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 755 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 756 | } else { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 757 | output1 " Omitted" |
mistachkin | 6c3c1a0 | 2011-11-12 03:17:40 +0000 | [diff] [blame] | 758 | omit_test $name "pattern mismatch" 0 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 759 | } |
drh | 6558db8 | 2007-04-13 03:23:21 +0000 | [diff] [blame] | 760 | flush stdout |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 761 | } |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 762 | |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 763 | proc dumpbytes {s} { |
| 764 | set r "" |
| 765 | for {set i 0} {$i < [string length $s]} {incr i} { |
| 766 | if {$i > 0} {append r " "} |
| 767 | append r [format %02X [scan [string index $s $i] %c]] |
| 768 | } |
| 769 | return $r |
| 770 | } |
| 771 | |
drh | 8df9185 | 2012-04-24 12:46:05 +0000 | [diff] [blame] | 772 | proc catchcmd {db {cmd ""}} { |
| 773 | global CLI |
| 774 | set out [open cmds.txt w] |
| 775 | puts $out $cmd |
| 776 | close $out |
| 777 | set line "exec $CLI $db < cmds.txt" |
| 778 | set rc [catch { eval $line } msg] |
| 779 | list $rc $msg |
| 780 | } |
| 781 | |
mistachkin | f21979d | 2015-01-18 05:35:01 +0000 | [diff] [blame] | 782 | proc catchcmdex {db {cmd ""}} { |
| 783 | global CLI |
| 784 | set out [open cmds.txt w] |
| 785 | fconfigure $out -encoding binary -translation binary |
| 786 | puts -nonewline $out $cmd |
| 787 | close $out |
| 788 | set line "exec -keepnewline -- $CLI $db < cmds.txt" |
| 789 | set chans [list stdin stdout stderr] |
| 790 | foreach chan $chans { |
| 791 | catch { |
| 792 | set modes($chan) [fconfigure $chan] |
| 793 | fconfigure $chan -encoding binary -translation binary -buffering none |
| 794 | } |
| 795 | } |
| 796 | set rc [catch { eval $line } msg] |
| 797 | foreach chan $chans { |
| 798 | catch { |
| 799 | eval fconfigure [list $chan] $modes($chan) |
| 800 | } |
| 801 | } |
| 802 | # puts [dumpbytes $msg] |
| 803 | list $rc $msg |
| 804 | } |
| 805 | |
shaneh | 5550517 | 2011-06-21 19:30:19 +0000 | [diff] [blame] | 806 | proc filepath_normalize {p} { |
| 807 | # test cases should be written to assume "unix"-like file paths |
shaneh | 285a18f | 2011-06-21 19:39:59 +0000 | [diff] [blame] | 808 | if {$::tcl_platform(platform)!="unix"} { |
shaneh | 5550517 | 2011-06-21 19:30:19 +0000 | [diff] [blame] | 809 | # lreverse*2 as a hack to remove any unneeded {} after the string map |
| 810 | lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]] |
shaneh | c489640 | 2011-06-21 19:38:16 +0000 | [diff] [blame] | 811 | } { |
| 812 | set p |
shaneh | 5550517 | 2011-06-21 19:30:19 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | proc do_filepath_test {name cmd expected} { |
| 816 | uplevel [list do_test $name [ |
| 817 | subst -nocommands { filepath_normalize [ $cmd ] } |
| 818 | ] [filepath_normalize $expected]] |
| 819 | } |
| 820 | |
dan | 33f5379 | 2011-05-05 19:44:22 +0000 | [diff] [blame] | 821 | proc realnum_normalize {r} { |
shaneh | 4f529e8 | 2011-06-20 17:41:41 +0000 | [diff] [blame] | 822 | # different TCL versions display floating point values differently. |
| 823 | 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] | 824 | } |
| 825 | proc do_realnum_test {name cmd expected} { |
| 826 | uplevel [list do_test $name [ |
| 827 | subst -nocommands { realnum_normalize [ $cmd ] } |
| 828 | ] [realnum_normalize $expected]] |
| 829 | } |
| 830 | |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 831 | proc fix_testname {varname} { |
| 832 | upvar $varname testname |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 833 | if {[info exists ::testprefix] |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 834 | && [string is digit [string range $testname 0 0]] |
| 835 | } { |
| 836 | set testname "${::testprefix}-$testname" |
| 837 | } |
| 838 | } |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 839 | |
dan | 57f7f4b | 2010-10-01 19:04:37 +0000 | [diff] [blame] | 840 | proc do_execsql_test {testname sql {result {}}} { |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 841 | fix_testname testname |
dan | 99ebad9 | 2011-06-13 09:11:01 +0000 | [diff] [blame] | 842 | uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]] |
dan | 153eda0 | 2010-06-21 07:45:47 +0000 | [diff] [blame] | 843 | } |
| 844 | proc do_catchsql_test {testname sql result} { |
dan | a3f5108 | 2010-09-30 18:43:14 +0000 | [diff] [blame] | 845 | fix_testname testname |
dan | 99ebad9 | 2011-06-13 09:11:01 +0000 | [diff] [blame] | 846 | uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result] |
dan | 153eda0 | 2010-06-21 07:45:47 +0000 | [diff] [blame] | 847 | } |
drh | 3c2aeae | 2014-01-24 14:37:44 +0000 | [diff] [blame] | 848 | proc do_timed_execsql_test {testname sql {result {}}} { |
| 849 | fix_testname testname |
| 850 | uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\ |
| 851 | [list [list {*}$result]] |
| 852 | } |
dan | 3985479 | 2010-11-15 16:12:58 +0000 | [diff] [blame] | 853 | proc do_eqp_test {name sql res} { |
| 854 | uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res] |
| 855 | } |
dan | 153eda0 | 2010-06-21 07:45:47 +0000 | [diff] [blame] | 856 | |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 857 | #------------------------------------------------------------------------- |
| 858 | # Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST |
| 859 | # |
| 860 | # Where switches are: |
| 861 | # |
| 862 | # -errorformat FMTSTRING |
| 863 | # -count |
dan | af1dcab | 2010-09-20 19:17:53 +0000 | [diff] [blame] | 864 | # -query SQL |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 865 | # -tclquery TCL |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 866 | # -repair TCL |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 867 | # |
| 868 | proc do_select_tests {prefix args} { |
| 869 | |
| 870 | set testlist [lindex $args end] |
| 871 | set switches [lrange $args 0 end-1] |
| 872 | |
| 873 | set errfmt "" |
| 874 | set countonly 0 |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 875 | set tclquery "" |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 876 | set repair "" |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 877 | |
| 878 | for {set i 0} {$i < [llength $switches]} {incr i} { |
| 879 | set s [lindex $switches $i] |
| 880 | set n [string length $s] |
dan | af1dcab | 2010-09-20 19:17:53 +0000 | [diff] [blame] | 881 | if {$n>=2 && [string equal -length $n $s "-query"]} { |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 882 | set tclquery [list execsql [lindex $switches [incr i]]] |
| 883 | } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} { |
| 884 | set tclquery [lindex $switches [incr i]] |
dan | af1dcab | 2010-09-20 19:17:53 +0000 | [diff] [blame] | 885 | } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} { |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 886 | set errfmt [lindex $switches [incr i]] |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 887 | } elseif {$n>=2 && [string equal -length $n $s "-repair"]} { |
| 888 | set repair [lindex $switches [incr i]] |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 889 | } elseif {$n>=2 && [string equal -length $n $s "-count"]} { |
| 890 | set countonly 1 |
| 891 | } else { |
| 892 | error "unknown switch: $s" |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | if {$countonly && $errfmt!=""} { |
| 897 | error "Cannot use -count and -errorformat together" |
| 898 | } |
| 899 | set nTestlist [llength $testlist] |
| 900 | if {$nTestlist%3 || $nTestlist==0 } { |
| 901 | error "SELECT test list contains [llength $testlist] elements" |
| 902 | } |
| 903 | |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 904 | eval $repair |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 905 | foreach {tn sql res} $testlist { |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 906 | if {$tclquery != ""} { |
dan | af1dcab | 2010-09-20 19:17:53 +0000 | [diff] [blame] | 907 | execsql $sql |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 908 | uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]] |
| 909 | } elseif {$countonly} { |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 910 | set nRow 0 |
| 911 | db eval $sql {incr nRow} |
| 912 | uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res] |
| 913 | } elseif {$errfmt==""} { |
| 914 | uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]] |
| 915 | } else { |
| 916 | set res [list 1 [string trim [format $errfmt {*}$res]]] |
| 917 | uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res] |
| 918 | } |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 919 | eval $repair |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 920 | } |
dan | af7626f | 2010-09-23 18:47:36 +0000 | [diff] [blame] | 921 | |
dan | 51f0698 | 2010-09-18 19:00:12 +0000 | [diff] [blame] | 922 | } |
| 923 | |
dan | b04757a | 2010-09-21 16:59:16 +0000 | [diff] [blame] | 924 | proc delete_all_data {} { |
| 925 | db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} { |
| 926 | db eval "DELETE FROM '[string map {' ''} $t]'" |
| 927 | } |
| 928 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 929 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 930 | # Run an SQL script. |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 931 | # Return the number of microseconds per statement. |
| 932 | # |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 933 | proc speed_trial {name numstmt units sql} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 934 | output2 -nonewline [format {%-21.21s } $name...] |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 935 | flush stdout |
| 936 | set speed [time {sqlite3_exec_nr db $sql}] |
| 937 | set tm [lindex $speed 0] |
danielk1977 | 0ee26d9 | 2008-02-08 18:25:29 +0000 | [diff] [blame] | 938 | if {$tm == 0} { |
| 939 | set rate [format %20s "many"] |
| 940 | } else { |
| 941 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 942 | } |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 943 | set u2 $units/s |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 944 | output2 [format {%12d uS %s %s} $tm $rate $u2] |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 945 | global total_time |
| 946 | set total_time [expr {$total_time+$tm}] |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 947 | lappend ::speed_trial_times $name $tm |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 948 | } |
drh | 45c236d | 2008-03-22 01:08:00 +0000 | [diff] [blame] | 949 | proc speed_trial_tcl {name numstmt units script} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 950 | output2 -nonewline [format {%-21.21s } $name...] |
drh | 45c236d | 2008-03-22 01:08:00 +0000 | [diff] [blame] | 951 | flush stdout |
| 952 | set speed [time {eval $script}] |
| 953 | set tm [lindex $speed 0] |
| 954 | if {$tm == 0} { |
| 955 | set rate [format %20s "many"] |
| 956 | } else { |
| 957 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 958 | } |
| 959 | set u2 $units/s |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 960 | output2 [format {%12d uS %s %s} $tm $rate $u2] |
drh | 45c236d | 2008-03-22 01:08:00 +0000 | [diff] [blame] | 961 | global total_time |
| 962 | set total_time [expr {$total_time+$tm}] |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 963 | lappend ::speed_trial_times $name $tm |
drh | 45c236d | 2008-03-22 01:08:00 +0000 | [diff] [blame] | 964 | } |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 965 | proc speed_trial_init {name} { |
| 966 | global total_time |
| 967 | set total_time 0 |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 968 | set ::speed_trial_times [list] |
drh | bb810a9 | 2010-07-03 12:00:53 +0000 | [diff] [blame] | 969 | sqlite3 versdb :memory: |
| 970 | set vers [versdb one {SELECT sqlite_source_id()}] |
| 971 | versdb close |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 972 | output2 "SQLite $vers" |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 973 | } |
| 974 | proc speed_trial_summary {name} { |
| 975 | global total_time |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 976 | output2 [format {%-21.21s %12d uS TOTAL} $name $total_time] |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 977 | |
| 978 | if { 0 } { |
| 979 | sqlite3 versdb :memory: |
| 980 | set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0] |
| 981 | versdb close |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 982 | output2 "CREATE TABLE IF NOT EXISTS time(version, script, test, us);" |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 983 | foreach {test us} $::speed_trial_times { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 984 | output2 "INSERT INTO time VALUES('$vers', '$name', '$test', $us);" |
dan | a975b59 | 2010-10-08 16:09:43 +0000 | [diff] [blame] | 985 | } |
| 986 | } |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 987 | } |
| 988 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 989 | # Run this routine last |
| 990 | # |
| 991 | proc finish_test {} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 992 | catch {db close} |
dan | e855983 | 2014-07-31 17:35:40 +0000 | [diff] [blame] | 993 | catch {db1 close} |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 994 | catch {db2 close} |
| 995 | catch {db3 close} |
| 996 | if {0==[info exists ::SLAVE]} { finalize_testing } |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 997 | } |
| 998 | proc finalize_testing {} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 999 | global sqlite_open_file_count |
| 1000 | |
| 1001 | set omitList [set_test_counter omit_list] |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1002 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1003 | catch {db close} |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1004 | catch {db2 close} |
| 1005 | catch {db3 close} |
| 1006 | |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 1007 | vfs_unlink_test |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1008 | sqlite3 db {} |
danielk1977 | cbb8496 | 2006-01-17 16:10:13 +0000 | [diff] [blame] | 1009 | # sqlite3_clear_tsd_memdebug |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 1010 | db close |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 1011 | sqlite3_reset_auto_extension |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1012 | |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 1013 | sqlite3_soft_heap_limit 0 |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1014 | set nTest [incr_ntest] |
| 1015 | set nErr [set_test_counter errors] |
| 1016 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 1017 | set nKnown 0 |
| 1018 | if {[file readable known-problems.txt]} { |
| 1019 | set fd [open known-problems.txt] |
| 1020 | set content [read $fd] |
| 1021 | close $fd |
| 1022 | foreach x $content {set known_error($x) 1} |
| 1023 | foreach x [set_test_counter fail_list] { |
| 1024 | if {[info exists known_error($x)]} {incr nKnown} |
| 1025 | } |
| 1026 | } |
| 1027 | if {$nKnown>0} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1028 | output2 "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 1029 | out of $nTest tests" |
| 1030 | } else { |
drh | 72bf6a3 | 2016-01-07 02:06:55 +0000 | [diff] [blame] | 1031 | set cpuinfo {} |
| 1032 | if {[catch {exec hostname} hname]==0} {set cpuinfo [string trim $hname]} |
| 1033 | append cpuinfo " $::tcl_platform(os)" |
| 1034 | append cpuinfo " [expr {$::tcl_platform(pointerSize)*8}]-bit" |
| 1035 | append cpuinfo " [string map {E -e} $::tcl_platform(byteOrder)]" |
| 1036 | output2 "SQLite [sqlite3 -sourceid]" |
| 1037 | output2 "$nErr errors out of $nTest tests on $cpuinfo" |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 1038 | } |
| 1039 | if {$nErr>$nKnown} { |
drh | 061d35c | 2015-11-13 00:03:14 +0000 | [diff] [blame] | 1040 | output2 -nonewline "!Failures on these tests:" |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 1041 | foreach x [set_test_counter fail_list] { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1042 | if {![info exists known_error($x)]} {output2 -nonewline " $x"} |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 1043 | } |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1044 | output2 "" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 1045 | } |
drh | cca17c3 | 2013-04-19 12:32:52 +0000 | [diff] [blame] | 1046 | foreach warning [set_test_counter warn_list] { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1047 | output2 "Warning: $warning" |
drh | cca17c3 | 2013-04-19 12:32:52 +0000 | [diff] [blame] | 1048 | } |
danielk1977 | ee8b799 | 2009-03-26 17:13:06 +0000 | [diff] [blame] | 1049 | run_thread_tests 1 |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 1050 | if {[llength $omitList]>0} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1051 | output2 "Omitted test cases:" |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 1052 | set prec {} |
| 1053 | foreach {rec} [lsort $omitList] { |
| 1054 | if {$rec==$prec} continue |
| 1055 | set prec $rec |
drh | d66b2e0 | 2015-11-12 21:42:40 +0000 | [diff] [blame] | 1056 | output2 [format {. %-12s %s} [lindex $rec 0] [lindex $rec 1]] |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 1057 | } |
| 1058 | } |
drh | 80788d8 | 2006-09-02 14:50:23 +0000 | [diff] [blame] | 1059 | if {$nErr>0 && ![working_64bit_int]} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1060 | output2 "******************************************************************" |
| 1061 | output2 "N.B.: The version of TCL that you used to build this test harness" |
| 1062 | output2 "is defective in that it does not support 64-bit integers. Some or" |
| 1063 | output2 "all of the test failures above might be a result from this defect" |
| 1064 | output2 "in your TCL build." |
| 1065 | output2 "******************************************************************" |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 1066 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1067 | if {$::cmdlinearg(binarylog)} { |
dan | fbefb89 | 2010-05-12 19:02:35 +0000 | [diff] [blame] | 1068 | vfslog finalize binarylog |
danielk1977 | 374177e | 2008-05-08 15:58:06 +0000 | [diff] [blame] | 1069 | } |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 1070 | if {$sqlite_open_file_count} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1071 | output2 "$sqlite_open_file_count files were left open" |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 1072 | incr nErr |
| 1073 | } |
drh | eafc43b | 2010-07-26 18:43:40 +0000 | [diff] [blame] | 1074 | if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 || |
| 1075 | [sqlite3_memory_used]>0} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1076 | output2 "Unfreed memory: [sqlite3_memory_used] bytes in\ |
drh | eafc43b | 2010-07-26 18:43:40 +0000 | [diff] [blame] | 1077 | [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 1078 | incr nErr |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 1079 | ifcapable memdebug||mem5||(mem3&&debug) { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1080 | output2 "Writing unfreed memory log to \"./memleak.txt\"" |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 1081 | sqlite3_memdebug_dump ./memleak.txt |
| 1082 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 1083 | } else { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1084 | output2 "All memory allocations freed - no leaks" |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 1085 | ifcapable memdebug||mem5 { |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 1086 | sqlite3_memdebug_dump ./memusage.txt |
| 1087 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 1088 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1089 | show_memstats |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1090 | output2 "Maximum memory usage: [sqlite3_memory_highwater 1] bytes" |
| 1091 | output2 "Current memory usage: [sqlite3_memory_highwater] bytes" |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1092 | if {[info commands sqlite3_memdebug_malloc_count] ne ""} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1093 | output2 "Number of malloc() : [sqlite3_memdebug_malloc_count] calls" |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 1094 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1095 | if {$::cmdlinearg(malloctrace)} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1096 | output2 "Writing mallocs.sql..." |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1097 | memdebug_log_sql |
| 1098 | sqlite3_memdebug_log stop |
| 1099 | sqlite3_memdebug_log clear |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 1100 | |
| 1101 | if {[sqlite3_memory_used]>0} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1102 | output2 "Writing leaks.sql..." |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 1103 | sqlite3_memdebug_log sync |
| 1104 | memdebug_log_sql leaks.sql |
| 1105 | } |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1106 | } |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame] | 1107 | foreach f [glob -nocomplain test.db-*-journal] { |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 1108 | forcedelete $f |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame] | 1109 | } |
| 1110 | foreach f [glob -nocomplain test.db-mj*] { |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 1111 | forcedelete $f |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame] | 1112 | } |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 1113 | exit [expr {$nErr>0}] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1116 | # Display memory statistics for analysis and debugging purposes. |
| 1117 | # |
| 1118 | proc show_memstats {} { |
| 1119 | set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 1120 | set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] |
| 1121 | set val [format {now %10d max %10d max-size %10d} \ |
| 1122 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1123 | output1 "Memory used: $val" |
drh | eafc43b | 2010-07-26 18:43:40 +0000 | [diff] [blame] | 1124 | set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] |
| 1125 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1126 | output1 "Allocation count: $val" |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1127 | set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 1128 | set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0] |
| 1129 | set val [format {now %10d max %10d max-size %10d} \ |
| 1130 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1131 | output1 "Page-cache used: $val" |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1132 | set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] |
| 1133 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1134 | output1 "Page-cache overflow: $val" |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1135 | set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] |
| 1136 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1137 | output1 "Scratch memory used: $val" |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1138 | set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 1139 | set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0] |
| 1140 | set val [format {now %10d max %10d max-size %10d} \ |
| 1141 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1142 | output1 "Scratch overflow: $val" |
drh | ec424a5 | 2008-07-25 15:39:03 +0000 | [diff] [blame] | 1143 | ifcapable yytrackmaxstackdepth { |
| 1144 | set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0] |
| 1145 | set val [format { max %10d} [lindex $x 2]] |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1146 | output2 "Parser stack depth: $val" |
drh | ec424a5 | 2008-07-25 15:39:03 +0000 | [diff] [blame] | 1147 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1150 | # A procedure to execute SQL |
| 1151 | # |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1152 | proc execsql {sql {db db}} { |
drh | acbcdc4 | 2001-01-22 00:31:53 +0000 | [diff] [blame] | 1153 | # puts "SQL = $sql" |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 1154 | uplevel [list $db eval $sql] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1155 | } |
drh | 3c2aeae | 2014-01-24 14:37:44 +0000 | [diff] [blame] | 1156 | proc execsql_timed {sql {db db}} { |
| 1157 | set tm [time { |
| 1158 | set x [uplevel [list $db eval $sql]] |
| 1159 | } 1] |
| 1160 | set tm [lindex $tm 0] |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1161 | output1 -nonewline " ([expr {$tm*0.001}]ms) " |
drh | 3c2aeae | 2014-01-24 14:37:44 +0000 | [diff] [blame] | 1162 | set x |
| 1163 | } |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 1164 | |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1165 | # Execute SQL and catch exceptions. |
| 1166 | # |
| 1167 | proc catchsql {sql {db db}} { |
| 1168 | # puts "SQL = $sql" |
dan | 81fa6dc | 2009-11-28 12:40:32 +0000 | [diff] [blame] | 1169 | set r [catch [list uplevel [list $db eval $sql]] msg] |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 1170 | lappend r $msg |
| 1171 | return $r |
| 1172 | } |
| 1173 | |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 1174 | # Do an VDBE code dump on the SQL given |
| 1175 | # |
| 1176 | proc explain {sql {db db}} { |
mistachkin | eeb31ff | 2015-06-10 23:02:38 +0000 | [diff] [blame] | 1177 | output2 "" |
| 1178 | output2 "addr opcode p1 p2 p3 p4 p5 #" |
| 1179 | output2 "---- ------------ ------ ------ ------ --------------- -- -" |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 1180 | $db eval "explain $sql" {} { |
mistachkin | eeb31ff | 2015-06-10 23:02:38 +0000 | [diff] [blame] | 1181 | output2 [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \ |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 1182 | $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment |
| 1183 | ] |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1187 | proc explain_i {sql {db db}} { |
mistachkin | eeb31ff | 2015-06-10 23:02:38 +0000 | [diff] [blame] | 1188 | output2 "" |
| 1189 | output2 "addr opcode p1 p2 p3 p4 p5 #" |
| 1190 | output2 "---- ------------ ------ ------ ------ ---------------- -- -" |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1191 | |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1192 | |
dan | 5c663c3 | 2013-11-06 14:52:40 +0000 | [diff] [blame] | 1193 | # Set up colors for the different opcodes. Scheme is as follows: |
| 1194 | # |
| 1195 | # Red: Opcodes that write to a b-tree. |
| 1196 | # Blue: Opcodes that reposition or seek a cursor. |
| 1197 | # Green: The ResultRow opcode. |
| 1198 | # |
dan | dea63f2 | 2014-03-03 16:48:47 +0000 | [diff] [blame] | 1199 | if { [catch {fconfigure stdout -mode}]==0 } { |
| 1200 | set R "\033\[31;1m" ;# Red fg |
| 1201 | set G "\033\[32;1m" ;# Green fg |
| 1202 | set B "\033\[34;1m" ;# Red fg |
| 1203 | set D "\033\[39;0m" ;# Default fg |
| 1204 | } else { |
| 1205 | set R "" |
| 1206 | set G "" |
| 1207 | set B "" |
| 1208 | set D "" |
| 1209 | } |
dan | 5c663c3 | 2013-11-06 14:52:40 +0000 | [diff] [blame] | 1210 | foreach opcode { |
| 1211 | Seek SeekGe SeekGt SeekLe SeekLt NotFound Last Rewind |
dan | e3ab729 | 2013-11-12 12:30:09 +0000 | [diff] [blame] | 1212 | NoConflict Next Prev VNext VPrev VFilter |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 1213 | SorterSort SorterNext |
dan | 5c663c3 | 2013-11-06 14:52:40 +0000 | [diff] [blame] | 1214 | } { |
| 1215 | set color($opcode) $B |
| 1216 | } |
| 1217 | foreach opcode {ResultRow} { |
| 1218 | set color($opcode) $G |
| 1219 | } |
| 1220 | foreach opcode {IdxInsert Insert Delete IdxDelete} { |
| 1221 | set color($opcode) $R |
| 1222 | } |
| 1223 | |
dan | 427ebba | 2013-11-05 16:39:31 +0000 | [diff] [blame] | 1224 | set bSeenGoto 0 |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1225 | $db eval "explain $sql" {} { |
| 1226 | set x($addr) 0 |
| 1227 | set op($addr) $opcode |
| 1228 | |
dan | 427ebba | 2013-11-05 16:39:31 +0000 | [diff] [blame] | 1229 | if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} { |
| 1230 | set linebreak($p2) 1 |
| 1231 | set bSeenGoto 1 |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
dan | e3ab729 | 2013-11-12 12:30:09 +0000 | [diff] [blame] | 1234 | if {$opcode=="Next" || $opcode=="Prev" |
| 1235 | || $opcode=="VNext" || $opcode=="VPrev" |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 1236 | || $opcode=="SorterNext" |
dan | e3ab729 | 2013-11-12 12:30:09 +0000 | [diff] [blame] | 1237 | } { |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1238 | for {set i $p2} {$i<$addr} {incr i} { |
| 1239 | incr x($i) 2 |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} { |
| 1244 | for {set i [expr $p2+1]} {$i<$addr} {incr i} { |
| 1245 | incr x($i) 2 |
| 1246 | } |
| 1247 | } |
dan | 427ebba | 2013-11-05 16:39:31 +0000 | [diff] [blame] | 1248 | |
| 1249 | if {$opcode == "Halt" && $comment == "End of coroutine"} { |
| 1250 | set linebreak([expr $addr+1]) 1 |
| 1251 | } |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | $db eval "explain $sql" {} { |
dan | 427ebba | 2013-11-05 16:39:31 +0000 | [diff] [blame] | 1255 | if {[info exists linebreak($addr)]} { |
mistachkin | eeb31ff | 2015-06-10 23:02:38 +0000 | [diff] [blame] | 1256 | output2 "" |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1257 | } |
| 1258 | set I [string repeat " " $x($addr)] |
dan | 5c663c3 | 2013-11-06 14:52:40 +0000 | [diff] [blame] | 1259 | |
| 1260 | set col "" |
| 1261 | catch { set col $color($opcode) } |
| 1262 | |
mistachkin | eeb31ff | 2015-06-10 23:02:38 +0000 | [diff] [blame] | 1263 | output2 [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \ |
dan | 5c663c3 | 2013-11-06 14:52:40 +0000 | [diff] [blame] | 1264 | $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1265 | ] |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1266 | } |
mistachkin | eeb31ff | 2015-06-10 23:02:38 +0000 | [diff] [blame] | 1267 | output2 "---- ------------ ------ ------ ------ ---------------- -- -" |
dan | e83267d | 2013-11-05 14:19:22 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1270 | # Show the VDBE program for an SQL statement but omit the Trace |
| 1271 | # opcode at the beginning. This procedure can be used to prove |
| 1272 | # that different SQL statements generate exactly the same VDBE code. |
| 1273 | # |
| 1274 | proc explain_no_trace {sql} { |
| 1275 | set tr [db eval "EXPLAIN $sql"] |
| 1276 | return [lrange $tr 7 end] |
| 1277 | } |
| 1278 | |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 1279 | # Another procedure to execute SQL. This one includes the field |
| 1280 | # names in the returned list. |
| 1281 | # |
| 1282 | proc execsql2 {sql} { |
| 1283 | set result {} |
| 1284 | db eval $sql data { |
| 1285 | foreach f $data(*) { |
| 1286 | lappend result $f $data($f) |
| 1287 | } |
| 1288 | } |
| 1289 | return $result |
| 1290 | } |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 1291 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1292 | # Use a temporary in-memory database to execute SQL statements |
| 1293 | # |
| 1294 | proc memdbsql {sql} { |
| 1295 | sqlite3 memdb :memory: |
| 1296 | set result [memdb eval $sql] |
| 1297 | memdb close |
| 1298 | return $result |
| 1299 | } |
| 1300 | |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 1301 | # Use the non-callback API to execute multiple SQL statements |
| 1302 | # |
| 1303 | proc stepsql {dbptr sql} { |
| 1304 | set sql [string trim $sql] |
| 1305 | set r 0 |
| 1306 | while {[string length $sql]>0} { |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1307 | if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 1308 | return [list 1 $vm] |
| 1309 | } |
| 1310 | set sql [string trim $sqltail] |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1311 | # while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} { |
| 1312 | # foreach v $VAL {lappend r $v} |
| 1313 | # } |
| 1314 | while {[sqlite3_step $vm]=="SQLITE_ROW"} { |
| 1315 | for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} { |
| 1316 | lappend r [sqlite3_column_text $vm $i] |
| 1317 | } |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 1318 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1319 | if {[catch {sqlite3_finalize $vm} errmsg]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 1320 | return [list 1 $errmsg] |
| 1321 | } |
| 1322 | } |
| 1323 | return $r |
| 1324 | } |
| 1325 | |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 1326 | # Do an integrity check of the entire database |
| 1327 | # |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 1328 | proc integrity_check {name {db db}} { |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 1329 | ifcapable integrityck { |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 1330 | do_test $name [list execsql {PRAGMA integrity_check} $db] {ok} |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | |
drh | 433dccf | 2013-02-09 15:37:11 +0000 | [diff] [blame] | 1334 | # Check the extended error code |
| 1335 | # |
| 1336 | proc verify_ex_errcode {name expected {db db}} { |
| 1337 | do_test $name [list sqlite3_extended_errcode $db] $expected |
| 1338 | } |
| 1339 | |
dan | c6055c7 | 2011-04-28 18:46:46 +0000 | [diff] [blame] | 1340 | |
| 1341 | # Return true if the SQL statement passed as the second argument uses a |
| 1342 | # statement transaction. |
| 1343 | # |
| 1344 | proc sql_uses_stmt {db sql} { |
| 1345 | set stmt [sqlite3_prepare $db $sql -1 dummy] |
| 1346 | set uses [uses_stmt_journal $stmt] |
| 1347 | sqlite3_finalize $stmt |
| 1348 | return $uses |
| 1349 | } |
| 1350 | |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 1351 | proc fix_ifcapable_expr {expr} { |
| 1352 | set ret "" |
| 1353 | set state 0 |
| 1354 | for {set i 0} {$i < [string length $expr]} {incr i} { |
| 1355 | set char [string range $expr $i $i] |
| 1356 | set newstate [expr {[string is alnum $char] || $char eq "_"}] |
| 1357 | if {$newstate && !$state} { |
| 1358 | append ret {$::sqlite_options(} |
| 1359 | } |
| 1360 | if {!$newstate && $state} { |
| 1361 | append ret ) |
| 1362 | } |
| 1363 | append ret $char |
| 1364 | set state $newstate |
| 1365 | } |
| 1366 | if {$state} {append ret )} |
| 1367 | return $ret |
| 1368 | } |
| 1369 | |
mistachkin | c60941f | 2012-09-13 01:51:02 +0000 | [diff] [blame] | 1370 | # Returns non-zero if the capabilities are present; zero otherwise. |
| 1371 | # |
| 1372 | proc capable {expr} { |
| 1373 | set e [fix_ifcapable_expr $expr]; return [expr ($e)] |
| 1374 | } |
| 1375 | |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 1376 | # Evaluate a boolean expression of capabilities. If true, execute the |
| 1377 | # code. Omit the code if false. |
| 1378 | # |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 1379 | proc ifcapable {expr code {else ""} {elsecode ""}} { |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 1380 | #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2 |
| 1381 | set e2 [fix_ifcapable_expr $expr] |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 1382 | if ($e2) { |
| 1383 | set c [catch {uplevel 1 $code} r] |
| 1384 | } else { |
| 1385 | set c [catch {uplevel 1 $elsecode} r] |
| 1386 | } |
| 1387 | return -code $c $r |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 1388 | } |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 1389 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1390 | # This proc execs a seperate process that crashes midway through executing |
| 1391 | # the SQL script $sql on database test.db. |
| 1392 | # |
| 1393 | # The crash occurs during a sync() of file $crashfile. When the crash |
| 1394 | # occurs a random subset of all unsynced writes made by the process are |
| 1395 | # written into the files on disk. Argument $crashdelay indicates the |
| 1396 | # number of file syncs to wait before crashing. |
| 1397 | # |
| 1398 | # The return value is a list of two elements. The first element is a |
| 1399 | # boolean, indicating whether or not the process actually crashed or |
| 1400 | # reported some other error. The second element in the returned list is the |
| 1401 | # error message. This is "child process exited abnormally" if the crash |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 1402 | # occurred. |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1403 | # |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 1404 | # crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1405 | # |
| 1406 | proc crashsql {args} { |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1407 | |
| 1408 | set blocksize "" |
| 1409 | set crashdelay 1 |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 1410 | set prngseed 0 |
dan | 999cd08 | 2013-12-09 20:42:03 +0000 | [diff] [blame] | 1411 | set opendb { sqlite3 db test.db -vfs crash } |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 1412 | set tclbody {} |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1413 | set crashfile "" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 1414 | set dc "" |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1415 | set sql [lindex $args end] |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1416 | |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1417 | for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} { |
| 1418 | set z [lindex $args $ii] |
| 1419 | set n [string length $z] |
| 1420 | set z2 [lindex $args [expr $ii+1]] |
| 1421 | |
| 1422 | if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \ |
dan | 999cd08 | 2013-12-09 20:42:03 +0000 | [diff] [blame] | 1423 | elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \ |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 1424 | elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1425 | elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \ |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 1426 | elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 1427 | elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \ |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 1428 | elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 1429 | else { error "Unrecognized option: $z" } |
| 1430 | } |
| 1431 | |
| 1432 | if {$crashfile eq ""} { |
| 1433 | error "Compulsory option -file missing" |
| 1434 | } |
| 1435 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1436 | # $crashfile gets compared to the native filename in |
shaneh | f2c0882 | 2010-07-08 16:30:44 +0000 | [diff] [blame] | 1437 | # cfSync(), which can be different then what TCL uses by |
| 1438 | # default, so here we force it to the "nativename" format. |
mistachkin | f8a7846 | 2012-03-08 20:00:36 +0000 | [diff] [blame] | 1439 | set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]] |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1440 | |
| 1441 | set f [open crash.tcl w] |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 1442 | puts $f "sqlite3_crash_enable 1" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 1443 | puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile" |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 1444 | puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte" |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 1445 | |
| 1446 | # This block sets the cache size of the main database to 10 |
| 1447 | # pages. This is done in case the build is configured to omit |
| 1448 | # "PRAGMA cache_size". |
dan | ec16d98 | 2015-04-17 08:36:05 +0000 | [diff] [blame] | 1449 | if {$opendb!=""} { |
| 1450 | puts $f $opendb |
| 1451 | puts $f {db eval {SELECT * FROM sqlite_master;}} |
| 1452 | puts $f {set bt [btree_from_db db]} |
| 1453 | puts $f {btree_set_cache_size $bt 10} |
| 1454 | } |
dan | 999cd08 | 2013-12-09 20:42:03 +0000 | [diff] [blame] | 1455 | |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 1456 | if {$prngseed} { |
| 1457 | set seed [expr {$prngseed%10007+1}] |
| 1458 | # puts seed=$seed |
| 1459 | puts $f "db eval {SELECT randomblob($seed)}" |
| 1460 | } |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 1461 | |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 1462 | if {[string length $tclbody]>0} { |
| 1463 | puts $f $tclbody |
| 1464 | } |
| 1465 | if {[string length $sql]>0} { |
| 1466 | puts $f "db eval {" |
| 1467 | puts $f "$sql" |
| 1468 | puts $f "}" |
| 1469 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1470 | close $f |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1471 | set r [catch { |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1472 | exec [info nameofexec] crash.tcl >@stdout |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1473 | } msg] |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1474 | |
shaneh | f2c0882 | 2010-07-08 16:30:44 +0000 | [diff] [blame] | 1475 | # Windows/ActiveState TCL returns a slightly different |
| 1476 | # error message. We map that to the expected message |
| 1477 | # so that we don't have to change all of the test |
| 1478 | # cases. |
| 1479 | if {$::tcl_platform(platform)=="windows"} { |
| 1480 | if {$msg=="child killed: unknown signal"} { |
| 1481 | set msg "child process exited abnormally" |
| 1482 | } |
| 1483 | } |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1484 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 1485 | lappend r $msg |
| 1486 | } |
| 1487 | |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1488 | proc run_ioerr_prep {} { |
| 1489 | set ::sqlite_io_error_pending 0 |
| 1490 | catch {db close} |
| 1491 | catch {db2 close} |
| 1492 | catch {forcedelete test.db} |
| 1493 | catch {forcedelete test.db-journal} |
| 1494 | catch {forcedelete test2.db} |
| 1495 | catch {forcedelete test2.db-journal} |
| 1496 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
| 1497 | sqlite3_extended_result_codes $::DB $::ioerropts(-erc) |
| 1498 | if {[info exists ::ioerropts(-tclprep)]} { |
| 1499 | eval $::ioerropts(-tclprep) |
| 1500 | } |
| 1501 | if {[info exists ::ioerropts(-sqlprep)]} { |
| 1502 | execsql $::ioerropts(-sqlprep) |
| 1503 | } |
| 1504 | expr 0 |
| 1505 | } |
| 1506 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1507 | # Usage: do_ioerr_test <test number> <options...> |
| 1508 | # |
| 1509 | # This proc is used to implement test cases that check that IO errors |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1510 | # are correctly handled. The first argument, <test number>, is an integer |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1511 | # used to name the tests executed by this proc. Options are as follows: |
| 1512 | # |
| 1513 | # -tclprep TCL script to run to prepare test. |
| 1514 | # -sqlprep SQL script to run to prepare test. |
| 1515 | # -tclbody TCL script to run with IO error simulation. |
| 1516 | # -sqlbody TCL script to run with IO error simulation. |
| 1517 | # -exclude List of 'N' values not to test. |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1518 | # -erc Use extended result codes |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 1519 | # -persist Make simulated I/O errors persistent |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1520 | # -start Value of 'N' to begin with (default 1) |
| 1521 | # |
| 1522 | # -cksum Boolean. If true, test that the database does |
| 1523 | # not change during the execution of the test case. |
| 1524 | # |
| 1525 | proc do_ioerr_test {testname args} { |
| 1526 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1527 | set ::ioerropts(-start) 1 |
| 1528 | set ::ioerropts(-cksum) 0 |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 1529 | set ::ioerropts(-erc) 0 |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 1530 | set ::ioerropts(-count) 100000000 |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 1531 | set ::ioerropts(-persist) 1 |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 1532 | set ::ioerropts(-ckrefcount) 0 |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1533 | set ::ioerropts(-restoreprng) 1 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1534 | array set ::ioerropts $args |
| 1535 | |
danielk1977 | 47cd39c | 2008-05-12 12:41:15 +0000 | [diff] [blame] | 1536 | # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are |
| 1537 | # a couple of obscure IO errors that do not return them. |
| 1538 | set ::ioerropts(-erc) 0 |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1539 | |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1540 | # Create a single TCL script from the TCL and SQL specified |
| 1541 | # as the body of the test. |
| 1542 | set ::ioerrorbody {} |
| 1543 | if {[info exists ::ioerropts(-tclbody)]} { |
| 1544 | append ::ioerrorbody "$::ioerropts(-tclbody)\n" |
| 1545 | } |
| 1546 | if {[info exists ::ioerropts(-sqlbody)]} { |
| 1547 | append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}" |
| 1548 | } |
| 1549 | |
| 1550 | save_prng_state |
| 1551 | if {$::ioerropts(-cksum)} { |
| 1552 | run_ioerr_prep |
| 1553 | eval $::ioerrorbody |
| 1554 | set ::goodcksum [cksum] |
| 1555 | } |
danielk1977 | 47cd39c | 2008-05-12 12:41:15 +0000 | [diff] [blame] | 1556 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1557 | set ::go 1 |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1558 | #reset_prng_state |
danielk1977 | ef165ce | 2009-04-06 17:50:03 +0000 | [diff] [blame] | 1559 | for {set n $::ioerropts(-start)} {$::go} {incr n} { |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 1560 | set ::TN $n |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 1561 | incr ::ioerropts(-count) -1 |
| 1562 | if {$::ioerropts(-count)<0} break |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1563 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1564 | # Skip this IO error if it was specified with the "-exclude" option. |
| 1565 | if {[info exists ::ioerropts(-exclude)]} { |
| 1566 | if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue |
| 1567 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1568 | if {$::ioerropts(-restoreprng)} { |
| 1569 | restore_prng_state |
| 1570 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1571 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1572 | # Delete the files test.db and test2.db, then execute the TCL and |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1573 | # SQL (in that order) to prepare for the test case. |
| 1574 | do_test $testname.$n.1 { |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1575 | run_ioerr_prep |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1576 | } {0} |
| 1577 | |
| 1578 | # Read the 'checksum' of the database. |
| 1579 | if {$::ioerropts(-cksum)} { |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1580 | set ::checksum [cksum] |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1581 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1582 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1583 | # Set the Nth IO error to fail. |
| 1584 | do_test $testname.$n.2 [subst { |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 1585 | set ::sqlite_io_error_persist $::ioerropts(-persist) |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1586 | set ::sqlite_io_error_pending $n |
| 1587 | }] $n |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1588 | |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1589 | # Execute the TCL script created for the body of this test. If |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1590 | # at least N IO operations performed by SQLite as a result of |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1591 | # the script, the Nth will fail. |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1592 | do_test $testname.$n.3 { |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1593 | set ::sqlite_io_error_hit 0 |
| 1594 | set ::sqlite_io_error_hardhit 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1595 | set r [catch $::ioerrorbody msg] |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1596 | set ::errseen $r |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 1597 | set rc [sqlite3_errcode $::DB] |
| 1598 | if {$::ioerropts(-erc)} { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 1599 | # If we are in extended result code mode, make sure all of the |
| 1600 | # IOERRs we get back really do have their extended code values. |
| 1601 | # If an extended result code is returned, the sqlite3_errcode |
| 1602 | # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn |
| 1603 | # where nnnn is a number |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 1604 | if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} { |
| 1605 | return $rc |
| 1606 | } |
| 1607 | } else { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 1608 | # If we are not in extended result code mode, make sure no |
| 1609 | # extended error codes are returned. |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 1610 | if {[regexp {\+\d} $rc]} { |
| 1611 | return $rc |
| 1612 | } |
| 1613 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1614 | # The test repeats as long as $::go is non-zero. $::go starts out |
| 1615 | # as 1. When a test runs to completion without hitting an I/O |
| 1616 | # error, that means there is no point in continuing with this test |
| 1617 | # case so set $::go to zero. |
| 1618 | # |
| 1619 | if {$::sqlite_io_error_pending>0} { |
| 1620 | set ::go 0 |
| 1621 | set q 0 |
| 1622 | set ::sqlite_io_error_pending 0 |
| 1623 | } else { |
| 1624 | set q 1 |
| 1625 | } |
| 1626 | |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 1627 | set s [expr $::sqlite_io_error_hit==0] |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1628 | if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} { |
| 1629 | set r 1 |
| 1630 | } |
danielk1977 | f2fa831 | 2006-01-24 13:09:33 +0000 | [diff] [blame] | 1631 | set ::sqlite_io_error_hit 0 |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 1632 | |
| 1633 | # One of two things must have happened. either |
| 1634 | # 1. We never hit the IO error and the SQL returned OK |
| 1635 | # 2. An IO error was hit and the SQL failed |
| 1636 | # |
dan | d41a29a | 2010-05-06 15:56:28 +0000 | [diff] [blame] | 1637 | #puts "s=$s r=$r q=$q" |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1638 | expr { ($s && !$r && !$q) || (!$s && $r && $q) } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1639 | } {1} |
| 1640 | |
danielk1977 | 80daec6 | 2008-05-12 10:57:02 +0000 | [diff] [blame] | 1641 | set ::sqlite_io_error_hit 0 |
| 1642 | set ::sqlite_io_error_pending 0 |
| 1643 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1644 | # Check that no page references were leaked. There should be |
| 1645 | # a single reference if there is still an active transaction, |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 1646 | # or zero otherwise. |
| 1647 | # |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 1648 | # UPDATE: If the IO error occurs after a 'BEGIN' but before any |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1649 | # locks are established on database files (i.e. if the error |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 1650 | # occurs while attempting to detect a hot-journal file), then |
| 1651 | # there may 0 page references and an active transaction according |
| 1652 | # to [sqlite3_get_autocommit]. |
| 1653 | # |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 1654 | if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} { |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 1655 | do_test $testname.$n.4 { |
| 1656 | set bt [btree_from_db db] |
| 1657 | db_enter db |
| 1658 | array set stats [btree_pager_stats $bt] |
| 1659 | db_leave db |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 1660 | set nRef $stats(ref) |
| 1661 | expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)} |
| 1662 | } {1} |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1665 | # If there is an open database handle and no open transaction, |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 1666 | # and the pager is not running in exclusive-locking mode, |
| 1667 | # check that the pager is in "unlocked" state. Theoretically, |
| 1668 | # if a call to xUnlock() failed due to an IO error the underlying |
| 1669 | # file may still be locked. |
| 1670 | # |
| 1671 | ifcapable pragma { |
| 1672 | if { [info commands db] ne "" |
danielk1977 | 0259fbe | 2008-05-05 17:14:53 +0000 | [diff] [blame] | 1673 | && $::ioerropts(-ckrefcount) |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 1674 | && [db one {pragma locking_mode}] eq "normal" |
| 1675 | && [sqlite3_get_autocommit db] |
| 1676 | } { |
| 1677 | do_test $testname.$n.5 { |
| 1678 | set bt [btree_from_db db] |
| 1679 | db_enter db |
| 1680 | array set stats [btree_pager_stats $bt] |
| 1681 | db_leave db |
| 1682 | set stats(state) |
| 1683 | } 0 |
| 1684 | } |
| 1685 | } |
| 1686 | |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 1687 | # If an IO error occurred, then the checksum of the database should |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1688 | # 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] | 1689 | # |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1690 | if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} { |
danielk1977 | 0259fbe | 2008-05-05 17:14:53 +0000 | [diff] [blame] | 1691 | do_test $testname.$n.6 { |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1692 | catch {db close} |
danielk1977 | a961339 | 2008-07-08 12:07:32 +0000 | [diff] [blame] | 1693 | catch {db2 close} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 1694 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1695 | set nowcksum [cksum] |
| 1696 | set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}] |
| 1697 | if {$res==0} { |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1698 | output2 "now=$nowcksum" |
| 1699 | output2 "the=$::checksum" |
| 1700 | output2 "fwd=$::goodcksum" |
dan | b88e24f | 2013-02-23 18:58:11 +0000 | [diff] [blame] | 1701 | } |
| 1702 | set res |
| 1703 | } 1 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1706 | set ::sqlite_io_error_hardhit 0 |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 1707 | set ::sqlite_io_error_pending 0 |
danielk1977 | 105afed | 2005-05-26 15:20:53 +0000 | [diff] [blame] | 1708 | if {[info exists ::ioerropts(-cleanup)]} { |
| 1709 | catch $::ioerropts(-cleanup) |
| 1710 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1711 | } |
| 1712 | set ::sqlite_io_error_pending 0 |
drh | 6d54da0 | 2007-03-25 19:08:46 +0000 | [diff] [blame] | 1713 | set ::sqlite_io_error_persist 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1714 | unset ::ioerropts |
| 1715 | } |
| 1716 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1717 | # Return a checksum based on the contents of the main database associated |
| 1718 | # with connection $db |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 1719 | # |
| 1720 | proc cksum {{db db}} { |
| 1721 | set txt [$db eval { |
| 1722 | SELECT name, type, sql FROM sqlite_master order by name |
| 1723 | }]\n |
| 1724 | foreach tbl [$db eval { |
| 1725 | SELECT name FROM sqlite_master WHERE type='table' order by name |
| 1726 | }] { |
| 1727 | append txt [$db eval "SELECT * FROM $tbl"]\n |
| 1728 | } |
| 1729 | foreach prag {default_synchronous default_cache_size} { |
| 1730 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 1731 | } |
| 1732 | set cksum [string length $txt]-[md5 $txt] |
| 1733 | # puts $cksum-[file size test.db] |
| 1734 | return $cksum |
| 1735 | } |
| 1736 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1737 | # Generate a checksum based on the contents of the main and temp tables |
| 1738 | # database $db. If the checksum of two databases is the same, and the |
| 1739 | # integrity-check passes for both, the two databases are identical. |
| 1740 | # |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 1741 | proc allcksum {{db db}} { |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1742 | set ret [list] |
| 1743 | ifcapable tempdb { |
| 1744 | set sql { |
| 1745 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 1746 | SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION |
| 1747 | SELECT 'sqlite_master' UNION |
| 1748 | SELECT 'sqlite_temp_master' ORDER BY 1 |
| 1749 | } |
| 1750 | } else { |
| 1751 | set sql { |
| 1752 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 1753 | SELECT 'sqlite_master' ORDER BY 1 |
| 1754 | } |
| 1755 | } |
| 1756 | set tbllist [$db eval $sql] |
| 1757 | set txt {} |
| 1758 | foreach tbl $tbllist { |
| 1759 | append txt [$db eval "SELECT * FROM $tbl"] |
| 1760 | } |
| 1761 | foreach prag {default_cache_size} { |
| 1762 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 1763 | } |
| 1764 | # puts txt=$txt |
| 1765 | return [md5 $txt] |
| 1766 | } |
| 1767 | |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 1768 | # Generate a checksum based on the contents of a single database with |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1769 | # a database connection. The name of the database is $dbname. |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 1770 | # Examples of $dbname are "temp" or "main". |
| 1771 | # |
| 1772 | proc dbcksum {db dbname} { |
| 1773 | if {$dbname=="temp"} { |
| 1774 | set master sqlite_temp_master |
| 1775 | } else { |
| 1776 | set master $dbname.sqlite_master |
| 1777 | } |
| 1778 | set alltab [$db eval "SELECT name FROM $master WHERE type='table'"] |
| 1779 | set txt [$db eval "SELECT * FROM $master"]\n |
| 1780 | foreach tab $alltab { |
| 1781 | append txt [$db eval "SELECT * FROM $dbname.$tab"]\n |
| 1782 | } |
| 1783 | return [md5 $txt] |
| 1784 | } |
| 1785 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1786 | proc memdebug_log_sql {{filename mallocs.sql}} { |
| 1787 | |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1788 | set data [sqlite3_memdebug_log dump] |
| 1789 | set nFrame [expr [llength [lindex $data 0]]-2] |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1790 | if {$nFrame < 0} { return "" } |
| 1791 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1792 | set database temp |
| 1793 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1794 | set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1795 | |
| 1796 | set sql "" |
| 1797 | foreach e $data { |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1798 | set nCall [lindex $e 0] |
| 1799 | set nByte [lindex $e 1] |
| 1800 | set lStack [lrange $e 2 end] |
| 1801 | append sql "INSERT INTO ${database}.malloc VALUES" |
| 1802 | append sql "('test', $nCall, $nByte, '$lStack');\n" |
| 1803 | foreach f $lStack { |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1804 | set frames($f) 1 |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n" |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1809 | set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1810 | |
| 1811 | foreach f [array names frames] { |
| 1812 | set addr [format %x $f] |
| 1813 | set cmd "addr2line -e [info nameofexec] $addr" |
| 1814 | set line [eval exec $cmd] |
| 1815 | append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n" |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1816 | |
| 1817 | set file [lindex [split $line :] 0] |
| 1818 | set files($file) 1 |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1821 | foreach f [array names files] { |
| 1822 | set contents "" |
| 1823 | catch { |
| 1824 | set fd [open $f] |
| 1825 | set contents [read $fd] |
| 1826 | close $fd |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1827 | } |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1828 | set contents [string map {' ''} $contents] |
| 1829 | append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1830 | } |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1831 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 1832 | set fd [open $filename w] |
| 1833 | puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;" |
| 1834 | close $fd |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 1835 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 1836 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1837 | # Drop all tables in database [db] |
| 1838 | proc drop_all_tables {{db db}} { |
shaneh | 4e7b32f | 2009-12-17 22:12:51 +0000 | [diff] [blame] | 1839 | ifcapable trigger&&foreignkey { |
| 1840 | set pk [$db one "PRAGMA foreign_keys"] |
| 1841 | $db eval "PRAGMA foreign_keys = OFF" |
| 1842 | } |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 1843 | foreach {idx name file} [db eval {PRAGMA database_list}] { |
| 1844 | if {$idx==1} { |
| 1845 | set master sqlite_temp_master |
| 1846 | } else { |
| 1847 | set master $name.sqlite_master |
| 1848 | } |
| 1849 | foreach {t type} [$db eval " |
| 1850 | SELECT name, type FROM $master |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 1851 | WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X' |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 1852 | "] { |
dan | a16d106 | 2010-09-28 17:37:28 +0000 | [diff] [blame] | 1853 | $db eval "DROP $type \"$t\"" |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 1854 | } |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1855 | } |
shaneh | 4e7b32f | 2009-12-17 22:12:51 +0000 | [diff] [blame] | 1856 | ifcapable trigger&&foreignkey { |
| 1857 | $db eval "PRAGMA foreign_keys = $pk" |
| 1858 | } |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1861 | #------------------------------------------------------------------------- |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1862 | # If a test script is executed with global variable $::G(perm:name) set to |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1863 | # "wal", then the tests are run in WAL mode. Otherwise, they should be run |
| 1864 | # in rollback mode. The following Tcl procs are used to make this less |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1865 | # intrusive: |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1866 | # |
| 1867 | # wal_set_journal_mode ?DB? |
| 1868 | # |
| 1869 | # If running a WAL test, execute "PRAGMA journal_mode = wal" using |
| 1870 | # connection handle DB. Otherwise, this command is a no-op. |
| 1871 | # |
| 1872 | # wal_check_journal_mode TESTNAME ?DB? |
| 1873 | # |
| 1874 | # If running a WAL test, execute a tests case that fails if the main |
| 1875 | # database for connection handle DB is not currently a WAL database. |
| 1876 | # Otherwise (if not running a WAL permutation) this is a no-op. |
| 1877 | # |
| 1878 | # wal_is_wal_mode |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1879 | # |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1880 | # Returns true if this test should be run in WAL mode. False otherwise. |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1881 | # |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1882 | proc wal_is_wal_mode {} { |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1883 | expr {[permutation] eq "wal"} |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1884 | } |
| 1885 | proc wal_set_journal_mode {{db db}} { |
| 1886 | if { [wal_is_wal_mode] } { |
| 1887 | $db eval "PRAGMA journal_mode = WAL" |
| 1888 | } |
| 1889 | } |
| 1890 | proc wal_check_journal_mode {testname {db db}} { |
| 1891 | if { [wal_is_wal_mode] } { |
| 1892 | $db eval { SELECT * FROM sqlite_master } |
| 1893 | do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal} |
| 1894 | } |
| 1895 | } |
| 1896 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1897 | proc permutation {} { |
| 1898 | set perm "" |
| 1899 | catch {set perm $::G(perm:name)} |
| 1900 | set perm |
| 1901 | } |
dan | cb79e51 | 2010-08-06 13:50:07 +0000 | [diff] [blame] | 1902 | proc presql {} { |
| 1903 | set presql "" |
| 1904 | catch {set presql $::G(perm:presql)} |
| 1905 | set presql |
| 1906 | } |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1907 | |
dan | be7721d | 2016-02-04 17:31:03 +0000 | [diff] [blame] | 1908 | proc isquick {} { |
| 1909 | set ret 0 |
| 1910 | catch {set ret $::G(isquick)} |
| 1911 | set ret |
| 1912 | } |
| 1913 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1914 | #------------------------------------------------------------------------- |
| 1915 | # |
| 1916 | proc slave_test_script {script} { |
| 1917 | |
| 1918 | # Create the interpreter used to run the test script. |
| 1919 | interp create tinterp |
| 1920 | |
| 1921 | # Populate some global variables that tester.tcl expects to see. |
| 1922 | foreach {var value} [list \ |
| 1923 | ::argv0 $::argv0 \ |
| 1924 | ::argv {} \ |
| 1925 | ::SLAVE 1 \ |
| 1926 | ] { |
| 1927 | interp eval tinterp [list set $var $value] |
| 1928 | } |
| 1929 | |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 1930 | # If output is being copied into a file, share the file-descriptor with |
| 1931 | # the interpreter. |
| 1932 | if {[info exists ::G(output_fd)]} { |
| 1933 | interp share {} $::G(output_fd) tinterp |
| 1934 | } |
| 1935 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1936 | # The alias used to access the global test counters. |
| 1937 | tinterp alias set_test_counter set_test_counter |
| 1938 | |
| 1939 | # Set up the ::cmdlinearg array in the slave. |
| 1940 | interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]] |
| 1941 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1942 | # Set up the ::G array in the slave. |
| 1943 | interp eval tinterp [list array set ::G [array get ::G]] |
| 1944 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1945 | # Load the various test interfaces implemented in C. |
| 1946 | load_testfixture_extensions tinterp |
| 1947 | |
| 1948 | # Run the test script. |
| 1949 | interp eval tinterp $script |
| 1950 | |
dan | ea5542d | 2010-07-06 11:26:15 +0000 | [diff] [blame] | 1951 | # Check if the interpreter call [run_thread_tests] |
| 1952 | if { [interp eval tinterp {info exists ::run_thread_tests_called}] } { |
| 1953 | set ::run_thread_tests_called 1 |
| 1954 | } |
| 1955 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 1956 | # Delete the interpreter used to run the test script. |
| 1957 | interp delete tinterp |
| 1958 | } |
| 1959 | |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1960 | proc slave_test_file {zFile} { |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1961 | set tail [file tail $zFile] |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1962 | |
dan | 6a64d67 | 2011-04-04 15:38:16 +0000 | [diff] [blame] | 1963 | if {[info exists ::G(start:permutation)]} { |
| 1964 | if {[permutation] != $::G(start:permutation)} return |
| 1965 | unset ::G(start:permutation) |
| 1966 | } |
| 1967 | if {[info exists ::G(start:file)]} { |
| 1968 | if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return |
| 1969 | unset ::G(start:file) |
| 1970 | } |
| 1971 | |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1972 | # Remember the value of the shared-cache setting. So that it is possible |
| 1973 | # to check afterwards that it was not modified by the test script. |
| 1974 | # |
| 1975 | ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] } |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1976 | |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1977 | # Run the test script in a slave interpreter. |
| 1978 | # |
dan | ea5542d | 2010-07-06 11:26:15 +0000 | [diff] [blame] | 1979 | unset -nocomplain ::run_thread_tests_called |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1980 | reset_prng_state |
| 1981 | set ::sqlite_open_file_count 0 |
| 1982 | set time [time { slave_test_script [list source $zFile] }] |
| 1983 | set ms [expr [lindex $time 0] / 1000] |
| 1984 | |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1985 | # Test that all files opened by the test script were closed. Omit this |
| 1986 | # if the test script has "thread" in its name. The open file counter |
| 1987 | # is not thread-safe. |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1988 | # |
dan | ea5542d | 2010-07-06 11:26:15 +0000 | [diff] [blame] | 1989 | if {[info exists ::run_thread_tests_called]==0} { |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 1990 | do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0} |
| 1991 | } |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 1992 | set ::sqlite_open_file_count 0 |
| 1993 | |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1994 | # Test that the global "shared-cache" setting was not altered by |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1995 | # the test script. |
| 1996 | # |
mistachkin | 1d406e0 | 2013-08-29 01:09:14 +0000 | [diff] [blame] | 1997 | ifcapable shared_cache { |
dan | 0626dfc | 2010-06-15 06:56:37 +0000 | [diff] [blame] | 1998 | set res [expr {[sqlite3_enable_shared_cache] == $scs}] |
| 1999 | do_test ${tail}-sharedcachesetting [list set {} $res] 1 |
| 2000 | } |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 2001 | |
dan | 92d516a | 2010-07-05 14:54:48 +0000 | [diff] [blame] | 2002 | # Add some info to the output. |
| 2003 | # |
dan | 17c0823 | 2015-06-09 15:58:28 +0000 | [diff] [blame] | 2004 | output2 "Time: $tail $ms ms" |
dan | 430e74c | 2010-06-07 17:47:26 +0000 | [diff] [blame] | 2005 | show_memstats |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
dan | 59257dc | 2010-08-04 11:34:31 +0000 | [diff] [blame] | 2008 | # Open a new connection on database test.db and execute the SQL script |
| 2009 | # supplied as an argument. Before returning, close the new conection and |
| 2010 | # restore the 4 byte fields starting at header offsets 28, 92 and 96 |
| 2011 | # to the values they held before the SQL was executed. This simulates |
| 2012 | # a write by a pre-3.7.0 client. |
| 2013 | # |
| 2014 | proc sql36231 {sql} { |
| 2015 | set B [hexio_read test.db 92 8] |
| 2016 | set A [hexio_read test.db 28 4] |
| 2017 | sqlite3 db36231 test.db |
| 2018 | catch { db36231 func a_string a_string } |
| 2019 | execsql $sql db36231 |
| 2020 | db36231 close |
| 2021 | hexio_write test.db 28 $A |
| 2022 | hexio_write test.db 92 $B |
| 2023 | return "" |
| 2024 | } |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 2025 | |
dan | ccc7ff4 | 2010-11-29 12:06:45 +0000 | [diff] [blame] | 2026 | proc db_save {} { |
| 2027 | foreach f [glob -nocomplain sv_test.db*] { forcedelete $f } |
| 2028 | foreach f [glob -nocomplain test.db*] { |
| 2029 | set f2 "sv_$f" |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 2030 | forcecopy $f $f2 |
dan | ccc7ff4 | 2010-11-29 12:06:45 +0000 | [diff] [blame] | 2031 | } |
| 2032 | } |
| 2033 | proc db_save_and_close {} { |
| 2034 | db_save |
| 2035 | catch { db close } |
| 2036 | return "" |
| 2037 | } |
| 2038 | proc db_restore {} { |
| 2039 | foreach f [glob -nocomplain test.db*] { forcedelete $f } |
| 2040 | foreach f2 [glob -nocomplain sv_test.db*] { |
| 2041 | set f [string range $f2 3 end] |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 2042 | forcecopy $f2 $f |
dan | ccc7ff4 | 2010-11-29 12:06:45 +0000 | [diff] [blame] | 2043 | } |
| 2044 | } |
| 2045 | proc db_restore_and_reopen {{dbfile test.db}} { |
| 2046 | catch { db close } |
| 2047 | db_restore |
| 2048 | sqlite3 db $dbfile |
| 2049 | } |
| 2050 | proc db_delete_and_reopen {{file test.db}} { |
| 2051 | catch { db close } |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 2052 | foreach f [glob -nocomplain test.db*] { forcedelete $f } |
dan | ccc7ff4 | 2010-11-29 12:06:45 +0000 | [diff] [blame] | 2053 | sqlite3 db $file |
| 2054 | } |
| 2055 | |
dan | 03bc525 | 2015-07-24 14:17:17 +0000 | [diff] [blame] | 2056 | # Close any connections named [db], [db2] or [db3]. Then use sqlite3_config |
| 2057 | # to configure the size of the PAGECACHE allocation using the parameters |
| 2058 | # provided to this command. Save the old PAGECACHE parameters in a global |
| 2059 | # variable so that [test_restore_config_pagecache] can restore the previous |
| 2060 | # configuration. |
| 2061 | # |
| 2062 | # Before returning, reopen connection [db] on file test.db. |
| 2063 | # |
| 2064 | proc test_set_config_pagecache {sz nPg} { |
| 2065 | catch {db close} |
| 2066 | catch {db2 close} |
| 2067 | catch {db3 close} |
| 2068 | |
| 2069 | sqlite3_shutdown |
| 2070 | set ::old_pagecache_config [sqlite3_config_pagecache $sz $nPg] |
| 2071 | sqlite3_initialize |
| 2072 | autoinstall_test_functions |
dan | d716c39 | 2015-07-24 18:22:29 +0000 | [diff] [blame] | 2073 | reset_db |
dan | 03bc525 | 2015-07-24 14:17:17 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | # Close any connections named [db], [db2] or [db3]. Then use sqlite3_config |
| 2077 | # to configure the size of the PAGECACHE allocation to the size saved in |
| 2078 | # the global variable by an earlier call to [test_set_config_pagecache]. |
| 2079 | # |
| 2080 | # Before returning, reopen connection [db] on file test.db. |
| 2081 | # |
| 2082 | proc test_restore_config_pagecache {} { |
| 2083 | catch {db close} |
| 2084 | catch {db2 close} |
| 2085 | catch {db3 close} |
| 2086 | |
| 2087 | sqlite3_shutdown |
| 2088 | eval sqlite3_config_pagecache $::old_pagecache_config |
| 2089 | unset ::old_pagecache_config |
| 2090 | sqlite3_initialize |
| 2091 | autoinstall_test_functions |
| 2092 | sqlite3 db test.db |
| 2093 | } |
| 2094 | |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 2095 | # If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set |
| 2096 | # to non-zero, then set the global variable $AUTOVACUUM to 1. |
| 2097 | set AUTOVACUUM $sqlite_options(default_autovacuum) |
danielk1977 | ee8b799 | 2009-03-26 17:13:06 +0000 | [diff] [blame] | 2098 | |
dan | 64b95bb | 2012-05-12 05:30:29 +0000 | [diff] [blame] | 2099 | # Make sure the FTS enhanced query syntax is disabled. |
| 2100 | set sqlite_fts3_enable_parentheses 0 |
| 2101 | |
drh | 09fe614 | 2013-11-29 15:06:27 +0000 | [diff] [blame] | 2102 | # During testing, assume that all database files are well-formed. The |
| 2103 | # few test cases that deliberately corrupt database files should rescind |
| 2104 | # this setting by invoking "database_can_be_corrupt" |
| 2105 | # |
| 2106 | database_never_corrupt |
| 2107 | |
danielk1977 | ee8b799 | 2009-03-26 17:13:06 +0000 | [diff] [blame] | 2108 | source $testdir/thread_common.tcl |
dan | ddf80eb | 2010-10-25 12:47:43 +0000 | [diff] [blame] | 2109 | source $testdir/malloc_common.tcl |