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