OSDN Git Service

Initial revision
[pf3gnuchains/pf3gnuchains3x.git] / tcl / tests / compExpr.test
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$
12
13 if {[lsearch [namespace children] ::tcltest] == -1} {
14     package require tcltest
15     namespace import -force ::tcltest::*
16 }
17
18 if {([catch {expr T1()} msg] == 1) && ($msg == {unknown math function "T1"})} {
19     set gotT1 0
20     puts "This application hasn't been compiled with the \"T1\" and"
21     puts "\"T2\" math functions, so I'll skip some of the expr tests."
22 } else {
23     set gotT1 1
24 }
25
26 catch {unset a}
27
28 test compExpr-1.1 {TclCompileExpr procedure, successful expr parse and compile} {
29     expr 1+2
30 } 3
31 test compExpr-1.2 {TclCompileExpr procedure, error parsing expr} {
32     list [catch {expr 1+2+} msg] $msg
33 } {1 {syntax error in expression "1+2+"}}
34 test compExpr-1.3 {TclCompileExpr procedure, error compiling expr} {
35     list [catch {expr "foo(123)"} msg] $msg
36 } {1 {unknown math function "foo"}}
37 test compExpr-1.4 {TclCompileExpr procedure, expr has no operators} {
38     set a {000123}
39     expr {$a}
40 } 83
41
42 test compExpr-2.1 {CompileSubExpr procedure, TCL_TOKEN_WORD parse token} {
43     catch {unset a}
44     set a 27
45     expr {"foo$a" < "bar"}
46 } 0
47 test compExpr-2.2 {CompileSubExpr procedure, error compiling TCL_TOKEN_WORD parse token} {
48     list [catch {expr {"00[expr 1+]" + 17}} msg] $msg
49 } {1 {syntax error in expression "1+"}}
50 test compExpr-2.3 {CompileSubExpr procedure, TCL_TOKEN_TEXT parse token} {
51     expr {{12345}}
52 } 12345
53 test compExpr-2.4 {CompileSubExpr procedure, empty TCL_TOKEN_TEXT parse token} {
54     expr {{}}
55 } {}
56 test compExpr-2.5 {CompileSubExpr procedure, TCL_TOKEN_BS parse token} {
57     expr "\{  \\
58  +123 \}"
59 } 123
60 test compExpr-2.6 {CompileSubExpr procedure, TCL_TOKEN_COMMAND parse token} {
61     expr {[info tclversion] != ""}
62 } 1
63 test compExpr-2.7 {CompileSubExpr procedure, TCL_TOKEN_COMMAND parse token} {
64     expr {[]}
65 } {}
66 test compExpr-2.8 {CompileSubExpr procedure, error in TCL_TOKEN_COMMAND parse token} {
67     list [catch {expr {[foo "bar"xxx] + 17}} msg] $msg
68 } {1 {extra characters after close-quote}}
69 test compExpr-2.9 {CompileSubExpr procedure, TCL_TOKEN_VARIABLE parse token} {
70     catch {unset a}
71     set a 123
72     expr {$a*2}
73 } 246
74 test compExpr-2.10 {CompileSubExpr procedure, TCL_TOKEN_VARIABLE parse token} {
75     catch {unset a}
76     catch {unset b}
77     set a(george) martha
78     set b geo
79     expr {$a(${b}rge)}
80 } martha
81 test compExpr-2.11 {CompileSubExpr procedure, error in TCL_TOKEN_VARIABLE parse token} {
82     catch {unset a}
83     list [catch {expr {$a + 17}} msg] $msg
84 } {1 {can't read "a": no such variable}}
85 test compExpr-2.12 {CompileSubExpr procedure, TCL_TOKEN_SUB_EXPR parse token} {
86     expr {27||3? 3<<(1+4) : 4&&9}
87 } 96
88 test compExpr-2.13 {CompileSubExpr procedure, error in TCL_TOKEN_SUB_EXPR parse token} {
89     catch {unset a}
90     set a 15
91     list [catch {expr {27 || "$a[expr 1+]00"}} msg] $msg
92 } {1 {syntax error in expression "1+"}}
93 test compExpr-2.14 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, op found} {
94     expr {5*6}
95 } 30
96 test compExpr-2.15 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, math function found} {
97     format %.6g [expr {sin(2.0)}]
98 } 0.909297
99 test compExpr-2.16 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, math function not found} {
100     list [catch {expr {fred(2.0)}} msg] $msg
101 } {1 {unknown math function "fred"}}
102 test compExpr-2.17 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
103     expr {4*2}
104 } 8
105 test compExpr-2.18 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
106     expr {4/2}
107 } 2
108 test compExpr-2.19 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
109     expr {4%2}
110 } 0
111 test compExpr-2.20 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
112     expr {4<<2}
113 } 16
114 test compExpr-2.21 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
115     expr {4>>2}
116 } 1
117 test compExpr-2.22 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
118     expr {4<2}
119 } 0
120 test compExpr-2.23 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
121     expr {4>2}
122 } 1
123 test compExpr-2.24 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
124     expr {4<=2}
125 } 0
126 test compExpr-2.25 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
127     expr {4>=2}
128 } 1
129 test compExpr-2.26 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
130     expr {4==2}
131 } 0
132 test compExpr-2.27 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
133     expr {4!=2}
134 } 1
135 test compExpr-2.28 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
136     expr {4&2}
137 } 0
138 test compExpr-2.29 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
139     expr {4^2}
140 } 6
141 test compExpr-2.30 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
142     expr {4|2}
143 } 6
144 test compExpr-2.31 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator, 1 operand} {
145     expr {!4}
146 } 0
147 test compExpr-2.32 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator, 1 operand} {
148     expr {~4}
149 } -5
150 test compExpr-2.33 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator, comparison} {
151     catch {unset a}
152     set a 15
153     expr {$a==15}  ;# compiled out-of-line to runtime call on Tcl_ExprObjCmd
154 } 1
155 test compExpr-2.34 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
156     expr {+2}
157 } 2
158 test compExpr-2.35 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, error in special operator} {
159     list [catch {expr {+[expr 1+]}} msg] $msg
160 } {1 {syntax error in expression "1+"}}
161 test compExpr-2.36 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
162     expr {4+2}
163 } 6
164 test compExpr-2.37 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, error in special operator} {
165     list [catch {expr {[expr 1+]+5}} msg] $msg
166 } {1 {syntax error in expression "1+"}}
167 test compExpr-2.38 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, error in special operator} {
168     list [catch {expr {5+[expr 1+]}} msg] $msg
169 } {1 {syntax error in expression "1+"}}
170 test compExpr-2.39 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
171     expr {-2}
172 } -2
173 test compExpr-2.40 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
174     expr {4-2}
175 } 2
176 test compExpr-2.41 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
177     catch {unset a}
178     set a true
179     expr {0||$a}
180 } 1
181 test compExpr-2.42 {CompileSubExpr procedure, error in TCL_TOKEN_SUB_EXPR parse token} {
182     catch {unset a}
183     set a 15
184     list [catch {expr {27 || "$a[expr 1+]00"}} msg] $msg
185 } {1 {syntax error in expression "1+"}}
186 test compExpr-2.43 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
187     catch {unset a}
188     set a false
189     expr {3&&$a}
190 } 0
191 test compExpr-2.44 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
192     catch {unset a}
193     set a false
194     expr {$a||1? 1 : 0}
195 } 1
196 test compExpr-2.45 {CompileSubExpr procedure, error in TCL_TOKEN_SUB_EXPR parse token} {
197     catch {unset a}
198     set a 15
199     list [catch {expr {1? 54 : "$a[expr 1+]00"}} msg] $msg
200 } {1 {syntax error in expression "1+"}}
201
202 test compExpr-3.1 {CompileLandOrLorExpr procedure, numeric 1st operand} {
203     catch {unset a}
204     set a 2
205     expr {[set a]||0}
206 } 1
207 test compExpr-3.2 {CompileLandOrLorExpr procedure, nonnumeric 1st operand} {
208     catch {unset a}
209     set a no
210     expr {$a&&1}
211 } 0
212 test compExpr-3.3 {CompileSubExpr procedure, error in 1st operand} {
213     list [catch {expr {[expr *2]||0}} msg] $msg
214 } {1 {syntax error in expression "*2"}}
215 test compExpr-3.4 {CompileLandOrLorExpr procedure, result is 1 or 0} {
216     catch {unset a}
217     catch {unset b}
218     set a no
219     set b true
220     expr {$a || $b}
221 } 1
222 test compExpr-3.5 {CompileLandOrLorExpr procedure, short-circuit semantics} {
223     catch {unset a}
224     set a yes
225     expr {$a || [exit]}
226 } 1
227 test compExpr-3.6 {CompileLandOrLorExpr procedure, short-circuit semantics} {
228     catch {unset a}
229     set a no
230     expr {$a && [exit]}
231 } 0
232 test compExpr-3.7 {CompileLandOrLorExpr procedure, numeric 2nd operand} {
233     catch {unset a}
234     set a 2
235     expr {0||[set a]}
236 } 1
237 test compExpr-3.8 {CompileLandOrLorExpr procedure, nonnumeric 2nd operand} {
238     catch {unset a}
239     set a no
240     expr {1&&$a}
241 } 0
242 test compExpr-3.9 {CompileLandOrLorExpr procedure, error in 2nd operand} {
243     list [catch {expr {0||[expr %2]}} msg] $msg
244 } {1 {syntax error in expression "%2"}}
245 test compExpr-3.10 {CompileLandOrLorExpr procedure, long lor/land arm} {
246     set a "abcdefghijkl"
247     set i 7
248     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]]}
249 } 1
250
251 test compExpr-4.1 {CompileCondExpr procedure, simple test} {
252     catch {unset a}
253     set a 2
254     expr {($a > 1)? "ok" : "nope"}
255 } ok
256 test compExpr-4.2 {CompileCondExpr procedure, complex test, convert to numeric} {
257     catch {unset a}
258     set a no
259     expr {[set a]? 27 : -54}
260 } -54
261 test compExpr-4.3 {CompileCondExpr procedure, error in test} {
262     list [catch {expr {[expr *2]? +1 : -1}} msg] $msg
263 } {1 {syntax error in expression "*2"}}
264 test compExpr-4.4 {CompileCondExpr procedure, simple "true" clause} {
265     catch {unset a}
266     set a no
267     expr {1? (27-2) : -54}
268 } 25
269 test compExpr-4.5 {CompileCondExpr procedure, convert "true" clause to numeric} {
270     catch {unset a}
271     set a no
272     expr {1? $a : -54}
273 } no
274 test compExpr-4.6 {CompileCondExpr procedure, error in "true" clause} {
275     list [catch {expr {1? [expr *2] : -127}} msg] $msg
276 } {1 {syntax error in expression "*2"}}
277 test compExpr-4.7 {CompileCondExpr procedure, simple "false" clause} {
278     catch {unset a}
279     set a no
280     expr {(2-2)? -3.14159 : "nope"}
281 } nope
282 test compExpr-4.8 {CompileCondExpr procedure, convert "false" clause to numeric} {
283     catch {unset a}
284     set a 00123
285     expr {0? 42 : $a}
286 } 83
287 test compExpr-4.9 {CompileCondExpr procedure, error in "false" clause} {
288     list [catch {expr {1? 15 : [expr *2]}} msg] $msg
289 } {1 {syntax error in expression "*2"}}
290
291 test compExpr-5.1 {CompileMathFuncCall procedure, math function found} {
292     format %.6g [expr atan2(1.0, 2.0)]
293 } 0.463648
294 test compExpr-5.2 {CompileMathFuncCall procedure, math function not found} {
295     list [catch {expr {do_it()}} msg] $msg
296 } {1 {unknown math function "do_it"}}
297 if $gotT1 {
298     test compExpr-5.3 {CompileMathFuncCall: call registered math function} {
299         expr 3*T1()-1
300     } 368
301     test compExpr-5.4 {CompileMathFuncCall: call registered math function} {
302         expr T2()*3
303     } 1035
304 }
305 test compExpr-5.5 {CompileMathFuncCall procedure, too few arguments} {
306     list [catch {expr {atan2(1.0)}} msg] $msg
307 } {1 {too few arguments for math function}}
308 test compExpr-5.6 {CompileMathFuncCall procedure, complex argument} {
309     format %.6g [expr pow(2.1, 27.5-(24.4*(5%2)))]
310 } 9.97424
311 test compExpr-5.7 {CompileMathFuncCall procedure, error in argument} {
312     list [catch {expr {sinh(2.*)}} msg] $msg
313 } {1 {syntax error in expression "sinh(2.*)"}}
314 test compExpr-5.8 {CompileMathFuncCall procedure, too many arguments} {
315     list [catch {expr {sinh(2.0, 3.0)}} msg] $msg
316 } {1 {too many arguments for math function}}
317 test compExpr-5.9 {CompileMathFuncCall procedure, too many arguments} {
318     list [catch {expr {0 <= rand(5.2)}} msg] $msg
319 } {1 {too many arguments for math function}}
320
321 test compExpr-6.1 {LogSyntaxError procedure, error in expr longer than 60 chars} {
322     list [catch {expr {(+0123456)*(+0123456)*(+0123456)*(+0123456)*(+0123456)*(+0123456)*(+0123456)/} -1 foo 3} msg] $msg
323 } {1 {syntax error in expression "(+0123456)*(+0123456)*(+0123456)*(+0123456)*(+0123456)*(+012"}}
324
325 # cleanup
326 catch {unset a}
327 catch {unset b}
328 ::tcltest::cleanupTests
329 return
330
331
332
333
334
335
336
337
338
339
340
341
342