OSDN Git Service

56cef1971e31c13b15da85b6f02d950a3edadb2c
[pf3gnuchains/sourceware.git] / tcl / tests / expr.test
1 # Commands covered: expr
2 #
3 # This file contains a collection of tests for one or more of the Tcl
4 # built-in commands. Sourcing this file into Tcl runs the tests and
5 # generates output for errors.  No output means no errors were found.
6 #
7 # Copyright (c) 1996-1997 Sun Microsystems, Inc.
8 # Copyright (c) 1998-1999 by Scriptics Corporation.
9 #
10 # See the file "license.terms" for information on usage and redistribution
11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 #
13 # RCS: @(#) $Id$
14
15 if {[lsearch [namespace children] ::tcltest] == -1} {
16     package require tcltest
17     namespace import -force ::tcltest::*
18 }
19
20 if {([catch {expr T1()} msg] == 1) && ($msg == {unknown math function "T1"})} {
21     set gotT1 0
22     puts "This application hasn't been compiled with the \"T1\" and"
23     puts "\"T2\" math functions, so I'll skip some of the expr tests."
24 } else {
25     set gotT1 1
26 }
27
28 # procedures used below
29
30 proc put_hello_char {c} {
31     global a
32     append a [format %c $c]
33     return $c
34 }
35 proc hello_world {} {
36     global a
37     set a ""
38     set L1 [set l0 [set h_1 [set q 0]]]
39     for {put_hello_char [expr [put_hello_char [expr [set h 7]*10+2]]+29]} {$l0?[put_hello_char $l0]
40         :!$h_1} {put_hello_char $ll;expr {$L1==2?[set ll [expr 32+0-0+[set bar 0]]]:0}} {expr {[incr L1]==[expr 1+([string length "abc"]-[string length "abc"])]
41         ?[set ll [set l0 [expr 54<<1]]]:$ll==108&&$L1<3?
42         [incr ll [expr 1|1<<1]; set ll $ll; set ll $ll; set ll $ll; set ll $ll; set l0 [expr ([string length "abc"]-[string length "abc"])+([string length "abc"]-[string length "abc"])-([string length "abc"]-[string length "abc"])+([string length "abc"]-[string length "abc"])]; set l0; set l0 $l0; set l0; set l0]:$L1==4&&$ll==32?[set ll [expr 19+$h1+([string length "abc"]-[string length "abc"])-([string length "abc"]-[string length "abc"])+([string length "abc"]-[string length "abc"])-([string length "abc"]-[string length "abc"])+[set foo [expr ([string length "abc"]-[string length "abc"])+([string length "abc"]-[string length "abc"])+([string length "abc"]-[string length "abc"])]]]]
43         :[set q [expr $q-$h1+([string length "abc"]-[string length "abc"])-([string length "abc"]-[string length "abc"])]]};expr {$L1==5?[incr ll -8; set ll $ll; set ll]:$q&&$h1&&1};expr {$L1==4+2
44         ?[incr ll 3]:[expr ([string length "abc"]-[string length "abc"])+1]};expr {$ll==($h<<4)+2+0&&$L1!=6?[incr ll -6]:[set h1 [expr 100+([string length "abc"]-[string length "abc"])-([string length "abc"]-[string length "abc"])]]}
45         expr {$L1!=1<<3?[incr q [expr ([string length "abc"]-[string length "abc"])-1]]:[set h_1 [set ll $h1]]}
46     }
47     set a
48 }
49
50 proc 12days {a b c} {
51     global xxx
52     expr {1<$a?[expr {$a<3?[12days -79 -13 [string range $c [12days -87 \
53         [expr 1-$b] [string range $c [12days -86 0 [string range $c 1 end]] \
54         end]] end]]:1};expr {$a<$b?[12days [expr $a+1] $b $c]:3};expr {[12days \
55         -94 [expr $a-27] $c]&&$a==2?$b<13?[12days 2 [expr $b+1] "%s %d %d\n"]:9
56         :16}]:$a<0?$a<-72?[12days $b $a "@n'+,#'/*\{\}w+/w#cdnr/+,\{\}r/*de\}+,/*\{*+,/w\{%+,/w#q#n+,/#\{l+,/n\{n+,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,\}\{w+K w'K:'+\}e#';dq#'l q#'+d'K#!/+k#;q#'r\}eKK#\}w'r\}eKK\{nl\]'/#;#q#n')\{)#\}w')\{)\{nl\]'/+#n';d\}rw' i;# )\{nl\]!/n\{n#'; r\{#w'r nc\{nl\]'/#\{l,+'K \{rw' iK\{;\[\{nl\]'/w#q#n'wk nw' iwk\{KK\{nl\]!/w\{%'l##w#' i; :\{nl\]'/*\{q#'ld;r'\}\{nlwb!/*de\}'c ;;\{nl'-\{\}rw\]'/+,\}##'*\}#nc,',#nw\]'/+kd'+e\}+;#'rdq#w! nr'/ ') \}+\}\{rl#'\{n' ')# \}'+\}##(!!/"]
57         :$a<-50?[string compare [format %c $b] [string index $c 0]]==0?[append \
58         xxx [string index $c 31];scan [string index $c 31] %c x;set x]
59         :[12days -65 $b [string range $c 1 end]]:[12days [expr ([string compare \
60         [string index $c 0] "/"]==0)+$a] $b [string range $c 1 end]]:0<$a
61         ?[12days 2 2 "%s"]:[string compare [string index $c 0] "/"]==0||
62         [12days 0 [12days -61 [scan [string index $c 0] %c x; set x] \
63         "!ek;dc i@bK'(q)-\[w\]*%n+r3#l,\{\}:\nuwloca-O;m .vpbks,fxntdCeghiry"] \
64         [string range $c 1 end]]}
65 }
66 proc do_twelve_days {} {
67     global xxx
68     set xxx ""
69     12days 1 1 1
70     string length $xxx
71 }
72
73 # start of tests
74
75 catch {unset a b i x}
76
77 test expr-1.1 {TclCompileExprCmd: no expression} {
78     list [catch {expr  } msg] $msg
79 } {1 {wrong # args: should be "expr arg ?arg ...?"}}
80 test expr-1.2 {TclCompileExprCmd: one expression word} {
81     expr -25
82 } -25
83 test expr-1.3 {TclCompileExprCmd: two expression words} {
84     expr -8.2   -6
85 } -14.2
86 test expr-1.4 {TclCompileExprCmd: five expression words} {
87     expr 20 - 5 +10 -7
88 } 18
89 test expr-1.5 {TclCompileExprCmd: quoted expression word} {
90     expr "0005"
91 } 5
92 test expr-1.6 {TclCompileExprCmd: quoted expression word} {
93     catch {expr "0005"zxy} msg
94     set msg
95 } {extra characters after close-quote}
96 test expr-1.7 {TclCompileExprCmd: expression word in braces} {
97     expr {-0005}
98 } -5
99 test expr-1.8 {TclCompileExprCmd: expression word in braces} {
100     expr {{-0x1234}}
101 } -4660
102 test expr-1.9 {TclCompileExprCmd: expression word in braces} {
103     catch {expr {-0005}foo} msg
104     set msg
105 } {extra characters after close-brace}
106 test expr-1.10 {TclCompileExprCmd: other expression word in braces} {
107     expr 4*[llength "6 2"]
108 } 8
109 test expr-1.11 {TclCompileExprCmd: expression word terminated by ;} {
110     expr 4*[llength "6 2"];
111 } 8
112 test expr-1.12 {TclCompileExprCmd: inlined expr (in "catch") inside other catch} {
113     set a xxx
114     catch {
115         # Might not be a number
116         set a [expr 10*$a]
117     }
118 } 1
119 test expr-1.13 {TclCompileExprCmd: second level of substitutions in expr not in braces with single var reference} {
120     set a xxx
121     set x 27;  set bool {$x};  if $bool {set a foo}
122     set a
123 } foo
124 test expr-1.14 {TclCompileExprCmd: second level of substitutions in expr with comparison as top-level operator} {
125     set a xxx
126     set x 2;  set b {$x};  set a [expr $b == 2]
127     set a
128 } 1
129
130 test expr-2.1 {TclCompileExpr: are builtin functions registered?} {
131     expr double(5*[llength "6 2"])
132 } 10.0
133 test expr-2.2 {TclCompileExpr: error in expr} {
134     catch {expr 2**3} msg
135     set msg
136 } {syntax error in expression "2**3"}
137 test expr-2.3 {TclCompileExpr: junk after legal expr} {
138     catch {expr 7*[llength "a b"]foo} msg
139     set msg
140 } {syntax error in expression "7*2foo"}
141 test expr-2.4 {TclCompileExpr: numeric expr string rep == formatted int rep} {
142     expr {0001}
143 } 1
144
145 test expr-3.1 {CompileCondExpr: just lor expr} {expr 3||0} 1
146 test expr-3.2 {CompileCondExpr: error in lor expr} {
147     catch {expr x||3} msg
148     set msg
149 } {syntax error in expression "x||3"} 
150 test expr-3.3 {CompileCondExpr: test true arm} {expr 3>2?44:66} 44
151 test expr-3.4 {CompileCondExpr: error compiling true arm} {
152     catch {expr 3>2?2**3:66} msg
153     set msg
154 } {syntax error in expression "3>2?2**3:66"}
155 test expr-3.5 {CompileCondExpr: test false arm} {expr 2>3?44:66} 66
156 test expr-3.6 {CompileCondExpr: error compiling false arm} {
157     catch {expr 2>3?44:2**3} msg
158     set msg
159 } {syntax error in expression "2>3?44:2**3"}
160 test expr-3.7 {CompileCondExpr: long arms & nested cond exprs} {unixOnly nonPortable} {
161     puts "Note: doing test expr-3.7 which can take several minutes to run"
162     hello_world
163 } {Hello world}
164 catch {unset xxx}
165 test expr-3.8 {CompileCondExpr: long arms & nested cond exprs} {unixOnly nonPortable} {
166     puts "Note: doing test expr-3.8 which can take several minutes to run"
167     do_twelve_days
168 } 2358
169 catch {unset xxx}
170
171 test expr-4.1 {CompileLorExpr: just land expr} {expr 1.3&&3.3} 1
172 test expr-4.2 {CompileLorExpr: error in land expr} {
173     catch {expr x&&3} msg
174     set msg
175 } {syntax error in expression "x&&3"} 
176 test expr-4.3 {CompileLorExpr: simple lor exprs} {expr 0||1.0} 1
177 test expr-4.4 {CompileLorExpr: simple lor exprs} {expr 3.0||0.0} 1
178 test expr-4.5 {CompileLorExpr: simple lor exprs} {expr 0||0||1} 1
179 test expr-4.6 {CompileLorExpr: error compiling lor arm} {
180     catch {expr 2**3||4.0} msg
181     set msg
182 } {syntax error in expression "2**3||4.0"}
183 test expr-4.7 {CompileLorExpr: error compiling lor arm} {
184     catch {expr 1.3||2**3} msg
185     set msg
186 } {syntax error in expression "1.3||2**3"}
187 test expr-4.8 {CompileLorExpr: error compiling lor arms} {
188     list [catch {expr {"a"||"b"}} msg] $msg
189 } {1 {expected boolean value but got "a"}}
190 test expr-4.9 {CompileLorExpr: long lor arm} {
191     set a "abcdefghijkl"
192     set i 7
193     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]]}
194 } 1
195
196 test expr-5.1 {CompileLandExpr: just bitor expr} {expr 7|0x13} 23
197 test expr-5.2 {CompileLandExpr: error in bitor expr} {
198     catch {expr x|3} msg
199     set msg
200 } {syntax error in expression "x|3"} 
201 test expr-5.3 {CompileLandExpr: simple land exprs} {expr 0&&1.0} 0
202 test expr-5.4 {CompileLandExpr: simple land exprs} {expr 0&&0} 0
203 test expr-5.5 {CompileLandExpr: simple land exprs} {expr 3.0&&1.2} 1
204 test expr-5.6 {CompileLandExpr: simple land exprs} {expr 1&&1&&2} 1
205 test expr-5.7 {CompileLandExpr: error compiling land arm} {
206     catch {expr 2**3&&4.0} msg
207     set msg
208 } {syntax error in expression "2**3&&4.0"}
209 test expr-5.8 {CompileLandExpr: error compiling land arm} {
210     catch {expr 1.3&&2**3} msg
211     set msg
212 } {syntax error in expression "1.3&&2**3"}
213 test expr-5.9 {CompileLandExpr: error compiling land arm} {
214     list [catch {expr {"a"&&"b"}} msg] $msg
215 } {1 {expected boolean value but got "a"}}
216 test expr-5.10 {CompileLandExpr: long land arms} {
217     set a "abcdefghijkl"
218     set i 7
219     expr {[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]] && [string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]] && [string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]] && [string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]^[string compare [format %c 103] [string index $a $i]]^[string compare [format %c 105] [string index $a $i]]}
220 } 1
221
222 test expr-6.1 {CompileBitXorExpr: just bitand expr} {expr 7&0x13} 3
223 test expr-6.2 {CompileBitXorExpr: error in bitand expr} {
224     catch {expr x|3} msg
225     set msg
226 } {syntax error in expression "x|3"} 
227 test expr-6.3 {CompileBitXorExpr: simple bitxor exprs} {expr 7^0x13} 20
228 test expr-6.4 {CompileBitXorExpr: simple bitxor exprs} {expr 3^0x10} 19
229 test expr-6.5 {CompileBitXorExpr: simple bitxor exprs} {expr 0^7} 7
230 test expr-6.6 {CompileBitXorExpr: simple bitxor exprs} {expr -1^7} -8
231 test expr-6.7 {CompileBitXorExpr: error compiling bitxor arm} {
232     catch {expr 2**3|6} msg
233     set msg
234 } {syntax error in expression "2**3|6"}
235 test expr-6.8 {CompileBitXorExpr: error compiling bitxor arm} {
236     catch {expr 2^x} msg
237     set msg
238 } {syntax error in expression "2^x"}
239 test expr-6.9 {CompileBitXorExpr: runtime error in bitxor arm} {
240     list [catch {expr {24.0^3}} msg] $msg
241 } {1 {can't use floating-point value as operand of "^"}}
242 test expr-6.10 {CompileBitXorExpr: runtime error in bitxor arm} {
243     list [catch {expr {"a"^"b"}} msg] $msg
244 } {1 {can't use non-numeric string as operand of "^"}}
245
246 test expr-7.1 {CompileBitAndExpr: just equality expr} {expr 3==2} 0
247 test expr-7.2 {CompileBitAndExpr: just equality expr} {expr 2.0==2} 1
248 test expr-7.3 {CompileBitAndExpr: just equality expr} {expr 3.2!=2.2} 1
249 test expr-7.4 {CompileBitAndExpr: just equality expr} {expr {"abc" == "abd"}} 0
250 test expr-7.5 {CompileBitAndExpr: error in equality expr} {
251     catch {expr x==3} msg
252     set msg
253 } {syntax error in expression "x==3"} 
254 test expr-7.6 {CompileBitAndExpr: simple bitand exprs} {expr 7&0x13} 3
255 test expr-7.7 {CompileBitAndExpr: simple bitand exprs} {expr 0xf2&0x53} 82
256 test expr-7.8 {CompileBitAndExpr: simple bitand exprs} {expr 3&6} 2
257 test expr-7.9 {CompileBitAndExpr: simple bitand exprs} {expr -1&-7} -7
258 test expr-7.10 {CompileBitAndExpr: error compiling bitand arm} {
259     catch {expr 2**3&6} msg
260     set msg
261 } {syntax error in expression "2**3&6"}
262 test expr-7.11 {CompileBitAndExpr: error compiling bitand arm} {
263     catch {expr 2&x} msg
264     set msg
265 } {syntax error in expression "2&x"}
266 test expr-7.12 {CompileBitAndExpr: runtime error in bitand arm} {
267     list [catch {expr {24.0&3}} msg] $msg
268 } {1 {can't use floating-point value as operand of "&"}}
269 test expr-7.13 {CompileBitAndExpr: runtime error in bitand arm} {
270     list [catch {expr {"a"&"b"}} msg] $msg
271 } {1 {can't use non-numeric string as operand of "&"}}
272
273 test expr-8.1 {CompileEqualityExpr: just relational expr} {expr 3>=2} 1
274 test expr-8.2 {CompileEqualityExpr: just relational expr} {expr 2<=2.1} 1
275 test expr-8.3 {CompileEqualityExpr: just relational expr} {expr 3.2>"2.2"} 1
276 test expr-8.4 {CompileEqualityExpr: just relational expr} {expr {"0y"<"0x12"}} 0
277 test expr-8.5 {CompileEqualityExpr: error in relational expr} {
278     catch {expr x>3} msg
279     set msg
280 } {syntax error in expression "x>3"} 
281 test expr-8.6 {CompileEqualityExpr: simple equality exprs} {expr 7==0x13} 0
282 test expr-8.7 {CompileEqualityExpr: simple equality exprs} {expr -0xf2!=0x53} 1
283 test expr-8.8 {CompileEqualityExpr: simple equality exprs} {expr {"12398712938788234-1298379" != ""}} 1
284 test expr-8.9 {CompileEqualityExpr: simple equality exprs} {expr -1!="abc"} 1
285 test expr-8.10 {CompileEqualityExpr: error compiling equality arm} {
286     catch {expr 2**3==6} msg
287     set msg
288 } {syntax error in expression "2**3==6"}
289 test expr-8.11 {CompileEqualityExpr: error compiling equality arm} {
290     catch {expr 2!=x} msg
291     set msg
292 } {syntax error in expression "2!=x"}
293
294
295 test expr-9.1 {CompileRelationalExpr: just shift expr} {expr 3<<2} 12
296 test expr-9.2 {CompileRelationalExpr: just shift expr} {expr 0xff>>2} 63
297 test expr-9.3 {CompileRelationalExpr: just shift expr} {expr -1>>2} -1
298 test expr-9.4 {CompileRelationalExpr: just shift expr} {expr {1<<3}} 8
299
300 # The following test is different for 32-bit versus 64-bit
301 # architectures because LONG_MIN is different
302
303 if {0x80000000 > 0} {
304     test expr-9.5 {CompileRelationalExpr: shift expr producing LONG_MIN} {nonPortable} {
305         expr {1<<63}
306     } -9223372036854775808
307 } else {
308     test expr-9.5 {CompileRelationalExpr: shift expr producing LONG_MIN} {nonPortable} {
309         expr {1<<31}
310     } -2147483648
311 }
312 test expr-9.6 {CompileRelationalExpr: error in shift expr} {
313     catch {expr x>>3} msg
314     set msg
315 } {syntax error in expression "x>>3"} 
316 test expr-9.7 {CompileRelationalExpr: simple relational exprs} {expr 0xff>=+0x3} 1
317 test expr-9.8 {CompileRelationalExpr: simple relational exprs} {expr -0xf2<0x3} 1
318 test expr-9.9 {CompileRelationalExpr: error compiling relational arm} {
319     catch {expr 2**3>6} msg
320     set msg
321 } {syntax error in expression "2**3>6"}
322 test expr-9.10 {CompileRelationalExpr: error compiling relational arm} {
323     catch {expr 2<x} msg
324     set msg
325 } {syntax error in expression "2<x"}
326
327 test expr-10.1 {CompileShiftExpr: just add expr} {expr 4+-2} 2
328 test expr-10.2 {CompileShiftExpr: just add expr} {expr 0xff-2} 253
329 test expr-10.3 {CompileShiftExpr: just add expr} {expr -1--2} 1
330 test expr-10.4 {CompileShiftExpr: just add expr} {expr 1-0123} -82
331 test expr-10.5 {CompileShiftExpr: error in add expr} {
332     catch {expr x+3} msg
333     set msg
334 } {syntax error in expression "x+3"}
335 test expr-10.6 {CompileShiftExpr: simple shift exprs} {expr 0xff>>0x3} 31
336 test expr-10.7 {CompileShiftExpr: simple shift exprs} {expr -0xf2<<0x3} -1936
337 test expr-10.8 {CompileShiftExpr: error compiling shift arm} {
338     catch {expr 2**3>>6} msg
339     set msg
340 } {syntax error in expression "2**3>>6"}
341 test expr-10.9 {CompileShiftExpr: error compiling shift arm} {
342     catch {expr 2<<x} msg
343     set msg
344 } {syntax error in expression "2<<x"}
345 test expr-10.10 {CompileShiftExpr: runtime error} {
346     list [catch {expr {24.0>>43}} msg] $msg
347 } {1 {can't use floating-point value as operand of ">>"}}
348 test expr-10.11 {CompileShiftExpr: runtime error} {
349     list [catch {expr {"a"<<"b"}} msg] $msg
350 } {1 {can't use non-numeric string as operand of "<<"}}
351
352 test expr-11.1 {CompileAddExpr: just multiply expr} {expr 4*-2} -8
353 test expr-11.2 {CompileAddExpr: just multiply expr} {expr 0xff%2} 1
354 test expr-11.3 {CompileAddExpr: just multiply expr} {expr -1/2} -1
355 test expr-11.4 {CompileAddExpr: just multiply expr} {expr 7891%0123} 6
356 test expr-11.5 {CompileAddExpr: error in multiply expr} {
357     catch {expr x*3} msg
358     set msg
359 } {syntax error in expression "x*3"}
360 test expr-11.6 {CompileAddExpr: simple add exprs} {expr 0xff++0x3} 258
361 test expr-11.7 {CompileAddExpr: simple add exprs} {expr -0xf2--0x3} -239
362 test expr-11.8 {CompileAddExpr: error compiling add arm} {
363     catch {expr 2**3+6} msg
364     set msg
365 } {syntax error in expression "2**3+6"}
366 test expr-11.9 {CompileAddExpr: error compiling add arm} {
367     catch {expr 2-x} msg
368     set msg
369 } {syntax error in expression "2-x"}
370 test expr-11.10 {CompileAddExpr: runtime error} {
371     list [catch {expr {24.0+"xx"}} msg] $msg
372 } {1 {can't use non-numeric string as operand of "+"}}
373 test expr-11.11 {CompileAddExpr: runtime error} {
374     list [catch {expr {"a"-"b"}} msg] $msg
375 } {1 {can't use non-numeric string as operand of "-"}}
376 test expr-11.12 {CompileAddExpr: runtime error} {
377     list [catch {expr {3/0}} msg] $msg
378 } {1 {divide by zero}}
379 test expr-11.13 {CompileAddExpr: runtime error} {
380     list [catch {expr {2.3/0.0}} msg] $msg
381 } {1 {divide by zero}}
382
383 test expr-12.1 {CompileMultiplyExpr: just unary expr} {expr ~4} -5
384 test expr-12.2 {CompileMultiplyExpr: just unary expr} {expr --5} 5
385 test expr-12.3 {CompileMultiplyExpr: just unary expr} {expr !27} 0
386 test expr-12.4 {CompileMultiplyExpr: just unary expr} {expr ~0xff00ff} -16711936
387 test expr-12.5 {CompileMultiplyExpr: error in unary expr} {
388     catch {expr ~x} msg
389     set msg
390 } {syntax error in expression "~x"}
391 test expr-12.6 {CompileMultiplyExpr: simple multiply exprs} {expr 0xff*0x3} 765
392 test expr-12.7 {CompileMultiplyExpr: simple multiply exprs} {expr -0xf2%-0x3} -2
393 test expr-12.8 {CompileMultiplyExpr: error compiling multiply arm} {
394     catch {expr 2*3%%6} msg
395     set msg
396 } {syntax error in expression "2*3%%6"}
397 test expr-12.9 {CompileMultiplyExpr: error compiling multiply arm} {
398     catch {expr 2*x} msg
399     set msg
400 } {syntax error in expression "2*x"}
401 test expr-12.10 {CompileMultiplyExpr: runtime error} {
402     list [catch {expr {24.0*"xx"}} msg] $msg
403 } {1 {can't use non-numeric string as operand of "*"}}
404 test expr-12.11 {CompileMultiplyExpr: runtime error} {
405     list [catch {expr {"a"/"b"}} msg] $msg
406 } {1 {can't use non-numeric string as operand of "/"}}
407
408 test expr-13.1 {CompileUnaryExpr: unary exprs} {expr -0xff} -255
409 test expr-13.2 {CompileUnaryExpr: unary exprs} {expr +000123} 83
410 test expr-13.3 {CompileUnaryExpr: unary exprs} {expr +--++36} 36
411 test expr-13.4 {CompileUnaryExpr: unary exprs} {expr !2} 0
412 test expr-13.5 {CompileUnaryExpr: unary exprs} {expr +--+-62.0} -62.0
413 test expr-13.6 {CompileUnaryExpr: unary exprs} {expr !0.0} 1
414 test expr-13.7 {CompileUnaryExpr: unary exprs} {expr !0xef} 0
415 test expr-13.8 {CompileUnaryExpr: error compiling unary expr} {
416     catch {expr ~x} msg
417     set msg
418 } {syntax error in expression "~x"}
419 test expr-13.9 {CompileUnaryExpr: error compiling unary expr} {
420     catch {expr !1.x} msg
421     set msg
422 } {syntax error in expression "!1.x"}
423 test expr-13.10 {CompileUnaryExpr: runtime error} {
424     list [catch {expr {~"xx"}} msg] $msg
425 } {1 {can't use non-numeric string as operand of "~"}}
426 test expr-13.11 {CompileUnaryExpr: runtime error} {
427     list [catch {expr ~4.0} msg] $msg
428 } {1 {can't use floating-point value as operand of "~"}}
429 test expr-13.12 {CompileUnaryExpr: just primary expr} {expr 0x123} 291
430 test expr-13.13 {CompileUnaryExpr: just primary expr} {
431     set a 27
432     expr $a
433 } 27
434 test expr-13.14 {CompileUnaryExpr: just primary expr} {
435     expr double(27)
436 } 27.0
437 test expr-13.15 {CompileUnaryExpr: just primary expr} {expr "123"} 123
438 test expr-13.16 {CompileUnaryExpr: error in primary expr} {
439     catch {expr [set]} msg
440     set msg
441 } {wrong # args: should be "set varName ?newValue?"}
442 test expr-14.1 {CompilePrimaryExpr: literal primary} {expr 1} 1
443 test expr-14.2 {CompilePrimaryExpr: literal primary} {expr 123} 123
444 test expr-14.3 {CompilePrimaryExpr: literal primary} {expr 0xff} 255
445 test expr-14.4 {CompilePrimaryExpr: literal primary} {expr 00010} 8
446 test expr-14.5 {CompilePrimaryExpr: literal primary} {expr 62.0} 62.0
447 test expr-14.6 {CompilePrimaryExpr: literal primary} {
448     expr 3.1400000
449 } 3.14
450 test expr-14.7 {CompilePrimaryExpr: literal primary} {expr {{abcde}<{abcdef}}} 1
451 test expr-14.8 {CompilePrimaryExpr: literal primary} {expr {{abc\
452 def} < {abcdef}}} 1
453 test expr-14.9 {CompilePrimaryExpr: literal primary} {expr {{abc\tde} > {abc\tdef}}} 0
454 test expr-14.10 {CompilePrimaryExpr: literal primary} {expr {{123}}} 123
455 test expr-14.11 {CompilePrimaryExpr: var reference primary} {
456     set i 789
457     list [expr {$i}] [expr $i]
458 } {789 789}
459 test expr-14.12 {CompilePrimaryExpr: var reference primary} {
460     set i {789}    ;# test expr's aggressive conversion to numeric semantics
461     list [expr {$i}] [expr $i]
462 } {789 789}
463 test expr-14.13 {CompilePrimaryExpr: var reference primary} {
464     catch {unset a}
465     set a(foo) foo
466     set a(bar) bar
467     set a(123) 123
468     set result ""
469     lappend result [expr $a(123)] [expr {$a(bar)<$a(foo)}]
470     catch {unset a}
471     set result
472 } {123 1}
473 test expr-14.14 {CompilePrimaryExpr: var reference primary} {
474     set i 123    ;# test "$var.0" floating point conversion hack
475     list [expr $i] [expr $i.0] [expr $i.0/12.0]
476 } {123 123.0 10.25}
477 test expr-14.15 {CompilePrimaryExpr: var reference primary} {
478     set i 123
479     catch {expr $i.2} msg
480     set msg
481 } 123.2
482 test expr-14.16 {CompilePrimaryExpr: error compiling var reference primary} {
483     catch {expr {$a(foo}} msg
484     set errorInfo
485 } {missing )
486     while compiling
487 "expr {$a(foo}"}
488 test expr-14.17 {CompilePrimaryExpr: string primary that looks like var ref} {
489     expr $
490 } $
491 test expr-14.18 {CompilePrimaryExpr: quoted string primary} {
492     expr "21"
493 } 21
494 test expr-14.19 {CompilePrimaryExpr: quoted string primary} {
495     set i 123
496     set x 456
497     expr "$i+$x"
498 } 579
499 test expr-14.20 {CompilePrimaryExpr: quoted string primary} {
500     set i 3
501     set x 6
502     expr 2+"$i.$x"
503 } 5.6
504 test expr-14.21 {CompilePrimaryExpr: error in quoted string primary} {
505     catch {expr "[set]"} msg
506     set msg
507 } {wrong # args: should be "set varName ?newValue?"}
508 test expr-14.22 {CompilePrimaryExpr: subcommand primary} {
509     expr {[set i 123; set i]}
510 } 123
511 test expr-14.23 {CompilePrimaryExpr: error in subcommand primary} {
512     catch {expr {[set]}} msg
513     set errorInfo
514 } {wrong # args: should be "set varName ?newValue?"
515     while compiling
516 "set"
517     while compiling
518 "expr {[set]}"}
519 test expr-14.24 {CompilePrimaryExpr: error in subcommand primary} {
520     catch {expr {[set i}} msg
521     set errorInfo
522 } {missing close-bracket
523     while compiling
524 "expr {[set i}"}
525 test expr-14.25 {CompilePrimaryExpr: math function primary} {
526     format %.6g [expr exp(1.0)]
527 } 2.71828
528 test expr-14.26 {CompilePrimaryExpr: math function primary} {
529     format %.6g [expr pow(2.0+0.1,3.0+0.1)]
530 } 9.97424
531 test expr-14.27 {CompilePrimaryExpr: error in math function primary} {
532     catch {expr sinh::(2.0)} msg
533     set errorInfo
534 } {syntax error in expression "sinh::(2.0)"
535     while compiling
536 "expr sinh::(2.0)"}
537 test expr-14.28 {CompilePrimaryExpr: subexpression primary} {
538     expr 2+(3*4)
539 } 14
540 test expr-14.29 {CompilePrimaryExpr: error in subexpression primary} {
541     catch {expr 2+(3*[set])} msg
542     set errorInfo
543 } {wrong # args: should be "set varName ?newValue?"
544     while compiling
545 "set"
546     while compiling
547 "expr 2+(3*[set])"}
548 test expr-14.30 {CompilePrimaryExpr: missing paren in subexpression primary} {
549     catch {expr 2+(3*(4+5)} msg
550     set errorInfo
551 } {syntax error in expression "2+(3*(4+5)"
552     while compiling
553 "expr 2+(3*(4+5)"}
554 test expr-14.31 {CompilePrimaryExpr: just var ref in subexpression primary} {
555     set i "5+10"
556     list "[expr $i] == 15" "[expr ($i)] == 15" "[eval expr ($i)] == 15"
557 } {{15 == 15} {15 == 15} {15 == 15}}
558 test expr-14.32 {CompilePrimaryExpr: unexpected token} {
559     catch {expr @} msg
560     set errorInfo
561 } {syntax error in expression "@"
562     while compiling
563 "expr @"}
564
565 test expr-15.1 {CompileMathFuncCall: missing parenthesis} {
566     catch {expr sinh2.0)} msg
567     set errorInfo
568 } {syntax error in expression "sinh2.0)"
569     while compiling
570 "expr sinh2.0)"}
571 test expr-15.2 {CompileMathFuncCall: unknown math function} {
572     catch {expr whazzathuh(1)} msg
573     set errorInfo
574 } {unknown math function "whazzathuh"
575     while compiling
576 "expr whazzathuh(1)"}
577 test expr-15.3 {CompileMathFuncCall: too many arguments} {
578     catch {expr sin(1,2,3)} msg
579     set errorInfo
580 } {too many arguments for math function
581     while compiling
582 "expr sin(1,2,3)"}
583 test expr-15.4 {CompileMathFuncCall: ')' found before last required arg} {
584     catch {expr sin()} msg
585     set errorInfo
586 } {too few arguments for math function
587     while compiling
588 "expr sin()"}
589 test expr-15.5 {CompileMathFuncCall: too few arguments} {
590     catch {expr pow(1)} msg
591     set errorInfo
592 } {too few arguments for math function
593     while compiling
594 "expr pow(1)"}
595 test expr-15.6 {CompileMathFuncCall: missing ')'} {
596     catch {expr sin(1} msg
597     set errorInfo
598 } {syntax error in expression "sin(1"
599     while compiling
600 "expr sin(1"}
601 if $gotT1 {
602     test expr-15.7 {CompileMathFuncCall: call registered math function} {
603         expr 2*T1()
604     } 246
605     test expr-15.8 {CompileMathFuncCall: call registered math function} {
606         expr T2()*3
607     } 1035
608
609     test expr-15.9 {CompileMathFuncCall: call registered math function} {
610         expr T3(21, 37)
611     } 37
612     test expr-15.10 {CompileMathFuncCall: call registered math function} {
613         expr T3(21.2, 37)
614     } 37.0
615     test expr-15.11 {CompileMathFuncCall: call registered math function} {
616         expr T3(-21.2, -17.5)
617     } -17.5
618 }
619
620 test expr-16.1 {GetToken: checks whether integer token starting with "0x" (e.g., "0x$") is invalid} {
621     catch {unset a}
622     set a(VALUE) ff15
623     set i 123
624     if {[expr 0x$a(VALUE)] & 16} {
625         set i {}
626     }
627     set i
628 } {}
629 test expr-16.2 {GetToken: check for string literal in braces} {
630     expr {{1}}
631 } {1}
632
633 # Check "expr" and computed command names.
634
635 test expr-17.1 {expr and computed command names} {
636     set i 0
637     set z expr
638     $z 1+2
639 } 3
640
641 # Check correct conversion of operands to numbers: If the string looks like
642 # an integer, convert to integer. Otherwise, if the string looks like a
643 # double, convert to double.
644
645 test expr-18.1 {expr and conversion of operands to numbers} {
646     set x [lindex 11 0]
647     catch {expr int($x)}
648     expr {$x}
649 } 11
650 test expr-18.2 {whitespace strings should not be == 0 (buggy strtod)} {
651     expr {" "}
652 } { }
653
654 # Check "expr" and interpreter result object resetting before appending
655 # an error msg during evaluation of exprs not in {}s
656
657 test expr-19.1 {expr and interpreter result object resetting} {
658     proc p {} {
659         set t  10.0
660         set x  2.0
661         set dx 0.2
662         set f  {$dx-$x/10}
663         set g  {-$x/5}
664         set center 1.0
665         set x  [expr $x-$center]
666         set dx [expr $dx+$g]
667         set x  [expr $x+$f+$center]
668         set x  [expr $x+$f+$center]
669         set y  [expr round($x)]
670     }
671     p
672 } 3
673
674 # Test for incorrect "double evaluation" semantics
675
676 test expr-20.1 {wrong brace matching} {
677     catch {unset l}
678     catch {unset r}
679     catch {unset q}
680     catch {unset cmd}
681     catch {unset a}
682     set l "\{"; set r "\}"; set q "\""
683     set cmd "expr $l$q|$q == $q$r$q$r"
684     list [catch $cmd a] $a
685 } {1 {extra characters after close-brace}}
686 test expr-20.2 {double invocation of variable traces} {
687     set exprtracecounter 0
688     proc exprtraceproc {args} {
689        upvar #0 exprtracecounter counter
690        set argc [llength $args]
691        set extraargs [lrange $args 0 [expr {$argc - 4}]]
692        set name [lindex $args [expr {$argc - 3}]]
693        upvar 1 $name var
694        if {[incr counter] % 2 == 1} {
695            set var "$counter oops [concat $extraargs]"
696        } else {
697            set var "$counter + [concat $extraargs]"
698        }
699     }
700     trace variable exprtracevar r [list exprtraceproc 10]
701     list [catch {expr "$exprtracevar + 20"} a] $a \
702         [catch {expr "$exprtracevar + 20"} b] $b \
703         [unset exprtracevar exprtracecounter]
704 } {1 {syntax error in expression "1 oops 10 + 20"} 0 32 {}}
705 test expr-20.3 {broken substitution of integer digits} {
706     # fails with 8.0.x, but not 8.1b2
707     list [set a 000; expr 0x1$a] [set a 1; expr ${a}000]
708 } {4096 1000}
709 test expr-20.4 {proper double evaluation compilation, error case} {
710     catch {unset a}; # make sure $a doesn't exist
711     list [catch {expr 1?{$a}:0} msg] $msg
712 } {1 {can't read "a": no such variable}}
713 test expr-20.5 {proper double evaluation compilation, working case} {
714     set a yellow
715     expr 1?{$a}:0
716 } yellow
717 test expr-20.6 {handling of compile error in trial compile} {
718     list [catch {expr + {[incr]}} msg] $msg
719 } {1 {wrong # args: should be "incr varName ?increment?"}}
720 test expr-20.7 {handling of compile error in runtime case} {
721     list [catch {expr + {[error foo]}} msg] $msg
722 } {1 foo}
723
724 # cleanup
725 if {[info exists a]} {
726     unset a
727 }
728 ::tcltest::cleanupTests
729 return
730