Debian Codegraph @

tests/expr-old.test

raw
1 # Commands covered: expr
2 #
3 # This file contains the original set of tests for Tcl's expr command.
4 # Since the expr command is now compiled, a new set of tests covering
5 # the new implementation are in the files "parseExpr.test" and
6 # "compExpr.test". Sourcing this file into Tcl runs the tests and generates
7 # output for errors. No output means no errors were found.
8 #
9 # Copyright (c) 1991-1994 The Regents of the University of California.
10 # Copyright (c) 1994-1997 Sun Microsystems, Inc.
11 # Copyright (c) 1998-2000 by Scriptics Corporation.
12 #
13 # See the file "license.terms" for information on usage and redistribution
14 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15
16 package require tcltest 2.1
17 namespace import ::tcltest::*
18
19 ::tcltest::loadTestedCommands
20 catch [list package require -exact Tcltest [info patchlevel]]
21
22 testConstraint testexprlong [llength [info commands testexprlong]]
23 testConstraint testexprdouble [llength [info commands testexprdouble]]
24 testConstraint testexprstring [llength [info commands testexprstring]]
25 testConstraint longIs32bit [expr {int(0x80000000) < 0}]
26
27 if {[catch {expr T1()} msg] && $msg eq {invalid command name "tcl::mathfunc::T1"}} {
28 testConstraint testmathfunctions 0
29 } else {
30 testConstraint testmathfunctions 1
31 }
32
33 # Big test for correct ordering of data in [expr]
34
35 proc testIEEE {} {
36 variable ieeeValues
37 binary scan [binary format dd -1.0 1.0] c* c
38 switch -exact -- $c {
39 {0 0 0 0 0 0 -16 -65 0 0 0 0 0 0 -16 63} {
40 # little endian
41 binary scan \x00\x00\x00\x00\x00\x00\xf0\xff d \
42 ieeeValues(-Infinity)
43 binary scan \x00\x00\x00\x00\x00\x00\xf0\xbf d \
44 ieeeValues(-Normal)
45 binary scan \x00\x00\x00\x00\x00\x00\x08\x80 d \
46 ieeeValues(-Subnormal)
47 binary scan \x00\x00\x00\x00\x00\x00\x00\x80 d \
48 ieeeValues(-0)
49 binary scan \x00\x00\x00\x00\x00\x00\x00\x00 d \
50 ieeeValues(+0)
51 binary scan \x00\x00\x00\x00\x00\x00\x08\x00 d \
52 ieeeValues(+Subnormal)
53 binary scan \x00\x00\x00\x00\x00\x00\xf0\x3f d \
54 ieeeValues(+Normal)
55 binary scan \x00\x00\x00\x00\x00\x00\xf0\x7f d \
56 ieeeValues(+Infinity)
57 binary scan \x00\x00\x00\x00\x00\x00\xf8\x7f d \
58 ieeeValues(NaN)
59 set ieeeValues(littleEndian) 1
60 return 1
61 }
62 {-65 -16 0 0 0 0 0 0 63 -16 0 0 0 0 0 0} {
63 binary scan \xff\xf0\x00\x00\x00\x00\x00\x00 d \
64 ieeeValues(-Infinity)
65 binary scan \xbf\xf0\x00\x00\x00\x00\x00\x00 d \
66 ieeeValues(-Normal)
67 binary scan \x80\x08\x00\x00\x00\x00\x00\x00 d \
68 ieeeValues(-Subnormal)
69 binary scan \x80\x00\x00\x00\x00\x00\x00\x00 d \
70 ieeeValues(-0)
71 binary scan \x00\x00\x00\x00\x00\x00\x00\x00 d \
72 ieeeValues(+0)
73 binary scan \x00\x08\x00\x00\x00\x00\x00\x00 d \
74 ieeeValues(+Subnormal)
75 binary scan \x3f\xf0\x00\x00\x00\x00\x00\x00 d \
76 ieeeValues(+Normal)
77 binary scan \x7f\xf0\x00\x00\x00\x00\x00\x00 d \
78 ieeeValues(+Infinity)
79 binary scan \x7f\xf8\x00\x00\x00\x00\x00\x00 d \
80 ieeeValues(NaN)
81 set ieeeValues(littleEndian) 0
82 return 1
83 }
84 default {
85 return 0
86 }
87 }
88 }
89 testConstraint ieeeFloatingPoint [testIEEE]
90
91 # First, test all of the integer operators individually.
92
93 test expr-old-1.1 {integer operators} {expr -4} -4
94 test expr-old-1.2 {integer operators} {expr -(1+4)} -5
95 test expr-old-1.3 {integer operators} {expr ~3} -4
96 test expr-old-1.4 {integer operators} {expr !2} 0
97 test expr-old-1.5 {integer operators} {expr !0} 1
98 test expr-old-1.6 {integer operators} {expr 4*6} 24
99 test expr-old-1.7 {integer operators} {expr 36/12} 3
100 test expr-old-1.8 {integer operators} {expr 27/4} 6
101 test expr-old-1.9 {integer operators} {expr 27%4} 3
102 test expr-old-1.10 {integer operators} {expr 2+2} 4
103 test expr-old-1.11 {integer operators} {expr 2-6} -4
104 test expr-old-1.12 {integer operators} {expr 1<<3} 8
105 test expr-old-1.13 {integer operators} {expr 0xff>>2} 63
106 test expr-old-1.14 {integer operators} {expr -1>>2} -1
107 test expr-old-1.15 {integer operators} {expr 3>2} 1
108 test expr-old-1.16 {integer operators} {expr 2>2} 0
109 test expr-old-1.17 {integer operators} {expr 1>2} 0
110 test expr-old-1.18 {integer operators} {expr 3<2} 0
111 test expr-old-1.19 {integer operators} {expr 2<2} 0
112 test expr-old-1.20 {integer operators} {expr 1<2} 1
113 test expr-old-1.21 {integer operators} {expr 3>=2} 1
114 test expr-old-1.22 {integer operators} {expr 2>=2} 1
115 test expr-old-1.23 {integer operators} {expr 1>=2} 0
116 test expr-old-1.24 {integer operators} {expr 3<=2} 0
117 test expr-old-1.25 {integer operators} {expr 2<=2} 1
118 test expr-old-1.26 {integer operators} {expr 1<=2} 1
119 test expr-old-1.27 {integer operators} {expr 3==2} 0
120 test expr-old-1.28 {integer operators} {expr 2==2} 1
121 test expr-old-1.29 {integer operators} {expr 3!=2} 1
122 test expr-old-1.30 {integer operators} {expr 2!=2} 0
123 test expr-old-1.31 {integer operators} {expr 7&0x13} 3
124 test expr-old-1.32 {integer operators} {expr 7^0x13} 20
125 test expr-old-1.33 {integer operators} {expr 7|0x13} 23
126 test expr-old-1.34 {integer operators} {expr 0&&1} 0
127 test expr-old-1.35 {integer operators} {expr 0&&0} 0
128 test expr-old-1.36 {integer operators} {expr 1&&3} 1
129 test expr-old-1.37 {integer operators} {expr 0||1} 1
130 test expr-old-1.38 {integer operators} {expr 3||0} 1
131 test expr-old-1.39 {integer operators} {expr 0||0} 0
132 test expr-old-1.40 {integer operators} {expr 3>2?44:66} 44
133 test expr-old-1.41 {integer operators} {expr 2>3?44:66} 66
134 test expr-old-1.42 {integer operators} {expr 36/5} 7
135 test expr-old-1.43 {integer operators} {expr 36%5} 1
136 test expr-old-1.44 {integer operators} {expr -36/5} -8
137 test expr-old-1.45 {integer operators} {expr -36%5} 4
138 test expr-old-1.46 {integer operators} {expr 36/-5} -8
139 test expr-old-1.47 {integer operators} {expr 36%-5} -4
140 test expr-old-1.48 {integer operators} {expr -36/-5} 7
141 test expr-old-1.49 {integer operators} {expr -36%-5} -1
142 test expr-old-1.50 {integer operators} {expr +36} 36
143 test expr-old-1.51 {integer operators} {expr +--++36} 36
144 test expr-old-1.52 {integer operators} {expr +36%+5} 1
145 test expr-old-1.53 {integer operators} {
146 unset -nocomplain x
147 set x yes
148 list [expr {1 && $x}] [expr {$x && 1}] \
149 [expr {0 || $x}] [expr {$x || 0}]
150 } {1 1 1 1}
151
152 # Check the floating-point operators individually, along with
153 # automatic conversion to integers where needed.
154
155 test expr-old-2.1 {floating-point operators} {expr -4.2} -4.2
156 test expr-old-2.2 {floating-point operators} {expr -(1.125+4.25)} -5.375
157 test expr-old-2.3 {floating-point operators} {expr +5.7} 5.7
158 test expr-old-2.4 {floating-point operators} {expr +--+-62.0} -62.0
159 test expr-old-2.5 {floating-point operators} {expr !2.1} 0
160 test expr-old-2.6 {floating-point operators} {expr !0.0} 1
161 test expr-old-2.7 {floating-point operators} {expr 4.2*6.3} 26.46
162 test expr-old-2.8 {floating-point operators} {expr 36.0/12.0} 3.0
163 test expr-old-2.9 {floating-point operators} {expr 27/4.0} 6.75
164 test expr-old-2.10 {floating-point operators} {expr 2.3+2.1} 4.4
165 test expr-old-2.11 {floating-point operators} {expr 2.3-6.5} -4.2
166 test expr-old-2.12 {floating-point operators} {expr 3.1>2.1} 1
167 test expr-old-2.13 {floating-point operators} {expr {2.1 > 2.1}} 0
168 test expr-old-2.14 {floating-point operators} {expr 1.23>2.34e+1} 0
169 test expr-old-2.15 {floating-point operators} {expr 3.45<2.34} 0
170 test expr-old-2.16 {floating-point operators} {expr 0.002e3<--200e-2} 0
171 test expr-old-2.17 {floating-point operators} {expr 1.1<2.1} 1
172 test expr-old-2.18 {floating-point operators} {expr 3.1>=2.2} 1
173 test expr-old-2.19 {floating-point operators} {expr 2.345>=2.345} 1
174 test expr-old-2.20 {floating-point operators} {expr 1.1>=2.2} 0
175 test expr-old-2.21 {floating-point operators} {expr 3.0<=2.0} 0
176 test expr-old-2.22 {floating-point operators} {expr 2.2<=2.2} 1
177 test expr-old-2.23 {floating-point operators} {expr 2.2<=2.2001} 1
178 test expr-old-2.24 {floating-point operators} {expr 3.2==2.2} 0
179 test expr-old-2.25 {floating-point operators} {expr 2.2==2.2} 1
180 test expr-old-2.26 {floating-point operators} {expr 3.2!=2.2} 1
181 test expr-old-2.27 {floating-point operators} {expr 2.2!=2.2} 0
182 test expr-old-2.28 {floating-point operators} {expr 0.0&&0.0} 0
183 test expr-old-2.29 {floating-point operators} {expr 0.0&&1.3} 0
184 test expr-old-2.30 {floating-point operators} {expr 1.3&&0.0} 0
185 test expr-old-2.31 {floating-point operators} {expr 1.3&&3.3} 1
186 test expr-old-2.32 {floating-point operators} {expr 0.0||0.0} 0
187 test expr-old-2.33 {floating-point operators} {expr 0.0||1.3} 1
188 test expr-old-2.34 {floating-point operators} {expr 1.3||0.0} 1
189 test expr-old-2.35 {floating-point operators} {expr 3.3||0.0} 1
190 test expr-old-2.36 {floating-point operators} {expr 3.3>2.3?44.3:66.3} 44.3
191 test expr-old-2.37 {floating-point operators} {expr 2.3>3.3?44.3:66.3} 66.3
192 test expr-old-2.38 {floating-point operators} {
193 list [catch {expr 028.1 + 09.2} msg] $msg
194 } {0 37.3}
195
196 # Operators that aren't legal on floating-point numbers
197
198 test expr-old-3.1 {illegal floating-point operations} {
199 list [catch {expr ~4.0} msg] $msg
200 } {1 {can't use floating-point value as operand of "~"}}
201 test expr-old-3.2 {illegal floating-point operations} {
202 list [catch {expr 27%4.0} msg] $msg
203 } {1 {can't use floating-point value as operand of "%"}}
204 test expr-old-3.3 {illegal floating-point operations} {
205 list [catch {expr 27.0%4} msg] $msg
206 } {1 {can't use floating-point value as operand of "%"}}
207 test expr-old-3.4 {illegal floating-point operations} {
208 list [catch {expr 1.0<<3} msg] $msg
209 } {1 {can't use floating-point value as operand of "<<"}}
210 test expr-old-3.5 {illegal floating-point operations} {
211 list [catch {expr 3<<1.0} msg] $msg
212 } {1 {can't use floating-point value as operand of "<<"}}
213 test expr-old-3.6 {illegal floating-point operations} {
214 list [catch {expr 24.0>>3} msg] $msg
215 } {1 {can't use floating-point value as operand of ">>"}}
216 test expr-old-3.7 {illegal floating-point operations} {
217 list [catch {expr 24>>3.0} msg] $msg
218 } {1 {can't use floating-point value as operand of ">>"}}
219 test expr-old-3.8 {illegal floating-point operations} {
220 list [catch {expr 24&3.0} msg] $msg
221 } {1 {can't use floating-point value as operand of "&"}}
222 test expr-old-3.9 {illegal floating-point operations} {
223 list [catch {expr 24.0|3} msg] $msg
224 } {1 {can't use floating-point value as operand of "|"}}
225 test expr-old-3.10 {illegal floating-point operations} {
226 list [catch {expr 24.0^3} msg] $msg
227 } {1 {can't use floating-point value as operand of "^"}}
228
229 # Check the string operators individually.
230
231 test expr-old-4.1 {string operators} {expr {"abc" > "def"}} 0
232 test expr-old-4.2 {string operators} {expr {"def" > "def"}} 0
233 test expr-old-4.3 {string operators} {expr {"g" > "def"}} 1
234 test expr-old-4.4 {string operators} {expr {"abc" < "abd"}} 1
235 test expr-old-4.5 {string operators} {expr {"abd" < "abd"}} 0
236 test expr-old-4.6 {string operators} {expr {"abe" < "abd"}} 0
237 test expr-old-4.7 {string operators} {expr {"abc" >= "def"}} 0
238 test expr-old-4.8 {string operators} {expr {"def" >= "def"}} 1
239 test expr-old-4.9 {string operators} {expr {"g" >= "def"}} 1
240 test expr-old-4.10 {string operators} {expr {"abc" <= "abd"}} 1
241 test expr-old-4.11 {string operators} {expr {"abd" <= "abd"}} 1
242 test expr-old-4.12 {string operators} {expr {"abe" <= "abd"}} 0
243 test expr-old-4.13 {string operators} {expr {"abc" == "abd"}} 0
244 test expr-old-4.14 {string operators} {expr {"abd" == "abd"}} 1
245 test expr-old-4.15 {string operators} {expr {"abc" != "abd"}} 1
246 test expr-old-4.16 {string operators} {expr {"abd" != "abd"}} 0
247 test expr-old-4.17 {string operators} {expr {"0y" < "0x12"}} 0
248 test expr-old-4.18 {string operators} {expr {"." < " "}} 0
249 test expr-old-4.19 {string operators} {expr {"abc" eq "abd"}} 0
250 test expr-old-4.20 {string operators} {expr {"abd" eq "abd"}} 1
251 test expr-old-4.21 {string operators} {expr {"abc" ne "abd"}} 1
252 test expr-old-4.22 {string operators} {expr {"abd" ne "abd"}} 0
253 test expr-old-4.23 {string operators} {expr {"" eq "abd"}} 0
254 test expr-old-4.24 {string operators} {expr {"" eq ""}} 1
255 test expr-old-4.25 {string operators} {expr {"abd" ne ""}} 1
256 test expr-old-4.26 {string operators} {expr {"" ne ""}} 0
257 test expr-old-4.27 {string operators} {expr {"longerstring" eq "shorter"}} 0
258 test expr-old-4.28 {string operators} {expr {"longerstring" ne "shorter"}} 1
259 test expr-old-4.29 {string operators} {expr {"0" == "+"}} 0
260 test expr-old-4.30 {string operators} {expr {"0" == "-"}} 0
261 test expr-old-4.31 {string operators} {expr {1?"foo":"bar"}} foo
262 test expr-old-4.32 {string operators} {expr {0?"foo":"bar"}} bar
263
264 # Operators that aren't legal on string operands.
265
266 test expr-old-5.1 {illegal string operations} {
267 list [catch {expr {-"a"}} msg] $msg
268 } {1 {can't use non-numeric string as operand of "-"}}
269 test expr-old-5.2 {illegal string operations} {
270 list [catch {expr {+"a"}} msg] $msg
271 } {1 {can't use non-numeric string as operand of "+"}}
272 test expr-old-5.3 {illegal string operations} {
273 list [catch {expr {~"a"}} msg] $msg
274 } {1 {can't use non-numeric string as operand of "~"}}
275 test expr-old-5.4 {illegal string operations} {
276 list [catch {expr {!"a"}} msg] $msg
277 } {1 {can't use non-numeric string as operand of "!"}}
278 test expr-old-5.5 {illegal string operations} {
279 list [catch {expr {"a"*"b"}} msg] $msg
280 } {1 {can't use non-numeric string as operand of "*"}}
281 test expr-old-5.6 {illegal string operations} {
282 list [catch {expr {"a"/"b"}} msg] $msg
283 } {1 {can't use non-numeric string as operand of "/"}}
284 test expr-old-5.7 {illegal string operations} {
285 list [catch {expr {"a"%"b"}} msg] $msg
286 } {1 {can't use non-numeric string as operand of "%"}}
287 test expr-old-5.8 {illegal string operations} {
288 list [catch {expr {"a"+"b"}} msg] $msg
289 } {1 {can't use non-numeric string as operand of "+"}}
290 test expr-old-5.9 {illegal string operations} {
291 list [catch {expr {"a"-"b"}} msg] $msg
292 } {1 {can't use non-numeric string as operand of "-"}}
293 test expr-old-5.10 {illegal string operations} {
294 list [catch {expr {"a"<<"b"}} msg] $msg
295 } {1 {can't use non-numeric string as operand of "<<"}}
296 test expr-old-5.11 {illegal string operations} {
297 list [catch {expr {"a">>"b"}} msg] $msg
298 } {1 {can't use non-numeric string as operand of ">>"}}
299 test expr-old-5.12 {illegal string operations} {
300 list [catch {expr {"a"&"b"}} msg] $msg
301 } {1 {can't use non-numeric string as operand of "&"}}
302 test expr-old-5.13 {illegal string operations} {
303 list [catch {expr {"a"^"b"}} msg] $msg
304 } {1 {can't use non-numeric string as operand of "^"}}
305 test expr-old-5.14 {illegal string operations} {
306 list [catch {expr {"a"|"b"}} msg] $msg
307 } {1 {can't use non-numeric string as operand of "|"}}
308 test expr-old-5.15 {illegal string operations} {
309 list [catch {expr {"a"&&"b"}} msg] $msg
310 } {1 {expected boolean value but got "a"}}
311 test expr-old-5.16 {illegal string operations} {
312 list [catch {expr {"a"||"b"}} msg] $msg
313 } {1 {expected boolean value but got "a"}}
314 test expr-old-5.17 {illegal string operations} {
315 list [catch {expr {"a"?4:2}} msg] $msg
316 } {1 {expected boolean value but got "a"}}
317
318 # Check precedence pairwise.
319
320 test expr-old-6.1 {precedence checks} {expr -~3} 4
321 test expr-old-6.2 {precedence checks} {expr -!3} 0
322 test expr-old-6.3 {precedence checks} {expr -~0} 1
323
324 test expr-old-7.1 {precedence checks} {expr 2*4/6} 1
325 test expr-old-7.2 {precedence checks} {expr 24/6*3} 12
326 test expr-old-7.3 {precedence checks} {expr 24/6/2} 2
327
328 test expr-old-8.1 {precedence checks} {expr -2+4} 2
329 test expr-old-8.2 {precedence checks} {expr -2-4} -6
330 test expr-old-8.3 {precedence checks} {expr +2-4} -2
331
332 test expr-old-9.1 {precedence checks} {expr 2*3+4} 10
333 test expr-old-9.2 {precedence checks} {expr 8/2+4} 8
334 test expr-old-9.3 {precedence checks} {expr 8%3+4} 6
335 test expr-old-9.4 {precedence checks} {expr 2*3-1} 5
336 test expr-old-9.5 {precedence checks} {expr 8/2-1} 3
337 test expr-old-9.6 {precedence checks} {expr 8%3-1} 1
338
339 test expr-old-10.1 {precedence checks} {expr 6-3-2} 1
340
341 test expr-old-11.1 {precedence checks} {expr 7+1>>2} 2
342 test expr-old-11.2 {precedence checks} {expr 7+1<<2} 32
343 test expr-old-11.3 {precedence checks} {expr 7>>3-2} 3
344 test expr-old-11.4 {precedence checks} {expr 7<<3-2} 14
345
346 test expr-old-12.1 {precedence checks} {expr 6>>1>4} 0
347 test expr-old-12.2 {precedence checks} {expr 6>>1<2} 0
348 test expr-old-12.3 {precedence checks} {expr 6>>1>=3} 1
349 test expr-old-12.4 {precedence checks} {expr 6>>1<=2} 0
350 test expr-old-12.5 {precedence checks} {expr 6<<1>5} 1
351 test expr-old-12.6 {precedence checks} {expr 6<<1<5} 0
352 test expr-old-12.7 {precedence checks} {expr 5<=6<<1} 1
353 test expr-old-12.8 {precedence checks} {expr 5>=6<<1} 0
354
355 test expr-old-13.1 {precedence checks} {expr 2<3<4} 1
356 test expr-old-13.2 {precedence checks} {expr 0<4>2} 0
357 test expr-old-13.3 {precedence checks} {expr 4>2<1} 0
358 test expr-old-13.4 {precedence checks} {expr 4>3>2} 0
359 test expr-old-13.5 {precedence checks} {expr 4>3>=2} 0
360 test expr-old-13.6 {precedence checks} {expr 4>=3>2} 0
361 test expr-old-13.7 {precedence checks} {expr 4>=3>=2} 0
362 test expr-old-13.8 {precedence checks} {expr 0<=4>=2} 0
363 test expr-old-13.9 {precedence checks} {expr 4>=2<=0} 0
364 test expr-old-13.10 {precedence checks} {expr 2<=3<=4} 1
365
366 test expr-old-14.1 {precedence checks} {expr 1==4>3} 1
367 test expr-old-14.2 {precedence checks} {expr 0!=4>3} 1
368 test expr-old-14.3 {precedence checks} {expr 1==3<4} 1
369 test expr-old-14.4 {precedence checks} {expr 0!=3<4} 1
370 test expr-old-14.5 {precedence checks} {expr 1==4>=3} 1
371 test expr-old-14.6 {precedence checks} {expr 0!=4>=3} 1
372 test expr-old-14.7 {precedence checks} {expr 1==3<=4} 1
373 test expr-old-14.8 {precedence checks} {expr 0!=3<=4} 1
374 test expr-old-14.9 {precedence checks} {expr 1eq4>3} 1
375 test expr-old-14.10 {precedence checks} {expr 0ne4>3} 1
376 test expr-old-14.11 {precedence checks} {expr 1eq3<4} 1
377 test expr-old-14.12 {precedence checks} {expr 0ne3<4} 1
378 test expr-old-14.13 {precedence checks} {expr 1eq4>=3} 1
379 test expr-old-14.14 {precedence checks} {expr 0ne4>=3} 1
380 test expr-old-14.15 {precedence checks} {expr 1eq3<=4} 1
381 test expr-old-14.16 {precedence checks} {expr 0ne3<=4} 1
382
383 test expr-old-15.1 {precedence checks} {expr 1==3==3} 0
384 test expr-old-15.2 {precedence checks} {expr 3==3!=2} 1
385 test expr-old-15.3 {precedence checks} {expr 2!=3==3} 0
386 test expr-old-15.4 {precedence checks} {expr 2!=1!=1} 0
387 test expr-old-15.5 {precedence checks} {expr 1eq3eq3} 0
388 test expr-old-15.6 {precedence checks} {expr 3eq3ne2} 1
389 test expr-old-15.7 {precedence checks} {expr 2ne3eq3} 0
390 test expr-old-15.8 {precedence checks} {expr 2ne1ne1} 0
391
392 test expr-old-16.1 {precedence checks} {expr 2&3eq2} 0
393 test expr-old-16.2 {precedence checks} {expr 1&3ne3} 0
394 test expr-old-16.3 {precedence checks} {expr 2&3eq2} 0
395 test expr-old-16.4 {precedence checks} {expr 1&3ne3} 0
396
397 test expr-old-17.1 {precedence checks} {expr 7&3^0x10} 19
398 test expr-old-17.2 {precedence checks} {expr 7^0x10&3} 7
399
400 test expr-old-18.1 {precedence checks} {expr 7^0x10|3} 23
401 test expr-old-18.2 {precedence checks} {expr 7|0x10^3} 23
402
403 test expr-old-19.1 {precedence checks} {expr 7|3&&1} 1
404 test expr-old-19.2 {precedence checks} {expr 1&&3|7} 1
405 test expr-old-19.3 {precedence checks} {expr 0&&1||1} 1
406 test expr-old-19.4 {precedence checks} {expr 1||1&&0} 1
407
408 test expr-old-20.1 {precedence checks} {expr 1||0?3:4} 3
409 test expr-old-20.2 {precedence checks} {expr 1?0:4||1} 0
410 test expr-old-20.3 {precedence checks} {expr 1?2:0?3:4} 2
411 test expr-old-20.4 {precedence checks} {expr 0?2:0?3:4} 4
412 test expr-old-20.5 {precedence checks} {expr 1?2?3:4:0} 3
413 test expr-old-20.6 {precedence checks} {expr 0?2?3:4:0} 0
414
415 # Parentheses.
416
417 test expr-old-21.1 {parenthesization} {expr (2+4)*6} 36
418 test expr-old-21.2 {parenthesization} {expr (1?0:4)||1} 1
419 test expr-old-21.3 {parenthesization} {expr +(3-4)} -1
420
421 # Embedded commands and variable names.
422
423 set a 16
424 test expr-old-22.1 {embedded variables} {expr {2*$a}} 32
425 test expr-old-22.2 {embedded variables} {
426 set x -5
427 set y 10
428 expr {$x + $y}
429 } {5}
430 test expr-old-22.3 {embedded variables} {
431 set x " -5"
432 set y " +10"
433 expr {$x + $y}
434 } {5}
435 test expr-old-22.4 {embedded commands and variables} {expr {[set a] - 14}} 2
436 test expr-old-22.5 {embedded commands and variables} {
437 list [catch {expr {12 - [bad_command_name]}} msg] $msg
438 } {1 {invalid command name "bad_command_name"}}
439
440 # Double-quotes and things inside them.
441
442 test expr-old-23.1 {double quotes} {expr {"abc"}} abc
443 test expr-old-23.2 {double quotes} {
444 set a 189
445 expr {"$a.bc"}
446 } 189.bc
447 test expr-old-23.3 {double quotes} {
448 set b2 xyx
449 expr {"$b2$b2$b2.[set b2].[set b2]"}
450 } xyxxyxxyx.xyx.xyx
451 test expr-old-23.4 {double quotes} {expr {"11\}\}22"}} 11}}22
452 test expr-old-23.5 {double quotes} {expr {"\*bc"}} {*bc}
453 test expr-old-23.6 {double quotes} {
454 unset -nocomplain bogus__
455 list [catch {expr {"$bogus__"}} msg] $msg
456 } {1 {can't read "bogus__": no such variable}}
457 test expr-old-23.7 {double quotes} {
458 list [catch {expr {"a[error Testing]bc"}} msg] $msg
459 } {1 Testing}
460 test expr-old-23.8 {double quotes} {
461 list [catch {expr {"12398712938788234-1298379" != ""}} msg] $msg
462 } {0 1}
463
464 # Numbers in various bases.
465
466 test expr-old-24.1 {numbers in different bases} {expr 0x20} 32
467 test expr-old-24.2 {numbers in different bases} {expr 0o15} 13
468
469 # Conversions between various data types.
470
471 test expr-old-25.1 {type conversions} {expr 2+2.5} 4.5
472 test expr-old-25.2 {type conversions} {expr 2.5+2} 4.5
473 test expr-old-25.3 {type conversions} {expr 2-2.5} -0.5
474 test expr-old-25.4 {type conversions} {expr 2/2.5} 0.8
475 test expr-old-25.5 {type conversions} {expr 2>2.5} 0
476 test expr-old-25.6 {type conversions} {expr 2.5>2} 1
477 test expr-old-25.7 {type conversions} {expr 2<2.5} 1
478 test expr-old-25.8 {type conversions} {expr 2>=2.5} 0
479 test expr-old-25.9 {type conversions} {expr 2<=2.5} 1
480 test expr-old-25.10 {type conversions} {expr 2==2.5} 0
481 test expr-old-25.11 {type conversions} {expr 2!=2.5} 1
482 test expr-old-25.12 {type conversions} {expr 2>"ab"} 0
483 test expr-old-25.13 {type conversions} {expr {2>" "}} 1
484 test expr-old-25.14 {type conversions} {expr {"24.1a" > 24.1}} 1
485 test expr-old-25.15 {type conversions} {expr {24.1 > "24.1a"}} 0
486 test expr-old-25.16 {type conversions} {expr 2+2.5} 4.5
487 test expr-old-25.17 {type conversions} {expr 2+2.5} 4.5
488 test expr-old-25.18 {type conversions} {expr 2.0e2} 200.0
489 test expr-old-25.19 {type conversions} {expr 2.0e15} 2000000000000000.0
490 test expr-old-25.20 {type conversions} {expr 10.0} 10.0
491
492 # Various error conditions.
493
494 test expr-old-26.1 {error conditions} {
495 list [catch {expr 2+"a"} msg] $msg
496 } {1 {can't use non-numeric string as operand of "+"}}
497 test expr-old-26.2 {error conditions} -body {
498 expr 2+4*
499 } -returnCodes error -match glob -result *
500 test expr-old-26.3 {error conditions} -body {
501 expr 2+4*(
502 } -returnCodes error -match glob -result *
503 unset -nocomplain _non_existent_
504 test expr-old-26.4 {error conditions} {
505 list [catch {expr 2+$_non_existent_} msg] $msg
506 } {1 {can't read "_non_existent_": no such variable}}
507 set a xx
508 test expr-old-26.5 {error conditions} {
509 list [catch {expr {2+$a}} msg] $msg
510 } {1 {can't use non-numeric string as operand of "+"}}
511 test expr-old-26.6 {error conditions} {
512 list [catch {expr {2+[set a]}} msg] $msg
513 } {1 {can't use non-numeric string as operand of "+"}}
514 test expr-old-26.7 {error conditions} -body {
515 expr {2+(4}
516 } -returnCodes error -match glob -result *
517 test expr-old-26.8 {error conditions} {
518 list [catch {expr 2/0} msg] $msg $errorCode
519 } {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
520 test expr-old-26.9 {error conditions} {
521 list [catch {expr 2%0} msg] $msg $errorCode
522 } {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
523 test expr-old-26.10a {error conditions} !ieeeFloatingPoint {
524 list [catch {expr 2.0/0.0} msg] $msg $errorCode
525 } {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
526 test expr-old-26.10b {error conditions} ieeeFloatingPoint {
527 list [catch {expr 2.0/0.0} msg] $msg
528 } {0 Inf}
529 test expr-old-26.11 {error conditions} -body {
530 expr 2#
531 } -returnCodes error -match glob -result *
532 test expr-old-26.12 {error conditions} -body {
533 expr a.b
534 } -returnCodes error -match glob -result *
535 test expr-old-26.13 {error conditions} {
536 list [catch {expr {"a"/"b"}} msg] $msg
537 } {1 {can't use non-numeric string as operand of "/"}}
538 test expr-old-26.14 {error conditions} -body {
539 expr 2:3
540 } -returnCodes error -match glob -result *
541 test expr-old-26.15 {error conditions} -body {
542 expr a@b
543 } -returnCodes error -match glob -result *
544 test expr-old-26.16 {error conditions} {
545 list [catch {expr a[b} msg] $msg
546 } {1 {missing close-bracket}}
547 test expr-old-26.17 {error conditions} -body {
548 expr a`b
549 } -returnCodes error -match glob -result *
550 test expr-old-26.18 {error conditions} -body {
551 expr \"a\"\{b
552 } -returnCodes error -match glob -result *
553 test expr-old-26.19 {error conditions} -body {
554 expr a
555 } -returnCodes error -match glob -result *
556 test expr-old-26.20 {error conditions} {
557 list [catch expr msg] $msg
558 } {1 {wrong # args: should be "expr arg ?arg ...?"}}
559
560 # Cancelled evaluation.
561
562 test expr-old-27.1 {cancelled evaluation} {
563 set a 1
564 expr {0&&[set a 2]}
565 set a
566 } 1
567 test expr-old-27.2 {cancelled evaluation} {
568 set a 1
569 expr {1||[set a 2]}
570 set a
571 } 1
572 test expr-old-27.3 {cancelled evaluation} {
573 set a 1
574 expr {0?[set a 2]:1}
575 set a
576 } 1
577 test expr-old-27.4 {cancelled evaluation} {
578 set a 1
579 expr {1?2:[set a 2]}
580 set a
581 } 1
582 unset -nocomplain x
583 test expr-old-27.5 {cancelled evaluation} {
584 list [catch {expr {[info exists x] && $x}} msg] $msg
585 } {0 0}
586 test expr-old-27.6 {cancelled evaluation} {
587 list [catch {expr {0 && [concat $x]}} msg] $msg
588 } {0 0}
589 test expr-old-27.7 {cancelled evaluation} {
590 set one 1
591 list [catch {expr {1 || 1/$one}} msg] $msg
592 } {0 1}
593 test expr-old-27.8 {cancelled evaluation} {
594 list [catch {expr {1 || -"string"}} msg] $msg
595 } {0 1}
596 test expr-old-27.9 {cancelled evaluation} {
597 list [catch {expr {1 || ("string" * ("x" && "y"))}} msg] $msg
598 } {0 1}
599 test expr-old-27.10 {cancelled evaluation} {
600 set x -1.0
601 list [catch {expr {($x > 0) ? round(log($x)) : 0}} msg] $msg
602 } {0 0}
603 test expr-old-27.11 {cancelled evaluation} -body {
604 expr {0 && foo}
605 } -returnCodes error -match glob -result *
606 test expr-old-27.12 {cancelled evaluation} -body {
607 expr {0 ? 1 : foo}
608 } -returnCodes error -match glob -result *
609
610 # Tcl_ExprBool as used in "if" statements
611
612 test expr-old-28.1 {Tcl_ExprBoolean usage} {
613 set a 1
614 if {2} {set a 2}
615 set a
616 } 2
617 test expr-old-28.2 {Tcl_ExprBoolean usage} {
618 set a 1
619 if {0} {set a 2}
620 set a
621 } 1
622 test expr-old-28.3 {Tcl_ExprBoolean usage} {
623 set a 1
624 if {1.2} {set a 2}
625 set a
626 } 2
627 test expr-old-28.4 {Tcl_ExprBoolean usage} {
628 set a 1
629 if {-1.1} {set a 2}
630 set a
631 } 2
632 test expr-old-28.5 {Tcl_ExprBoolean usage} {
633 set a 1
634 if {0.0} {set a 2}
635 set a
636 } 1
637 test expr-old-28.6 {Tcl_ExprBoolean usage} {
638 set a 1
639 if {"YES"} {set a 2}
640 set a
641 } 2
642 test expr-old-28.7 {Tcl_ExprBoolean usage} {
643 set a 1
644 if {"no"} {set a 2}
645 set a
646 } 1
647 test expr-old-28.8 {Tcl_ExprBoolean usage} {
648 set a 1
649 if {"true"} {set a 2}
650 set a
651 } 2
652 test expr-old-28.9 {Tcl_ExprBoolean usage} {
653 set a 1
654 if {"fAlse"} {set a 2}
655 set a
656 } 1
657 test expr-old-28.10 {Tcl_ExprBoolean usage} {
658 set a 1
659 if {"on"} {set a 2}
660 set a
661 } 2
662 test expr-old-28.11 {Tcl_ExprBoolean usage} {
663 set a 1
664 if {"Off"} {set a 2}
665 set a
666 } 1
667 test expr-old-28.12 {Tcl_ExprBool usage} {
668 list [catch {if {"abc"} {}} msg] $msg
669 } {1 {expected boolean value but got "abc"}}
670 test expr-old-28.13 {Tcl_ExprBool usage} {
671 list [catch {if {"ogle"} {}} msg] $msg
672 } {1 {expected boolean value but got "ogle"}}
673 test expr-old-28.14 {Tcl_ExprBool usage} {
674 list [catch {if {"o"} {}} msg] $msg
675 } {1 {expected boolean value but got "o"}}
676
677 # Operands enclosed in braces
678
679 test expr-old-29.1 {braces} {expr {{abc}}} abc
680 test expr-old-29.2 {braces} {expr {{0o0010}}} 8
681 test expr-old-29.3 {braces} {expr {{3.1200000}}} 3.12
682 test expr-old-29.4 {braces} {expr {{a{b}{1 {2 3}}c}}} "a{b}{1 {2 3}}c"
683 test expr-old-29.5 {braces} -body {
684 expr "\{abc"
685 } -returnCodes error -match glob -result *
686
687 # Very long values
688
689 test expr-old-30.1 {long values} {
690 set a "0000 1111 2222 3333 4444"
691 set a "$a | $a | $a | $a | $a"
692 set a "$a || $a || $a || $a || $a"
693 expr {$a}
694 } {0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 || 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 || 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 || 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 || 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444}
695 test expr-old-30.2 {long values} {
696 set a "000000000000000000000000000000"
697 set a "$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a${a}5"
698 expr $a
699 } 5
700
701 # Expressions spanning multiple arguments
702
703 test expr-old-31.1 {multiple arguments to expr command} {
704 expr 4 + ( 6 *12) -3
705 } 73
706 test expr-old-31.2 {multiple arguments to expr command} -body {
707 expr 2 + (3 + 4
708 } -returnCodes error -match glob -result *
709 test expr-old-31.3 {multiple arguments to expr command} -body {
710 expr 2 + 3 +
711 } -returnCodes error -match glob -result *
712 test expr-old-31.4 {multiple arguments to expr command} -body {
713 expr 2 + 3 )
714 } -returnCodes error -match glob -result *
715
716 # Math functions
717
718 test expr-old-32.1 {math functions in expressions} {
719 format %.6g [expr acos(0.5)]
720 } {1.0472}
721 test expr-old-32.2 {math functions in expressions} {
722 format %.6g [expr asin(0.5)]
723 } {0.523599}
724 test expr-old-32.3 {math functions in expressions} {
725 format %.6g [expr atan(1.0)]
726 } {0.785398}
727 test expr-old-32.4 {math functions in expressions} {
728 format %.6g [expr atan2(2.0, 2.0)]
729 } {0.785398}
730 test expr-old-32.5 {math functions in expressions} {
731 format %.6g [expr ceil(1.999)]
732 } {2}
733 test expr-old-32.6 {math functions in expressions} {
734 format %.6g [expr cos(.1)]
735 } {0.995004}
736 test expr-old-32.7 {math functions in expressions} {
737 format %.6g [expr cosh(.1)]
738 } {1.005}
739 test expr-old-32.8 {math functions in expressions} {
740 format %.6g [expr exp(1.0)]
741 } {2.71828}
742 test expr-old-32.9 {math functions in expressions} {
743 format %.6g [expr floor(2.000)]
744 } {2}
745 test expr-old-32.10 {math functions in expressions} {
746 format %.6g [expr floor(2.001)]
747 } {2}
748 test expr-old-32.11 {math functions in expressions} {
749 format %.6g [expr fmod(7.3, 3.2)]
750 } {0.9}
751 test expr-old-32.12 {math functions in expressions} {
752 format %.6g [expr hypot(3.0, 4.0)]
753 } {5}
754 test expr-old-32.13 {math functions in expressions} {
755 format %.6g [expr log(2.8)]
756 } {1.02962}
757 test expr-old-32.14 {math functions in expressions} {
758 format %.6g [expr log10(2.8)]
759 } {0.447158}
760 test expr-old-32.15 {math functions in expressions} {
761 format %.6g [expr pow(2.1, 3.1)]
762 } {9.97424}
763 test expr-old-32.16 {math functions in expressions} {
764 format %.6g [expr sin(.1)]
765 } {0.0998334}
766 test expr-old-32.17 {math functions in expressions} {
767 format %.6g [expr sinh(.1)]
768 } {0.100167}
769 test expr-old-32.18 {math functions in expressions} {
770 format %.6g [expr sqrt(2.0)]
771 } {1.41421}
772 test expr-old-32.19 {math functions in expressions} {
773 format %.6g [expr tan(0.8)]
774 } {1.02964}
775 test expr-old-32.20 {math functions in expressions} {
776 format %.6g [expr tanh(0.8)]
777 } {0.664037}
778 test expr-old-32.21 {math functions in expressions} {
779 format %.6g [expr abs(-1.8)]
780 } {1.8}
781 test expr-old-32.22 {math functions in expressions} {
782 expr abs(10.0)
783 } {10.0}
784 test expr-old-32.23 {math functions in expressions} {
785 format %.6g [expr abs(-4)]
786 } {4}
787 test expr-old-32.24 {math functions in expressions} {
788 format %.6g [expr abs(66)]
789 } {66}
790
791 test expr-old-32.25a {math functions in expressions} {
792 expr abs(0x8000000000000000)
793 } [expr 1<<63]
794
795 test expr-old-32.25b {math functions in expressions} {
796 expr abs(0x80000000)
797 } 2147483648
798
799 test expr-old-32.26 {math functions in expressions} {
800 expr double(1)
801 } {1.0}
802 test expr-old-32.27 {math functions in expressions} {
803 expr double(1.1)
804 } {1.1}
805 test expr-old-32.28 {math functions in expressions} {
806 expr int(1)
807 } {1}
808 test expr-old-32.29 {math functions in expressions} {
809 expr int(1.4)
810 } {1}
811 test expr-old-32.30 {math functions in expressions} {
812 expr int(1.6)
813 } {1}
814 test expr-old-32.31 {math functions in expressions} {
815 expr int(-1.4)
816 } {-1}
817 test expr-old-32.32 {math functions in expressions} {
818 expr int(-1.6)
819 } {-1}
820 test expr-old-32.33 {math functions in expressions} {
821 expr int(1e60)
822 } 0
823 test expr-old-32.34 {math functions in expressions} {
824 expr int(-1e60)
825 } 0
826 test expr-old-32.35 {math functions in expressions} {
827 expr round(1.49)
828 } {1}
829 test expr-old-32.36 {math functions in expressions} {
830 expr round(1.51)
831 } {2}
832 test expr-old-32.37 {math functions in expressions} {
833 expr round(-1.49)
834 } {-1}
835 test expr-old-32.38 {math functions in expressions} {
836 expr round(-1.51)
837 } {-2}
838 test expr-old-32.39 {math functions in expressions} {
839 expr round(1e60)
840 } 999999999999999949387135297074018866963645011013410073083904
841 test expr-old-32.40 {math functions in expressions} {
842 expr round(-1e60)
843 } -999999999999999949387135297074018866963645011013410073083904
844 test expr-old-32.41 {math functions in expressions} {
845 list [catch {expr pow(1.0 + 3.0 - 2, .8 * 5)} msg] $msg
846 } {0 16.0}
847 test expr-old-32.42 {math functions in expressions} {
848 list [catch {expr hypot(5*.8,3)} msg] $msg
849 } {0 5.0}
850 test expr-old-32.43 {math functions in expressions} testmathfunctions {
851 expr 2*T1()
852 } 246
853 test expr-old-32.44 {math functions in expressions} testmathfunctions {
854 expr T2()*3
855 } 1035
856 test expr-old-32.45 {math functions in expressions} {
857 expr (0 <= rand()) && (rand() < 1)
858 } {1}
859 test expr-old-32.46 {math functions in expressions} -body {
860 list [catch {expr rand(24)} msg] $msg
861 } -match glob -result {1 {too many arguments for math function*}}
862 test expr-old-32.47 {math functions in expressions} -body {
863 list [catch {expr srand()} msg] $msg
864 } -match glob -result {1 {not enough arguments for math function*}}
865 test expr-old-32.48 {math functions in expressions} -body {
866 expr srand(3.79)
867 } -returnCodes error -match glob -result *
868 test expr-old-32.49 {math functions in expressions} -body {
869 expr srand("")
870 } -returnCodes error -match glob -result *
871 test expr-old-32.50 {math functions in expressions} {
872 set result [expr round(srand(12345) * 1000)]
873 for {set i 0} {$i < 10} {incr i} {
874 lappend result [expr round(rand() * 1000)]
875 }
876 set result
877 } {97 834 948 36 12 51 766 585 914 784 333}
878 test expr-old-32.51 {math functions in expressions} -body {
879 expr {srand([lindex "6ty" 0])}
880 } -returnCodes error -match glob -result *
881 test expr-old-32.52 {math functions in expressions} {
882 expr {srand(int(1<<37)) < 1}
883 } {1}
884 test expr-old-32.53 {math functions in expressions} {
885 expr {srand((1<<31) - 1) > 0}
886 } {1}
887
888 test expr-old-33.1 {conversions and fancy args to math functions} {
889 expr hypot ( 3 , 4 )
890 } 5.0
891 test expr-old-33.2 {conversions and fancy args to math functions} {
892 expr hypot ( (2.0+1.0) , 4 )
893 } 5.0
894 test expr-old-33.3 {conversions and fancy args to math functions} {
895 expr hypot ( 3 , (3.0 + 1.0) )
896 } 5.0
897 test expr-old-33.4 {conversions and fancy args to math functions} {
898 format %.6g [expr cos(acos(0.1))]
899 } 0.1
900
901 test expr-old-34.1 {errors in math functions} -body {
902 list [catch {expr func_2(1.0)} msg] $msg
903 } -match glob -result {1 {* "*func_2"}}
904 test expr-old-34.2 {errors in math functions} -body {
905 expr func|(1.0)
906 } -returnCodes error -match glob -result *
907 test expr-old-34.3 {errors in math functions} {
908 list [catch {expr {hypot("a b", 2.0)}} msg] $msg
909 } {1 {expected floating-point number but got "a b"}}
910 test expr-old-34.4 {errors in math functions} -body {
911 expr hypot(1.0 2.0)
912 } -returnCodes error -match glob -result *
913 test expr-old-34.5 {errors in math functions} -body {
914 expr hypot(1.0, 2.0
915 } -returnCodes error -match glob -result *
916 test expr-old-34.6 {errors in math functions} -body {
917 expr hypot(1.0 ,
918 } -returnCodes error -match glob -result *
919 test expr-old-34.7 {errors in math functions} -body {
920 list [catch {expr hypot(1.0)} msg] $msg
921 } -match glob -result {1 {not enough arguments for math function*}}
922 test expr-old-34.8 {errors in math functions} -body {
923 list [catch {expr hypot(1.0, 2.0, 3.0)} msg] $msg
924 } -match glob -result {1 {too many arguments for math function*}}
925 test expr-old-34.9 {errors in math functions} {
926 list [catch {expr acos(-2.0)} msg] $msg $errorCode
927 } {1 {domain error: argument not in valid range} {ARITH DOMAIN {domain error: argument not in valid range}}}
928 test expr-old-34.10 {errors in math functions} {
929 list [catch {expr pow(-3, 1000001)} msg] $msg
930 } {0 -Inf}
931 test expr-old-34.11a {errors in math functions} !ieeeFloatingPoint {
932 list [catch {expr pow(3, 1000001)} msg] $msg $errorCode
933 } {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}}
934 test expr-old-34.11b {errors in math functions} ieeeFloatingPoint {
935 list [catch {expr pow(3, 1000001)} msg] $msg
936 } {0 Inf}
937 test expr-old-34.12a {errors in math functions} !ieeeFloatingPoint {
938 list [catch {expr -14.0*exp(100000)} msg] $msg $errorCode
939 } {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}}
940 test expr-old-34.12b {errors in math functions} ieeeFloatingPoint {
941 list [catch {expr -14.0*exp(100000)} msg] $msg
942 } {0 -Inf}
943 test expr-old-34.13 {errors in math functions} {
944 expr wide(1.0e30)
945 } 5076964154930102272
946 test expr-old-34.14 {errors in math functions} {
947 expr wide(-1.0e30)
948 } -5076964154930102272
949 test expr-old-34.15 {errors in math functions} {
950 expr round(1.0e30)
951 } 1000000000000000019884624838656
952 test expr-old-34.16 {errors in math functions} {
953 expr round(-1.0e30)
954 } -1000000000000000019884624838656
955 test expr-old-34.17 {errors in math functions} -constraints testmathfunctions \
956 -body {
957 list [catch {expr T1(4)} msg] $msg
958 } -match glob -result {1 {too many arguments for math function*}}
959
960 test expr-old-36.1 {ExprLooksLikeInt procedure} -body {
961 expr 0o289
962 } -returnCodes error -match glob -result {*invalid octal number*}
963 test expr-old-36.2 {ExprLooksLikeInt procedure} {
964 set x 0o289
965 list [catch {expr {$x+1}} msg] $msg
966 } {1 {can't use invalid octal number as operand of "+"}}
967 test expr-old-36.3 {ExprLooksLikeInt procedure} {
968 list [catch {expr 0289.1} msg] $msg
969 } {0 289.1}
970 test expr-old-36.4 {ExprLooksLikeInt procedure} {
971 set x 0289.1
972 list [catch {expr {$x+1}} msg] $msg
973 } {0 290.1}
974 test expr-old-36.5 {ExprLooksLikeInt procedure} {
975 set x { +22}
976 list [catch {expr {$x+1}} msg] $msg
977 } {0 23}
978 test expr-old-36.6 {ExprLooksLikeInt procedure} {
979 set x { -22}
980 list [catch {expr {$x+1}} msg] $msg
981 } {0 -21}
982 test expr-old-36.7 {ExprLooksLikeInt procedure} {
983 list [catch {expr nan} msg] $msg
984 } {1 {domain error: argument not in valid range}}
985 test expr-old-36.8 {ExprLooksLikeInt procedure} {
986 list [catch {expr 78e1} msg] $msg
987 } {0 780.0}
988 test expr-old-36.9 {ExprLooksLikeInt procedure} {
989 list [catch {expr 24E1} msg] $msg
990 } {0 240.0}
991 test expr-old-36.10 {ExprLooksLikeInt procedure} -body {
992 expr 78e
993 } -returnCodes error -match glob -result *
994
995 # test for [Bug #542588]
996 test expr-old-36.11 {ExprLooksLikeInt procedure} {
997 # define a "too large integer"; this one works also for 64bit arith
998 set x 665802003400000000000000
999 expr {$x+1}
1000 } 665802003400000000000001
1001
1002 # tests for [Bug #587140]
1003 test expr-old-36.12 {ExprLooksLikeInt procedure} {
1004 set x "10;"
1005 list [catch {expr {$x+1}} msg] $msg
1006 } {1 {can't use non-numeric string as operand of "+"}}
1007 test expr-old-36.13 {ExprLooksLikeInt procedure} {
1008 set x " +"
1009 list [catch {expr {$x+1}} msg] $msg
1010 } {1 {can't use non-numeric string as operand of "+"}}
1011 test expr-old-36.14 {ExprLooksLikeInt procedure} {
1012 set x "123456789012345678901234567890 "
1013 expr {$x+1}
1014 } 123456789012345678901234567891
1015 test expr-old-36.15 {ExprLooksLikeInt procedure} {
1016 set x "0o99 "
1017 list [catch {expr {$x+1}} msg] $msg
1018 } {1 {can't use invalid octal number as operand of "+"}}
1019 test expr-old-36.16 {ExprLooksLikeInt procedure} {
1020 set x " 0xffffffffffffffffffffffffffffffffffffff "
1021 expr {$x+1}
1022 } [expr 0x100000000000000000000000000000000000000]
1023
1024 test expr-old-37.1 {Check that Tcl_ExprLong doesn't modify interpreter result if no error} testexprlong {
1025 testexprlong 4+1
1026 } {This is a result: 5}
1027 #Check for [Bug 1109484]
1028 test expr-old-37.2 {Tcl_ExprLong handles wide ints gracefully} testexprlong {
1029 testexprlong wide(1)+2
1030 } {This is a result: 3}
1031
1032 test expr-old-37.3 {Tcl_ExprLong on the empty string} testexprlong {
1033 testexprlong ""
1034 } {This is a result: 0}
1035 test expr-old-37.4 {Tcl_ExprLong coerces doubles} testexprlong {
1036 testexprlong 3+.14159
1037 } {This is a result: 3}
1038 test expr-old-37.5 {Tcl_ExprLong handles overflows} {testexprlong longIs32bit} {
1039 testexprlong 0x80000000
1040 } {This is a result: -2147483648}
1041 test expr-old-37.6 {Tcl_ExprLong handles overflows} {testexprlong longIs32bit} {
1042 testexprlong 0xffffffff
1043 } {This is a result: -1}
1044 test expr-old-37.7 {Tcl_ExprLong handles overflows} \
1045 -constraints {testexprlong longIs32bit} \
1046 -match glob \
1047 -body {
1048 list [catch {testexprlong 0x100000000} result] $result
1049 } \
1050 -result {1 {integer value too large to represent*}}
1051 test expr-old-37.8 {Tcl_ExprLong handles overflows} testexprlong {
1052 testexprlong -0x80000000
1053 } {This is a result: -2147483648}
1054 test expr-old-37.9 {Tcl_ExprLong handles overflows} {testexprlong longIs32bit} {
1055 testexprlong -0xffffffff
1056 } {This is a result: 1}
1057 test expr-old-37.10 {Tcl_ExprLong handles overflows} \
1058 -constraints {testexprlong longIs32bit} \
1059 -match glob \
1060 -body {
1061 list [catch {testexprlong -0x100000000} result] $result
1062 } \
1063 -result {1 {integer value too large to represent*}}
1064 test expr-old-37.11 {Tcl_ExprLong handles overflows} {testexprlong longIs32bit} {
1065 testexprlong 2147483648.
1066 } {This is a result: -2147483648}
1067 test expr-old-37.12 {Tcl_ExprLong handles overflows} {testexprlong longIs32bit} {
1068 testexprlong 4294967295.
1069 } {This is a result: -1}
1070 test expr-old-37.13 {Tcl_ExprLong handles overflows} \
1071 -constraints {testexprlong longIs32bit} \
1072 -match glob \
1073 -body {
1074 list [catch {testexprlong 4294967296.} result] $result
1075 } \
1076 -result {1 {integer value too large to represent*}}
1077 test expr-old-37.14 {Tcl_ExprLong handles overflows} testexprlong {
1078 testexprlong -2147483648.
1079 } {This is a result: -2147483648}
1080 test expr-old-37.15 {Tcl_ExprLong handles overflows} {testexprlong longIs32bit} {
1081 testexprlong -4294967295.
1082 } {This is a result: 1}
1083 test expr-old-37.16 {Tcl_ExprLong handles overflows} \
1084 -constraints {testexprlong longIs32bit} \
1085 -match glob \
1086 -body {
1087 list [catch {testexprlong 4294967296.} result] $result
1088 } \
1089 -result {1 {integer value too large to represent*}}
1090
1091 test expr-old-37.17 {Check that Tcl_ExprDouble doesn't modify interpreter result if no error} testexprdouble {
1092 testexprdouble 4.+1.
1093 } {This is a result: 5.0}
1094 #Check for [Bug 1109484]
1095 test expr-old-37.18 {Tcl_ExprDouble on the empty string} testexprdouble {
1096 testexprdouble ""
1097 } {This is a result: 0.0}
1098 test expr-old-37.19 {Tcl_ExprDouble coerces wides} testexprdouble {
1099 testexprdouble 1[string repeat 0 17]
1100 } {This is a result: 1e+17}
1101 test expr-old-37.20 {Tcl_ExprDouble coerces bignums} testexprdouble {
1102 testexprdouble 1[string repeat 0 38]
1103 } {This is a result: 1e+38}
1104 test expr-old-37.21 {Tcl_ExprDouble handles overflows} testexprdouble {
1105 testexprdouble 17976931348623157[string repeat 0 292].
1106 } {This is a result: 1.7976931348623157e+308}
1107 test expr-old-37.22 {Tcl_ExprDouble handles overflows that look like int} \
1108 testexprdouble {
1109 testexprdouble 17976931348623157[string repeat 0 292]
1110 } {This is a result: 1.7976931348623157e+308}
1111 test expr-old-37.23 {Tcl_ExprDouble handles overflows} \
1112 ieeeFloatingPoint&&testexprdouble {
1113 testexprdouble 17976931348623165[string repeat 0 292].
1114 } {This is a result: Inf}
1115 test expr-old-37.24 {Tcl_ExprDouble handles overflows that look like int} \
1116 ieeeFloatingPoint&&testexprdouble {
1117 testexprdouble 17976931348623165[string repeat 0 292]
1118 } {This is a result: Inf}
1119 test expr-old-37.25 {Tcl_ExprDouble and NaN} \
1120 {ieeeFloatingPoint testexprdouble} {
1121 list [catch {testexprdouble 0.0/0.0} result] $result
1122 } {1 {domain error: argument not in valid range}}
1123
1124 test expr-old-38.1 {Verify Tcl_ExprString's basic operation} -constraints {testexprstring} -body {
1125 list [testexprstring "1+4"] [testexprstring "2*3+4.2"] \
1126 [catch {testexprstring "1+"} msg] $msg
1127 } -match glob -result {5 10.2 1 *}
1128 test expr-old-38.2 {Tcl_ExprString} testexprstring {
1129 # This one is "magical"
1130 testexprstring {}
1131 } 0
1132 test expr-old-38.3 {Tcl_ExprString} -constraints testexprstring -body {
1133 testexprstring { }
1134 } -returnCodes error -match glob -result *
1135
1136 #
1137 # Test for bug #908375: rounding numbers that do not fit in a
1138 # long but do fit in a wide
1139 #
1140
1141 test expr-old-39.1 {Rounding with wide result} {
1142 set x 1.0e10
1143 set y [expr $x + 0.1]
1144 catch {
1145 set x [list [expr {$x == round($y)}] [expr $x == -round(-$y)]]
1146 }
1147 set x
1148 } {1 1}
1149 unset -nocomplain x y
1150
1151 #
1152 # TIP #255 min and max math functions
1153 #
1154
1155 test expr-old-40.1 {min math function} -body {
1156 expr {min(0)}
1157 } -result 0
1158 test expr-old-40.2 {min math function} -body {
1159 expr {min(0.0)}
1160 } -result 0.0
1161 test expr-old-40.3 {min math function} -body {
1162 list [catch {expr {min()}} msg] $msg
1163 } -result {1 {not enough arguments to math function "min"}}
1164 test expr-old-40.4 {min math function} -body {
1165 expr {min(wide(-1) << 30, 4.5, -10)}
1166 } -result [expr {wide(-1) << 30}]
1167 test expr-old-40.5 {min math function} -body {
1168 expr {min("a", 0)}
1169 } -returnCodes error -match glob -result *
1170 test expr-old-40.6 {min math function} -body {
1171 expr {min(300, "0xFF")}
1172 } -result 255
1173
1174 test expr-old-41.1 {max math function} -body {
1175 expr {max(0)}
1176 } -result 0
1177 test expr-old-41.2 {max math function} -body {
1178 expr {max(0.0)}
1179 } -result 0.0
1180 test expr-old-41.3 {max math function} -body {
1181 list [catch {expr {max()}} msg] $msg
1182 } -result {1 {not enough arguments to math function "max"}}
1183 test expr-old-41.4 {max math function} -body {
1184 expr {max(wide(1) << 30, 4.5, -10)}
1185 } -result [expr {wide(1) << 30}]
1186 test expr-old-41.5 {max math function} -body {
1187 expr {max("a", 0)}
1188 } -returnCodes error -match glob -result *
1189 test expr-old-41.6 {max math function} -body {
1190 expr {max(200, "0xFF")}
1191 } -result 255
1192
1193 # Special test for Pentium arithmetic bug of 1994:
1194
1195 if {(4195835.0 - (4195835.0/3145727.0)*3145727.0) == 256.0} {
1196 puts "Warning: this machine contains a defective Pentium processor"
1197 puts "that performs arithmetic incorrectly. I recommend that you"
1198 puts "call Intel customer service immediately at 1-800-628-8686"
1199 puts "to request a replacement processor."
1200 }
1201
1202 # cleanup
1203 ::tcltest::cleanupTests
1204 return
1205
1206 # Local Variables:
1207 # mode: tcl
1208 # End: