Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/compExpr.test @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 16.2 KB
Line 
1# This file contains a collection of tests for the procedures in the
2# file tclCompExpr.c.  Sourcing this file into Tcl runs the tests and
3# generates output for errors.  No output means no errors were found.
4#
5# Copyright (c) 1997 Sun Microsystems, Inc.
6# Copyright (c) 1998-1999 by Scriptics Corporation.
7#
8# See the file "license.terms" for information on usage and redistribution
9# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10#
11# RCS: @(#) $Id: compExpr.test,v 1.17 2008/01/16 21:54:33 dgp Exp $
12
13if {[lsearch [namespace children] ::tcltest] == -1} {
14    package require tcltest
15    namespace import -force ::tcltest::*
16}
17
18if {[catch {expr T1()} msg] && $msg eq {invalid command name "tcl::mathfunc::T1"}} {
19    testConstraint testmathfunctions 0
20} else {
21    testConstraint testmathfunctions 1
22}
23
24# Constrain memory leak tests
25testConstraint memory [llength [info commands memory]]
26
27catch {unset a}
28
29test compExpr-1.1 {TclCompileExpr procedure, successful expr parse and compile} {
30    expr 1+2
31} 3
32test compExpr-1.2 {TclCompileExpr procedure, error parsing expr} -body {
33    expr 1+2+
34} -returnCodes error -match glob -result *
35test compExpr-1.3 {TclCompileExpr procedure, error compiling expr} -body {
36    list [catch {expr "foo(123)"} msg] $msg
37} -match glob -result {1 {* "*foo"}}
38
39test compExpr-1.4 {TclCompileExpr procedure, expr has no operators} {
40    set a {0o00123}
41    expr {$a}
42} 83
43
44test compExpr-2.1 {CompileSubExpr procedure, TCL_TOKEN_WORD parse token} {
45    catch {unset a}
46    set a 27
47    expr {"foo$a" < "bar"}
48} 0
49test compExpr-2.2 {CompileSubExpr procedure, error compiling TCL_TOKEN_WORD parse token} -body {
50    expr {"00[expr 1+]" + 17}
51} -returnCodes error -match glob -result *
52test compExpr-2.3 {CompileSubExpr procedure, TCL_TOKEN_TEXT parse token} {
53    expr {{12345}}
54} 12345
55test compExpr-2.4 {CompileSubExpr procedure, empty TCL_TOKEN_TEXT parse token} {
56    expr {{}}
57} {}
58test compExpr-2.5 {CompileSubExpr procedure, TCL_TOKEN_BS parse token} {
59    expr "\{  \\
60 +123 \}"
61} 123
62test compExpr-2.6 {CompileSubExpr procedure, TCL_TOKEN_COMMAND parse token} {
63    expr {[info tclversion] != ""}
64} 1
65test compExpr-2.7 {CompileSubExpr procedure, TCL_TOKEN_COMMAND parse token} {
66    expr {[]}
67} {}
68test compExpr-2.8 {CompileSubExpr procedure, error in TCL_TOKEN_COMMAND parse token} -body {
69    expr {[foo "bar"xxx] + 17}
70} -returnCodes error -match glob -result *
71test compExpr-2.9 {CompileSubExpr procedure, TCL_TOKEN_VARIABLE parse token} {
72    catch {unset a}
73    set a 123
74    expr {$a*2}
75} 246
76test compExpr-2.10 {CompileSubExpr procedure, TCL_TOKEN_VARIABLE parse token} {
77    catch {unset a}
78    catch {unset b}
79    set a(george) martha
80    set b geo
81    expr {$a(${b}rge)}
82} martha
83test compExpr-2.11 {CompileSubExpr procedure, error in TCL_TOKEN_VARIABLE parse token} {
84    catch {unset a}
85    list [catch {expr {$a + 17}} msg] $msg
86} {1 {can't read "a": no such variable}}
87test compExpr-2.12 {CompileSubExpr procedure, TCL_TOKEN_SUB_EXPR parse token} {
88    expr {27||3? 3<<(1+4) : 4&&9}
89} 96
90test compExpr-2.13 {CompileSubExpr procedure, error in TCL_TOKEN_SUB_EXPR parse token} {
91    catch {unset a}
92    set a 15
93    list [catch {expr {27 || "$a[expr 1+]00"}} msg] $msg
94} {0 1}
95test compExpr-2.14 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, op found} {
96    expr {5*6}
97} 30
98test compExpr-2.15 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, math function found} {
99    format %.6g [expr {sin(2.0)}]
100} 0.909297
101test compExpr-2.16 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, math function not found} -body {
102    list [catch {expr {fred(2.0)}} msg] $msg
103} -match glob -result {1 {* "*fred"}}
104test compExpr-2.17 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
105    expr {4*2}
106} 8
107test compExpr-2.18 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
108    expr {4/2}
109} 2
110test compExpr-2.19 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
111    expr {4%2}
112} 0
113test compExpr-2.20 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
114    expr {4<<2}
115} 16
116test compExpr-2.21 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
117    expr {4>>2}
118} 1
119test compExpr-2.22 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
120    expr {4<2}
121} 0
122test compExpr-2.23 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
123    expr {4>2}
124} 1
125test compExpr-2.24 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
126    expr {4<=2}
127} 0
128test compExpr-2.25 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
129    expr {4>=2}
130} 1
131test compExpr-2.26 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
132    expr {4==2}
133} 0
134test compExpr-2.27 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
135    expr {4!=2}
136} 1
137test compExpr-2.28 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
138    expr {4&2}
139} 0
140test compExpr-2.29 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
141    expr {4^2}
142} 6
143test compExpr-2.30 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
144    expr {4|2}
145} 6
146test compExpr-2.31 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator, 1 operand} {
147    expr {!4}
148} 0
149test compExpr-2.32 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator, 1 operand} {
150    expr {~4}
151} -5
152test compExpr-2.33 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator, comparison} {
153    catch {unset a}
154    set a 15
155    expr {$a==15}  ;# compiled out-of-line to runtime call on Tcl_ExprObjCmd
156} 1
157test compExpr-2.34 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
158    expr {+2}
159} 2
160test compExpr-2.35 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, error in special operator} -body {
161    expr {+[expr 1+]}
162} -returnCodes error -match glob -result *
163test compExpr-2.36 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
164    expr {4+2}
165} 6
166test compExpr-2.37 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, error in special operator} -body {
167    expr {[expr 1+]+5}
168} -returnCodes error -match glob -result *
169test compExpr-2.38 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, error in special operator} -body {
170    expr {5+[expr 1+]}
171} -returnCodes error -match glob -result *
172test compExpr-2.39 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
173    expr {-2}
174} -2
175test compExpr-2.40 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
176    expr {4-2}
177} 2
178test compExpr-2.41 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
179    catch {unset a}
180    set a true
181    expr {0||$a}
182} 1
183test compExpr-2.42 {CompileSubExpr procedure, error in TCL_TOKEN_SUB_EXPR parse token} {
184    catch {unset a}
185    set a 15
186    list [catch {expr {27 || "$a[expr 1+]00"}} msg] $msg
187} {0 1}
188test compExpr-2.43 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
189    catch {unset a}
190    set a false
191    expr {3&&$a}
192} 0
193test compExpr-2.44 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
194    catch {unset a}
195    set a false
196    expr {$a||1? 1 : 0}
197} 1
198test compExpr-2.45 {CompileSubExpr procedure, error in TCL_TOKEN_SUB_EXPR parse token} {
199    catch {unset a}
200    set a 15
201    list [catch {expr {1? 54 : "$a[expr 1+]00"}} msg] $msg
202} {0 54}
203
204test compExpr-3.1 {CompileLandOrLorExpr procedure, numeric 1st operand} {
205    catch {unset a}
206    set a 2
207    expr {[set a]||0}
208} 1
209test compExpr-3.2 {CompileLandOrLorExpr procedure, nonnumeric 1st operand} {
210    catch {unset a}
211    set a no
212    expr {$a&&1}
213} 0
214test compExpr-3.3 {CompileSubExpr procedure, error in 1st operand} -body {
215    expr {[expr *2]||0}
216} -returnCodes error -match glob -result *
217test compExpr-3.4 {CompileLandOrLorExpr procedure, result is 1 or 0} {
218    catch {unset a}
219    catch {unset b}
220    set a no
221    set b true
222    expr {$a || $b}
223} 1
224test compExpr-3.5 {CompileLandOrLorExpr procedure, short-circuit semantics} {
225    catch {unset a}
226    set a yes
227    expr {$a || [exit]}
228} 1
229test compExpr-3.6 {CompileLandOrLorExpr procedure, short-circuit semantics} {
230    catch {unset a}
231    set a no
232    expr {$a && [exit]}
233} 0
234test compExpr-3.7 {CompileLandOrLorExpr procedure, numeric 2nd operand} {
235    catch {unset a}
236    set a 2
237    expr {0||[set a]}
238} 1
239test compExpr-3.8 {CompileLandOrLorExpr procedure, nonnumeric 2nd operand} {
240    catch {unset a}
241    set a no
242    expr {1&&$a}
243} 0
244test compExpr-3.9 {CompileLandOrLorExpr procedure, error in 2nd operand} -body {
245    expr {0||[expr %2]}
246} -returnCodes error -match glob -result *
247test compExpr-3.10 {CompileLandOrLorExpr procedure, long lor/land arm} {
248    set a "abcdefghijkl"
249    set i 7
250    expr {[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]] || [string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]] || [string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]] || [string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]}
251} 1
252
253test compExpr-4.1 {CompileCondExpr procedure, simple test} {
254    catch {unset a}
255    set a 2
256    expr {($a > 1)? "ok" : "nope"}
257} ok
258test compExpr-4.2 {CompileCondExpr procedure, complex test, convert to numeric} {
259    catch {unset a}
260    set a no
261    expr {[set a]? 27 : -54}
262} -54
263test compExpr-4.3 {CompileCondExpr procedure, error in test} -body {
264    expr {[expr *2]? +1 : -1}
265} -returnCodes error -match glob -result *
266test compExpr-4.4 {CompileCondExpr procedure, simple "true" clause} {
267    catch {unset a}
268    set a no
269    expr {1? (27-2) : -54}
270} 25
271test compExpr-4.5 {CompileCondExpr procedure, convert "true" clause to numeric} {
272    catch {unset a}
273    set a no
274    expr {1? $a : -54}
275} no
276test compExpr-4.6 {CompileCondExpr procedure, error in "true" clause} -body {
277    expr {1? [expr *2] : -127}
278} -returnCodes error -match glob -result *
279test compExpr-4.7 {CompileCondExpr procedure, simple "false" clause} {
280    catch {unset a}
281    set a no
282    expr {(2-2)? -3.14159 : "nope"}
283} nope
284test compExpr-4.8 {CompileCondExpr procedure, convert "false" clause to numeric} {
285    catch {unset a}
286    set a 0o0123
287    expr {0? 42 : $a}
288} 83
289test compExpr-4.9 {CompileCondExpr procedure, error in "false" clause} {
290    list [catch {expr {1? 15 : [expr *2]}} msg] $msg
291} {0 15}
292
293test compExpr-5.1 {CompileMathFuncCall procedure, math function found} {
294    format %.6g [expr atan2(1.0, 2.0)]
295} 0.463648
296test compExpr-5.2 {CompileMathFuncCall procedure, math function not found} -body {
297    list [catch {expr {do_it()}} msg] $msg
298} -match glob -result {1 {* "*do_it"}}
299test compExpr-5.3 {CompileMathFuncCall: call registered math function} testmathfunctions {
300    expr 3*T1()-1
301} 368
302test compExpr-5.4 {CompileMathFuncCall: call registered math function} testmathfunctions {
303    expr T2()*3
304} 1035
305test compExpr-5.5 {CompileMathFuncCall procedure, too few arguments} -body {
306    list [catch {expr {atan2(1.0)}} msg] $msg
307} -match glob -result {1 {too few arguments for math function*}}
308test compExpr-5.6 {CompileMathFuncCall procedure, complex argument} {
309    format %.6g [expr pow(2.1, 27.5-(24.4*(5%2)))]
310} 9.97424
311test compExpr-5.7 {CompileMathFuncCall procedure, error in argument} -body {
312    expr {sinh(2.*)}
313} -returnCodes error -match glob -result *
314test compExpr-5.8 {CompileMathFuncCall procedure, too many arguments} -body {
315    list [catch {expr {sinh(2.0, 3.0)}} msg] $msg
316} -match glob -result {1 {too many arguments for math function*}}
317test compExpr-5.9 {CompileMathFuncCall procedure, too many arguments} -body {
318    list [catch {expr {0 <= rand(5.2)}} msg] $msg
319} -match glob -result {1 {too many arguments for math function*}}
320
321test compExpr-6.1 {LogSyntaxError procedure, error in expr longer than 60 chars} -body {
322    expr {(+0123456)*(+0123456)*(+0123456)*(+0123456)*(+0123456)*(+0123456)*(+0123456)/} -1 foo 3
323} -returnCodes error -match glob -result *
324
325test compExpr-7.1 {Memory Leak} -constraints memory -setup {
326    proc getbytes {} {
327        set lines [split [memory info] \n]
328        lindex $lines 3 3
329    }
330} -body {
331    set end [getbytes]
332    for {set i 0} {$i < 5} {incr i} {
333        interp create slave
334        slave eval expr 1+2+3+4+5+6+7+8+9+10+11+12+13
335        interp delete slave
336        set tmp $end
337        set end [getbytes]
338    }
339    set leakedBytes [expr {$end - $tmp}]
340} -cleanup {
341    unset end i tmp
342    rename getbytes {}
343} -result 0
344
345test compExpr-7.2 {[Bug 1869989]: expr parser memleak} -constraints memory -setup {
346    proc getbytes {} {
347        set lines [split [memory info] \n]
348        lindex $lines 3 3
349    }
350} -body {
351    set i 5
352    set end [getbytes]
353    while {[incr i -1]} {
354        expr ${i}000
355        set tmp $end
356        set end [getbytes]
357    }
358    set leakedBytes [expr {$end - $tmp}]
359} -cleanup {
360    unset end i tmp
361    rename getbytes {}
362} -result 0
363
364# cleanup
365catch {unset a}
366catch {unset b}
367::tcltest::cleanupTests
368return
Note: See TracBrowser for help on using the repository browser.