blob: 9082e811beb2519b1f81b829769a60e0577d2402 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements some common TCL routines used for regression
12# testing the SQLite library
13#
danielk1977287fb612008-01-04 19:10:28 +000014# $Id: tester.tcl,v 1.95 2008/01/04 19:10:29 danielk1977 Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
drh348784e2000-05-29 20:41:49 +000016
drh92febd92004-08-20 18:34:20 +000017set tcl_precision 15
drh49285702005-09-17 15:20:26 +000018set sqlite_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000019
drh3a7fb7c2007-08-10 16:41:08 +000020#
21# Check the command-line arguments for a default soft-heap-limit.
22# Store this default value in the global variable ::soft_limit and
23# update the soft-heap-limit each time this script is run. In that
24# way if an individual test file changes the soft-heap-limit, it
25# will be reset at the start of the next test file.
26#
27if {![info exists soft_limit]} {
28 set soft_limit 0
29 for {set i 0} {$i<[llength $argv]} {incr i} {
30 if {[regexp {^--soft-heap-limit=(.+)$} [lindex $argv $i] all value]} {
31 if {$value!="off"} {
32 set soft_limit $value
33 }
34 set argv [lreplace $argv $i $i]
35 }
36 }
37}
38sqlite3_soft_heap_limit $soft_limit
39
drh4a50aac2007-08-23 02:47:53 +000040#
41# Check the command-line arguments to set the memory debugger
42# backtrace depth.
43#
44# See the sqlite3_memdebug_backtrace() function in mem2.c or
45# test_malloc.c for additional information.
46#
47for {set i 0} {$i<[llength $argv]} {incr i} {
48 if {[regexp {^--backtrace=(\d+)$} [lindex $argv $i] all value]} {
49 sqlite3_memdebug_backtrace $value
50 set argv [lreplace $argv $i $i]
51 }
52}
53
54
drh9eb9e262004-02-11 02:18:05 +000055# Use the pager codec if it is available
56#
drhef4ac8f2004-06-19 00:16:31 +000057if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
58 rename sqlite3 sqlite_orig
59 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +000060 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
61 lappend args -key {xyzzy}
62 }
63 uplevel 1 sqlite_orig $args
64 }
65}
66
67
drhbec2bf42000-05-29 23:48:22 +000068# Create a test database
69#
drh254cba22001-09-20 01:44:42 +000070catch {db close}
71file delete -force test.db
72file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +000073sqlite3 db ./test.db
74set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +000075if {[info exists ::SETUP_SQL]} {
76 db eval $::SETUP_SQL
77}
drhbec2bf42000-05-29 23:48:22 +000078
79# Abort early if this script has been run before.
80#
81if {[info exists nTest]} return
82
83# Set the test counters to zero
84#
drh348784e2000-05-29 20:41:49 +000085set nErr 0
86set nTest 0
drh767c2002000-10-19 14:10:08 +000087set skip_test 0
drha1b351a2001-09-14 16:42:12 +000088set failList {}
drhd3d39e92004-05-20 22:16:29 +000089set maxErr 1000
drhb62c3352006-11-23 09:39:16 +000090if {![info exists speedTest]} {
91 set speedTest 0
92}
drh348784e2000-05-29 20:41:49 +000093
94# Invoke the do_test procedure to run a single test
95#
96proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +000097 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +000098 sqlite3_memdebug_settitle $name
drh767c2002000-10-19 14:10:08 +000099 if {$skip_test} {
100 set skip_test 0
101 return
102 }
103 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000104 set go 1
105 } else {
106 set go 0
107 foreach pattern $argv {
108 if {[string match $pattern $name]} {
109 set go 1
110 break
111 }
112 }
113 }
114 if {!$go} return
115 incr nTest
drh5edc3122001-09-13 21:53:09 +0000116 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000117 flush stdout
118 if {[catch {uplevel #0 "$cmd;\n"} result]} {
119 puts "\nError: $result"
120 incr nErr
drha1b351a2001-09-14 16:42:12 +0000121 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000122 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000123 } elseif {[string compare $result $expected]} {
124 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
125 incr nErr
drha1b351a2001-09-14 16:42:12 +0000126 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000127 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000128 } else {
129 puts " Ok"
130 }
drh6558db82007-04-13 03:23:21 +0000131 flush stdout
drh348784e2000-05-29 20:41:49 +0000132}
133
drhb62c3352006-11-23 09:39:16 +0000134# Run an SQL script.
135# Return the number of microseconds per statement.
136#
drh3590f152006-11-23 21:09:10 +0000137proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000138 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000139 flush stdout
140 set speed [time {sqlite3_exec_nr db $sql}]
141 set tm [lindex $speed 0]
drhb62c3352006-11-23 09:39:16 +0000142 set rate [expr {1000000.0*$numstmt/$tm}]
drh3590f152006-11-23 21:09:10 +0000143 set u2 $units/s
drhdd924312007-03-31 22:34:16 +0000144 puts [format {%12d uS %20.5f %s} $tm $rate $u2]
145 global total_time
146 set total_time [expr {$total_time+$tm}]
147}
148proc speed_trial_init {name} {
149 global total_time
150 set total_time 0
151}
152proc speed_trial_summary {name} {
153 global total_time
154 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000155}
156
drh348784e2000-05-29 20:41:49 +0000157# Run this routine last
158#
159proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000160 finalize_testing
161}
162proc finalize_testing {} {
drh80788d82006-09-02 14:50:23 +0000163 global nTest nErr sqlite_open_file_count
danielk19772e588c72005-12-09 14:25:08 +0000164
drh6e142f52000-06-08 13:36:40 +0000165 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000166 catch {db2 close}
167 catch {db3 close}
168
drh9bc54492007-10-23 14:49:59 +0000169 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +0000170 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000171 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000172 db close
drh3a7fb7c2007-08-10 16:41:08 +0000173 set heaplimit [sqlite3_soft_heap_limit]
174 if {$heaplimit!=$::soft_limit} {
175 puts "soft-heap-limit changed by this script\
176 from $::soft_limit to $heaplimit"
177 } elseif {$heaplimit!="" && $heaplimit>0} {
178 puts "soft-heap-limit set to $heaplimit"
179 }
180 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000181 incr nTest
drh348784e2000-05-29 20:41:49 +0000182 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000183 if {$nErr>0} {
184 puts "Failures on these tests: $::failList"
185 }
drh80788d82006-09-02 14:50:23 +0000186 if {$nErr>0 && ![working_64bit_int]} {
187 puts "******************************************************************"
188 puts "N.B.: The version of TCL that you used to build this test harness"
189 puts "is defective in that it does not support 64-bit integers. Some or"
190 puts "all of the test failures above might be a result from this defect"
191 puts "in your TCL build."
192 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000193 }
drh94e92032003-02-16 22:21:32 +0000194 if {$sqlite_open_file_count} {
195 puts "$sqlite_open_file_count files were left open"
196 incr nErr
197 }
drhf3a65f72007-08-22 20:18:21 +0000198 if {[sqlite3_memory_used]>0} {
199 puts "Unfreed memory: [sqlite3_memory_used] bytes"
200 incr nErr
drha4e5d582007-10-20 15:41:57 +0000201 ifcapable memdebug||(mem3&&debug) {
drh4a50aac2007-08-23 02:47:53 +0000202 puts "Writing unfreed memory log to \"./memleak.txt\""
203 sqlite3_memdebug_dump ./memleak.txt
204 }
drhf3a65f72007-08-22 20:18:21 +0000205 } else {
206 puts "All memory allocations freed - no leaks"
drhd2bb3272007-10-15 19:34:32 +0000207 ifcapable memdebug {
208 sqlite3_memdebug_dump ./memusage.txt
209 }
drhf3a65f72007-08-22 20:18:21 +0000210 }
211 puts "Maximum memory usage: [sqlite3_memory_highwater] bytes"
drhd9910fe2006-10-04 11:55:49 +0000212 foreach f [glob -nocomplain test.db-*-journal] {
213 file delete -force $f
214 }
215 foreach f [glob -nocomplain test.db-mj*] {
216 file delete -force $f
217 }
drhdb25e382001-03-15 18:21:22 +0000218 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000219}
220
drh348784e2000-05-29 20:41:49 +0000221# A procedure to execute SQL
222#
drhc4a3c772001-04-04 11:48:57 +0000223proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000224 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000225 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000226}
drh3aadb2e2000-05-31 17:59:25 +0000227
drhadbca9c2001-09-27 15:11:53 +0000228# Execute SQL and catch exceptions.
229#
230proc catchsql {sql {db db}} {
231 # puts "SQL = $sql"
232 set r [catch {$db eval $sql} msg]
233 lappend r $msg
234 return $r
235}
236
drh04096482001-11-09 22:41:44 +0000237# Do an VDBE code dump on the SQL given
238#
239proc explain {sql {db db}} {
240 puts ""
danielk1977287fb612008-01-04 19:10:28 +0000241 puts "addr opcode p1 p2 p3 p4 p5 #"
242 puts "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +0000243 $db eval "explain $sql" {} {
danielk1977287fb612008-01-04 19:10:28 +0000244 puts [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
245 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
246 ]
drh04096482001-11-09 22:41:44 +0000247 }
248}
249
drh3aadb2e2000-05-31 17:59:25 +0000250# Another procedure to execute SQL. This one includes the field
251# names in the returned list.
252#
253proc execsql2 {sql} {
254 set result {}
255 db eval $sql data {
256 foreach f $data(*) {
257 lappend result $f $data($f)
258 }
259 }
260 return $result
261}
drh17a68932001-01-31 13:28:08 +0000262
drhdde85d92003-03-01 19:45:34 +0000263# Use the non-callback API to execute multiple SQL statements
264#
265proc stepsql {dbptr sql} {
266 set sql [string trim $sql]
267 set r 0
268 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000269 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000270 return [list 1 $vm]
271 }
272 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000273# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
274# foreach v $VAL {lappend r $v}
275# }
276 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
277 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
278 lappend r [sqlite3_column_text $vm $i]
279 }
drhdde85d92003-03-01 19:45:34 +0000280 }
danielk1977106bb232004-05-21 10:08:53 +0000281 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000282 return [list 1 $errmsg]
283 }
284 }
285 return $r
286}
287
drh17a68932001-01-31 13:28:08 +0000288# Delete a file or directory
289#
290proc forcedelete {filename} {
291 if {[catch {file delete -force $filename}]} {
292 exec rm -rf $filename
293 }
294}
drh21504322002-06-25 13:16:02 +0000295
296# Do an integrity check of the entire database
297#
298proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000299 ifcapable integrityck {
300 do_test $name {
301 execsql {PRAGMA integrity_check}
302 } {ok}
303 }
304}
305
306# Evaluate a boolean expression of capabilities. If true, execute the
307# code. Omit the code if false.
308#
danielk19773e8c37e2005-01-21 03:12:14 +0000309proc ifcapable {expr code {else ""} {elsecode ""}} {
drh5436dc22004-11-14 04:04:17 +0000310 regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
danielk19773e8c37e2005-01-21 03:12:14 +0000311 if ($e2) {
312 set c [catch {uplevel 1 $code} r]
313 } else {
314 set c [catch {uplevel 1 $elsecode} r]
315 }
316 return -code $c $r
drh21504322002-06-25 13:16:02 +0000317}
danielk197745901d62004-11-10 15:27:38 +0000318
danielk1977aca790a2005-01-13 11:07:52 +0000319# This proc execs a seperate process that crashes midway through executing
320# the SQL script $sql on database test.db.
321#
322# The crash occurs during a sync() of file $crashfile. When the crash
323# occurs a random subset of all unsynced writes made by the process are
324# written into the files on disk. Argument $crashdelay indicates the
325# number of file syncs to wait before crashing.
326#
327# The return value is a list of two elements. The first element is a
328# boolean, indicating whether or not the process actually crashed or
329# reported some other error. The second element in the returned list is the
330# error message. This is "child process exited abnormally" if the crash
331# occured.
332#
danielk1977f8940ae2007-08-23 11:07:10 +0000333# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000334#
335proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000336 if {$::tcl_platform(platform)!="unix"} {
337 error "crashsql should only be used on unix"
338 }
danielk197759a33f92007-03-17 10:26:59 +0000339
340 set blocksize ""
341 set crashdelay 1
342 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000343 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000344 set sql [lindex $args end]
345
346 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
347 set z [lindex $args $ii]
348 set n [string length $z]
349 set z2 [lindex $args [expr $ii+1]]
350
351 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
352 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000353 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000354 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000355 else { error "Unrecognized option: $z" }
356 }
357
358 if {$crashfile eq ""} {
359 error "Compulsory option -file missing"
360 }
361
danielk1977aca790a2005-01-13 11:07:52 +0000362 set cfile [file join [pwd] $crashfile]
363
364 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +0000365 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +0000366 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000367 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk197795c8a542007-09-01 06:51:27 +0000368 puts $f "sqlite3 db test.db -vfs crash"
danielk1977933bbd62007-03-17 07:22:42 +0000369
370 # This block sets the cache size of the main database to 10
371 # pages. This is done in case the build is configured to omit
372 # "PRAGMA cache_size".
373 puts $f {db eval {SELECT * FROM sqlite_master;}}
374 puts $f {set bt [btree_from_db db]}
375 puts $f {btree_set_cache_size $bt 10}
376
danielk1977aca790a2005-01-13 11:07:52 +0000377 puts $f "db eval {"
378 puts $f "$sql"
379 puts $f "}"
380 close $f
381
382 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000383 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000384 } msg]
385 lappend r $msg
386}
387
danielk197732554c12005-01-22 03:39:39 +0000388# Usage: do_ioerr_test <test number> <options...>
389#
390# This proc is used to implement test cases that check that IO errors
391# are correctly handled. The first argument, <test number>, is an integer
392# used to name the tests executed by this proc. Options are as follows:
393#
394# -tclprep TCL script to run to prepare test.
395# -sqlprep SQL script to run to prepare test.
396# -tclbody TCL script to run with IO error simulation.
397# -sqlbody TCL script to run with IO error simulation.
398# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000399# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000400# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000401# -start Value of 'N' to begin with (default 1)
402#
403# -cksum Boolean. If true, test that the database does
404# not change during the execution of the test case.
405#
406proc do_ioerr_test {testname args} {
407
danielk197732554c12005-01-22 03:39:39 +0000408 set ::ioerropts(-start) 1
409 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000410 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000411 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000412 set ::ioerropts(-persist) 1
danielk197732554c12005-01-22 03:39:39 +0000413 array set ::ioerropts $args
414
415 set ::go 1
416 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000417 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000418 incr ::ioerropts(-count) -1
419 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000420
421 # Skip this IO error if it was specified with the "-exclude" option.
422 if {[info exists ::ioerropts(-exclude)]} {
423 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
424 }
425
426 # Delete the files test.db and test2.db, then execute the TCL and
427 # SQL (in that order) to prepare for the test case.
428 do_test $testname.$n.1 {
429 set ::sqlite_io_error_pending 0
430 catch {db close}
431 catch {file delete -force test.db}
432 catch {file delete -force test.db-journal}
433 catch {file delete -force test2.db}
434 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000435 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000436 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000437 if {[info exists ::ioerropts(-tclprep)]} {
438 eval $::ioerropts(-tclprep)
439 }
440 if {[info exists ::ioerropts(-sqlprep)]} {
441 execsql $::ioerropts(-sqlprep)
442 }
443 expr 0
444 } {0}
445
446 # Read the 'checksum' of the database.
447 if {$::ioerropts(-cksum)} {
448 set checksum [cksum]
449 }
450
451 # Set the Nth IO error to fail.
452 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000453 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000454 set ::sqlite_io_error_pending $n
455 }] $n
456
457 # Create a single TCL script from the TCL and SQL specified
458 # as the body of the test.
459 set ::ioerrorbody {}
460 if {[info exists ::ioerropts(-tclbody)]} {
461 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
462 }
463 if {[info exists ::ioerropts(-sqlbody)]} {
464 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
465 }
466
467 # Execute the TCL Script created in the above block. If
468 # there are at least N IO operations performed by SQLite as
469 # a result of the script, the Nth will fail.
470 do_test $testname.$n.3 {
471 set r [catch $::ioerrorbody msg]
drhe49f9822006-09-15 12:29:16 +0000472 set rc [sqlite3_errcode $::DB]
473 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000474 # If we are in extended result code mode, make sure all of the
475 # IOERRs we get back really do have their extended code values.
476 # If an extended result code is returned, the sqlite3_errcode
477 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
478 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000479 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
480 return $rc
481 }
482 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000483 # If we are not in extended result code mode, make sure no
484 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000485 if {[regexp {\+\d} $rc]} {
486 return $rc
487 }
488 }
drh5f7b5bf2007-04-19 12:30:54 +0000489 # The test repeats as long as $::go is true.
danielk197732554c12005-01-22 03:39:39 +0000490 set ::go [expr {$::sqlite_io_error_pending<=0}]
drhc9ac5ca2005-11-04 22:03:30 +0000491 set s [expr $::sqlite_io_error_hit==0]
danielk1977f2fa8312006-01-24 13:09:33 +0000492 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000493
494 # One of two things must have happened. either
495 # 1. We never hit the IO error and the SQL returned OK
496 # 2. An IO error was hit and the SQL failed
497 #
danielk1977f2fa8312006-01-24 13:09:33 +0000498 expr { ($s && !$r && !$::go) || (!$s && $r && $::go) }
danielk197732554c12005-01-22 03:39:39 +0000499 } {1}
500
501 # If an IO error occured, then the checksum of the database should
502 # be the same as before the script that caused the IO error was run.
503 if {$::go && $::ioerropts(-cksum)} {
504 do_test $testname.$n.4 {
505 catch {db close}
drha34c62d2006-01-06 22:11:20 +0000506 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000507 cksum
508 } $checksum
509 }
510
danielk1977c4da5b92006-01-21 12:08:54 +0000511 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000512 if {[info exists ::ioerropts(-cleanup)]} {
513 catch $::ioerropts(-cleanup)
514 }
danielk197732554c12005-01-22 03:39:39 +0000515 }
516 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000517 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000518 unset ::ioerropts
519}
520
521# Return a checksum based on the contents of database 'db'.
522#
523proc cksum {{db db}} {
524 set txt [$db eval {
525 SELECT name, type, sql FROM sqlite_master order by name
526 }]\n
527 foreach tbl [$db eval {
528 SELECT name FROM sqlite_master WHERE type='table' order by name
529 }] {
530 append txt [$db eval "SELECT * FROM $tbl"]\n
531 }
532 foreach prag {default_synchronous default_cache_size} {
533 append txt $prag-[$db eval "PRAGMA $prag"]\n
534 }
535 set cksum [string length $txt]-[md5 $txt]
536 # puts $cksum-[file size test.db]
537 return $cksum
538}
539
540# Copy file $from into $to. This is used because some versions of
541# TCL for windows (notably the 8.4.1 binary package shipped with the
542# current mingw release) have a broken "file copy" command.
543#
544proc copy_file {from to} {
545 if {$::tcl_platform(platform)=="unix"} {
546 file copy -force $from $to
547 } else {
548 set f [open $from]
549 fconfigure $f -translation binary
550 set t [open $to w]
551 fconfigure $t -translation binary
552 puts -nonewline $t [read $f [file size $from]]
553 close $t
554 close $f
555 }
556}
557
danielk197745901d62004-11-10 15:27:38 +0000558# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
559# to non-zero, then set the global variable $AUTOVACUUM to 1.
560set AUTOVACUUM $sqlite_options(default_autovacuum)