drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 1 | # Copyright (c) 1999, 2000 D. Richard Hipp |
| 2 | # |
| 3 | # This program is free software; you can redistribute it and/or |
| 4 | # modify it under the terms of the GNU General Public |
| 5 | # License as published by the Free Software Foundation; either |
| 6 | # version 2 of the License, or (at your option) any later version. |
| 7 | # |
| 8 | # This program is distributed in the hope that it will be useful, |
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | # General Public License for more details. |
| 12 | # |
| 13 | # You should have received a copy of the GNU General Public |
| 14 | # License along with this library; if not, write to the |
| 15 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | # Boston, MA 02111-1307, USA. |
| 17 | # |
| 18 | # Author contact information: |
| 19 | # drh@hwaci.com |
| 20 | # http://www.hwaci.com/drh/ |
| 21 | # |
| 22 | #*********************************************************************** |
| 23 | # This file implements some common TCL routines used for regression |
| 24 | # testing the SQLite library |
| 25 | # |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame^] | 26 | # $Id: tester.tcl,v 1.17 2001/09/14 16:42:13 drh Exp $ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 27 | |
| 28 | # Make sure tclsqlite was compiled correctly. Abort now with an |
| 29 | # error message if not. |
| 30 | # |
| 31 | if {[sqlite -tcl-uses-utf]} { |
| 32 | if {"\u1234"=="u1234"} { |
| 33 | puts stderr "***** BUILD PROBLEM *****" |
| 34 | puts stderr "$argv0 was linked against an older version" |
| 35 | puts stderr "of TCL that does not support Unicode, but uses a header" |
| 36 | puts stderr "file (\"tcl.h\") from a new TCL version that does support" |
| 37 | puts stderr "Unicode. This combination causes internal errors." |
| 38 | puts stderr "Recompile using a TCL library and header file that match" |
| 39 | puts stderr "and try again.\n**************************" |
| 40 | exit 1 |
| 41 | } |
| 42 | } else { |
| 43 | if {"\u1234"!="u1234"} { |
| 44 | puts stderr "***** BUILD PROBLEM *****" |
| 45 | puts stderr "$argv0 was linked against an newer version" |
| 46 | puts stderr "of TCL that supports Unicode, but uses a header file" |
| 47 | puts stderr "(\"tcl.h\") from a old TCL version that does not support" |
| 48 | puts stderr "Unicode. This combination causes internal errors." |
| 49 | puts stderr "Recompile using a TCL library and header file that match" |
| 50 | puts stderr "and try again.\n**************************" |
| 51 | exit 1 |
| 52 | } |
| 53 | } |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 54 | |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 55 | # Create a test database |
| 56 | # |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 57 | file delete -force ./test.db |
| 58 | file delete -force ./test.db-journal |
| 59 | sqlite db ./test.db |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 60 | |
| 61 | # Abort early if this script has been run before. |
| 62 | # |
| 63 | if {[info exists nTest]} return |
| 64 | |
| 65 | # Set the test counters to zero |
| 66 | # |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 67 | set nErr 0 |
| 68 | set nTest 0 |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 69 | set nProb 0 |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 70 | set skip_test 0 |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame^] | 71 | set failList {} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 72 | |
| 73 | # Invoke the do_test procedure to run a single test |
| 74 | # |
| 75 | proc do_test {name cmd expected} { |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 76 | global argv nErr nTest skip_test |
| 77 | if {$skip_test} { |
| 78 | set skip_test 0 |
| 79 | return |
| 80 | } |
| 81 | if {[llength $argv]==0} { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 82 | set go 1 |
| 83 | } else { |
| 84 | set go 0 |
| 85 | foreach pattern $argv { |
| 86 | if {[string match $pattern $name]} { |
| 87 | set go 1 |
| 88 | break |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | if {!$go} return |
| 93 | incr nTest |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 94 | puts -nonewline $name... |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 95 | flush stdout |
| 96 | if {[catch {uplevel #0 "$cmd;\n"} result]} { |
| 97 | puts "\nError: $result" |
| 98 | incr nErr |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame^] | 99 | lappend ::failList $name |
| 100 | if {$nErr>10} {puts "*** Giving up..."; finalize_testing} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 101 | } elseif {[string compare $result $expected]} { |
| 102 | puts "\nExpected: \[$expected\]\n Got: \[$result\]" |
| 103 | incr nErr |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame^] | 104 | lappend ::failList $name |
| 105 | if {$nErr>10} {puts "*** Giving up..."; finalize_testing} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 106 | } else { |
| 107 | puts " Ok" |
| 108 | } |
| 109 | } |
| 110 | |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 111 | # Invoke this procedure on a test that is probabilistic |
| 112 | # and might fail sometimes. |
| 113 | # |
| 114 | proc do_probtest {name cmd expected} { |
| 115 | global argv nProb nTest skip_test |
| 116 | if {$skip_test} { |
| 117 | set skip_test 0 |
| 118 | return |
| 119 | } |
| 120 | if {[llength $argv]==0} { |
| 121 | set go 1 |
| 122 | } else { |
| 123 | set go 0 |
| 124 | foreach pattern $argv { |
| 125 | if {[string match $pattern $name]} { |
| 126 | set go 1 |
| 127 | break |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | if {!$go} return |
| 132 | incr nTest |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 133 | puts -nonewline $name... |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 134 | flush stdout |
| 135 | if {[catch {uplevel #0 "$cmd;\n"} result]} { |
| 136 | puts "\nError: $result" |
| 137 | incr nErr |
| 138 | } elseif {[string compare $result $expected]} { |
| 139 | puts "\nExpected: \[$expected\]\n Got: \[$result\]" |
| 140 | puts "NOTE: The results of the previous test depend on system load" |
| 141 | puts "and processor speed. The test may sometimes fail even if the" |
| 142 | puts "library is working correctly." |
| 143 | incr nProb |
| 144 | } else { |
| 145 | puts " Ok" |
| 146 | } |
| 147 | } |
| 148 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 149 | # The procedure uses the special "sqlite_malloc_stat" command |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 150 | # (which is only available if SQLite is compiled with -DMEMORY_DEBUG=1) |
| 151 | # to see how many malloc()s have not been free()ed. The number |
| 152 | # of surplus malloc()s is stored in the global variable $::Leak. |
| 153 | # If the value in $::Leak grows, it may mean there is a memory leak |
| 154 | # in the library. |
| 155 | # |
| 156 | proc memleak_check {} { |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 157 | if {[info command sqlite_malloc_stat]!=""} { |
| 158 | set r [sqlite_malloc_stat] |
| 159 | set ::Leak [expr {[lindex $r 0]-[lindex $r 1]}] |
| 160 | } |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 161 | } |
| 162 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 163 | # Run this routine last |
| 164 | # |
| 165 | proc finish_test {} { |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame^] | 166 | finalize_testing |
| 167 | } |
| 168 | proc finalize_testing {} { |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 169 | global nTest nErr nProb |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame^] | 170 | if {$nErr==0} memleak_check |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 171 | catch {db close} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 172 | puts "$nErr errors out of $nTest tests" |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame^] | 173 | puts "Failures on these tests: $::failList" |
drh | db25e38 | 2001-03-15 18:21:22 +0000 | [diff] [blame] | 174 | if {$nProb>0} { |
| 175 | puts "$nProb probabilistic tests also failed, but this does" |
| 176 | puts "not necessarily indicate a malfunction." |
| 177 | } |
| 178 | exit [expr {$nErr>0}] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 179 | } |
| 180 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 181 | # A procedure to execute SQL |
| 182 | # |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 183 | proc execsql {sql {db db}} { |
drh | acbcdc4 | 2001-01-22 00:31:53 +0000 | [diff] [blame] | 184 | # puts "SQL = $sql" |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 185 | return [$db eval $sql] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 186 | } |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 187 | |
| 188 | # Another procedure to execute SQL. This one includes the field |
| 189 | # names in the returned list. |
| 190 | # |
| 191 | proc execsql2 {sql} { |
| 192 | set result {} |
| 193 | db eval $sql data { |
| 194 | foreach f $data(*) { |
| 195 | lappend result $f $data($f) |
| 196 | } |
| 197 | } |
| 198 | return $result |
| 199 | } |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame] | 200 | |
| 201 | # Delete a file or directory |
| 202 | # |
| 203 | proc forcedelete {filename} { |
| 204 | if {[catch {file delete -force $filename}]} { |
| 205 | exec rm -rf $filename |
| 206 | } |
| 207 | } |