blob: c235a8c181efb0b2fd66b94d6ffcdbe446d58a0b [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#
drhacbcdc42001-01-22 00:31:53 +000026# $Id: tester.tcl,v 1.9 2001/01/22 00:31:53 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: {
39 file delete -force testdb
40 file mkdir testdb
41 }
42 memory: {
43 # do nothing
44 }
45}
46sqlite db ${dbprefix}testdb
drhbec2bf42000-05-29 23:48:22 +000047
48# Abort early if this script has been run before.
49#
50if {[info exists nTest]} return
51
52# Set the test counters to zero
53#
drh348784e2000-05-29 20:41:49 +000054set nErr 0
55set nTest 0
drh767c2002000-10-19 14:10:08 +000056set skip_test 0
drh348784e2000-05-29 20:41:49 +000057
58# Invoke the do_test procedure to run a single test
59#
60proc do_test {name cmd expected} {
drh767c2002000-10-19 14:10:08 +000061 global argv nErr nTest skip_test
62 if {$skip_test} {
63 set skip_test 0
64 return
65 }
66 if {[llength $argv]==0} {
drh348784e2000-05-29 20:41:49 +000067 set go 1
68 } else {
69 set go 0
70 foreach pattern $argv {
71 if {[string match $pattern $name]} {
72 set go 1
73 break
74 }
75 }
76 }
77 if {!$go} return
78 incr nTest
drh767c2002000-10-19 14:10:08 +000079 puts -nonewline $::dbprefix$name...
drh348784e2000-05-29 20:41:49 +000080 flush stdout
81 if {[catch {uplevel #0 "$cmd;\n"} result]} {
82 puts "\nError: $result"
83 incr nErr
84 } elseif {[string compare $result $expected]} {
85 puts "\nExpected: \[$expected\]\n Got: \[$result\]"
86 incr nErr
87 } else {
88 puts " Ok"
89 }
90}
91
drh767c2002000-10-19 14:10:08 +000092# Skip a test based on the dbprefix
93#
94proc skipif {args} {
95 foreach a $args {
96 if {$::dbprefix==$a} {
97 set ::skip_test 1
98 return
99 }
100 }
101}
102
103# Run the next test only if the dbprefix is among the listed arguments
104#
105proc testif {args} {
106 foreach a $args {
107 if {$::dbprefix==$a} {
108 set ::skip_test 0
109 return
110 }
111 }
112 set ::skip_test 1
113}
114
drh8c82b352000-12-10 18:23:50 +0000115# The procedure uses the special "--malloc-stats--" macro of SQLite
116# (which is only available if SQLite is compiled with -DMEMORY_DEBUG=1)
117# to see how many malloc()s have not been free()ed. The number
118# of surplus malloc()s is stored in the global variable $::Leak.
119# If the value in $::Leak grows, it may mean there is a memory leak
120# in the library.
121#
122proc memleak_check {} {
123 set r [execsql {--malloc-stats--}]
124 if {$r==""} return
125 set ::Leak [expr {[lindex $r 0]-[lindex $r 1]}]
126 # puts "*** $::Leak mallocs have not been freed ***"
127}
128
drh348784e2000-05-29 20:41:49 +0000129# Run this routine last
130#
131proc finish_test {} {
132 global nTest nErr
drh8c82b352000-12-10 18:23:50 +0000133 memleak_check
drh6e142f52000-06-08 13:36:40 +0000134 catch {db close}
drh348784e2000-05-29 20:41:49 +0000135 puts "$nErr errors out of $nTest tests"
136 exit $nErr
137}
138
drh348784e2000-05-29 20:41:49 +0000139# A procedure to execute SQL
140#
141proc execsql {sql} {
drhacbcdc42001-01-22 00:31:53 +0000142 # puts "SQL = $sql"
drh6d313162000-09-21 13:01:35 +0000143 return [db eval $sql]
drh348784e2000-05-29 20:41:49 +0000144}
drh3aadb2e2000-05-31 17:59:25 +0000145
146# Another procedure to execute SQL. This one includes the field
147# names in the returned list.
148#
149proc execsql2 {sql} {
150 set result {}
151 db eval $sql data {
152 foreach f $data(*) {
153 lappend result $f $data($f)
154 }
155 }
156 return $result
157}