Debian Codegraph @

tests/compExpr.test

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