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 | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame^] | 14 | # $Id: tester.tcl,v 1.37 2004/06/19 00:16:31 drh Exp $ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 15 | |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame^] | 16 | # Make sure tclsqlite3 was compiled correctly. Abort now with an |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 17 | # error message if not. |
| 18 | # |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame^] | 19 | if {[sqlite3 -tcl-uses-utf]} { |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 20 | 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 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 42 | |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 43 | # Use the pager codec if it is available |
| 44 | # |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame^] | 45 | if {[sqlite3 -has-codec] && [info command sqlite_orig]==""} { |
| 46 | rename sqlite3 sqlite_orig |
| 47 | proc sqlite3 {args} { |
drh | 9eb9e26 | 2004-02-11 02:18:05 +0000 | [diff] [blame] | 48 | if {[llength $args]==2 && [string index [lindex $args 0] 0]!="-"} { |
| 49 | lappend args -key {xyzzy} |
| 50 | } |
| 51 | uplevel 1 sqlite_orig $args |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 56 | # Create a test database |
| 57 | # |
drh | 254cba2 | 2001-09-20 01:44:42 +0000 | [diff] [blame] | 58 | catch {db close} |
| 59 | file delete -force test.db |
| 60 | file delete -force test.db-journal |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame^] | 61 | sqlite3 db ./test.db |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 62 | if {[info exists ::SETUP_SQL]} { |
| 63 | db eval $::SETUP_SQL |
| 64 | } |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 65 | |
| 66 | # Abort early if this script has been run before. |
| 67 | # |
| 68 | if {[info exists nTest]} return |
| 69 | |
| 70 | # Set the test counters to zero |
| 71 | # |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 72 | set nErr 0 |
| 73 | set nTest 0 |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 74 | set nProb 0 |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 75 | set skip_test 0 |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 76 | set failList {} |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 77 | set maxErr 1000 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 78 | |
| 79 | # Invoke the do_test procedure to run a single test |
| 80 | # |
| 81 | proc do_test {name cmd expected} { |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 82 | global argv nErr nTest skip_test maxErr |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 83 | if {$skip_test} { |
| 84 | set skip_test 0 |
| 85 | return |
| 86 | } |
| 87 | if {[llength $argv]==0} { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 88 | set go 1 |
| 89 | } else { |
| 90 | set go 0 |
| 91 | foreach pattern $argv { |
| 92 | if {[string match $pattern $name]} { |
| 93 | set go 1 |
| 94 | break |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | if {!$go} return |
| 99 | incr nTest |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 100 | puts -nonewline $name... |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 101 | flush stdout |
| 102 | if {[catch {uplevel #0 "$cmd;\n"} result]} { |
| 103 | puts "\nError: $result" |
| 104 | incr nErr |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 105 | lappend ::failList $name |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 106 | if {$nErr>$maxErr} {puts "*** Giving up..."; finalize_testing} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 107 | } elseif {[string compare $result $expected]} { |
| 108 | puts "\nExpected: \[$expected\]\n Got: \[$result\]" |
| 109 | incr nErr |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 110 | lappend ::failList $name |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 111 | if {$nErr>=$maxErr} {puts "*** Giving up..."; finalize_testing} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 112 | } else { |
| 113 | puts " Ok" |
| 114 | } |
| 115 | } |
| 116 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 117 | # The procedure uses the special "sqlite_malloc_stat" command |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 118 | # (which is only available if SQLite is compiled with -DMEMORY_DEBUG=1) |
| 119 | # to see how many malloc()s have not been free()ed. The number |
| 120 | # of surplus malloc()s is stored in the global variable $::Leak. |
| 121 | # If the value in $::Leak grows, it may mean there is a memory leak |
| 122 | # in the library. |
| 123 | # |
| 124 | proc memleak_check {} { |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 125 | if {[info command sqlite_malloc_stat]!=""} { |
| 126 | set r [sqlite_malloc_stat] |
| 127 | set ::Leak [expr {[lindex $r 0]-[lindex $r 1]}] |
| 128 | } |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 129 | } |
| 130 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 131 | # Run this routine last |
| 132 | # |
| 133 | proc finish_test {} { |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 134 | finalize_testing |
| 135 | } |
| 136 | proc finalize_testing {} { |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 137 | global nTest nErr nProb sqlite_open_file_count |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 138 | if {$nErr==0} memleak_check |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 139 | catch {db close} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 140 | puts "$nErr errors out of $nTest tests" |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 141 | puts "Failures on these tests: $::failList" |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 142 | if {$nProb>0} { |
| 143 | puts "$nProb probabilistic tests also failed, but this does" |
| 144 | puts "not necessarily indicate a malfunction." |
| 145 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 146 | if 0 { |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 147 | if {$sqlite_open_file_count} { |
| 148 | puts "$sqlite_open_file_count files were left open" |
| 149 | incr nErr |
| 150 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 151 | } |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 152 | exit [expr {$nErr>0}] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 153 | } |
| 154 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 155 | # A procedure to execute SQL |
| 156 | # |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 157 | proc execsql {sql {db db}} { |
drh | acbcdc4 | 2001-01-22 00:31:53 +0000 | [diff] [blame] | 158 | # puts "SQL = $sql" |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 159 | return [$db eval $sql] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 160 | } |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 161 | |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 162 | # Execute SQL and catch exceptions. |
| 163 | # |
| 164 | proc catchsql {sql {db db}} { |
| 165 | # puts "SQL = $sql" |
| 166 | set r [catch {$db eval $sql} msg] |
| 167 | lappend r $msg |
| 168 | return $r |
| 169 | } |
| 170 | |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 171 | # Do an VDBE code dump on the SQL given |
| 172 | # |
| 173 | proc explain {sql {db db}} { |
| 174 | puts "" |
| 175 | puts "addr opcode p1 p2 p3 " |
| 176 | puts "---- ------------ ------ ------ ---------------" |
| 177 | $db eval "explain $sql" {} { |
| 178 | puts [format {%-4d %-12.12s %-6d %-6d %s} $addr $opcode $p1 $p2 $p3] |
| 179 | } |
| 180 | } |
| 181 | |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 182 | # Another procedure to execute SQL. This one includes the field |
| 183 | # names in the returned list. |
| 184 | # |
| 185 | proc execsql2 {sql} { |
| 186 | set result {} |
| 187 | db eval $sql data { |
| 188 | foreach f $data(*) { |
| 189 | lappend result $f $data($f) |
| 190 | } |
| 191 | } |
| 192 | return $result |
| 193 | } |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 194 | |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 195 | # Use the non-callback API to execute multiple SQL statements |
| 196 | # |
| 197 | proc stepsql {dbptr sql} { |
| 198 | set sql [string trim $sql] |
| 199 | set r 0 |
| 200 | while {[string length $sql]>0} { |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 201 | if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 202 | return [list 1 $vm] |
| 203 | } |
| 204 | set sql [string trim $sqltail] |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 205 | # while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} { |
| 206 | # foreach v $VAL {lappend r $v} |
| 207 | # } |
| 208 | while {[sqlite3_step $vm]=="SQLITE_ROW"} { |
| 209 | for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} { |
| 210 | lappend r [sqlite3_column_text $vm $i] |
| 211 | } |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 212 | } |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 213 | if {[catch {sqlite3_finalize $vm} errmsg]} { |
drh | dde85d9 | 2003-03-01 19:45:34 +0000 | [diff] [blame] | 214 | return [list 1 $errmsg] |
| 215 | } |
| 216 | } |
| 217 | return $r |
| 218 | } |
| 219 | |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 220 | # Delete a file or directory |
| 221 | # |
| 222 | proc forcedelete {filename} { |
| 223 | if {[catch {file delete -force $filename}]} { |
| 224 | exec rm -rf $filename |
| 225 | } |
| 226 | } |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 227 | |
| 228 | # Do an integrity check of the entire database |
| 229 | # |
| 230 | proc integrity_check {name} { |
| 231 | do_test $name { |
| 232 | execsql {PRAGMA integrity_check} |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 233 | } {ok} |
drh | 2150432 | 2002-06-25 13:16:02 +0000 | [diff] [blame] | 234 | } |