blob: 013818dd851464bc00f2d94687a01ac4788c84ab [file] [log] [blame]
drh348784e2000-05-29 20:41:49 +00001# 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#
drh17a68932001-01-31 13:28:08 +000026# $Id: tester.tcl,v 1.10 2001/01/31 13:28:09 drh Exp $
drh348784e2000-05-29 20:41:49 +000027
drhbec2bf42000-05-29 23:48:22 +000028# Create a test database
29#
drh767c2002000-10-19 14:10:08 +000030if {![info exists dbprefix]} {
31 if {[info exists env(SQLITE_PREFIX)]} {
32 set dbprefix $env(SQLITE_PREFIX):
33 } else {
34 set dbprefix "gdbm:"
35 }
36}
37switch $dbprefix {
38 gdbm: {
drh17a68932001-01-31 13:28:08 +000039 if {[catch {file delete -force testdb}]} {
40 exec rm -rf testdb
41 }
drh767c2002000-10-19 14:10:08 +000042 file mkdir testdb
43 }
44 memory: {
45 # do nothing
46 }
47}
48sqlite db ${dbprefix}testdb
drhbec2bf42000-05-29 23:48:22 +000049
50# Abort early if this script has been run before.
51#
52if {[info exists nTest]} return
53
54# Set the test counters to zero
55#
drh348784e2000-05-29 20:41:49 +000056set nErr 0
57set nTest 0
drh767c2002000-10-19 14:10:08 +000058set skip_test 0
drh348784e2000-05-29 20:41:49 +000059
60# Invoke the do_test procedure to run a single test
61#
62proc do_test {name cmd expected} {
drh767c2002000-10-19 14:10:08 +000063 global argv nErr nTest skip_test
64 if {$skip_test} {
65 set skip_test 0
66 return
67 }
68 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +000069 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
drh767c2002000-10-19 14:10:08 +000081 puts -nonewline $::dbprefix$name...
drh348784e2000-05-29 20:41:49 +000082 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
drh767c2002000-10-19 14:10:08 +000094# Skip a test based on the dbprefix
95#
96proc 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#
107proc 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
drh8c82b352000-12-10 18:23:50 +0000117# 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#
124proc 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
drh348784e2000-05-29 20:41:49 +0000131# Run this routine last
132#
133proc finish_test {} {
134 global nTest nErr
drh8c82b352000-12-10 18:23:50 +0000135 memleak_check
drh6e142f52000-06-08 13:36:40 +0000136 catch {db close}
drh348784e2000-05-29 20:41:49 +0000137 puts "$nErr errors out of $nTest tests"
138 exit $nErr
139}
140
drh348784e2000-05-29 20:41:49 +0000141# A procedure to execute SQL
142#
143proc execsql {sql} {
drhacbcdc42001-01-22 00:31:53 +0000144 # puts "SQL = $sql"
drh6d313162000-09-21 13:01:35 +0000145 return [db eval $sql]
drh348784e2000-05-29 20:41:49 +0000146}
drh3aadb2e2000-05-31 17:59:25 +0000147
148# Another procedure to execute SQL. This one includes the field
149# names in the returned list.
150#
151proc 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}
drh17a68932001-01-31 13:28:08 +0000160
161# Delete a file or directory
162#
163proc forcedelete {filename} {
164 if {[catch {file delete -force $filename}]} {
165 exec rm -rf $filename
166 }
167}