Trim bootstrap jimsh
[jimtcl.git] / tests / expr-new.test
blob86e776ba242808a2fda26b6fdb96e909436c8d5a
1 # Commands covered: expr
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.
7 # Copyright (c) 1996-1997 Sun Microsystems, Inc.
8 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 # See the file "license.terms" for information on usage and redistribution
11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 # RCS: @(#) $Id: expr.test,v 1.9 2000/04/10 17:18:59 ericm Exp $
15 source [file dirname [info script]]/testing.tcl
17 # procedures used below
19 proc put_hello_char {c} {
20     global a
21     append a [format %c $c]
22     return $c
24 proc hello_world {} {
25     global a
26     set a ""
27     set L1 [set l0 [set h_1 [set q 0]]]
28     for {put_hello_char [expr [put_hello_char [expr [set h 7]*10+2]]+29]} {$l0?[put_hello_char $l0]
29         :!$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"])]
30         ?[set ll [set l0 [expr 54<<1]]]:$ll==108&&$L1<3?
31         [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"])]]]]
32         :[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
33         ?[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"])]]}
34         expr {$L1!=1<<3?[incr q [expr ([string length "abc"]-[string length "abc"])-1]]:[set h_1 [set ll $h1]]}
35     }
36     set a
39 proc 12days {a b c} {
40     global xxx
41     expr {1<$a?[expr {$a<3?[12days -79 -13 [string range $c [12days -87 \
42         [expr 1-$b] [string range $c [12days -86 0 [string range $c 1 end]] \
43         end]] end]]:1};expr {$a<$b?[12days [expr $a+1] $b $c]:3};expr {[12days \
44         -94 [expr $a-27] $c]&&$a==2?$b<13?[12days 2 [expr $b+1] "%s %d %d\n"]:9
45         :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' ')# \}'+\}##(!!/"]
46         :$a<-50?[string compare [format %c $b] [string index $c 0]]==0?[append \
47         xxx [string index $c 31];scan [string index $c 31] %c x;set x]
48         :[12days -65 $b [string range $c 1 end]]:[12days [expr ([string compare \
49         [string index $c 0] "/"]==0)+$a] $b [string range $c 1 end]]:0<$a
50         ?[12days 2 2 "%s"]:[string compare [string index $c 0] "/"]==0||
51         [12days 0 [12days -61 [scan [string index $c 0] %c x; set x] \
52         "!ek;dc i@bK'(q)-\[w\]*%n+r3#l,\{\}:\nuwloca-O;m .vpbks,fxntdCeghiry"] \
53         [string range $c 1 end]]}
55 proc do_twelve_days {} {
56     global xxx
57     set xxx ""
58     12days 1 1 1
59     string length $xxx
62 # start of tests
64 catch {unset a b i x}
66 test expr-1.1 {TclCompileExprCmd: no expression} {
67     list [catch {expr  } msg]
68 } {1}
69 test expr-1.2 {TclCompileExprCmd: one expression word} {
70     expr -25
71 } -25
72 test expr-1.3 {TclCompileExprCmd: two expression words} {
73     expr -8.2   -6
74 } -14.2
75 test expr-1.4 {TclCompileExprCmd: five expression words} {
76     expr 20 - 5 +10 -7
77 } 18
78 test expr-1.5 {TclCompileExprCmd: quoted expression word} {
79     expr "0005"
80 } 5
81 test expr-1.6 {TclCompileExprCmd: quoted expression word} {
82     catch {expr "0005"zxy} msg
83 } {1}
84 test expr-1.7 {TclCompileExprCmd: expression word in braces} {
85     expr {-0005}
86 } -5
87 # XXX: I believe that this ought to return a string, thus -0x1234
88 #test expr-1.8 {TclCompileExprCmd: expression word in braces} {
89 #    expr {{-0x1234}}
90 #} -4660
91 test expr-1.9 {TclCompileExprCmd: expression word in braces} {
92     catch {expr {-0005}foo} msg
93 } {1}
94 test expr-1.10 {TclCompileExprCmd: other expression word in braces} {
95     expr 4*[llength "6 2"]
96 } 8
97 test expr-1.11 {TclCompileExprCmd: expression word terminated by ;} {
98     expr 4*[llength "6 2"];
99 } 8
100 test expr-1.12 {TclCompileExprCmd: inlined expr (in "catch") inside other catch} {
101     set a xxx
102     catch {
103         # Might not be a number
104         set a [expr 10*$a]
105     }
106 } 1
107 test expr-1.13 {TclCompileExprCmd: second level of substitutions in expr not in braces with single var reference} {
108     set a xxx
109     set x 27;  set bool {$x};  if $bool {set a foo}
110     set a
111 } foo
112 test expr-1.14 {TclCompileExprCmd: second level of substitutions in expr with comparison as top-level operator} {
113     set a xxx
114     set x 2;  set b {$x};  set a [expr $b == 2]
115     set a
116 } 1
118 test expr-2.1 {TclCompileExpr: are builtin functions registered?} {
119     expr double(5*[llength "6 2"])
120 } 10.0
121 test expr-2.2 {TclCompileExpr: error in expr} {
122     catch {expr 2//3} msg
123 } {1}
124 test expr-2.3 {TclCompileExpr: junk after legal expr} {
125     catch {expr 7*[llength "a b"]foo} msg
126 } {1}
127 test expr-2.4 {TclCompileExpr: numeric expr string rep == formatted int rep} {
128     expr {0001}
129 } 1
131 test expr-3.1 {CompileCondExpr: just lor expr} {expr 3||0} 1
132 test expr-3.2 {CompileCondExpr: error in lor expr} {
133     catch {expr x||3} msg
134 } {1}
135 test expr-3.3 {CompileCondExpr: test true arm} {expr 3>2?44:66} 44
136 test expr-3.4 {CompileCondExpr: error compiling true arm} {
137     catch {expr 3>2?2//3:66} msg
138 } {1}
139 test expr-3.5 {CompileCondExpr: test false arm} {expr 2>3?44:66} 66
140 test expr-3.6 {CompileCondExpr: error compiling false arm} {
141     catch {expr 2>3?44:2//3} msg
142 } {1}
143 if {0} {
144 test expr-3.7 {CompileCondExpr: long arms & nested cond exprs} {
145     puts "Note: doing test expr-3.7 which can take several minutes to run"
146     hello_world
147 } {Hello world}
148 catch {unset xxx}
149 test expr-3.8 {CompileCondExpr: long arms & nested cond exprs} {
150     puts "Note: doing test expr-3.8 which can take several minutes to run"
151     do_twelve_days
152 } 2358
153 catch {unset xxx}
156 test expr-4.1 {CompileLorExpr: just land expr} {expr 1.3&&3.3} 1
157 test expr-4.2 {CompileLorExpr: error in land expr} {
158     catch {expr x&&3} msg
159 } {1}
160 test expr-4.3 {CompileLorExpr: simple lor exprs} {expr 0||1.0} 1
161 test expr-4.4 {CompileLorExpr: simple lor exprs} {expr 3.0||0.0} 1
162 test expr-4.5 {CompileLorExpr: simple lor exprs} {expr 0||0||1} 1
163 test expr-4.6 {CompileLorExpr: error compiling lor arm} {
164     catch {expr 2//3||4.0} msg
165 } {1}
166 test expr-4.7 {CompileLorExpr: error compiling lor arm} {
167     catch {expr 1.3||2//3} msg
168 } {1}
169 test expr-4.8 {CompileLorExpr: error compiling lor arms} {
170     list [catch {expr {"a"||"b"}} msg]
171 } {1}
172 test expr-4.9 {CompileLorExpr: long lor arm} {
173     set a "abcdefghijkl"
174     set i 7
175     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]]}
176 } 1
178 test expr-5.1 {CompileLandExpr: just bitor expr} {expr 7|0x13} 23
179 test expr-5.2 {CompileLandExpr: error in bitor expr} {
180     catch {expr x|3} msg
181 } {1}
182 test expr-5.3 {CompileLandExpr: simple land exprs} {expr 0&&1.0} 0
183 test expr-5.4 {CompileLandExpr: simple land exprs} {expr 0&&0} 0
184 test expr-5.5 {CompileLandExpr: simple land exprs} {expr 3.0&&1.2} 1
185 test expr-5.6 {CompileLandExpr: simple land exprs} {expr 1&&1&&2} 1
186 test expr-5.7 {CompileLandExpr: error compiling land arm} {
187     catch {expr 2//3&&4.0} msg
188 } {1}
189 test expr-5.8 {CompileLandExpr: error compiling land arm} {
190     catch {expr 1.3&&2//3} msg
191 } {1}
192 test expr-5.9 {CompileLandExpr: error compiling land arm} {
193     list [catch {expr {"a"&&"b"}} msg]
194 } {1}
195 test expr-5.10 {CompileLandExpr: long land arms} {
196     set a "abcdefghijkl"
197     set i 7
198     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]]}
199 } 1
201 test expr-6.1 {CompileBitXorExpr: just bitand expr} {expr 7&0x13} 3
202 test expr-6.2 {CompileBitXorExpr: error in bitand expr} {
203     catch {expr x|3} msg
204 } {1}
205 test expr-6.3 {CompileBitXorExpr: simple bitxor exprs} {expr 7^0x13} 20
206 test expr-6.4 {CompileBitXorExpr: simple bitxor exprs} {expr 3^0x10} 19
207 test expr-6.5 {CompileBitXorExpr: simple bitxor exprs} {expr 0^7} 7
208 test expr-6.6 {CompileBitXorExpr: simple bitxor exprs} {expr -1^7} -8
209 test expr-6.7 {CompileBitXorExpr: error compiling bitxor arm} {
210     catch {expr 2//3|6} msg
211 } {1}
212 test expr-6.8 {CompileBitXorExpr: error compiling bitxor arm} {
213     catch {expr 2^x} msg
214 } {1}
215 test expr-6.9 {CompileBitXorExpr: runtime error in bitxor arm} {
216     list [catch {expr {24.0^3}} msg]
217 } {1}
218 test expr-6.10 {CompileBitXorExpr: runtime error in bitxor arm} {
219     list [catch {expr {"a"^"b"}} msg]
220 } {1}
222 test expr-7.1 {CompileBitAndExpr: just equality expr} {expr 3==2} 0
223 test expr-7.2 {CompileBitAndExpr: just equality expr} {expr 2.0==2} 1
224 test expr-7.3 {CompileBitAndExpr: just equality expr} {expr 3.2!=2.2} 1
225 test expr-7.4 {CompileBitAndExpr: just equality expr} {expr {"abc" == "abd"}} 0
226 test expr-7.5 {CompileBitAndExpr: error in equality expr} {
227     catch {expr x==3} msg
228 } {1}
229 test expr-7.6 {CompileBitAndExpr: simple bitand exprs} {expr 7&0x13} 3
230 test expr-7.7 {CompileBitAndExpr: simple bitand exprs} {expr 0xf2&0x53} 82
231 test expr-7.8 {CompileBitAndExpr: simple bitand exprs} {expr 3&6} 2
232 test expr-7.9 {CompileBitAndExpr: simple bitand exprs} {expr -1&-7} -7
233 test expr-7.10 {CompileBitAndExpr: error compiling bitand arm} {
234     catch {expr 2//3&6} msg
235 } {1}
236 test expr-7.11 {CompileBitAndExpr: error compiling bitand arm} {
237     catch {expr 2&x} msg
238 } {1}
239 test expr-7.12 {CompileBitAndExpr: runtime error in bitand arm} {
240     list [catch {expr {24.0&3}} msg]
241 } {1}
242 test expr-7.13 {CompileBitAndExpr: runtime error in bitand arm} {
243     list [catch {expr {"a"&"b"}} msg]
244 } {1}
246 test expr-8.1 {CompileEqualityExpr: just relational expr} {expr 3>=2} 1
247 test expr-8.2 {CompileEqualityExpr: just relational expr} {expr 2<=2.1} 1
248 test expr-8.3 {CompileEqualityExpr: just relational expr} {expr 3.2>"2.2"} 1
249 test expr-8.4 {CompileEqualityExpr: just relational expr} {expr {"0y"<"0x12"}} 0
250 test expr-8.5 {CompileEqualityExpr: error in relational expr} {
251     catch {expr x>3} msg
252 } {1}
253 test expr-8.6 {CompileEqualityExpr: simple equality exprs} {expr 7==0x13} 0
254 test expr-8.7 {CompileEqualityExpr: simple equality exprs} {expr -0xf2!=0x53} 1
255 test expr-8.8 {CompileEqualityExpr: simple equality exprs} {expr {"12398712938788234-1298379" != ""}} 1
256 test expr-8.9 {CompileEqualityExpr: simple equality exprs} {expr -1!="abc"} 1
257 test expr-8.10 {CompileEqualityExpr: error compiling equality arm} {
258     catch {expr 2//3==6} msg
259 } {1}
260 test expr-8.11 {CompileEqualityExpr: error compiling equality arm} {
261     catch {expr 2!=x} msg
262 } {1}
265 test expr-9.1 {CompileRelationalExpr: just shift expr} {expr 3<<2} 12
266 test expr-9.2 {CompileRelationalExpr: just shift expr} {expr 0xff>>2} 63
267 test expr-9.3 {CompileRelationalExpr: just shift expr} {expr -1>>2} -1
268 test expr-9.4 {CompileRelationalExpr: just shift expr} {expr {1<<3}} 8
270 # The following test is different for 32-bit versus 64-bit
271 # architectures because LONG_MIN is different
273 if {0x80000000 > 0} {
274     test expr-9.5 {CompileRelationalExpr: shift expr producing LONG_MIN (64bit)} jim {
275         expr {1<<63}
276     } -9223372036854775808
277 } else {
278     test expr-9.5 {CompileRelationalExpr: shift expr producing LONG_MIN (32bit)} jim {
279         expr {1<<31}
280     } -2147483648
282 test expr-9.6 {CompileRelationalExpr: error in shift expr} {
283     catch {expr x>>3} msg
284 } {1}
285 test expr-9.7 {CompileRelationalExpr: simple relational exprs} {expr 0xff>=+0x3} 1
286 test expr-9.8 {CompileRelationalExpr: simple relational exprs} {expr -0xf2<0x3} 1
287 test expr-9.9 {CompileRelationalExpr: error compiling relational arm} {
288     catch {expr 2//3>6} msg
289 } {1}
290 test expr-9.10 {CompileRelationalExpr: error compiling relational arm} {
291     catch {expr 2<x} msg
292 } {1}
294 test expr-10.1 {CompileShiftExpr: just add expr} {expr 4+-2} 2
295 test expr-10.2 {CompileShiftExpr: just add expr} {expr 0xff-2} 253
296 test expr-10.3 {CompileShiftExpr: just add expr} {expr -1--2} 1
297 test expr-10.4 {CompileShiftExpr: just add expr} {expr 1-0123} -122
298 test expr-10.5 {CompileShiftExpr: error in add expr} {
299     catch {expr x+3} msg
300 } {1}
301 test expr-10.6 {CompileShiftExpr: simple shift exprs} {expr 0xff>>0x3} 31
302 test expr-10.7 {CompileShiftExpr: simple shift exprs} {expr -0xf2<<0x3} -1936
303 test expr-10.8 {CompileShiftExpr: error compiling shift arm} {
304     catch {expr 2//3>>6} msg
305 } {1}
306 test expr-10.9 {CompileShiftExpr: error compiling shift arm} {
307     catch {expr 2<<x} msg
308 } {1}
309 test expr-10.10 {CompileShiftExpr: runtime error} {
310     list [catch {expr {24.0>>43}} msg]
311 } {1}
312 test expr-10.11 {CompileShiftExpr: runtime error} {
313     list [catch {expr {"a"<<"b"}} msg]
314 } {1}
316 test expr-11.1 {CompileAddExpr: just multiply expr} {expr 4*-2} -8
317 test expr-11.2 {CompileAddExpr: just multiply expr} {expr 0xff%2} 1
318 test expr-11.3 {CompileAddExpr: just multiply expr} {expr -1/2} -1
319 test expr-11.4 {CompileAddExpr: just multiply expr} {expr 7891%0123} 19
320 test expr-11.5 {CompileAddExpr: error in multiply expr} {
321     catch {expr x*3} msg
322 } {1}
323 test expr-11.6 {CompileAddExpr: simple add exprs} {expr 0xff++0x3} 258
324 test expr-11.7 {CompileAddExpr: simple add exprs} {expr -0xf2--0x3} -239
325 test expr-11.8 {CompileAddExpr: error compiling add arm} {
326     catch {expr 2//3+6} msg
327 } {1}
328 test expr-11.9 {CompileAddExpr: error compiling add arm} {
329     catch {expr 2-x} msg
330 } {1}
331 test expr-11.10 {CompileAddExpr: runtime error} {
332     list [catch {expr {24.0+"xx"}} msg]
333 } {1}
334 test expr-11.11 {CompileAddExpr: runtime error} {
335     list [catch {expr {"a"-"b"}} msg]
336 } {1}
337 test expr-11.12 {CompileAddExpr: runtime error} {
338     list [catch {expr {3/0}} msg]
339 } {1}
340 test expr-11.13 {CompileAddExpr: divide by zero} {
341     expr {2.3/0.0}
342 } {Inf}
343 test expr-11.14 {CompileAddExpr: divide by zero} {
344     expr {-2.3/0.0}
345 } {-Inf}
347 test expr-12.1 {CompileMultiplyExpr: just unary expr} {expr ~4} -5
348 test expr-12.2 {CompileMultiplyExpr: just unary expr} {expr --5} 5
349 test expr-12.3 {CompileMultiplyExpr: just unary expr} {expr !27} 0
350 test expr-12.4 {CompileMultiplyExpr: just unary expr} {expr ~0xff00ff} -16711936
351 test expr-12.5 {CompileMultiplyExpr: error in unary expr} {
352     catch {expr ~x} msg
353 } {1}
354 test expr-12.6 {CompileMultiplyExpr: simple multiply exprs} {expr 0xff*0x3} 765
355 test expr-12.7 {CompileMultiplyExpr: simple multiply exprs} {expr -0xf2%-0x3} -2
356 test expr-12.8 {CompileMultiplyExpr: error compiling multiply arm} {
357     catch {expr 2*3%%6} msg
358 } {1}
359 test expr-12.9 {CompileMultiplyExpr: error compiling multiply arm} {
360     catch {expr 2*x} msg
361 } {1}
362 test expr-12.10 {CompileMultiplyExpr: runtime error} {
363     list [catch {expr {24.0*"xx"}} msg]
364 } {1}
365 test expr-12.11 {CompileMultiplyExpr: runtime error} {
366     list [catch {expr {"a"/"b"}} msg]
367 } {1}
369 test expr-13.1 {CompileUnaryExpr: unary exprs} {expr -0xff} -255
370 test expr-13.2 {CompileUnaryExpr: unary exprs} {expr +000123} 123
371 test expr-13.3 {CompileUnaryExpr: unary exprs} {expr +--++36} 36
372 test expr-13.4 {CompileUnaryExpr: unary exprs} {expr !2} 0
373 test expr-13.5 {CompileUnaryExpr: unary exprs} {expr +--+-62.0} -62.0
374 test expr-13.6 {CompileUnaryExpr: unary exprs} {expr !0.0} 1
375 test expr-13.7 {CompileUnaryExpr: unary exprs} {expr !0xef} 0
376 test expr-13.8 {CompileUnaryExpr: error compiling unary expr} {
377     catch {expr ~x} msg
378 } {1}
379 test expr-13.9 {CompileUnaryExpr: error compiling unary expr} {
380     catch {expr !1.x} msg
381 } {1}
382 test expr-13.10 {CompileUnaryExpr: runtime error} {
383     list [catch {expr {~"xx"}} msg]
384 } {1}
385 test expr-13.11 {CompileUnaryExpr: runtime error} {
386     list [catch {expr ~4.0} msg]
387 } {1}
388 test expr-13.12 {CompileUnaryExpr: just primary expr} {expr 0x123} 291
389 test expr-13.13 {CompileUnaryExpr: just primary expr} {
390     set a 27
391     expr $a
392 } 27
393 test expr-13.14 {CompileUnaryExpr: just primary expr} {
394     expr double(27)
395 } 27.0
396 test expr-13.15 {CompileUnaryExpr: just primary expr} {expr "123"} 123
397 test expr-13.16 {CompileUnaryExpr: error in primary expr} {
398     catch {expr [set]} msg
399 } {1}
400 test expr-14.1 {CompilePrimaryExpr: literal primary} {expr 1} 1
401 test expr-14.2 {CompilePrimaryExpr: literal primary} {expr 123} 123
402 test expr-14.3 {CompilePrimaryExpr: literal primary} {expr 0xff} 255
403 test expr-14.4 {CompilePrimaryExpr: literal primary} {expr 00010} 10
404 test expr-14.5 {CompilePrimaryExpr: literal primary} {expr 62.0} 62.0
405 test expr-14.6 {CompilePrimaryExpr: literal primary} {
406     expr 3.1400000
407 } 3.14
408 test expr-14.7 {CompilePrimaryExpr: literal primary} {expr {{abcde}<{abcdef}}} 1
409 test expr-14.8 {CompilePrimaryExpr: literal primary} {expr {{abc\
410 def} < {abcdef}}} 1
411 test expr-14.9 {CompilePrimaryExpr: literal primary} {expr {{abc\tde} > {abc\tdef}}} 0
412 test expr-14.10 {CompilePrimaryExpr: literal primary} {expr {{123}}} 123
413 test expr-14.11 {CompilePrimaryExpr: var reference primary} {
414     set i 789
415     list [expr {$i}] [expr $i]
416 } {789 789}
417 test expr-14.12 {CompilePrimaryExpr: var reference primary} {
418     set i {789}    ;# test expr's aggressive conversion to numeric semantics
419     list [expr {$i}] [expr $i]
420 } {789 789}
421 test expr-14.13 {CompilePrimaryExpr: var reference primary} {
422     catch {unset a}
423     set a(foo) foo
424     set a(bar) bar
425     set a(123) 123
426     set result ""
427     lappend result [expr $a(123)] [expr {$a(bar)<$a(foo)}]
428     catch {unset a}
429     set result
430 } {123 1}
431 test expr-14.14 {CompilePrimaryExpr: var reference primary} {
432     set i 123    ;# test "$var.0" floating point conversion hack
433     list [expr $i] [expr $i.0] [expr $i.0/12.0]
434 } {123 123.0 10.25}
435 test expr-14.15 {CompilePrimaryExpr: var reference primary} {
436     set i 123
437     catch {expr $i.2} msg
438     set msg
439 } 123.2
440 test expr-14.16 {CompilePrimaryExpr: error compiling var reference primary} {
441     catch {expr {$a(foo}} msg
442 } {1}
443 test expr-14.18 {CompilePrimaryExpr: quoted string primary} {
444     expr "21"
445 } 21
446 test expr-14.19 {CompilePrimaryExpr: quoted string primary} {
447     set i 123
448     set x 456
449     expr "$i+$x"
450 } 579
451 test expr-14.20 {CompilePrimaryExpr: quoted string primary} {
452     set i 3
453     set x 6
454     expr 2+"$i.$x"
455 } 5.6
456 test expr-14.21 {CompilePrimaryExpr: error in quoted string primary} {
457     catch {expr "[set]"} msg
458 } {1}
459 test expr-14.22 {CompilePrimaryExpr: subcommand primary} {
460     expr {[set i 123; set i]}
461 } 123
462 test expr-14.23 {CompilePrimaryExpr: error in subcommand primary} {
463     catch {expr {[set]}} msg
464 } {1}
465 test expr-14.24 {CompilePrimaryExpr: error in subcommand primary} {
466     catch {expr {[set blah}} msg
467 } {1}
468 test expr-14.28 {CompilePrimaryExpr: subexpression primary} {
469     expr 2+(3*4)
470 } 14
471 test expr-14.29 {CompilePrimaryExpr: error in subexpression primary} {
472     catch {expr 2+(3*[set])} msg
473 } {1}
474 test expr-14.30 {CompilePrimaryExpr: missing paren in subexpression primary} {
475     catch {expr 2+(3*(4+5)} msg
476 } {1}
477 test expr-14.31 {CompilePrimaryExpr: just var ref in subexpression primary} {
478     set i "5+10"
479     list "[expr $i] == 15" "[expr ($i)] == 15" "[eval expr ($i)] == 15"
480 } {{15 == 15} {15 == 15} {15 == 15}}
481 test expr-14.32 {CompilePrimaryExpr: unexpected token} {
482     catch {expr @} msg
483 } {1}
485 test expr-15.2 {CompileMathFuncCall: unknown math function} {
486     catch {expr whazzathuh(1)} msg
487 } {1}
489 test expr-16.1 {GetToken: checks whether integer token starting with "0x" (e.g., "0x$") is invalid} {
490     catch {unset a}
491     set a(VALUE) ff15
492     set i 123
493     if {[expr 0x$a(VALUE)] & 16} {
494         set i {}
495     }
496     set i
497 } {}
498 test expr-16.2 {GetToken: check for string literal in braces} {
499     expr {{1}}
500 } {1}
502 # Check "expr" and computed command names.
504 test expr-17.1 {expr and computed command names} {
505     set i 0
506     set z expr
507     $z 1+2
508 } 3
510 # Check correct conversion of operands to numbers: If the string looks like
511 # an integer, convert to integer. Otherwise, if the string looks like a
512 # double, convert to double.
514 test expr-18.1 {expr and conversion of operands to numbers} {
515     set x [lindex 11 0]
516     catch {expr int($x)}
517     expr {$x}
518 } 11
519 test expr-18.2 {whitespace strings should not be == 0 (buggy strtod)} {
520     expr {" "}
521 } { }
523 # Check "expr" and interpreter result object resetting before appending
524 # an error msg during evaluation of exprs not in {}s
526 test expr-19.1 {expr and interpreter result object resetting} {
527     proc p {} {
528         set t  10.0
529         set x  2.0
530         set dx 0.2
531         set f  {$dx-$x/10}
532         set g  {-$x/5}
533         set center 1.0
534         set x  [expr $x-$center]
535         set dx [expr $dx+$g]
536         set x  [expr $x+$f+$center]
537         set x  [expr $x+$f+$center]
538         set y  [expr round($x)]
539     }
540     p
541 } 3
543 catch {unset a}
545 # Test for incorrect "double evaluation" semantics
547 #XXX: Jim doesn't care about missing braces
548 #test expr-20.1 {wrong brace matching} {
549 #    catch {unset l}
550 #    catch {unset r}
551 #    catch {unset q}
552 #    catch {unset cmd}
553 #    catch {unset a}
554 #    set l "\{"; set r "\}"; set q "\""
555 #    set cmd "expr $l$q|$q == $q$r$q$r"
556 #    catch $cmd a
557 #} {1}
558 test expr-20.3 {broken substitution of integer digits} {
559     # fails with 8.0.x, but not 8.1b2
560     list [set a 000; expr 0x1$a] [set a 1; expr ${a}000]
561 } {4096 1000}
562 test expr-20.4 {proper double evaluation compilation, error case} {
563     catch {unset a}; # make sure $a doesn't exist
564     list [catch {expr 1?{$a}:0} msg]
565 } {1}
566 test expr-20.5 {proper double evaluation compilation, working case} {
567     set a yellow
568     expr 1?{$a}:0
569 } yellow
570 test expr-20.6 {handling of compile error in trial compile} {
571     list [catch {expr + {[incr]}} msg]
572 } {1}
573 test expr-20.7 {handling of compile error in runtime case} {
574     list [catch {expr + {[error foo]}} msg]
575 } {1}
577 test expr-21.1 {checking boolean 0} {
578   expr 0
579 } 0
580 test expr-21.2 {checking boolean 0} {
581   expr {0 || 0}
582 } 0
583 test expr-21.3 {checking boolean false} {
584   expr {0 || false}
585 } 0
586 test expr-21.4 {checking boolean no} {
587   expr {0 || no}
588 } 0
589 test expr-21.5 {checking boolean off} {
590   expr {0 || off}
591 } 0
592 test expr-21.6 {checking boolean 1} {
593   expr {0 || 1}
594 } 1
595 test expr-21.7 {checking boolean true} {
596   expr {0 || true}
597 } 1
598 test expr-21.8 {checking boolean yes} {
599   expr {0 || yes}
600 } 1
601 test expr-21.9 {checking boolean on} {
602   expr {0 || on}
603 } 1
604 test expr-21.7 {checking boolean mixed case} {
605   set res 0
606   try {
607     expr {0 || True}
608   } on error {msg} {
609     set res 1
610   }
611 } 1
613 # cleanup
614 if {[info exists a]} {
615     unset a
618 testreport