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