drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6 | # May you do good and not evil. |
| 7 | # May you find forgiveness for yourself and forgive others. |
| 8 | # May you share freely, never taking more than you give. |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements some common TCL routines used for regression |
| 12 | # testing the SQLite library |
| 13 | # |
drh | bb77b75 | 2009-04-09 01:23:49 +0000 | [diff] [blame] | 14 | # $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 15 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 16 | #------------------------------------------------------------------------- |
| 17 | # The commands provided by the code in this file to help with creating |
| 18 | # test cases are as follows: |
drh | 8359d8c | 2008-03-29 11:00:54 +0000 | [diff] [blame] | 19 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 20 | # Commands to manipulate the db and the file-system at a high level: |
drh | 8359d8c | 2008-03-29 11:00:54 +0000 | [diff] [blame] | 21 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 22 | # copy_file FROM TO |
| 23 | # drop_all_table ?DB? |
| 24 | # forcedelete FILENAME |
| 25 | # |
| 26 | # Test the capability of the SQLite version built into the interpreter to |
| 27 | # determine if a specific test can be run: |
| 28 | # |
| 29 | # ifcapable EXPR |
| 30 | # |
| 31 | # Calulate checksums based on database contents: |
| 32 | # |
| 33 | # dbcksum DB DBNAME |
| 34 | # allcksum ?DB? |
| 35 | # cksum ?DB? |
| 36 | # |
| 37 | # Commands to execute/explain SQL statements: |
| 38 | # |
| 39 | # stepsql DB SQL |
| 40 | # execsql2 SQL |
| 41 | # explain_no_trace SQL |
| 42 | # explain SQL ?DB? |
| 43 | # catchsql SQL ?DB? |
| 44 | # execsql SQL ?DB? |
| 45 | # |
| 46 | # Commands to run test cases: |
| 47 | # |
| 48 | # do_ioerr_test TESTNAME ARGS... |
| 49 | # crashsql ARGS... |
| 50 | # integrity_check TESTNAME ?DB? |
| 51 | # do_test TESTNAME SCRIPT EXPECTED |
| 52 | # |
| 53 | # Commands providing a lower level interface to the test counters: |
| 54 | # |
| 55 | # set_test_counter COUNTER ?VALUE? |
| 56 | # omit_test TESTNAME REASON |
| 57 | # fail_test TESTNAME |
| 58 | # incr_ntest |
| 59 | # |
| 60 | # Command run at the end of each test file: |
| 61 | # |
| 62 | # finish_test |
| 63 | # |
| 64 | # Commands to help create test files that run with the "WAL" permutation: |
| 65 | # |
| 66 | # wal_is_wal_mode |
| 67 | # wal_set_journal_mode ?DB? |
| 68 | # wal_check_journal_mode TESTNAME?DB? |
| 69 | # |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 70 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 71 | # Set the precision of FP arithmatic used by the interpreter. And |
| 72 | # configure SQLite to take database file locks on the page that begins |
| 73 | # 64KB into the database file instead of the one 1GB in. This means |
| 74 | # the code that handles that special case can be tested without creating |
| 75 | # very large database files. |
| 76 | # |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 77 | set tcl_precision 15 |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 78 | sqlite3_test_control_pending_byte 0x0010000 |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 79 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 80 | |
| 81 | # If the pager codec is available, create a wrapper for the [sqlite3] |
| 82 | # command that appends "-key {xyzzy}" to the command line. i.e. this: |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 83 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 84 | # sqlite3 db test.db |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 85 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 86 | # becomes |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 87 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 88 | # sqlite3 db test.db -key {xyzzy} |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 89 | # |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 90 | if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} { |
| 91 | rename sqlite3 sqlite_orig |
| 92 | proc sqlite3 {args} { |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 93 | if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} { |
| 94 | lappend args -key {xyzzy} |
| 95 | } |
| 96 | uplevel 1 sqlite_orig $args |
| 97 | } |
| 98 | } |
| 99 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 100 | # The following block only runs the first time this file is sourced. |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 101 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 102 | if {[info exists cmdlinearg]==0} { |
| 103 | |
| 104 | # Parse any options specified in the $argv array. This script accepts the |
| 105 | # following options: |
| 106 | # |
| 107 | # --pause |
| 108 | # --soft-heap-limit=NN |
| 109 | # --maxerror=NN |
| 110 | # --malloctrace=N |
| 111 | # --backtrace=N |
| 112 | # --binarylog=N |
| 113 | # |
| 114 | set cmdlinearg(soft-heap-limit) 0 |
| 115 | set cmdlinearg(maxerror) 1000 |
| 116 | set cmdlinearg(malloctrace) 0 |
| 117 | set cmdlinearg(backtrace) 10 |
| 118 | set cmdlinearg(binarylog) 0 |
| 119 | |
| 120 | set leftover [list] |
| 121 | foreach a $argv { |
| 122 | switch -regexp -- $a { |
| 123 | {^-+pause$} { |
| 124 | # Wait for user input before continuing. This is to give the user an |
| 125 | # opportunity to connect profiling tools to the process. |
| 126 | puts -nonewline "Press RETURN to begin..." |
| 127 | flush stdout |
| 128 | gets stdin |
| 129 | } |
| 130 | {^-+soft-heap-limit=.+$} { |
| 131 | foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break |
| 132 | } |
| 133 | {^-+maxerror=.+$} { |
| 134 | foreach {dummy cmdlinearg(maxerror)} [split $a =] break |
| 135 | } |
| 136 | {^-+malloctrace=.+$} { |
| 137 | foreach {dummy cmdlinearg(malloctrace)} [split $a =] break |
| 138 | if {$cmdlinearg(malloctrace)} { |
| 139 | sqlite3_memdebug_log start |
| 140 | } |
| 141 | } |
| 142 | {^-+backtrace=.+$} { |
| 143 | foreach {dummy cmdlinearg(backtrace)} [split $a =] break |
| 144 | } |
| 145 | sqlite3_memdebug_backtrace $value |
| 146 | {^-+binarylog=.+$} { |
| 147 | foreach {dummy cmdlinearg(binarylog)} [split $a =] break |
| 148 | } |
| 149 | default { |
| 150 | lappend leftover $a |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | set argv $leftover |
| 155 | |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 156 | sqlite3_shutdown |
| 157 | install_malloc_faultsim 1 |
| 158 | sqlite3_initialize |
drh | bb77b75 | 2009-04-09 01:23:49 +0000 | [diff] [blame] | 159 | autoinstall_test_functions |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 160 | |
| 161 | if {$cmdlinearg(binarylog)} { |
dan | fbefb89 | 2010-05-12 19:02:35 +0000 | [diff] [blame] | 162 | vfslog new binarylog {} vfslog.bin |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 163 | } |
| 164 | |
| 165 | # Set the backtrace depth, if malloc tracing is enabled. |
| 166 | # |
| 167 | if {$cmdlinearg(malloctrace)} { |
| 168 | sqlite3_memdebug_backtrace $cmdlinearg(backtrace) |
danielk1977 | 185eac9 | 2008-07-12 15:55:54 +0000 | [diff] [blame] | 169 | } |
danielk1977 | f7b9d66 | 2008-06-23 18:49:43 +0000 | [diff] [blame] | 170 | } |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 171 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 172 | # Update the soft-heap-limit each time this script is run. In that |
| 173 | # way if an individual test file changes the soft-heap-limit, it |
| 174 | # will be reset at the start of the next test file. |
| 175 | # |
| 176 | sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit) |
| 177 | |
| 178 | # Create a test database |
| 179 | # |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 180 | proc reset_db {} { |
| 181 | catch {db close} |
| 182 | file delete -force test.db |
| 183 | file delete -force test.db-journal |
dan | d3f8f94 | 2010-04-13 11:35:01 +0000 | [diff] [blame] | 184 | file delete -force test.db-wal |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 185 | sqlite3 db ./test.db |
| 186 | set ::DB [sqlite3_connection_pointer db] |
| 187 | if {[info exists ::SETUP_SQL]} { |
| 188 | db eval $::SETUP_SQL |
| 189 | } |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 190 | } |
danielk1977 | 03ba3fa | 2009-01-09 10:49:14 +0000 | [diff] [blame] | 191 | reset_db |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 192 | |
| 193 | # Abort early if this script has been run before. |
| 194 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 195 | if {[info exists TC(count)]} return |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 196 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 197 | # Initialize the test counters and set up commands to access them. |
| 198 | # Or, if this is a slave interpreter, set up aliases to write the |
| 199 | # counters in the parent interpreter. |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 200 | # |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 201 | if {0==[info exists ::SLAVE]} { |
| 202 | set TC(errors) 0 |
| 203 | set TC(count) 0 |
| 204 | set TC(fail_list) [list] |
| 205 | set TC(omit_list) [list] |
| 206 | |
| 207 | proc set_test_counter {counter args} { |
| 208 | if {[llength $args]} { |
| 209 | set ::TC($counter) [lindex $args 0] |
| 210 | } |
| 211 | set ::TC($counter) |
| 212 | } |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 213 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 214 | |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 215 | # Record the fact that a sequence of tests were omitted. |
| 216 | # |
| 217 | proc omit_test {name reason} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 218 | set omitList [set_test_counter omit_list] |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 219 | lappend omitList [list $name $reason] |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 220 | set_test_counter omit_list $omitList |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 221 | } |
| 222 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 223 | # Record the fact that a test failed. |
| 224 | # |
| 225 | proc fail_test {name} { |
| 226 | set f [set_test_counter fail_list] |
| 227 | lappend f $name |
| 228 | set_test_counter fail_list $f |
| 229 | set_test_counter errors [expr [set_test_counter errors] + 1] |
| 230 | |
| 231 | set nFail [set_test_counter errors] |
| 232 | if {$nFail>=$::cmdlinearg(maxerror)} { |
| 233 | puts "*** Giving up..." |
| 234 | finalize_testing |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | # Increment the number of tests run |
| 239 | # |
| 240 | proc incr_ntest {} { |
| 241 | set_test_counter count [expr [set_test_counter count] + 1] |
| 242 | } |
| 243 | |
| 244 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 245 | # Invoke the do_test procedure to run a single test |
| 246 | # |
| 247 | proc do_test {name cmd expected} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 248 | |
| 249 | global argv cmdlinearg |
| 250 | |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 251 | sqlite3_memdebug_settitle $name |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 252 | |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 253 | if {[llength $argv]==0} { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 254 | set go 1 |
| 255 | } else { |
| 256 | set go 0 |
| 257 | foreach pattern $argv { |
| 258 | if {[string match $pattern $name]} { |
| 259 | set go 1 |
| 260 | break |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | if {!$go} return |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 265 | incr_ntest |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 266 | puts -nonewline $name... |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 267 | flush stdout |
| 268 | if {[catch {uplevel #0 "$cmd;\n"} result]} { |
| 269 | puts "\nError: $result" |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 270 | fail_test $name |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 271 | } elseif {[string compare $result $expected]} { |
| 272 | puts "\nExpected: \[$expected\]\n Got: \[$result\]" |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 273 | fail_test $name |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 274 | } else { |
| 275 | puts " Ok" |
| 276 | } |
drh | 6558db8 | 2007-04-13 03:23:21 +0000 | [diff] [blame] | 277 | flush stdout |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 278 | } |
| 279 | |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 280 | # Run an SQL script. |
| 281 | # Return the number of microseconds per statement. |
| 282 | # |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 283 | proc speed_trial {name numstmt units sql} { |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 284 | puts -nonewline [format {%-21.21s } $name...] |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 285 | flush stdout |
| 286 | set speed [time {sqlite3_exec_nr db $sql}] |
| 287 | set tm [lindex $speed 0] |
danielk1977 | 0ee26d9 | 2008-02-08 18:25:29 +0000 | [diff] [blame] | 288 | if {$tm == 0} { |
| 289 | set rate [format %20s "many"] |
| 290 | } else { |
| 291 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 292 | } |
drh | 3590f15 | 2006-11-23 21:09:10 +0000 | [diff] [blame] | 293 | set u2 $units/s |
danielk1977 | 0ee26d9 | 2008-02-08 18:25:29 +0000 | [diff] [blame] | 294 | puts [format {%12d uS %s %s} $tm $rate $u2] |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 295 | global total_time |
| 296 | set total_time [expr {$total_time+$tm}] |
| 297 | } |
drh | 45c236d | 2008-03-22 01:08:00 +0000 | [diff] [blame] | 298 | proc speed_trial_tcl {name numstmt units script} { |
| 299 | puts -nonewline [format {%-21.21s } $name...] |
| 300 | flush stdout |
| 301 | set speed [time {eval $script}] |
| 302 | set tm [lindex $speed 0] |
| 303 | if {$tm == 0} { |
| 304 | set rate [format %20s "many"] |
| 305 | } else { |
| 306 | set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]] |
| 307 | } |
| 308 | set u2 $units/s |
| 309 | puts [format {%12d uS %s %s} $tm $rate $u2] |
| 310 | global total_time |
| 311 | set total_time [expr {$total_time+$tm}] |
| 312 | } |
drh | dd92431 | 2007-03-31 22:34:16 +0000 | [diff] [blame] | 313 | proc speed_trial_init {name} { |
| 314 | global total_time |
| 315 | set total_time 0 |
| 316 | } |
| 317 | proc speed_trial_summary {name} { |
| 318 | global total_time |
| 319 | puts [format {%-21.21s %12d uS TOTAL} $name $total_time] |
drh | b62c335 | 2006-11-23 09:39:16 +0000 | [diff] [blame] | 320 | } |
| 321 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 322 | # Run this routine last |
| 323 | # |
| 324 | proc finish_test {} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 325 | catch {db close} |
| 326 | catch {db2 close} |
| 327 | catch {db3 close} |
| 328 | if {0==[info exists ::SLAVE]} { finalize_testing } |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 329 | } |
| 330 | proc finalize_testing {} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 331 | global sqlite_open_file_count |
| 332 | |
| 333 | set omitList [set_test_counter omit_list] |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 334 | |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 335 | catch {db close} |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 336 | catch {db2 close} |
| 337 | catch {db3 close} |
| 338 | |
drh | 9bc5449 | 2007-10-23 14:49:59 +0000 | [diff] [blame] | 339 | vfs_unlink_test |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 340 | sqlite3 db {} |
danielk1977 | cbb8496 | 2006-01-17 16:10:13 +0000 | [diff] [blame] | 341 | # sqlite3_clear_tsd_memdebug |
drh | b4bc705 | 2006-01-11 23:40:33 +0000 | [diff] [blame] | 342 | db close |
drh | 984bfaa | 2008-03-19 16:08:53 +0000 | [diff] [blame] | 343 | sqlite3_reset_auto_extension |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 344 | |
drh | 3a7fb7c | 2007-08-10 16:41:08 +0000 | [diff] [blame] | 345 | sqlite3_soft_heap_limit 0 |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 346 | set nTest [incr_ntest] |
| 347 | set nErr [set_test_counter errors] |
| 348 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 349 | puts "$nErr errors out of $nTest tests" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 350 | if {$nErr>0} { |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 351 | puts "Failures on these tests: [set_test_counter fail_list]" |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 352 | } |
danielk1977 | ee8b799 | 2009-03-26 17:13:06 +0000 | [diff] [blame] | 353 | run_thread_tests 1 |
drh | 521cc84 | 2008-04-15 02:36:33 +0000 | [diff] [blame] | 354 | if {[llength $omitList]>0} { |
| 355 | puts "Omitted test cases:" |
| 356 | set prec {} |
| 357 | foreach {rec} [lsort $omitList] { |
| 358 | if {$rec==$prec} continue |
| 359 | set prec $rec |
| 360 | puts [format { %-12s %s} [lindex $rec 0] [lindex $rec 1]] |
| 361 | } |
| 362 | } |
drh | 80788d8 | 2006-09-02 14:50:23 +0000 | [diff] [blame] | 363 | if {$nErr>0 && ![working_64bit_int]} { |
| 364 | puts "******************************************************************" |
| 365 | puts "N.B.: The version of TCL that you used to build this test harness" |
| 366 | puts "is defective in that it does not support 64-bit integers. Some or" |
| 367 | puts "all of the test failures above might be a result from this defect" |
| 368 | puts "in your TCL build." |
| 369 | puts "******************************************************************" |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 370 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 371 | if {$::cmdlinearg(binarylog)} { |
dan | fbefb89 | 2010-05-12 19:02:35 +0000 | [diff] [blame] | 372 | vfslog finalize binarylog |
danielk1977 | 374177e | 2008-05-08 15:58:06 +0000 | [diff] [blame] | 373 | } |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 374 | if {$sqlite_open_file_count} { |
| 375 | puts "$sqlite_open_file_count files were left open" |
| 376 | incr nErr |
| 377 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 378 | if {[sqlite3_memory_used]>0} { |
| 379 | puts "Unfreed memory: [sqlite3_memory_used] bytes" |
| 380 | incr nErr |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 381 | ifcapable memdebug||mem5||(mem3&&debug) { |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 382 | puts "Writing unfreed memory log to \"./memleak.txt\"" |
| 383 | sqlite3_memdebug_dump ./memleak.txt |
| 384 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 385 | } else { |
| 386 | puts "All memory allocations freed - no leaks" |
drh | 2d7636e | 2008-02-16 16:21:45 +0000 | [diff] [blame] | 387 | ifcapable memdebug||mem5 { |
drh | d2bb327 | 2007-10-15 19:34:32 +0000 | [diff] [blame] | 388 | sqlite3_memdebug_dump ./memusage.txt |
| 389 | } |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 390 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 391 | show_memstats |
drh | 91fd4d4 | 2008-01-19 20:11:25 +0000 | [diff] [blame] | 392 | puts "Maximum memory usage: [sqlite3_memory_highwater 1] bytes" |
| 393 | puts "Current memory usage: [sqlite3_memory_highwater] bytes" |
danielk1977 | a7a8e14 | 2008-02-13 18:25:27 +0000 | [diff] [blame] | 394 | if {[info commands sqlite3_memdebug_malloc_count] ne ""} { |
| 395 | puts "Number of malloc() : [sqlite3_memdebug_malloc_count] calls" |
| 396 | } |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 397 | if {$::cmdlinearg(malloctrace)} { |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 398 | puts "Writing mallocs.sql..." |
| 399 | memdebug_log_sql |
| 400 | sqlite3_memdebug_log stop |
| 401 | sqlite3_memdebug_log clear |
danielk1977 | dbdc4d4 | 2008-03-28 07:42:53 +0000 | [diff] [blame] | 402 | |
| 403 | if {[sqlite3_memory_used]>0} { |
| 404 | puts "Writing leaks.sql..." |
| 405 | sqlite3_memdebug_log sync |
| 406 | memdebug_log_sql leaks.sql |
| 407 | } |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 408 | } |
drh | d9910fe | 2006-10-04 11:55:49 +0000 | [diff] [blame] | 409 | foreach f [glob -nocomplain test.db-*-journal] { |
| 410 | file delete -force $f |
| 411 | } |
| 412 | foreach f [glob -nocomplain test.db-mj*] { |
| 413 | file delete -force $f |
| 414 | } |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 415 | exit [expr {$nErr>0}] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 416 | } |
| 417 | |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 418 | # Display memory statistics for analysis and debugging purposes. |
| 419 | # |
| 420 | proc show_memstats {} { |
| 421 | set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 422 | set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] |
| 423 | set val [format {now %10d max %10d max-size %10d} \ |
| 424 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 425 | puts "Memory used: $val" |
| 426 | set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 427 | set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0] |
| 428 | set val [format {now %10d max %10d max-size %10d} \ |
| 429 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 430 | puts "Page-cache used: $val" |
| 431 | set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] |
| 432 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
| 433 | puts "Page-cache overflow: $val" |
| 434 | set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] |
| 435 | set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]] |
| 436 | puts "Scratch memory used: $val" |
| 437 | set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 438 | set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0] |
| 439 | set val [format {now %10d max %10d max-size %10d} \ |
| 440 | [lindex $x 1] [lindex $x 2] [lindex $y 2]] |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 441 | puts "Scratch overflow: $val" |
drh | ec424a5 | 2008-07-25 15:39:03 +0000 | [diff] [blame] | 442 | ifcapable yytrackmaxstackdepth { |
| 443 | set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0] |
| 444 | set val [format { max %10d} [lindex $x 2]] |
| 445 | puts "Parser stack depth: $val" |
| 446 | } |
drh | f714199 | 2008-06-19 00:16:08 +0000 | [diff] [blame] | 447 | } |
| 448 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 449 | # A procedure to execute SQL |
| 450 | # |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 451 | proc execsql {sql {db db}} { |
drh | acbcdc4 | 2001-01-22 00:31:53 +0000 | [diff] [blame] | 452 | # puts "SQL = $sql" |
danielk1977 | 3bdca9c | 2006-01-17 09:35:01 +0000 | [diff] [blame] | 453 | uplevel [list $db eval $sql] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 454 | } |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 455 | |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 456 | # Execute SQL and catch exceptions. |
| 457 | # |
| 458 | proc catchsql {sql {db db}} { |
| 459 | # puts "SQL = $sql" |
dan | 81fa6dc | 2009-11-28 12:40:32 +0000 | [diff] [blame] | 460 | set r [catch [list uplevel [list $db eval $sql]] msg] |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 461 | lappend r $msg |
| 462 | return $r |
| 463 | } |
| 464 | |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 465 | # Do an VDBE code dump on the SQL given |
| 466 | # |
| 467 | proc explain {sql {db db}} { |
| 468 | puts "" |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 469 | puts "addr opcode p1 p2 p3 p4 p5 #" |
| 470 | puts "---- ------------ ------ ------ ------ --------------- -- -" |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 471 | $db eval "explain $sql" {} { |
danielk1977 | 287fb61 | 2008-01-04 19:10:28 +0000 | [diff] [blame] | 472 | puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \ |
| 473 | $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment |
| 474 | ] |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 478 | # Show the VDBE program for an SQL statement but omit the Trace |
| 479 | # opcode at the beginning. This procedure can be used to prove |
| 480 | # that different SQL statements generate exactly the same VDBE code. |
| 481 | # |
| 482 | proc explain_no_trace {sql} { |
| 483 | set tr [db eval "EXPLAIN $sql"] |
| 484 | return [lrange $tr 7 end] |
| 485 | } |
| 486 | |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 487 | # Another procedure to execute SQL. This one includes the field |
| 488 | # names in the returned list. |
| 489 | # |
| 490 | proc execsql2 {sql} { |
| 491 | set result {} |
| 492 | db eval $sql data { |
| 493 | foreach f $data(*) { |
| 494 | lappend result $f $data($f) |
| 495 | } |
| 496 | } |
| 497 | return $result |
| 498 | } |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 499 | |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 500 | # Use the non-callback API to execute multiple SQL statements |
| 501 | # |
| 502 | proc stepsql {dbptr sql} { |
| 503 | set sql [string trim $sql] |
| 504 | set r 0 |
| 505 | while {[string length $sql]>0} { |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 506 | if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 507 | return [list 1 $vm] |
| 508 | } |
| 509 | set sql [string trim $sqltail] |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 510 | # while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} { |
| 511 | # foreach v $VAL {lappend r $v} |
| 512 | # } |
| 513 | while {[sqlite3_step $vm]=="SQLITE_ROW"} { |
| 514 | for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} { |
| 515 | lappend r [sqlite3_column_text $vm $i] |
| 516 | } |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 517 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 518 | if {[catch {sqlite3_finalize $vm} errmsg]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 519 | return [list 1 $errmsg] |
| 520 | } |
| 521 | } |
| 522 | return $r |
| 523 | } |
| 524 | |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 525 | # Delete a file or directory |
| 526 | # |
| 527 | proc forcedelete {filename} { |
| 528 | if {[catch {file delete -force $filename}]} { |
| 529 | exec rm -rf $filename |
| 530 | } |
| 531 | } |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 532 | |
| 533 | # Do an integrity check of the entire database |
| 534 | # |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 535 | proc integrity_check {name {db db}} { |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 536 | ifcapable integrityck { |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 537 | do_test $name [list execsql {PRAGMA integrity_check} $db] {ok} |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 541 | proc fix_ifcapable_expr {expr} { |
| 542 | set ret "" |
| 543 | set state 0 |
| 544 | for {set i 0} {$i < [string length $expr]} {incr i} { |
| 545 | set char [string range $expr $i $i] |
| 546 | set newstate [expr {[string is alnum $char] || $char eq "_"}] |
| 547 | if {$newstate && !$state} { |
| 548 | append ret {$::sqlite_options(} |
| 549 | } |
| 550 | if {!$newstate && $state} { |
| 551 | append ret ) |
| 552 | } |
| 553 | append ret $char |
| 554 | set state $newstate |
| 555 | } |
| 556 | if {$state} {append ret )} |
| 557 | return $ret |
| 558 | } |
| 559 | |
drh | 27d258a | 2004-10-30 20:23:09 +0000 | [diff] [blame] | 560 | # Evaluate a boolean expression of capabilities. If true, execute the |
| 561 | # code. Omit the code if false. |
| 562 | # |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 563 | proc ifcapable {expr code {else ""} {elsecode ""}} { |
danielk1977 | db4e886 | 2008-01-16 08:24:46 +0000 | [diff] [blame] | 564 | #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2 |
| 565 | set e2 [fix_ifcapable_expr $expr] |
danielk1977 | 3e8c37e | 2005-01-21 03:12:14 +0000 | [diff] [blame] | 566 | if ($e2) { |
| 567 | set c [catch {uplevel 1 $code} r] |
| 568 | } else { |
| 569 | set c [catch {uplevel 1 $elsecode} r] |
| 570 | } |
| 571 | return -code $c $r |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 572 | } |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 573 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 574 | # This proc execs a seperate process that crashes midway through executing |
| 575 | # the SQL script $sql on database test.db. |
| 576 | # |
| 577 | # The crash occurs during a sync() of file $crashfile. When the crash |
| 578 | # occurs a random subset of all unsynced writes made by the process are |
| 579 | # written into the files on disk. Argument $crashdelay indicates the |
| 580 | # number of file syncs to wait before crashing. |
| 581 | # |
| 582 | # The return value is a list of two elements. The first element is a |
| 583 | # boolean, indicating whether or not the process actually crashed or |
| 584 | # reported some other error. The second element in the returned list is the |
| 585 | # error message. This is "child process exited abnormally" if the crash |
| 586 | # occured. |
| 587 | # |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 588 | # crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 589 | # |
| 590 | proc crashsql {args} { |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 591 | if {$::tcl_platform(platform)!="unix"} { |
| 592 | error "crashsql should only be used on unix" |
| 593 | } |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 594 | |
| 595 | set blocksize "" |
| 596 | set crashdelay 1 |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 597 | set prngseed 0 |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 598 | set tclbody {} |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 599 | set crashfile "" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 600 | set dc "" |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 601 | set sql [lindex $args end] |
| 602 | |
| 603 | for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} { |
| 604 | set z [lindex $args $ii] |
| 605 | set n [string length $z] |
| 606 | set z2 [lindex $args [expr $ii+1]] |
| 607 | |
| 608 | if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \ |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 609 | elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 610 | elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \ |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 611 | elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 612 | elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \ |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 613 | elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \ |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 614 | else { error "Unrecognized option: $z" } |
| 615 | } |
| 616 | |
| 617 | if {$crashfile eq ""} { |
| 618 | error "Compulsory option -file missing" |
| 619 | } |
| 620 | |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 621 | set cfile [file join [pwd] $crashfile] |
| 622 | |
| 623 | set f [open crash.tcl w] |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 624 | puts $f "sqlite3_crash_enable 1" |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 625 | puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile" |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 626 | puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte" |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 627 | puts $f "sqlite3 db test.db -vfs crash" |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 628 | |
| 629 | # This block sets the cache size of the main database to 10 |
| 630 | # pages. This is done in case the build is configured to omit |
| 631 | # "PRAGMA cache_size". |
| 632 | puts $f {db eval {SELECT * FROM sqlite_master;}} |
| 633 | puts $f {set bt [btree_from_db db]} |
| 634 | puts $f {btree_set_cache_size $bt 10} |
drh | cb1f0f6 | 2008-01-08 15:18:52 +0000 | [diff] [blame] | 635 | if {$prngseed} { |
| 636 | set seed [expr {$prngseed%10007+1}] |
| 637 | # puts seed=$seed |
| 638 | puts $f "db eval {SELECT randomblob($seed)}" |
| 639 | } |
danielk1977 | 933bbd6 | 2007-03-17 07:22:42 +0000 | [diff] [blame] | 640 | |
drh | f858740 | 2008-01-08 16:03:49 +0000 | [diff] [blame] | 641 | if {[string length $tclbody]>0} { |
| 642 | puts $f $tclbody |
| 643 | } |
| 644 | if {[string length $sql]>0} { |
| 645 | puts $f "db eval {" |
| 646 | puts $f "$sql" |
| 647 | puts $f "}" |
| 648 | } |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 649 | close $f |
| 650 | |
| 651 | set r [catch { |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 652 | exec [info nameofexec] crash.tcl >@stdout |
danielk1977 | aca790a | 2005-01-13 11:07:52 +0000 | [diff] [blame] | 653 | } msg] |
| 654 | lappend r $msg |
| 655 | } |
| 656 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 657 | # Usage: do_ioerr_test <test number> <options...> |
| 658 | # |
| 659 | # This proc is used to implement test cases that check that IO errors |
| 660 | # are correctly handled. The first argument, <test number>, is an integer |
| 661 | # used to name the tests executed by this proc. Options are as follows: |
| 662 | # |
| 663 | # -tclprep TCL script to run to prepare test. |
| 664 | # -sqlprep SQL script to run to prepare test. |
| 665 | # -tclbody TCL script to run with IO error simulation. |
| 666 | # -sqlbody TCL script to run with IO error simulation. |
| 667 | # -exclude List of 'N' values not to test. |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 668 | # -erc Use extended result codes |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 669 | # -persist Make simulated I/O errors persistent |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 670 | # -start Value of 'N' to begin with (default 1) |
| 671 | # |
| 672 | # -cksum Boolean. If true, test that the database does |
| 673 | # not change during the execution of the test case. |
| 674 | # |
| 675 | proc do_ioerr_test {testname args} { |
| 676 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 677 | set ::ioerropts(-start) 1 |
| 678 | set ::ioerropts(-cksum) 0 |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 679 | set ::ioerropts(-erc) 0 |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 680 | set ::ioerropts(-count) 100000000 |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 681 | set ::ioerropts(-persist) 1 |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 682 | set ::ioerropts(-ckrefcount) 0 |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 683 | set ::ioerropts(-restoreprng) 1 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 684 | array set ::ioerropts $args |
| 685 | |
danielk1977 | 47cd39c | 2008-05-12 12:41:15 +0000 | [diff] [blame] | 686 | # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are |
| 687 | # a couple of obscure IO errors that do not return them. |
| 688 | set ::ioerropts(-erc) 0 |
| 689 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 690 | set ::go 1 |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 691 | #reset_prng_state |
| 692 | save_prng_state |
danielk1977 | ef165ce | 2009-04-06 17:50:03 +0000 | [diff] [blame] | 693 | for {set n $::ioerropts(-start)} {$::go} {incr n} { |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 694 | set ::TN $n |
drh | c2ee76c | 2007-01-04 14:58:14 +0000 | [diff] [blame] | 695 | incr ::ioerropts(-count) -1 |
| 696 | if {$::ioerropts(-count)<0} break |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 697 | |
| 698 | # Skip this IO error if it was specified with the "-exclude" option. |
| 699 | if {[info exists ::ioerropts(-exclude)]} { |
| 700 | if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue |
| 701 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 702 | if {$::ioerropts(-restoreprng)} { |
| 703 | restore_prng_state |
| 704 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 705 | |
| 706 | # Delete the files test.db and test2.db, then execute the TCL and |
| 707 | # SQL (in that order) to prepare for the test case. |
| 708 | do_test $testname.$n.1 { |
| 709 | set ::sqlite_io_error_pending 0 |
| 710 | catch {db close} |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 711 | catch {db2 close} |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 712 | catch {file delete -force test.db} |
| 713 | catch {file delete -force test.db-journal} |
| 714 | catch {file delete -force test2.db} |
| 715 | catch {file delete -force test2.db-journal} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 716 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
drh | 4ac285a | 2006-09-15 07:28:50 +0000 | [diff] [blame] | 717 | sqlite3_extended_result_codes $::DB $::ioerropts(-erc) |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 718 | if {[info exists ::ioerropts(-tclprep)]} { |
| 719 | eval $::ioerropts(-tclprep) |
| 720 | } |
| 721 | if {[info exists ::ioerropts(-sqlprep)]} { |
| 722 | execsql $::ioerropts(-sqlprep) |
| 723 | } |
| 724 | expr 0 |
| 725 | } {0} |
| 726 | |
| 727 | # Read the 'checksum' of the database. |
| 728 | if {$::ioerropts(-cksum)} { |
| 729 | set checksum [cksum] |
| 730 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 731 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 732 | # Set the Nth IO error to fail. |
| 733 | do_test $testname.$n.2 [subst { |
drh | d5eb79e | 2007-03-15 12:17:42 +0000 | [diff] [blame] | 734 | set ::sqlite_io_error_persist $::ioerropts(-persist) |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 735 | set ::sqlite_io_error_pending $n |
| 736 | }] $n |
| 737 | |
| 738 | # Create a single TCL script from the TCL and SQL specified |
| 739 | # as the body of the test. |
| 740 | set ::ioerrorbody {} |
| 741 | if {[info exists ::ioerropts(-tclbody)]} { |
| 742 | append ::ioerrorbody "$::ioerropts(-tclbody)\n" |
| 743 | } |
| 744 | if {[info exists ::ioerropts(-sqlbody)]} { |
| 745 | append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}" |
| 746 | } |
| 747 | |
| 748 | # Execute the TCL Script created in the above block. If |
| 749 | # there are at least N IO operations performed by SQLite as |
| 750 | # a result of the script, the Nth will fail. |
| 751 | do_test $testname.$n.3 { |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 752 | set ::sqlite_io_error_hit 0 |
| 753 | set ::sqlite_io_error_hardhit 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 754 | set r [catch $::ioerrorbody msg] |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 755 | set ::errseen $r |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 756 | set rc [sqlite3_errcode $::DB] |
| 757 | if {$::ioerropts(-erc)} { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 758 | # If we are in extended result code mode, make sure all of the |
| 759 | # IOERRs we get back really do have their extended code values. |
| 760 | # If an extended result code is returned, the sqlite3_errcode |
| 761 | # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn |
| 762 | # where nnnn is a number |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 763 | if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} { |
| 764 | return $rc |
| 765 | } |
| 766 | } else { |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 767 | # If we are not in extended result code mode, make sure no |
| 768 | # extended error codes are returned. |
drh | e49f982 | 2006-09-15 12:29:16 +0000 | [diff] [blame] | 769 | if {[regexp {\+\d} $rc]} { |
| 770 | return $rc |
| 771 | } |
| 772 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 773 | # The test repeats as long as $::go is non-zero. $::go starts out |
| 774 | # as 1. When a test runs to completion without hitting an I/O |
| 775 | # error, that means there is no point in continuing with this test |
| 776 | # case so set $::go to zero. |
| 777 | # |
| 778 | if {$::sqlite_io_error_pending>0} { |
| 779 | set ::go 0 |
| 780 | set q 0 |
| 781 | set ::sqlite_io_error_pending 0 |
| 782 | } else { |
| 783 | set q 1 |
| 784 | } |
| 785 | |
drh | c9ac5ca | 2005-11-04 22:03:30 +0000 | [diff] [blame] | 786 | set s [expr $::sqlite_io_error_hit==0] |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 787 | if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} { |
| 788 | set r 1 |
| 789 | } |
danielk1977 | f2fa831 | 2006-01-24 13:09:33 +0000 | [diff] [blame] | 790 | set ::sqlite_io_error_hit 0 |
drh | 5f7b5bf | 2007-04-19 12:30:54 +0000 | [diff] [blame] | 791 | |
| 792 | # One of two things must have happened. either |
| 793 | # 1. We never hit the IO error and the SQL returned OK |
| 794 | # 2. An IO error was hit and the SQL failed |
| 795 | # |
dan | d41a29a | 2010-05-06 15:56:28 +0000 | [diff] [blame] | 796 | #puts "s=$s r=$r q=$q" |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 797 | expr { ($s && !$r && !$q) || (!$s && $r && $q) } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 798 | } {1} |
| 799 | |
danielk1977 | 80daec6 | 2008-05-12 10:57:02 +0000 | [diff] [blame] | 800 | set ::sqlite_io_error_hit 0 |
| 801 | set ::sqlite_io_error_pending 0 |
| 802 | |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 803 | # Check that no page references were leaked. There should be |
| 804 | # a single reference if there is still an active transaction, |
| 805 | # or zero otherwise. |
| 806 | # |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 807 | # UPDATE: If the IO error occurs after a 'BEGIN' but before any |
| 808 | # locks are established on database files (i.e. if the error |
| 809 | # occurs while attempting to detect a hot-journal file), then |
| 810 | # there may 0 page references and an active transaction according |
| 811 | # to [sqlite3_get_autocommit]. |
| 812 | # |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 813 | if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} { |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 814 | do_test $testname.$n.4 { |
| 815 | set bt [btree_from_db db] |
| 816 | db_enter db |
| 817 | array set stats [btree_pager_stats $bt] |
| 818 | db_leave db |
danielk1977 | 8162054 | 2008-06-07 05:19:37 +0000 | [diff] [blame] | 819 | set nRef $stats(ref) |
| 820 | expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)} |
| 821 | } {1} |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 822 | } |
| 823 | |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 824 | # If there is an open database handle and no open transaction, |
| 825 | # and the pager is not running in exclusive-locking mode, |
| 826 | # check that the pager is in "unlocked" state. Theoretically, |
| 827 | # if a call to xUnlock() failed due to an IO error the underlying |
| 828 | # file may still be locked. |
| 829 | # |
| 830 | ifcapable pragma { |
| 831 | if { [info commands db] ne "" |
danielk1977 | 0259fbe | 2008-05-05 17:14:53 +0000 | [diff] [blame] | 832 | && $::ioerropts(-ckrefcount) |
danielk1977 | 52b472a | 2008-05-05 16:23:55 +0000 | [diff] [blame] | 833 | && [db one {pragma locking_mode}] eq "normal" |
| 834 | && [sqlite3_get_autocommit db] |
| 835 | } { |
| 836 | do_test $testname.$n.5 { |
| 837 | set bt [btree_from_db db] |
| 838 | db_enter db |
| 839 | array set stats [btree_pager_stats $bt] |
| 840 | db_leave db |
| 841 | set stats(state) |
| 842 | } 0 |
| 843 | } |
| 844 | } |
| 845 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 846 | # If an IO error occured, then the checksum of the database should |
| 847 | # 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] | 848 | # |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 849 | if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} { |
danielk1977 | 0259fbe | 2008-05-05 17:14:53 +0000 | [diff] [blame] | 850 | do_test $testname.$n.6 { |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 851 | catch {db close} |
danielk1977 | a961339 | 2008-07-08 12:07:32 +0000 | [diff] [blame] | 852 | catch {db2 close} |
drh | a34c62d | 2006-01-06 22:11:20 +0000 | [diff] [blame] | 853 | set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db] |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 854 | cksum |
| 855 | } $checksum |
| 856 | } |
| 857 | |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 858 | set ::sqlite_io_error_hardhit 0 |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 859 | set ::sqlite_io_error_pending 0 |
danielk1977 | 105afed | 2005-05-26 15:20:53 +0000 | [diff] [blame] | 860 | if {[info exists ::ioerropts(-cleanup)]} { |
| 861 | catch $::ioerropts(-cleanup) |
| 862 | } |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 863 | } |
| 864 | set ::sqlite_io_error_pending 0 |
drh | 6d54da0 | 2007-03-25 19:08:46 +0000 | [diff] [blame] | 865 | set ::sqlite_io_error_persist 0 |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 866 | unset ::ioerropts |
| 867 | } |
| 868 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 869 | # Return a checksum based on the contents of the main database associated |
| 870 | # with connection $db |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 871 | # |
| 872 | proc cksum {{db db}} { |
| 873 | set txt [$db eval { |
| 874 | SELECT name, type, sql FROM sqlite_master order by name |
| 875 | }]\n |
| 876 | foreach tbl [$db eval { |
| 877 | SELECT name FROM sqlite_master WHERE type='table' order by name |
| 878 | }] { |
| 879 | append txt [$db eval "SELECT * FROM $tbl"]\n |
| 880 | } |
| 881 | foreach prag {default_synchronous default_cache_size} { |
| 882 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 883 | } |
| 884 | set cksum [string length $txt]-[md5 $txt] |
| 885 | # puts $cksum-[file size test.db] |
| 886 | return $cksum |
| 887 | } |
| 888 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 889 | # Generate a checksum based on the contents of the main and temp tables |
| 890 | # database $db. If the checksum of two databases is the same, and the |
| 891 | # integrity-check passes for both, the two databases are identical. |
| 892 | # |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 893 | proc allcksum {{db db}} { |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 894 | set ret [list] |
| 895 | ifcapable tempdb { |
| 896 | set sql { |
| 897 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 898 | SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION |
| 899 | SELECT 'sqlite_master' UNION |
| 900 | SELECT 'sqlite_temp_master' ORDER BY 1 |
| 901 | } |
| 902 | } else { |
| 903 | set sql { |
| 904 | SELECT name FROM sqlite_master WHERE type = 'table' UNION |
| 905 | SELECT 'sqlite_master' ORDER BY 1 |
| 906 | } |
| 907 | } |
| 908 | set tbllist [$db eval $sql] |
| 909 | set txt {} |
| 910 | foreach tbl $tbllist { |
| 911 | append txt [$db eval "SELECT * FROM $tbl"] |
| 912 | } |
| 913 | foreach prag {default_cache_size} { |
| 914 | append txt $prag-[$db eval "PRAGMA $prag"]\n |
| 915 | } |
| 916 | # puts txt=$txt |
| 917 | return [md5 $txt] |
| 918 | } |
| 919 | |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 920 | # Generate a checksum based on the contents of a single database with |
| 921 | # a database connection. The name of the database is $dbname. |
| 922 | # Examples of $dbname are "temp" or "main". |
| 923 | # |
| 924 | proc dbcksum {db dbname} { |
| 925 | if {$dbname=="temp"} { |
| 926 | set master sqlite_temp_master |
| 927 | } else { |
| 928 | set master $dbname.sqlite_master |
| 929 | } |
| 930 | set alltab [$db eval "SELECT name FROM $master WHERE type='table'"] |
| 931 | set txt [$db eval "SELECT * FROM $master"]\n |
| 932 | foreach tab $alltab { |
| 933 | append txt [$db eval "SELECT * FROM $dbname.$tab"]\n |
| 934 | } |
| 935 | return [md5 $txt] |
| 936 | } |
| 937 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 938 | proc memdebug_log_sql {{filename mallocs.sql}} { |
| 939 | |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 940 | set data [sqlite3_memdebug_log dump] |
| 941 | set nFrame [expr [llength [lindex $data 0]]-2] |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 942 | if {$nFrame < 0} { return "" } |
| 943 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 944 | set database temp |
| 945 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 946 | set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 947 | |
| 948 | set sql "" |
| 949 | foreach e $data { |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 950 | set nCall [lindex $e 0] |
| 951 | set nByte [lindex $e 1] |
| 952 | set lStack [lrange $e 2 end] |
| 953 | append sql "INSERT INTO ${database}.malloc VALUES" |
| 954 | append sql "('test', $nCall, $nByte, '$lStack');\n" |
| 955 | foreach f $lStack { |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 956 | set frames($f) 1 |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n" |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 961 | set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 962 | |
| 963 | foreach f [array names frames] { |
| 964 | set addr [format %x $f] |
| 965 | set cmd "addr2line -e [info nameofexec] $addr" |
| 966 | set line [eval exec $cmd] |
| 967 | append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n" |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 968 | |
| 969 | set file [lindex [split $line :] 0] |
| 970 | set files($file) 1 |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 971 | } |
| 972 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 973 | foreach f [array names files] { |
| 974 | set contents "" |
| 975 | catch { |
| 976 | set fd [open $f] |
| 977 | set contents [read $fd] |
| 978 | close $fd |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 979 | } |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 980 | set contents [string map {' ''} $contents] |
| 981 | append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n" |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 982 | } |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 983 | |
danielk1977 | 35754ac | 2008-03-21 17:29:37 +0000 | [diff] [blame] | 984 | set fd [open $filename w] |
| 985 | puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;" |
| 986 | close $fd |
danielk1977 | 6f332c1 | 2008-03-21 14:22:44 +0000 | [diff] [blame] | 987 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 988 | |
danielk1977 | 32554c1 | 2005-01-22 03:39:39 +0000 | [diff] [blame] | 989 | # Copy file $from into $to. This is used because some versions of |
| 990 | # TCL for windows (notably the 8.4.1 binary package shipped with the |
| 991 | # current mingw release) have a broken "file copy" command. |
| 992 | # |
| 993 | proc copy_file {from to} { |
| 994 | if {$::tcl_platform(platform)=="unix"} { |
| 995 | file copy -force $from $to |
| 996 | } else { |
| 997 | set f [open $from] |
| 998 | fconfigure $f -translation binary |
| 999 | set t [open $to w] |
| 1000 | fconfigure $t -translation binary |
| 1001 | puts -nonewline $t [read $f [file size $from]] |
| 1002 | close $t |
| 1003 | close $f |
| 1004 | } |
| 1005 | } |
| 1006 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1007 | # Drop all tables in database [db] |
| 1008 | proc drop_all_tables {{db db}} { |
shaneh | 4e7b32f | 2009-12-17 22:12:51 +0000 | [diff] [blame] | 1009 | ifcapable trigger&&foreignkey { |
| 1010 | set pk [$db one "PRAGMA foreign_keys"] |
| 1011 | $db eval "PRAGMA foreign_keys = OFF" |
| 1012 | } |
drh | 9a6ffc8 | 2010-02-15 18:03:20 +0000 | [diff] [blame] | 1013 | foreach {idx name file} [db eval {PRAGMA database_list}] { |
| 1014 | if {$idx==1} { |
| 1015 | set master sqlite_temp_master |
| 1016 | } else { |
| 1017 | set master $name.sqlite_master |
| 1018 | } |
| 1019 | foreach {t type} [$db eval " |
| 1020 | SELECT name, type FROM $master |
| 1021 | WHERE type IN('table', 'view') AND name NOT like 'sqlite_%' |
| 1022 | "] { |
| 1023 | $db eval "DROP $type $t" |
| 1024 | } |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1025 | } |
shaneh | 4e7b32f | 2009-12-17 22:12:51 +0000 | [diff] [blame] | 1026 | ifcapable trigger&&foreignkey { |
| 1027 | $db eval "PRAGMA foreign_keys = $pk" |
| 1028 | } |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
dan | 71cb518 | 2010-04-26 12:39:03 +0000 | [diff] [blame] | 1031 | #------------------------------------------------------------------------- |
| 1032 | # If a test script is executed with global variable |
| 1033 | # $::permutations_test_prefix set to "wal", then the tests are run |
| 1034 | # in WAL mode. Otherwise, they should be run in rollback mode. The |
| 1035 | # following Tcl procs are used to make this less intrusive: |
| 1036 | # |
| 1037 | # wal_set_journal_mode ?DB? |
| 1038 | # |
| 1039 | # If running a WAL test, execute "PRAGMA journal_mode = wal" using |
| 1040 | # connection handle DB. Otherwise, this command is a no-op. |
| 1041 | # |
| 1042 | # wal_check_journal_mode TESTNAME ?DB? |
| 1043 | # |
| 1044 | # If running a WAL test, execute a tests case that fails if the main |
| 1045 | # database for connection handle DB is not currently a WAL database. |
| 1046 | # Otherwise (if not running a WAL permutation) this is a no-op. |
| 1047 | # |
| 1048 | # wal_is_wal_mode |
| 1049 | # |
| 1050 | # Returns true if this test should be run in WAL mode. False otherwise. |
| 1051 | # |
| 1052 | proc wal_is_wal_mode {} { |
| 1053 | expr { [catch {set ::permutations_test_prefix} v]==0 && $v == "wal" } |
| 1054 | } |
| 1055 | proc wal_set_journal_mode {{db db}} { |
| 1056 | if { [wal_is_wal_mode] } { |
| 1057 | $db eval "PRAGMA journal_mode = WAL" |
| 1058 | } |
| 1059 | } |
| 1060 | proc wal_check_journal_mode {testname {db db}} { |
| 1061 | if { [wal_is_wal_mode] } { |
| 1062 | $db eval { SELECT * FROM sqlite_master } |
| 1063 | do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal} |
| 1064 | } |
| 1065 | } |
| 1066 | |
dan | c1a60c5 | 2010-06-07 14:28:16 +0000 | [diff] [blame^] | 1067 | #------------------------------------------------------------------------- |
| 1068 | # |
| 1069 | proc slave_test_script {script} { |
| 1070 | |
| 1071 | # Create the interpreter used to run the test script. |
| 1072 | interp create tinterp |
| 1073 | |
| 1074 | # Populate some global variables that tester.tcl expects to see. |
| 1075 | foreach {var value} [list \ |
| 1076 | ::argv0 $::argv0 \ |
| 1077 | ::argv {} \ |
| 1078 | ::SLAVE 1 \ |
| 1079 | ] { |
| 1080 | interp eval tinterp [list set $var $value] |
| 1081 | } |
| 1082 | |
| 1083 | # The alias used to access the global test counters. |
| 1084 | tinterp alias set_test_counter set_test_counter |
| 1085 | |
| 1086 | # Set up the ::cmdlinearg array in the slave. |
| 1087 | interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]] |
| 1088 | |
| 1089 | # Load the various test interfaces implemented in C. |
| 1090 | load_testfixture_extensions tinterp |
| 1091 | |
| 1092 | # Run the test script. |
| 1093 | interp eval tinterp $script |
| 1094 | |
| 1095 | # Delete the interpreter used to run the test script. |
| 1096 | interp delete tinterp |
| 1097 | } |
| 1098 | |
| 1099 | proc slave_test_file {file} { |
| 1100 | set zFile [file join $::testdir $file] |
| 1101 | set time [time { |
| 1102 | slave_test_script [list source $zFile] |
| 1103 | }] |
| 1104 | puts "time $file [lrange $time 0 1]" |
| 1105 | } |
| 1106 | |
dan | f589450 | 2009-10-07 18:41:19 +0000 | [diff] [blame] | 1107 | |
danielk1977 | 45901d6 | 2004-11-10 15:27:38 +0000 | [diff] [blame] | 1108 | # If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set |
| 1109 | # to non-zero, then set the global variable $AUTOVACUUM to 1. |
| 1110 | set AUTOVACUUM $sqlite_options(default_autovacuum) |
danielk1977 | ee8b799 | 2009-03-26 17:13:06 +0000 | [diff] [blame] | 1111 | |
| 1112 | source $testdir/thread_common.tcl |