blob: cc6acddb5fd0fe3133a176a851856d7853b95726 [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#
danielk1977f8940ae2007-08-23 11:07:10 +000014# $Id: tester.tcl,v 1.88 2007/08/23 11:07:10 danielk1977 Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
drhef4ac8f2004-06-19 00:16:31 +000016# Make sure tclsqlite3 was compiled correctly. Abort now with an
drhfbc3eab2001-04-06 16:13:42 +000017# error message if not.
18#
drhef4ac8f2004-06-19 00:16:31 +000019if {[sqlite3 -tcl-uses-utf]} {
drhfbc3eab2001-04-06 16:13:42 +000020 if {"\u1234"=="u1234"} {
21 puts stderr "***** BUILD PROBLEM *****"
22 puts stderr "$argv0 was linked against an older version"
23 puts stderr "of TCL that does not support Unicode, but uses a header"
24 puts stderr "file (\"tcl.h\") from a new TCL version that does support"
25 puts stderr "Unicode. This combination causes internal errors."
26 puts stderr "Recompile using a TCL library and header file that match"
27 puts stderr "and try again.\n**************************"
28 exit 1
29 }
30} else {
31 if {"\u1234"!="u1234"} {
32 puts stderr "***** BUILD PROBLEM *****"
33 puts stderr "$argv0 was linked against an newer version"
34 puts stderr "of TCL that supports Unicode, but uses a header file"
35 puts stderr "(\"tcl.h\") from a old TCL version that does not support"
36 puts stderr "Unicode. This combination causes internal errors."
37 puts stderr "Recompile using a TCL library and header file that match"
38 puts stderr "and try again.\n**************************"
39 exit 1
40 }
41}
drh348784e2000-05-29 20:41:49 +000042
drh92febd92004-08-20 18:34:20 +000043set tcl_precision 15
drh49285702005-09-17 15:20:26 +000044set sqlite_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000045
drh3a7fb7c2007-08-10 16:41:08 +000046#
47# Check the command-line arguments for a default soft-heap-limit.
48# Store this default value in the global variable ::soft_limit and
49# update the soft-heap-limit each time this script is run. In that
50# way if an individual test file changes the soft-heap-limit, it
51# will be reset at the start of the next test file.
52#
53if {![info exists soft_limit]} {
54 set soft_limit 0
55 for {set i 0} {$i<[llength $argv]} {incr i} {
56 if {[regexp {^--soft-heap-limit=(.+)$} [lindex $argv $i] all value]} {
57 if {$value!="off"} {
58 set soft_limit $value
59 }
60 set argv [lreplace $argv $i $i]
61 }
62 }
63}
64sqlite3_soft_heap_limit $soft_limit
65
drh4a50aac2007-08-23 02:47:53 +000066#
67# Check the command-line arguments to set the memory debugger
68# backtrace depth.
69#
70# See the sqlite3_memdebug_backtrace() function in mem2.c or
71# test_malloc.c for additional information.
72#
73for {set i 0} {$i<[llength $argv]} {incr i} {
74 if {[regexp {^--backtrace=(\d+)$} [lindex $argv $i] all value]} {
75 sqlite3_memdebug_backtrace $value
76 set argv [lreplace $argv $i $i]
77 }
78}
79
80
drh9eb9e262004-02-11 02:18:05 +000081# Use the pager codec if it is available
82#
drhef4ac8f2004-06-19 00:16:31 +000083if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
84 rename sqlite3 sqlite_orig
85 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +000086 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
87 lappend args -key {xyzzy}
88 }
89 uplevel 1 sqlite_orig $args
90 }
91}
92
93
drhbec2bf42000-05-29 23:48:22 +000094# Create a test database
95#
drh254cba22001-09-20 01:44:42 +000096catch {db close}
97file delete -force test.db
98file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +000099sqlite3 db ./test.db
100set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +0000101if {[info exists ::SETUP_SQL]} {
102 db eval $::SETUP_SQL
103}
drhbec2bf42000-05-29 23:48:22 +0000104
105# Abort early if this script has been run before.
106#
107if {[info exists nTest]} return
108
109# Set the test counters to zero
110#
drh348784e2000-05-29 20:41:49 +0000111set nErr 0
112set nTest 0
drh767c2002000-10-19 14:10:08 +0000113set skip_test 0
drha1b351a2001-09-14 16:42:12 +0000114set failList {}
drhd3d39e92004-05-20 22:16:29 +0000115set maxErr 1000
drhb62c3352006-11-23 09:39:16 +0000116if {![info exists speedTest]} {
117 set speedTest 0
118}
drh348784e2000-05-29 20:41:49 +0000119
120# Invoke the do_test procedure to run a single test
121#
122proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000123 global argv nErr nTest skip_test maxErr
drh4a50aac2007-08-23 02:47:53 +0000124 sqlite3_memdebug_settitle $name
drh767c2002000-10-19 14:10:08 +0000125 if {$skip_test} {
126 set skip_test 0
127 return
128 }
129 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000130 set go 1
131 } else {
132 set go 0
133 foreach pattern $argv {
134 if {[string match $pattern $name]} {
135 set go 1
136 break
137 }
138 }
139 }
140 if {!$go} return
141 incr nTest
drh5edc3122001-09-13 21:53:09 +0000142 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000143 flush stdout
144 if {[catch {uplevel #0 "$cmd;\n"} result]} {
145 puts "\nError: $result"
146 incr nErr
drha1b351a2001-09-14 16:42:12 +0000147 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000148 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000149 } elseif {[string compare $result $expected]} {
150 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
151 incr nErr
drha1b351a2001-09-14 16:42:12 +0000152 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000153 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000154 } else {
155 puts " Ok"
156 }
drh6558db82007-04-13 03:23:21 +0000157 flush stdout
drh348784e2000-05-29 20:41:49 +0000158}
159
drhb62c3352006-11-23 09:39:16 +0000160# Run an SQL script.
161# Return the number of microseconds per statement.
162#
drh3590f152006-11-23 21:09:10 +0000163proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000164 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000165 flush stdout
166 set speed [time {sqlite3_exec_nr db $sql}]
167 set tm [lindex $speed 0]
drhb62c3352006-11-23 09:39:16 +0000168 set rate [expr {1000000.0*$numstmt/$tm}]
drh3590f152006-11-23 21:09:10 +0000169 set u2 $units/s
drhdd924312007-03-31 22:34:16 +0000170 puts [format {%12d uS %20.5f %s} $tm $rate $u2]
171 global total_time
172 set total_time [expr {$total_time+$tm}]
173}
174proc speed_trial_init {name} {
175 global total_time
176 set total_time 0
177}
178proc speed_trial_summary {name} {
179 global total_time
180 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000181}
182
drh348784e2000-05-29 20:41:49 +0000183# Run this routine last
184#
185proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000186 finalize_testing
187}
188proc finalize_testing {} {
drh80788d82006-09-02 14:50:23 +0000189 global nTest nErr sqlite_open_file_count
danielk19772e588c72005-12-09 14:25:08 +0000190
drh6e142f52000-06-08 13:36:40 +0000191 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +0000192 catch {db2 close}
193 catch {db3 close}
194
drhb4bc7052006-01-11 23:40:33 +0000195 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000196 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000197 db close
drh3a7fb7c2007-08-10 16:41:08 +0000198 set heaplimit [sqlite3_soft_heap_limit]
199 if {$heaplimit!=$::soft_limit} {
200 puts "soft-heap-limit changed by this script\
201 from $::soft_limit to $heaplimit"
202 } elseif {$heaplimit!="" && $heaplimit>0} {
203 puts "soft-heap-limit set to $heaplimit"
204 }
205 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000206 incr nTest
drh348784e2000-05-29 20:41:49 +0000207 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000208 if {$nErr>0} {
209 puts "Failures on these tests: $::failList"
210 }
drh80788d82006-09-02 14:50:23 +0000211 if {$nErr>0 && ![working_64bit_int]} {
212 puts "******************************************************************"
213 puts "N.B.: The version of TCL that you used to build this test harness"
214 puts "is defective in that it does not support 64-bit integers. Some or"
215 puts "all of the test failures above might be a result from this defect"
216 puts "in your TCL build."
217 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000218 }
drh94e92032003-02-16 22:21:32 +0000219 if {$sqlite_open_file_count} {
220 puts "$sqlite_open_file_count files were left open"
221 incr nErr
222 }
drhf3a65f72007-08-22 20:18:21 +0000223 if {[sqlite3_memory_used]>0} {
224 puts "Unfreed memory: [sqlite3_memory_used] bytes"
225 incr nErr
drh4a50aac2007-08-23 02:47:53 +0000226 ifcapable memdebug {
227 puts "Writing unfreed memory log to \"./memleak.txt\""
228 sqlite3_memdebug_dump ./memleak.txt
229 }
drhf3a65f72007-08-22 20:18:21 +0000230 } else {
231 puts "All memory allocations freed - no leaks"
232 }
233 puts "Maximum memory usage: [sqlite3_memory_highwater] bytes"
drhd9910fe2006-10-04 11:55:49 +0000234 foreach f [glob -nocomplain test.db-*-journal] {
235 file delete -force $f
236 }
237 foreach f [glob -nocomplain test.db-mj*] {
238 file delete -force $f
239 }
drhdb25e382001-03-15 18:21:22 +0000240 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000241}
242
drh348784e2000-05-29 20:41:49 +0000243# A procedure to execute SQL
244#
drhc4a3c772001-04-04 11:48:57 +0000245proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000246 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000247 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000248}
drh3aadb2e2000-05-31 17:59:25 +0000249
drhadbca9c2001-09-27 15:11:53 +0000250# Execute SQL and catch exceptions.
251#
252proc catchsql {sql {db db}} {
253 # puts "SQL = $sql"
254 set r [catch {$db eval $sql} msg]
255 lappend r $msg
256 return $r
257}
258
drh04096482001-11-09 22:41:44 +0000259# Do an VDBE code dump on the SQL given
260#
261proc explain {sql {db db}} {
262 puts ""
263 puts "addr opcode p1 p2 p3 "
264 puts "---- ------------ ------ ------ ---------------"
265 $db eval "explain $sql" {} {
266 puts [format {%-4d %-12.12s %-6d %-6d %s} $addr $opcode $p1 $p2 $p3]
267 }
268}
269
drh3aadb2e2000-05-31 17:59:25 +0000270# Another procedure to execute SQL. This one includes the field
271# names in the returned list.
272#
273proc execsql2 {sql} {
274 set result {}
275 db eval $sql data {
276 foreach f $data(*) {
277 lappend result $f $data($f)
278 }
279 }
280 return $result
281}
drh17a68932001-01-31 13:28:08 +0000282
drhdde85d92003-03-01 19:45:34 +0000283# Use the non-callback API to execute multiple SQL statements
284#
285proc stepsql {dbptr sql} {
286 set sql [string trim $sql]
287 set r 0
288 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000289 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000290 return [list 1 $vm]
291 }
292 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000293# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
294# foreach v $VAL {lappend r $v}
295# }
296 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
297 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
298 lappend r [sqlite3_column_text $vm $i]
299 }
drhdde85d92003-03-01 19:45:34 +0000300 }
danielk1977106bb232004-05-21 10:08:53 +0000301 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000302 return [list 1 $errmsg]
303 }
304 }
305 return $r
306}
307
drh17a68932001-01-31 13:28:08 +0000308# Delete a file or directory
309#
310proc forcedelete {filename} {
311 if {[catch {file delete -force $filename}]} {
312 exec rm -rf $filename
313 }
314}
drh21504322002-06-25 13:16:02 +0000315
316# Do an integrity check of the entire database
317#
318proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000319 ifcapable integrityck {
320 do_test $name {
321 execsql {PRAGMA integrity_check}
322 } {ok}
323 }
324}
325
326# Evaluate a boolean expression of capabilities. If true, execute the
327# code. Omit the code if false.
328#
danielk19773e8c37e2005-01-21 03:12:14 +0000329proc ifcapable {expr code {else ""} {elsecode ""}} {
drh5436dc22004-11-14 04:04:17 +0000330 regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
danielk19773e8c37e2005-01-21 03:12:14 +0000331 if ($e2) {
332 set c [catch {uplevel 1 $code} r]
333 } else {
334 set c [catch {uplevel 1 $elsecode} r]
335 }
336 return -code $c $r
drh21504322002-06-25 13:16:02 +0000337}
danielk197745901d62004-11-10 15:27:38 +0000338
danielk1977aca790a2005-01-13 11:07:52 +0000339# This proc execs a seperate process that crashes midway through executing
340# the SQL script $sql on database test.db.
341#
342# The crash occurs during a sync() of file $crashfile. When the crash
343# occurs a random subset of all unsynced writes made by the process are
344# written into the files on disk. Argument $crashdelay indicates the
345# number of file syncs to wait before crashing.
346#
347# The return value is a list of two elements. The first element is a
348# boolean, indicating whether or not the process actually crashed or
349# reported some other error. The second element in the returned list is the
350# error message. This is "child process exited abnormally" if the crash
351# occured.
352#
danielk1977f8940ae2007-08-23 11:07:10 +0000353# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +0000354#
355proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000356 if {$::tcl_platform(platform)!="unix"} {
357 error "crashsql should only be used on unix"
358 }
danielk197759a33f92007-03-17 10:26:59 +0000359
360 set blocksize ""
361 set crashdelay 1
362 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +0000363 set dc ""
danielk197759a33f92007-03-17 10:26:59 +0000364 set sql [lindex $args end]
365
366 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
367 set z [lindex $args $ii]
368 set n [string length $z]
369 set z2 [lindex $args [expr $ii+1]]
370
371 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
372 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000373 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +0000374 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +0000375 else { error "Unrecognized option: $z" }
376 }
377
378 if {$crashfile eq ""} {
379 error "Compulsory option -file missing"
380 }
381
danielk1977aca790a2005-01-13 11:07:52 +0000382 set cfile [file join [pwd] $crashfile]
383
384 set f [open crash.tcl w]
danielk1977f8940ae2007-08-23 11:07:10 +0000385 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000386 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk1977aca790a2005-01-13 11:07:52 +0000387 puts $f "sqlite3 db test.db"
danielk1977933bbd62007-03-17 07:22:42 +0000388
389 # This block sets the cache size of the main database to 10
390 # pages. This is done in case the build is configured to omit
391 # "PRAGMA cache_size".
392 puts $f {db eval {SELECT * FROM sqlite_master;}}
393 puts $f {set bt [btree_from_db db]}
394 puts $f {btree_set_cache_size $bt 10}
395
danielk1977aca790a2005-01-13 11:07:52 +0000396 puts $f "db eval {"
397 puts $f "$sql"
398 puts $f "}"
399 close $f
400
401 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000402 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000403 } msg]
404 lappend r $msg
405}
406
danielk197732554c12005-01-22 03:39:39 +0000407# Usage: do_ioerr_test <test number> <options...>
408#
409# This proc is used to implement test cases that check that IO errors
410# are correctly handled. The first argument, <test number>, is an integer
411# used to name the tests executed by this proc. Options are as follows:
412#
413# -tclprep TCL script to run to prepare test.
414# -sqlprep SQL script to run to prepare test.
415# -tclbody TCL script to run with IO error simulation.
416# -sqlbody TCL script to run with IO error simulation.
417# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000418# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000419# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000420# -start Value of 'N' to begin with (default 1)
421#
422# -cksum Boolean. If true, test that the database does
423# not change during the execution of the test case.
424#
425proc do_ioerr_test {testname args} {
426
danielk197732554c12005-01-22 03:39:39 +0000427 set ::ioerropts(-start) 1
428 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000429 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000430 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000431 set ::ioerropts(-persist) 1
danielk197732554c12005-01-22 03:39:39 +0000432 array set ::ioerropts $args
433
434 set ::go 1
435 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000436 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000437 incr ::ioerropts(-count) -1
438 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000439
440 # Skip this IO error if it was specified with the "-exclude" option.
441 if {[info exists ::ioerropts(-exclude)]} {
442 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
443 }
444
445 # Delete the files test.db and test2.db, then execute the TCL and
446 # SQL (in that order) to prepare for the test case.
447 do_test $testname.$n.1 {
448 set ::sqlite_io_error_pending 0
449 catch {db close}
450 catch {file delete -force test.db}
451 catch {file delete -force test.db-journal}
452 catch {file delete -force test2.db}
453 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000454 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000455 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000456 if {[info exists ::ioerropts(-tclprep)]} {
457 eval $::ioerropts(-tclprep)
458 }
459 if {[info exists ::ioerropts(-sqlprep)]} {
460 execsql $::ioerropts(-sqlprep)
461 }
462 expr 0
463 } {0}
464
465 # Read the 'checksum' of the database.
466 if {$::ioerropts(-cksum)} {
467 set checksum [cksum]
468 }
469
470 # Set the Nth IO error to fail.
471 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000472 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000473 set ::sqlite_io_error_pending $n
474 }] $n
475
476 # Create a single TCL script from the TCL and SQL specified
477 # as the body of the test.
478 set ::ioerrorbody {}
479 if {[info exists ::ioerropts(-tclbody)]} {
480 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
481 }
482 if {[info exists ::ioerropts(-sqlbody)]} {
483 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
484 }
485
486 # Execute the TCL Script created in the above block. If
487 # there are at least N IO operations performed by SQLite as
488 # a result of the script, the Nth will fail.
489 do_test $testname.$n.3 {
490 set r [catch $::ioerrorbody msg]
drhe49f9822006-09-15 12:29:16 +0000491 set rc [sqlite3_errcode $::DB]
492 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000493 # If we are in extended result code mode, make sure all of the
494 # IOERRs we get back really do have their extended code values.
495 # If an extended result code is returned, the sqlite3_errcode
496 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
497 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000498 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
499 return $rc
500 }
501 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000502 # If we are not in extended result code mode, make sure no
503 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000504 if {[regexp {\+\d} $rc]} {
505 return $rc
506 }
507 }
drh5f7b5bf2007-04-19 12:30:54 +0000508 # The test repeats as long as $::go is true.
danielk197732554c12005-01-22 03:39:39 +0000509 set ::go [expr {$::sqlite_io_error_pending<=0}]
drhc9ac5ca2005-11-04 22:03:30 +0000510 set s [expr $::sqlite_io_error_hit==0]
danielk1977f2fa8312006-01-24 13:09:33 +0000511 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000512
513 # One of two things must have happened. either
514 # 1. We never hit the IO error and the SQL returned OK
515 # 2. An IO error was hit and the SQL failed
516 #
danielk1977f2fa8312006-01-24 13:09:33 +0000517 expr { ($s && !$r && !$::go) || (!$s && $r && $::go) }
danielk197732554c12005-01-22 03:39:39 +0000518 } {1}
519
520 # If an IO error occured, then the checksum of the database should
521 # be the same as before the script that caused the IO error was run.
522 if {$::go && $::ioerropts(-cksum)} {
523 do_test $testname.$n.4 {
524 catch {db close}
drha34c62d2006-01-06 22:11:20 +0000525 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000526 cksum
527 } $checksum
528 }
529
danielk1977c4da5b92006-01-21 12:08:54 +0000530 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000531 if {[info exists ::ioerropts(-cleanup)]} {
532 catch $::ioerropts(-cleanup)
533 }
danielk197732554c12005-01-22 03:39:39 +0000534 }
535 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000536 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000537 unset ::ioerropts
538}
539
540# Return a checksum based on the contents of database 'db'.
541#
542proc cksum {{db db}} {
543 set txt [$db eval {
544 SELECT name, type, sql FROM sqlite_master order by name
545 }]\n
546 foreach tbl [$db eval {
547 SELECT name FROM sqlite_master WHERE type='table' order by name
548 }] {
549 append txt [$db eval "SELECT * FROM $tbl"]\n
550 }
551 foreach prag {default_synchronous default_cache_size} {
552 append txt $prag-[$db eval "PRAGMA $prag"]\n
553 }
554 set cksum [string length $txt]-[md5 $txt]
555 # puts $cksum-[file size test.db]
556 return $cksum
557}
558
559# Copy file $from into $to. This is used because some versions of
560# TCL for windows (notably the 8.4.1 binary package shipped with the
561# current mingw release) have a broken "file copy" command.
562#
563proc copy_file {from to} {
564 if {$::tcl_platform(platform)=="unix"} {
565 file copy -force $from $to
566 } else {
567 set f [open $from]
568 fconfigure $f -translation binary
569 set t [open $to w]
570 fconfigure $t -translation binary
571 puts -nonewline $t [read $f [file size $from]]
572 close $t
573 close $f
574 }
575}
576
danielk197745901d62004-11-10 15:27:38 +0000577# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
578# to non-zero, then set the global variable $AUTOVACUUM to 1.
579set AUTOVACUUM $sqlite_options(default_autovacuum)