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 | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame^] | 26 | # $Id: tester.tcl,v 1.10 2001/01/31 13:28:09 drh Exp $ |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 27 | |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 28 | # Create a test database |
| 29 | # |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 30 | if {![info exists dbprefix]} { |
| 31 | if {[info exists env(SQLITE_PREFIX)]} { |
| 32 | set dbprefix $env(SQLITE_PREFIX): |
| 33 | } else { |
| 34 | set dbprefix "gdbm:" |
| 35 | } |
| 36 | } |
| 37 | switch $dbprefix { |
| 38 | gdbm: { |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame^] | 39 | if {[catch {file delete -force testdb}]} { |
| 40 | exec rm -rf testdb |
| 41 | } |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 42 | file mkdir testdb |
| 43 | } |
| 44 | memory: { |
| 45 | # do nothing |
| 46 | } |
| 47 | } |
| 48 | sqlite db ${dbprefix}testdb |
drh | bec2bf4 | 2000-05-29 23:48:22 +0000 | [diff] [blame] | 49 | |
| 50 | # Abort early if this script has been run before. |
| 51 | # |
| 52 | if {[info exists nTest]} return |
| 53 | |
| 54 | # Set the test counters to zero |
| 55 | # |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 56 | set nErr 0 |
| 57 | set nTest 0 |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 58 | set skip_test 0 |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 59 | |
| 60 | # Invoke the do_test procedure to run a single test |
| 61 | # |
| 62 | proc do_test {name cmd expected} { |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 63 | global argv nErr nTest skip_test |
| 64 | if {$skip_test} { |
| 65 | set skip_test 0 |
| 66 | return |
| 67 | } |
| 68 | if {[llength $argv]==0} { |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 69 | set go 1 |
| 70 | } else { |
| 71 | set go 0 |
| 72 | foreach pattern $argv { |
| 73 | if {[string match $pattern $name]} { |
| 74 | set go 1 |
| 75 | break |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | if {!$go} return |
| 80 | incr nTest |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 81 | puts -nonewline $::dbprefix$name... |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 82 | flush stdout |
| 83 | if {[catch {uplevel #0 "$cmd;\n"} result]} { |
| 84 | puts "\nError: $result" |
| 85 | incr nErr |
| 86 | } elseif {[string compare $result $expected]} { |
| 87 | puts "\nExpected: \[$expected\]\n Got: \[$result\]" |
| 88 | incr nErr |
| 89 | } else { |
| 90 | puts " Ok" |
| 91 | } |
| 92 | } |
| 93 | |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 94 | # Skip a test based on the dbprefix |
| 95 | # |
| 96 | proc skipif {args} { |
| 97 | foreach a $args { |
| 98 | if {$::dbprefix==$a} { |
| 99 | set ::skip_test 1 |
| 100 | return |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | # Run the next test only if the dbprefix is among the listed arguments |
| 106 | # |
| 107 | proc testif {args} { |
| 108 | foreach a $args { |
| 109 | if {$::dbprefix==$a} { |
| 110 | set ::skip_test 0 |
| 111 | return |
| 112 | } |
| 113 | } |
| 114 | set ::skip_test 1 |
| 115 | } |
| 116 | |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 117 | # The procedure uses the special "--malloc-stats--" macro of SQLite |
| 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 {} { |
| 125 | set r [execsql {--malloc-stats--}] |
| 126 | if {$r==""} return |
| 127 | set ::Leak [expr {[lindex $r 0]-[lindex $r 1]}] |
| 128 | # puts "*** $::Leak mallocs have not been freed ***" |
| 129 | } |
| 130 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 131 | # Run this routine last |
| 132 | # |
| 133 | proc finish_test {} { |
| 134 | global nTest nErr |
drh | 8c82b35 | 2000-12-10 18:23:50 +0000 | [diff] [blame] | 135 | memleak_check |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 136 | catch {db close} |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 137 | puts "$nErr errors out of $nTest tests" |
| 138 | exit $nErr |
| 139 | } |
| 140 | |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 141 | # A procedure to execute SQL |
| 142 | # |
| 143 | proc execsql {sql} { |
drh | acbcdc4 | 2001-01-22 00:31:53 +0000 | [diff] [blame] | 144 | # puts "SQL = $sql" |
drh | 6d31316 | 2000-09-21 13:01:35 +0000 | [diff] [blame] | 145 | return [db eval $sql] |
drh | 348784e | 2000-05-29 20:41:49 +0000 | [diff] [blame] | 146 | } |
drh | 3aadb2e | 2000-05-31 17:59:25 +0000 | [diff] [blame] | 147 | |
| 148 | # Another procedure to execute SQL. This one includes the field |
| 149 | # names in the returned list. |
| 150 | # |
| 151 | proc execsql2 {sql} { |
| 152 | set result {} |
| 153 | db eval $sql data { |
| 154 | foreach f $data(*) { |
| 155 | lappend result $f $data($f) |
| 156 | } |
| 157 | } |
| 158 | return $result |
| 159 | } |
drh | 17a6893 | 2001-01-31 13:28:08 +0000 | [diff] [blame^] | 160 | |
| 161 | # Delete a file or directory |
| 162 | # |
| 163 | proc forcedelete {filename} { |
| 164 | if {[catch {file delete -force $filename}]} { |
| 165 | exec rm -rf $filename |
| 166 | } |
| 167 | } |