blob: 8b38beb3d669fe60b1b585f70d702652f080e04b [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#
drhf3a65f72007-08-22 20:18:21 +000014# $Id: tester.tcl,v 1.85 2007/08/22 20:18:22 drh 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
drh9eb9e262004-02-11 02:18:05 +000066# Use the pager codec if it is available
67#
drhef4ac8f2004-06-19 00:16:31 +000068if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} {
69 rename sqlite3 sqlite_orig
70 proc sqlite3 {args} {
drh9eb9e262004-02-11 02:18:05 +000071 if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} {
72 lappend args -key {xyzzy}
73 }
74 uplevel 1 sqlite_orig $args
75 }
76}
77
78
drhbec2bf42000-05-29 23:48:22 +000079# Create a test database
80#
drh254cba22001-09-20 01:44:42 +000081catch {db close}
82file delete -force test.db
83file delete -force test.db-journal
drhdddca282006-01-03 00:33:50 +000084sqlite3 db ./test.db
85set ::DB [sqlite3_connection_pointer db]
drhcd61c282002-03-06 22:01:34 +000086if {[info exists ::SETUP_SQL]} {
87 db eval $::SETUP_SQL
88}
drhbec2bf42000-05-29 23:48:22 +000089
90# Abort early if this script has been run before.
91#
92if {[info exists nTest]} return
93
94# Set the test counters to zero
95#
drh348784e2000-05-29 20:41:49 +000096set nErr 0
97set nTest 0
drh767c2002000-10-19 14:10:08 +000098set skip_test 0
drha1b351a2001-09-14 16:42:12 +000099set failList {}
drhd3d39e92004-05-20 22:16:29 +0000100set maxErr 1000
drhb62c3352006-11-23 09:39:16 +0000101if {![info exists speedTest]} {
102 set speedTest 0
103}
drh348784e2000-05-29 20:41:49 +0000104
105# Invoke the do_test procedure to run a single test
106#
107proc do_test {name cmd expected} {
drhd3d39e92004-05-20 22:16:29 +0000108 global argv nErr nTest skip_test maxErr
danielk19772e588c72005-12-09 14:25:08 +0000109 set ::sqlite_malloc_id $name
drh767c2002000-10-19 14:10:08 +0000110 if {$skip_test} {
111 set skip_test 0
112 return
113 }
114 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +0000115 set go 1
116 } else {
117 set go 0
118 foreach pattern $argv {
119 if {[string match $pattern $name]} {
120 set go 1
121 break
122 }
123 }
124 }
125 if {!$go} return
126 incr nTest
drh5edc3122001-09-13 21:53:09 +0000127 puts -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000128 flush stdout
129 if {[catch {uplevel #0 "$cmd;\n"} result]} {
130 puts "\nError: $result"
131 incr nErr
drha1b351a2001-09-14 16:42:12 +0000132 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000133 if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000134 } elseif {[string compare $result $expected]} {
135 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
136 incr nErr
drha1b351a2001-09-14 16:42:12 +0000137 lappend ::failList $name
drhd3d39e92004-05-20 22:16:29 +0000138 if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing}
drh348784e2000-05-29 20:41:49 +0000139 } else {
140 puts " Ok"
141 }
drh6558db82007-04-13 03:23:21 +0000142 flush stdout
drh348784e2000-05-29 20:41:49 +0000143}
144
drhb62c3352006-11-23 09:39:16 +0000145# Run an SQL script.
146# Return the number of microseconds per statement.
147#
drh3590f152006-11-23 21:09:10 +0000148proc speed_trial {name numstmt units sql} {
drhdd924312007-03-31 22:34:16 +0000149 puts -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +0000150 flush stdout
151 set speed [time {sqlite3_exec_nr db $sql}]
152 set tm [lindex $speed 0]
drhb62c3352006-11-23 09:39:16 +0000153 set rate [expr {1000000.0*$numstmt/$tm}]
drh3590f152006-11-23 21:09:10 +0000154 set u2 $units/s
drhdd924312007-03-31 22:34:16 +0000155 puts [format {%12d uS %20.5f %s} $tm $rate $u2]
156 global total_time
157 set total_time [expr {$total_time+$tm}]
158}
159proc speed_trial_init {name} {
160 global total_time
161 set total_time 0
162}
163proc speed_trial_summary {name} {
164 global total_time
165 puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
drhb62c3352006-11-23 09:39:16 +0000166}
167
drhdaffd0e2001-04-11 14:28:42 +0000168# The procedure uses the special "sqlite_malloc_stat" command
drhc89b91b2005-01-03 01:32:59 +0000169# (which is only available if SQLite is compiled with -DSQLITE_DEBUG=1)
drh8c82b352000-12-10 18:23:50 +0000170# to see how many malloc()s have not been free()ed. The number
171# of surplus malloc()s is stored in the global variable $::Leak.
172# If the value in $::Leak grows, it may mean there is a memory leak
173# in the library.
174#
175proc memleak_check {} {
drhdaffd0e2001-04-11 14:28:42 +0000176 if {[info command sqlite_malloc_stat]!=""} {
177 set r [sqlite_malloc_stat]
178 set ::Leak [expr {[lindex $r 0]-[lindex $r 1]}]
179 }
drh8c82b352000-12-10 18:23:50 +0000180}
181
drh348784e2000-05-29 20:41:49 +0000182# Run this routine last
183#
184proc finish_test {} {
drha1b351a2001-09-14 16:42:12 +0000185 finalize_testing
186}
187proc finalize_testing {} {
drh80788d82006-09-02 14:50:23 +0000188 global nTest nErr sqlite_open_file_count
drha1b351a2001-09-14 16:42:12 +0000189 if {$nErr==0} memleak_check
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
danielk197797cb2e92005-12-09 14:39:04 +0000195 catch {
196 pp_check_for_leaks
197 }
drhb4bc7052006-01-11 23:40:33 +0000198 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +0000199 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +0000200 db close
drh3a7fb7c2007-08-10 16:41:08 +0000201 set heaplimit [sqlite3_soft_heap_limit]
202 if {$heaplimit!=$::soft_limit} {
203 puts "soft-heap-limit changed by this script\
204 from $::soft_limit to $heaplimit"
205 } elseif {$heaplimit!="" && $heaplimit>0} {
206 puts "soft-heap-limit set to $heaplimit"
207 }
208 sqlite3_soft_heap_limit 0
drhb4bc7052006-01-11 23:40:33 +0000209 incr nTest
drh348784e2000-05-29 20:41:49 +0000210 puts "$nErr errors out of $nTest tests"
drhf3a65f72007-08-22 20:18:21 +0000211 if {$nErr>0} {
212 puts "Failures on these tests: $::failList"
213 }
drh80788d82006-09-02 14:50:23 +0000214 if {$nErr>0 && ![working_64bit_int]} {
215 puts "******************************************************************"
216 puts "N.B.: The version of TCL that you used to build this test harness"
217 puts "is defective in that it does not support 64-bit integers. Some or"
218 puts "all of the test failures above might be a result from this defect"
219 puts "in your TCL build."
220 puts "******************************************************************"
drhdb25e382001-03-15 18:21:22 +0000221 }
drh94e92032003-02-16 22:21:32 +0000222 if {$sqlite_open_file_count} {
223 puts "$sqlite_open_file_count files were left open"
224 incr nErr
225 }
drhf3a65f72007-08-22 20:18:21 +0000226 if {[sqlite3_memory_used]>0} {
227 puts "Unfreed memory: [sqlite3_memory_used] bytes"
228 incr nErr
229 } else {
230 puts "All memory allocations freed - no leaks"
231 }
232 puts "Maximum memory usage: [sqlite3_memory_highwater] bytes"
drhd9910fe2006-10-04 11:55:49 +0000233 foreach f [glob -nocomplain test.db-*-journal] {
234 file delete -force $f
235 }
236 foreach f [glob -nocomplain test.db-mj*] {
237 file delete -force $f
238 }
drhdb25e382001-03-15 18:21:22 +0000239 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +0000240}
241
drh348784e2000-05-29 20:41:49 +0000242# A procedure to execute SQL
243#
drhc4a3c772001-04-04 11:48:57 +0000244proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +0000245 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +0000246 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +0000247}
drh3aadb2e2000-05-31 17:59:25 +0000248
drhadbca9c2001-09-27 15:11:53 +0000249# Execute SQL and catch exceptions.
250#
251proc catchsql {sql {db db}} {
252 # puts "SQL = $sql"
253 set r [catch {$db eval $sql} msg]
254 lappend r $msg
255 return $r
256}
257
drh04096482001-11-09 22:41:44 +0000258# Do an VDBE code dump on the SQL given
259#
260proc explain {sql {db db}} {
261 puts ""
262 puts "addr opcode p1 p2 p3 "
263 puts "---- ------------ ------ ------ ---------------"
264 $db eval "explain $sql" {} {
265 puts [format {%-4d %-12.12s %-6d %-6d %s} $addr $opcode $p1 $p2 $p3]
266 }
267}
268
drh3aadb2e2000-05-31 17:59:25 +0000269# Another procedure to execute SQL. This one includes the field
270# names in the returned list.
271#
272proc execsql2 {sql} {
273 set result {}
274 db eval $sql data {
275 foreach f $data(*) {
276 lappend result $f $data($f)
277 }
278 }
279 return $result
280}
drh17a68932001-01-31 13:28:08 +0000281
drhdde85d92003-03-01 19:45:34 +0000282# Use the non-callback API to execute multiple SQL statements
283#
284proc stepsql {dbptr sql} {
285 set sql [string trim $sql]
286 set r 0
287 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +0000288 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +0000289 return [list 1 $vm]
290 }
291 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +0000292# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
293# foreach v $VAL {lappend r $v}
294# }
295 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
296 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
297 lappend r [sqlite3_column_text $vm $i]
298 }
drhdde85d92003-03-01 19:45:34 +0000299 }
danielk1977106bb232004-05-21 10:08:53 +0000300 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +0000301 return [list 1 $errmsg]
302 }
303 }
304 return $r
305}
306
drh17a68932001-01-31 13:28:08 +0000307# Delete a file or directory
308#
309proc forcedelete {filename} {
310 if {[catch {file delete -force $filename}]} {
311 exec rm -rf $filename
312 }
313}
drh21504322002-06-25 13:16:02 +0000314
315# Do an integrity check of the entire database
316#
317proc integrity_check {name} {
drh27d258a2004-10-30 20:23:09 +0000318 ifcapable integrityck {
319 do_test $name {
320 execsql {PRAGMA integrity_check}
321 } {ok}
322 }
323}
324
325# Evaluate a boolean expression of capabilities. If true, execute the
326# code. Omit the code if false.
327#
danielk19773e8c37e2005-01-21 03:12:14 +0000328proc ifcapable {expr code {else ""} {elsecode ""}} {
drh5436dc22004-11-14 04:04:17 +0000329 regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
danielk19773e8c37e2005-01-21 03:12:14 +0000330 if ($e2) {
331 set c [catch {uplevel 1 $code} r]
332 } else {
333 set c [catch {uplevel 1 $elsecode} r]
334 }
335 return -code $c $r
drh21504322002-06-25 13:16:02 +0000336}
danielk197745901d62004-11-10 15:27:38 +0000337
danielk1977aca790a2005-01-13 11:07:52 +0000338# This proc execs a seperate process that crashes midway through executing
339# the SQL script $sql on database test.db.
340#
341# The crash occurs during a sync() of file $crashfile. When the crash
342# occurs a random subset of all unsynced writes made by the process are
343# written into the files on disk. Argument $crashdelay indicates the
344# number of file syncs to wait before crashing.
345#
346# The return value is a list of two elements. The first element is a
347# boolean, indicating whether or not the process actually crashed or
348# reported some other error. The second element in the returned list is the
349# error message. This is "child process exited abnormally" if the crash
350# occured.
351#
danielk197759a33f92007-03-17 10:26:59 +0000352# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE $sql
353#
354proc crashsql {args} {
danielk1977aca790a2005-01-13 11:07:52 +0000355 if {$::tcl_platform(platform)!="unix"} {
356 error "crashsql should only be used on unix"
357 }
danielk197759a33f92007-03-17 10:26:59 +0000358
359 set blocksize ""
360 set crashdelay 1
361 set crashfile ""
362 set sql [lindex $args end]
363
364 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
365 set z [lindex $args $ii]
366 set n [string length $z]
367 set z2 [lindex $args [expr $ii+1]]
368
369 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
370 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
danielk1977967a4a12007-08-20 14:23:44 +0000371 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk197759a33f92007-03-17 10:26:59 +0000372 else { error "Unrecognized option: $z" }
373 }
374
375 if {$crashfile eq ""} {
376 error "Compulsory option -file missing"
377 }
378
danielk1977aca790a2005-01-13 11:07:52 +0000379 set cfile [file join [pwd] $crashfile]
380
381 set f [open crash.tcl w]
danielk1977967a4a12007-08-20 14:23:44 +0000382 puts $f "sqlite3_crashparams $blocksize $crashdelay $cfile"
danielk1977933bbd62007-03-17 07:22:42 +0000383 puts $f "set sqlite_pending_byte $::sqlite_pending_byte"
danielk1977aca790a2005-01-13 11:07:52 +0000384 puts $f "sqlite3 db test.db"
danielk1977933bbd62007-03-17 07:22:42 +0000385
386 # This block sets the cache size of the main database to 10
387 # pages. This is done in case the build is configured to omit
388 # "PRAGMA cache_size".
389 puts $f {db eval {SELECT * FROM sqlite_master;}}
390 puts $f {set bt [btree_from_db db]}
391 puts $f {btree_set_cache_size $bt 10}
392
danielk1977aca790a2005-01-13 11:07:52 +0000393 puts $f "db eval {"
394 puts $f "$sql"
395 puts $f "}"
396 close $f
397
398 set r [catch {
drh9c06c952005-11-26 00:25:00 +0000399 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +0000400 } msg]
401 lappend r $msg
402}
403
danielk197732554c12005-01-22 03:39:39 +0000404# Usage: do_ioerr_test <test number> <options...>
405#
406# This proc is used to implement test cases that check that IO errors
407# are correctly handled. The first argument, <test number>, is an integer
408# used to name the tests executed by this proc. Options are as follows:
409#
410# -tclprep TCL script to run to prepare test.
411# -sqlprep SQL script to run to prepare test.
412# -tclbody TCL script to run with IO error simulation.
413# -sqlbody TCL script to run with IO error simulation.
414# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +0000415# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +0000416# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +0000417# -start Value of 'N' to begin with (default 1)
418#
419# -cksum Boolean. If true, test that the database does
420# not change during the execution of the test case.
421#
422proc do_ioerr_test {testname args} {
423
danielk197732554c12005-01-22 03:39:39 +0000424 set ::ioerropts(-start) 1
425 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +0000426 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +0000427 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +0000428 set ::ioerropts(-persist) 1
danielk197732554c12005-01-22 03:39:39 +0000429 array set ::ioerropts $args
430
431 set ::go 1
432 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +0000433 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +0000434 incr ::ioerropts(-count) -1
435 if {$::ioerropts(-count)<0} break
danielk197732554c12005-01-22 03:39:39 +0000436
437 # Skip this IO error if it was specified with the "-exclude" option.
438 if {[info exists ::ioerropts(-exclude)]} {
439 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
440 }
441
442 # Delete the files test.db and test2.db, then execute the TCL and
443 # SQL (in that order) to prepare for the test case.
444 do_test $testname.$n.1 {
445 set ::sqlite_io_error_pending 0
446 catch {db close}
447 catch {file delete -force test.db}
448 catch {file delete -force test.db-journal}
449 catch {file delete -force test2.db}
450 catch {file delete -force test2.db-journal}
drha34c62d2006-01-06 22:11:20 +0000451 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
drh4ac285a2006-09-15 07:28:50 +0000452 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
danielk197732554c12005-01-22 03:39:39 +0000453 if {[info exists ::ioerropts(-tclprep)]} {
454 eval $::ioerropts(-tclprep)
455 }
456 if {[info exists ::ioerropts(-sqlprep)]} {
457 execsql $::ioerropts(-sqlprep)
458 }
459 expr 0
460 } {0}
461
462 # Read the 'checksum' of the database.
463 if {$::ioerropts(-cksum)} {
464 set checksum [cksum]
465 }
466
467 # Set the Nth IO error to fail.
468 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +0000469 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +0000470 set ::sqlite_io_error_pending $n
471 }] $n
472
473 # Create a single TCL script from the TCL and SQL specified
474 # as the body of the test.
475 set ::ioerrorbody {}
476 if {[info exists ::ioerropts(-tclbody)]} {
477 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
478 }
479 if {[info exists ::ioerropts(-sqlbody)]} {
480 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
481 }
482
483 # Execute the TCL Script created in the above block. If
484 # there are at least N IO operations performed by SQLite as
485 # a result of the script, the Nth will fail.
486 do_test $testname.$n.3 {
487 set r [catch $::ioerrorbody msg]
drhe49f9822006-09-15 12:29:16 +0000488 set rc [sqlite3_errcode $::DB]
489 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +0000490 # If we are in extended result code mode, make sure all of the
491 # IOERRs we get back really do have their extended code values.
492 # If an extended result code is returned, the sqlite3_errcode
493 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
494 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +0000495 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
496 return $rc
497 }
498 } else {
drh5f7b5bf2007-04-19 12:30:54 +0000499 # If we are not in extended result code mode, make sure no
500 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +0000501 if {[regexp {\+\d} $rc]} {
502 return $rc
503 }
504 }
drh5f7b5bf2007-04-19 12:30:54 +0000505 # The test repeats as long as $::go is true.
danielk197732554c12005-01-22 03:39:39 +0000506 set ::go [expr {$::sqlite_io_error_pending<=0}]
drhc9ac5ca2005-11-04 22:03:30 +0000507 set s [expr $::sqlite_io_error_hit==0]
danielk1977f2fa8312006-01-24 13:09:33 +0000508 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +0000509
510 # One of two things must have happened. either
511 # 1. We never hit the IO error and the SQL returned OK
512 # 2. An IO error was hit and the SQL failed
513 #
danielk1977f2fa8312006-01-24 13:09:33 +0000514 expr { ($s && !$r && !$::go) || (!$s && $r && $::go) }
danielk197732554c12005-01-22 03:39:39 +0000515 } {1}
516
517 # If an IO error occured, then the checksum of the database should
518 # be the same as before the script that caused the IO error was run.
519 if {$::go && $::ioerropts(-cksum)} {
520 do_test $testname.$n.4 {
521 catch {db close}
drha34c62d2006-01-06 22:11:20 +0000522 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danielk197732554c12005-01-22 03:39:39 +0000523 cksum
524 } $checksum
525 }
526
danielk1977c4da5b92006-01-21 12:08:54 +0000527 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +0000528 if {[info exists ::ioerropts(-cleanup)]} {
529 catch $::ioerropts(-cleanup)
530 }
danielk197732554c12005-01-22 03:39:39 +0000531 }
532 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +0000533 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +0000534 unset ::ioerropts
535}
536
537# Return a checksum based on the contents of database 'db'.
538#
539proc cksum {{db db}} {
540 set txt [$db eval {
541 SELECT name, type, sql FROM sqlite_master order by name
542 }]\n
543 foreach tbl [$db eval {
544 SELECT name FROM sqlite_master WHERE type='table' order by name
545 }] {
546 append txt [$db eval "SELECT * FROM $tbl"]\n
547 }
548 foreach prag {default_synchronous default_cache_size} {
549 append txt $prag-[$db eval "PRAGMA $prag"]\n
550 }
551 set cksum [string length $txt]-[md5 $txt]
552 # puts $cksum-[file size test.db]
553 return $cksum
554}
555
556# Copy file $from into $to. This is used because some versions of
557# TCL for windows (notably the 8.4.1 binary package shipped with the
558# current mingw release) have a broken "file copy" command.
559#
560proc copy_file {from to} {
561 if {$::tcl_platform(platform)=="unix"} {
562 file copy -force $from $to
563 } else {
564 set f [open $from]
565 fconfigure $f -translation binary
566 set t [open $to w]
567 fconfigure $t -translation binary
568 puts -nonewline $t [read $f [file size $from]]
569 close $t
570 close $f
571 }
572}
573
drhf3a65f72007-08-22 20:18:21 +0000574# This command checks for outstanding calls to sqlite3_malloc()
575# A list is returned with one entry for each outstanding
danielk19772e588c72005-12-09 14:25:08 +0000576# malloc. Each list entry is itself a list of 5 items, as follows:
577#
578# { <number-bytes> <file-name> <line-number> <test-case> <stack-dump> }
579#
580proc check_for_leaks {} {
581 set ret [list]
danielk1977aef0bf62005-12-30 16:28:01 +0000582 set cnt 0
danielk19772e588c72005-12-09 14:25:08 +0000583 foreach alloc [sqlite_malloc_outstanding] {
584 foreach {nBytes file iLine userstring backtrace} $alloc {}
585 set stack [list]
586 set skip 0
587
588 # The first command in this block will probably fail on windows. This
589 # means there will be no stack dump available.
danielk1977da184232006-01-05 11:34:32 +0000590 if {$cnt < 25 && $backtrace!=""} {
danielk1977aef0bf62005-12-30 16:28:01 +0000591 catch {
592 set stuff [eval "exec addr2line -e ./testfixture -f $backtrace"]
593 foreach {func line} $stuff {
594 if {$func != "??" || $line != "??:0"} {
595 regexp {.*/(.*)} $line dummy line
596 lappend stack "${func}() $line"
597 } else {
598 if {[lindex $stack end] != "..."} {
599 lappend stack "..."
600 }
danielk19772e588c72005-12-09 14:25:08 +0000601 }
602 }
603 }
danielk1977aef0bf62005-12-30 16:28:01 +0000604 incr cnt
danielk19772e588c72005-12-09 14:25:08 +0000605 }
606
607 if {!$skip} {
608 lappend ret [list $nBytes $file $iLine $userstring $stack]
609 }
610 }
611 return $ret
612}
613
614# Pretty print a report based on the return value of [check_for_leaks] to
615# stdout.
616proc pp_check_for_leaks {} {
617 set l [check_for_leaks]
618 set n 0
619 foreach leak $l {
620 foreach {nBytes file iLine userstring stack} $leak {}
621 puts "$nBytes bytes leaked at $file:$iLine ($userstring)"
622 foreach frame $stack {
623 puts " $frame"
624 }
625 incr n $nBytes
626 }
627 puts "Memory leaked: $n bytes in [llength $l] allocations"
628 puts ""
629}
630
danielk197745901d62004-11-10 15:27:38 +0000631# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
632# to non-zero, then set the global variable $AUTOVACUUM to 1.
633set AUTOVACUUM $sqlite_options(default_autovacuum)