os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/expr.test
First public contribution.
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-2000 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.17.2.12 2006/03/23 16:40:32 dgp Exp $
15 if {[lsearch [namespace children] ::tcltest] == -1} {
16 package require tcltest
17 namespace import -force ::tcltest::*
20 testConstraint registeredMathFuncs [expr {
21 ([catch {expr T1()} msg] != 1) || ($msg ne {unknown math function "T1"})
24 testConstraint wideIs64bit [expr {(0x80000000 > 0) && (0x8000000000000000 < 0)}]
25 # procedures used below
27 proc put_hello_char {c} {
29 append a [format %c $c]
35 set L1 [set l0 [set h_1 [set q 0]]]
36 for {put_hello_char [expr [put_hello_char [expr [set h 7]*10+2]]+29]} {$l0?[put_hello_char $l0]
37 :!$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"])]
38 ?[set ll [set l0 [expr 54<<1]]]:$ll==108&&$L1<3?
39 [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"])]]]]
40 :[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
41 ?[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"])]]}
42 expr {$L1!=1<<3?[incr q [expr ([string length "abc"]-[string length "abc"])-1]]:[set h_1 [set ll $h1]]}
49 expr {1<$a?[expr {$a<3?[12days -79 -13 [string range $c [12days -87 \
50 [expr 1-$b] [string range $c [12days -86 0 [string range $c 1 end]] \
51 end]] end]]:1};expr {$a<$b?[12days [expr $a+1] $b $c]:3};expr {[12days \
52 -94 [expr $a-27] $c]&&$a==2?$b<13?[12days 2 [expr $b+1] "%s %d %d\n"]:9
53 :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' ')# \}'+\}##(!!/"]
54 :$a<-50?[string compare [format %c $b] [string index $c 0]]==0?[append \
55 xxx [string index $c 31];scan [string index $c 31] %c x;set x]
56 :[12days -65 $b [string range $c 1 end]]:[12days [expr ([string compare \
57 [string index $c 0] "/"]==0)+$a] $b [string range $c 1 end]]:0<$a
58 ?[12days 2 2 "%s"]:[string compare [string index $c 0] "/"]==0||
59 [12days 0 [12days -61 [scan [string index $c 0] %c x; set x] \
60 "!ek;dc i@bK'(q)-\[w\]*%n+r3#l,\{\}:\nuwloca-O;m .vpbks,fxntdCeghiry"] \
61 [string range $c 1 end]]}
63 proc do_twelve_days {} {
74 test expr-1.1 {TclCompileExprCmd: no expression} {
75 list [catch {expr } msg] $msg
76 } {1 {wrong # args: should be "expr arg ?arg ...?"}}
77 test expr-1.2 {TclCompileExprCmd: one expression word} {
80 test expr-1.3 {TclCompileExprCmd: two expression words} {
83 test expr-1.4 {TclCompileExprCmd: five expression words} {
86 test expr-1.5 {TclCompileExprCmd: quoted expression word} {
89 test expr-1.6 {TclCompileExprCmd: quoted expression word} {
90 catch {expr "0005"zxy} msg
92 } {extra characters after close-quote}
93 test expr-1.7 {TclCompileExprCmd: expression word in braces} {
96 test expr-1.8 {TclCompileExprCmd: expression word in braces} {
99 test expr-1.9 {TclCompileExprCmd: expression word in braces} {
100 catch {expr {-0005}foo} msg
102 } {extra characters after close-brace}
103 test expr-1.10 {TclCompileExprCmd: other expression word in braces} {
104 expr 4*[llength "6 2"]
106 test expr-1.11 {TclCompileExprCmd: expression word terminated by ;} {
107 expr 4*[llength "6 2"];
109 test expr-1.12 {TclCompileExprCmd: inlined expr (in "catch") inside other catch} {
112 # Might not be a number
116 test expr-1.13 {TclCompileExprCmd: second level of substitutions in expr not in braces with single var reference} {
118 set x 27; set bool {$x}; if $bool {set a foo}
121 test expr-1.14 {TclCompileExprCmd: second level of substitutions in expr with comparison as top-level operator} {
123 set x 2; set b {$x}; set a [expr $b == 2]
126 test expr-1.15 {TclCompileExprCmd: second level of substitutions in expr with comparison as top-level operator} {
128 set x 2; set b {$x}; set a [expr $b eq 2]
132 test expr-2.1 {TclCompileExpr: are builtin functions registered?} {
133 expr double(5*[llength "6 2"])
135 test expr-2.2 {TclCompileExpr: error in expr} {
136 catch {expr 2**3} msg
138 } {syntax error in expression "2**3": unexpected operator *}
139 test expr-2.3 {TclCompileExpr: junk after legal expr} {
140 catch {expr 7*[llength "a b"]foo} msg
142 } {syntax error in expression "7*2foo": extra tokens at end of expression}
143 test expr-2.4 {TclCompileExpr: numeric expr string rep == formatted int rep} {
147 test expr-3.1 {CompileCondExpr: just lor expr} {expr 3||0} 1
148 test expr-3.2 {CompileCondExpr: error in lor expr} {
149 catch {expr x||3} msg
151 } {syntax error in expression "x||3": variable references require preceding $}
152 test expr-3.3 {CompileCondExpr: test true arm} {expr 3>2?44:66} 44
153 test expr-3.4 {CompileCondExpr: error compiling true arm} {
154 catch {expr 3>2?2**3:66} msg
156 } {syntax error in expression "3>2?2**3:66": unexpected operator *}
157 test expr-3.5 {CompileCondExpr: test false arm} {expr 2>3?44:66} 66
158 test expr-3.6 {CompileCondExpr: error compiling false arm} {
159 catch {expr 2>3?44:2**3} msg
161 } {syntax error in expression "2>3?44:2**3": unexpected operator *}
162 test expr-3.7 {CompileCondExpr: long arms & nested cond exprs} {unixOnly nonPortable} {
163 puts "Note: doing test expr-3.7 which can take several minutes to run"
167 test expr-3.8 {CompileCondExpr: long arms & nested cond exprs} {unixOnly nonPortable} {
168 puts "Note: doing test expr-3.8 which can take several minutes to run"
173 test expr-4.1 {CompileLorExpr: just land expr} {expr 1.3&&3.3} 1
174 test expr-4.2 {CompileLorExpr: error in land expr} {
175 catch {expr x&&3} msg
177 } {syntax error in expression "x&&3": variable references require preceding $}
178 test expr-4.3 {CompileLorExpr: simple lor exprs} {expr 0||1.0} 1
179 test expr-4.4 {CompileLorExpr: simple lor exprs} {expr 3.0||0.0} 1
180 test expr-4.5 {CompileLorExpr: simple lor exprs} {expr 0||0||1} 1
181 test expr-4.6 {CompileLorExpr: error compiling lor arm} {
182 catch {expr 2**3||4.0} msg
184 } {syntax error in expression "2**3||4.0": unexpected operator *}
185 test expr-4.7 {CompileLorExpr: error compiling lor arm} {
186 catch {expr 1.3||2**3} msg
188 } {syntax error in expression "1.3||2**3": unexpected operator *}
189 test expr-4.8 {CompileLorExpr: error compiling lor arms} {
190 list [catch {expr {"a"||"b"}} msg] $msg
191 } {1 {expected boolean value but got "a"}}
192 test expr-4.9 {CompileLorExpr: long lor arm} {
195 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]]}
198 test expr-5.1 {CompileLandExpr: just bitor expr} {expr 7|0x13} 23
199 test expr-5.2 {CompileLandExpr: error in bitor expr} {
202 } {syntax error in expression "x|3": variable references require preceding $}
203 test expr-5.3 {CompileLandExpr: simple land exprs} {expr 0&&1.0} 0
204 test expr-5.4 {CompileLandExpr: simple land exprs} {expr 0&&0} 0
205 test expr-5.5 {CompileLandExpr: simple land exprs} {expr 3.0&&1.2} 1
206 test expr-5.6 {CompileLandExpr: simple land exprs} {expr 1&&1&&2} 1
207 test expr-5.7 {CompileLandExpr: error compiling land arm} {
208 catch {expr 2**3&&4.0} msg
210 } {syntax error in expression "2**3&&4.0": unexpected operator *}
211 test expr-5.8 {CompileLandExpr: error compiling land arm} {
212 catch {expr 1.3&&2**3} msg
214 } {syntax error in expression "1.3&&2**3": unexpected operator *}
215 test expr-5.9 {CompileLandExpr: error compiling land arm} {
216 list [catch {expr {"a"&&"b"}} msg] $msg
217 } {1 {expected boolean value but got "a"}}
218 test expr-5.10 {CompileLandExpr: long land arms} {
221 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]]}
224 test expr-6.1 {CompileBitXorExpr: just bitand expr} {expr 7&0x13} 3
225 test expr-6.2 {CompileBitXorExpr: error in bitand expr} {
228 } {syntax error in expression "x|3": variable references require preceding $}
229 test expr-6.3 {CompileBitXorExpr: simple bitxor exprs} {expr 7^0x13} 20
230 test expr-6.4 {CompileBitXorExpr: simple bitxor exprs} {expr 3^0x10} 19
231 test expr-6.5 {CompileBitXorExpr: simple bitxor exprs} {expr 0^7} 7
232 test expr-6.6 {CompileBitXorExpr: simple bitxor exprs} {expr -1^7} -8
233 test expr-6.7 {CompileBitXorExpr: error compiling bitxor arm} {
234 catch {expr 2**3|6} msg
236 } {syntax error in expression "2**3|6": unexpected operator *}
237 test expr-6.8 {CompileBitXorExpr: error compiling bitxor arm} {
240 } {syntax error in expression "2^x": variable references require preceding $}
241 test expr-6.9 {CompileBitXorExpr: runtime error in bitxor arm} {
242 list [catch {expr {24.0^3}} msg] $msg
243 } {1 {can't use floating-point value as operand of "^"}}
244 test expr-6.10 {CompileBitXorExpr: runtime error in bitxor arm} {
245 list [catch {expr {"a"^"b"}} msg] $msg
246 } {1 {can't use non-numeric string as operand of "^"}}
248 test expr-7.1 {CompileBitAndExpr: just equality expr} {expr 3==2} 0
249 test expr-7.2 {CompileBitAndExpr: just equality expr} {expr 2.0==2} 1
250 test expr-7.3 {CompileBitAndExpr: just equality expr} {expr 3.2!=2.2} 1
251 test expr-7.4 {CompileBitAndExpr: just equality expr} {expr {"abc" == "abd"}} 0
252 test expr-7.5 {CompileBitAndExpr: error in equality expr} {
253 catch {expr x==3} msg
255 } {syntax error in expression "x==3": variable references require preceding $}
256 test expr-7.6 {CompileBitAndExpr: simple bitand exprs} {expr 7&0x13} 3
257 test expr-7.7 {CompileBitAndExpr: simple bitand exprs} {expr 0xf2&0x53} 82
258 test expr-7.8 {CompileBitAndExpr: simple bitand exprs} {expr 3&6} 2
259 test expr-7.9 {CompileBitAndExpr: simple bitand exprs} {expr -1&-7} -7
260 test expr-7.10 {CompileBitAndExpr: error compiling bitand arm} {
261 catch {expr 2**3&6} msg
263 } {syntax error in expression "2**3&6": unexpected operator *}
264 test expr-7.11 {CompileBitAndExpr: error compiling bitand arm} {
267 } {syntax error in expression "2&x": variable references require preceding $}
268 test expr-7.12 {CompileBitAndExpr: runtime error in bitand arm} {
269 list [catch {expr {24.0&3}} msg] $msg
270 } {1 {can't use floating-point value as operand of "&"}}
271 test expr-7.13 {CompileBitAndExpr: runtime error in bitand arm} {
272 list [catch {expr {"a"&"b"}} msg] $msg
273 } {1 {can't use non-numeric string as operand of "&"}}
274 test expr-7.14 {CompileBitAndExpr: equality expr} {expr 3eq2} 0
275 test expr-7.18 {CompileBitAndExpr: equality expr} {expr {"abc" eq "abd"}} 0
276 test expr-7.20 {CompileBitAndExpr: error in equality expr} {
277 catch {expr xne3} msg
279 } {syntax error in expression "xne3": variable references require preceding $}
281 test expr-8.1 {CompileEqualityExpr: just relational expr} {expr 3>=2} 1
282 test expr-8.2 {CompileEqualityExpr: just relational expr} {expr 2<=2.1} 1
283 test expr-8.3 {CompileEqualityExpr: just relational expr} {expr 3.2>"2.2"} 1
284 test expr-8.4 {CompileEqualityExpr: just relational expr} {expr {"0y"<"0x12"}} 0
285 test expr-8.5 {CompileEqualityExpr: error in relational expr} {
288 } {syntax error in expression "x>3": variable references require preceding $}
289 test expr-8.6 {CompileEqualityExpr: simple equality exprs} {expr 7==0x13} 0
290 test expr-8.7 {CompileEqualityExpr: simple equality exprs} {expr -0xf2!=0x53} 1
291 test expr-8.8 {CompileEqualityExpr: simple equality exprs} {expr {"12398712938788234-1298379" != ""}} 1
292 test expr-8.9 {CompileEqualityExpr: simple equality exprs} {expr -1!="abc"} 1
293 test expr-8.10 {CompileEqualityExpr: error compiling equality arm} {
294 catch {expr 2**3==6} msg
296 } {syntax error in expression "2**3==6": unexpected operator *}
297 test expr-8.11 {CompileEqualityExpr: error compiling equality arm} {
298 catch {expr 2!=x} msg
300 } {syntax error in expression "2!=x": variable references require preceding $}
301 test expr-8.12 {CompileBitAndExpr: equality expr} {expr {"a"eq"a"}} 1
302 test expr-8.13 {CompileBitAndExpr: equality expr} {expr {"\374" eq "ü"}} 1
303 test expr-8.14 {CompileBitAndExpr: equality expr} {expr 3eq2} 0
304 test expr-8.15 {CompileBitAndExpr: equality expr} {expr 2.0eq2} 0
305 test expr-8.16 {CompileBitAndExpr: equality expr} {expr 3.2ne2.2} 1
306 test expr-8.17 {CompileBitAndExpr: equality expr} {expr 01eq1} 0
307 test expr-8.18 {CompileBitAndExpr: equality expr} {expr {"abc" eq "abd"}} 0
308 test expr-8.19 {CompileBitAndExpr: equality expr} {expr {"abc" ne "abd"}} 1
309 test expr-8.20 {CompileBitAndExpr: error in equality expr} {
310 catch {expr x ne3} msg
312 } {syntax error in expression "x ne3": variable references require preceding $}
313 test expr-8.21 {CompileBitAndExpr: error in equality expr} {
314 # These should be ""ed to avoid the error
315 catch {expr a eq b} msg
317 } {syntax error in expression "a eq b": variable references require preceding $}
319 test expr-9.1 {CompileRelationalExpr: just shift expr} {expr 3<<2} 12
320 test expr-9.2 {CompileRelationalExpr: just shift expr} {expr 0xff>>2} 63
321 test expr-9.3 {CompileRelationalExpr: just shift expr} {expr -1>>2} -1
322 test expr-9.4 {CompileRelationalExpr: just shift expr} {expr {1<<3}} 8
324 # The following test is different for 32-bit versus 64-bit
325 # architectures because LONG_MIN is different
327 if {0x80000000 > 0} {
328 test expr-9.5 {CompileRelationalExpr: shift expr producing LONG_MIN} {nonPortable} {
330 } -9223372036854775808
332 test expr-9.5 {CompileRelationalExpr: shift expr producing LONG_MIN} {nonPortable} {
336 test expr-9.6 {CompileRelationalExpr: error in shift expr} {
337 catch {expr x>>3} msg
339 } {syntax error in expression "x>>3": variable references require preceding $}
340 test expr-9.7 {CompileRelationalExpr: simple relational exprs} {expr 0xff>=+0x3} 1
341 test expr-9.8 {CompileRelationalExpr: simple relational exprs} {expr -0xf2<0x3} 1
342 test expr-9.9 {CompileRelationalExpr: error compiling relational arm} {
343 catch {expr 2**3>6} msg
345 } {syntax error in expression "2**3>6": unexpected operator *}
346 test expr-9.10 {CompileRelationalExpr: error compiling relational arm} {
349 } {syntax error in expression "2<x": variable references require preceding $}
351 test expr-10.1 {CompileShiftExpr: just add expr} {expr 4+-2} 2
352 test expr-10.2 {CompileShiftExpr: just add expr} {expr 0xff-2} 253
353 test expr-10.3 {CompileShiftExpr: just add expr} {expr -1--2} 1
354 test expr-10.4 {CompileShiftExpr: just add expr} {expr 1-0123} -82
355 test expr-10.5 {CompileShiftExpr: error in add expr} {
358 } {syntax error in expression "x+3": variable references require preceding $}
359 test expr-10.6 {CompileShiftExpr: simple shift exprs} {expr 0xff>>0x3} 31
360 test expr-10.7 {CompileShiftExpr: simple shift exprs} {expr -0xf2<<0x3} -1936
361 test expr-10.8 {CompileShiftExpr: error compiling shift arm} {
362 catch {expr 2**3>>6} msg
364 } {syntax error in expression "2**3>>6": unexpected operator *}
365 test expr-10.9 {CompileShiftExpr: error compiling shift arm} {
366 catch {expr 2<<x} msg
368 } {syntax error in expression "2<<x": variable references require preceding $}
369 test expr-10.10 {CompileShiftExpr: runtime error} {
370 list [catch {expr {24.0>>43}} msg] $msg
371 } {1 {can't use floating-point value as operand of ">>"}}
372 test expr-10.11 {CompileShiftExpr: runtime error} {
373 list [catch {expr {"a"<<"b"}} msg] $msg
374 } {1 {can't use non-numeric string as operand of "<<"}}
376 test expr-11.1 {CompileAddExpr: just multiply expr} {expr 4*-2} -8
377 test expr-11.2 {CompileAddExpr: just multiply expr} {expr 0xff%2} 1
378 test expr-11.3 {CompileAddExpr: just multiply expr} {expr -1/2} -1
379 test expr-11.4 {CompileAddExpr: just multiply expr} {expr 7891%0123} 6
380 test expr-11.5 {CompileAddExpr: error in multiply expr} {
383 } {syntax error in expression "x*3": variable references require preceding $}
384 test expr-11.6 {CompileAddExpr: simple add exprs} {expr 0xff++0x3} 258
385 test expr-11.7 {CompileAddExpr: simple add exprs} {expr -0xf2--0x3} -239
386 test expr-11.8 {CompileAddExpr: error compiling add arm} {
387 catch {expr 2**3+6} msg
389 } {syntax error in expression "2**3+6": unexpected operator *}
390 test expr-11.9 {CompileAddExpr: error compiling add arm} {
393 } {syntax error in expression "2-x": variable references require preceding $}
394 test expr-11.10 {CompileAddExpr: runtime error} {
395 list [catch {expr {24.0+"xx"}} msg] $msg
396 } {1 {can't use non-numeric string as operand of "+"}}
397 test expr-11.11 {CompileAddExpr: runtime error} {
398 list [catch {expr {"a"-"b"}} msg] $msg
399 } {1 {can't use non-numeric string as operand of "-"}}
400 test expr-11.12 {CompileAddExpr: runtime error} {
401 list [catch {expr {3/0}} msg] $msg
402 } {1 {divide by zero}}
403 test expr-11.13 {CompileAddExpr: runtime error} {
404 list [catch {expr {2.3/0.0}} msg] $msg
405 } {1 {divide by zero}}
407 test expr-12.1 {CompileMultiplyExpr: just unary expr} {expr ~4} -5
408 test expr-12.2 {CompileMultiplyExpr: just unary expr} {expr --5} 5
409 test expr-12.3 {CompileMultiplyExpr: just unary expr} {expr !27} 0
410 test expr-12.4 {CompileMultiplyExpr: just unary expr} {expr ~0xff00ff} -16711936
411 test expr-12.5 {CompileMultiplyExpr: error in unary expr} {
414 } {syntax error in expression "~x": variable references require preceding $}
415 test expr-12.6 {CompileMultiplyExpr: simple multiply exprs} {expr 0xff*0x3} 765
416 test expr-12.7 {CompileMultiplyExpr: simple multiply exprs} {expr -0xf2%-0x3} -2
417 test expr-12.8 {CompileMultiplyExpr: error compiling multiply arm} {
418 catch {expr 2*3%%6} msg
420 } {syntax error in expression "2*3%%6": unexpected operator %}
421 test expr-12.9 {CompileMultiplyExpr: error compiling multiply arm} {
424 } {syntax error in expression "2*x": variable references require preceding $}
425 test expr-12.10 {CompileMultiplyExpr: runtime error} {
426 list [catch {expr {24.0*"xx"}} msg] $msg
427 } {1 {can't use non-numeric string as operand of "*"}}
428 test expr-12.11 {CompileMultiplyExpr: runtime error} {
429 list [catch {expr {"a"/"b"}} msg] $msg
430 } {1 {can't use non-numeric string as operand of "/"}}
432 test expr-13.1 {CompileUnaryExpr: unary exprs} {expr -0xff} -255
433 test expr-13.2 {CompileUnaryExpr: unary exprs} {expr +000123} 83
434 test expr-13.3 {CompileUnaryExpr: unary exprs} {expr +--++36} 36
435 test expr-13.4 {CompileUnaryExpr: unary exprs} {expr !2} 0
436 test expr-13.5 {CompileUnaryExpr: unary exprs} {expr +--+-62.0} -62.0
437 test expr-13.6 {CompileUnaryExpr: unary exprs} {expr !0.0} 1
438 test expr-13.7 {CompileUnaryExpr: unary exprs} {expr !0xef} 0
439 test expr-13.8 {CompileUnaryExpr: error compiling unary expr} {
442 } {syntax error in expression "~x": variable references require preceding $}
443 test expr-13.9 {CompileUnaryExpr: error compiling unary expr} {
444 catch {expr !1.x} msg
446 } {syntax error in expression "!1.x": extra tokens at end of expression}
447 test expr-13.10 {CompileUnaryExpr: runtime error} {
448 list [catch {expr {~"xx"}} msg] $msg
449 } {1 {can't use non-numeric string as operand of "~"}}
450 test expr-13.11 {CompileUnaryExpr: runtime error} {
451 list [catch {expr ~4.0} msg] $msg
452 } {1 {can't use floating-point value as operand of "~"}}
453 test expr-13.12 {CompileUnaryExpr: just primary expr} {expr 0x123} 291
454 test expr-13.13 {CompileUnaryExpr: just primary expr} {
458 test expr-13.14 {CompileUnaryExpr: just primary expr} {
461 test expr-13.15 {CompileUnaryExpr: just primary expr} {expr "123"} 123
462 test expr-13.16 {CompileUnaryExpr: error in primary expr} {
463 catch {expr [set]} msg
465 } {wrong # args: should be "set varName ?newValue?"}
466 test expr-13.17 {CompileUnaryExpr: negating non-numeric boolean literals} {
467 set a1 yes; set a0 no; set b1 true; set b0 false
468 list [expr {!$a1}] [expr {!$a0}] [expr {!$b1}] [expr {!$b0}]
471 test expr-14.1 {CompilePrimaryExpr: literal primary} {expr 1} 1
472 test expr-14.2 {CompilePrimaryExpr: literal primary} {expr 123} 123
473 test expr-14.3 {CompilePrimaryExpr: literal primary} {expr 0xff} 255
474 test expr-14.4 {CompilePrimaryExpr: literal primary} {expr 00010} 8
475 test expr-14.5 {CompilePrimaryExpr: literal primary} {expr 62.0} 62.0
476 test expr-14.6 {CompilePrimaryExpr: literal primary} {
479 test expr-14.7 {CompilePrimaryExpr: literal primary} {expr {{abcde}<{abcdef}}} 1
480 test expr-14.8 {CompilePrimaryExpr: literal primary} {expr {{abc\
482 test expr-14.9 {CompilePrimaryExpr: literal primary} {expr {{abc\tde} > {abc\tdef}}} 0
483 test expr-14.10 {CompilePrimaryExpr: literal primary} {expr {{123}}} 123
484 test expr-14.11 {CompilePrimaryExpr: var reference primary} {
486 list [expr {$i}] [expr $i]
488 test expr-14.12 {CompilePrimaryExpr: var reference primary} {
489 set i {789} ;# test expr's aggressive conversion to numeric semantics
490 list [expr {$i}] [expr $i]
492 test expr-14.13 {CompilePrimaryExpr: var reference primary} {
498 lappend result [expr $a(123)] [expr {$a(bar)<$a(foo)}]
502 test expr-14.14 {CompilePrimaryExpr: var reference primary} {
503 set i 123 ;# test "$var.0" floating point conversion hack
504 list [expr $i] [expr $i.0] [expr $i.0/12.0]
506 test expr-14.15 {CompilePrimaryExpr: var reference primary} {
508 catch {expr $i.2} msg
511 test expr-14.16 {CompilePrimaryExpr: error compiling var reference primary} {
512 catch {expr {$a(foo}} msg
517 test expr-14.17 {CompilePrimaryExpr: string primary that looks like var ref} {
520 test expr-14.18 {CompilePrimaryExpr: quoted string primary} {
523 test expr-14.19 {CompilePrimaryExpr: quoted string primary} {
528 test expr-14.20 {CompilePrimaryExpr: quoted string primary} {
533 test expr-14.21 {CompilePrimaryExpr: error in quoted string primary} {
534 catch {expr "[set]"} msg
536 } {wrong # args: should be "set varName ?newValue?"}
537 test expr-14.22 {CompilePrimaryExpr: subcommand primary} {
538 expr {[set i 123; set i]}
540 test expr-14.23 {CompilePrimaryExpr: error in subcommand primary} {
541 catch {expr {[set]}} msg
543 } {wrong # args: should be "set varName ?newValue?"
548 test expr-14.24 {CompilePrimaryExpr: error in subcommand primary} {
549 catch {expr {[set i}} msg
551 } {missing close-bracket
554 test expr-14.25 {CompilePrimaryExpr: math function primary} {
555 format %.6g [expr exp(1.0)]
557 test expr-14.26 {CompilePrimaryExpr: math function primary} {
558 format %.6g [expr pow(2.0+0.1,3.0+0.1)]
560 test expr-14.27 {CompilePrimaryExpr: error in math function primary} {
561 catch {expr sinh::(2.0)} msg
563 } {syntax error in expression "sinh::(2.0)": expected parenthesis enclosing function arguments
566 test expr-14.28 {CompilePrimaryExpr: subexpression primary} {
569 test expr-14.29 {CompilePrimaryExpr: error in subexpression primary} {
570 catch {expr 2+(3*[set])} msg
572 } {wrong # args: should be "set varName ?newValue?"
577 test expr-14.30 {CompilePrimaryExpr: missing paren in subexpression primary} {
578 catch {expr 2+(3*(4+5)} msg
580 } {syntax error in expression "2+(3*(4+5)": looking for close parenthesis
583 test expr-14.31 {CompilePrimaryExpr: just var ref in subexpression primary} {
585 list "[expr $i] == 15" "[expr ($i)] == 15" "[eval expr ($i)] == 15"
586 } {{15 == 15} {15 == 15} {15 == 15}}
587 test expr-14.32 {CompilePrimaryExpr: unexpected token} {
590 } {syntax error in expression "@": character not legal in expressions
594 test expr-15.1 {CompileMathFuncCall: missing parenthesis} {
595 catch {expr sinh2.0)} msg
597 } {syntax error in expression "sinh2.0)": variable references require preceding $
600 test expr-15.2 {CompileMathFuncCall: unknown math function} {
601 catch {expr whazzathuh(1)} msg
603 } {unknown math function "whazzathuh"
605 "expr whazzathuh(1)"}
606 test expr-15.3 {CompileMathFuncCall: too many arguments} {
607 catch {expr sin(1,2,3)} msg
609 } {too many arguments for math function
612 test expr-15.4 {CompileMathFuncCall: ')' found before last required arg} {
613 catch {expr sin()} msg
615 } {too few arguments for math function
618 test expr-15.5 {CompileMathFuncCall: too few arguments} {
619 catch {expr pow(1)} msg
621 } {too few arguments for math function
624 test expr-15.6 {CompileMathFuncCall: missing ')'} {
625 catch {expr sin(1} msg
627 } {syntax error in expression "sin(1": missing close parenthesis at end of function call
630 test expr-15.7 {CompileMathFuncCall: call registered math function} {registeredMathFuncs} {
633 test expr-15.8 {CompileMathFuncCall: call registered math function} {registeredMathFuncs} {
636 test expr-15.9 {CompileMathFuncCall: call registered math function} {registeredMathFuncs} {
639 test expr-15.10 {CompileMathFuncCall: call registered math function} {registeredMathFuncs} {
642 test expr-15.11 {CompileMathFuncCall: call registered math function} {registeredMathFuncs} {
643 expr T3(-21.2, -17.5)
645 test expr-15.12 {ExprCallMathFunc: call registered math function} {registeredMathFuncs} {
646 expr T3(21, wide(37))
648 test expr=15.13 {ExprCallMathFunc: call registered math function} {registeredMathFuncs} {
649 expr T3(wide(21), 37)
651 test expr=15.14 {ExprCallMathFunc: call registered math function} {registeredMathFuncs} {
652 expr T3(wide(21), wide(37))
654 test expr-15.15 {ExprCallMathFunc: call registered math function} {registeredMathFuncs} {
655 expr T3(21.0, wide(37))
657 test expr=15.16 {ExprCallMathFunc: call registered math function} {registeredMathFuncs} {
658 expr T3(wide(21), 37.0)
661 test expr-16.1 {GetToken: checks whether integer token starting with "0x" (e.g., "0x$") is invalid} {
665 if {[expr 0x$a(VALUE)] & 16} {
670 test expr-16.2 {GetToken: check for string literal in braces} {
674 # Check "expr" and computed command names.
676 test expr-17.1 {expr and computed command names} {
682 # Check correct conversion of operands to numbers: If the string looks like
683 # an integer, convert to integer. Otherwise, if the string looks like a
684 # double, convert to double.
686 test expr-18.1 {expr and conversion of operands to numbers} {
691 test expr-18.2 {whitespace strings should not be == 0 (buggy strtod)} {
695 # Check "expr" and interpreter result object resetting before appending
696 # an error msg during evaluation of exprs not in {}s
698 test expr-19.1 {expr and interpreter result object resetting} {
706 set x [expr $x-$center]
708 set x [expr $x+$f+$center]
709 set x [expr $x+$f+$center]
710 set y [expr round($x)]
715 # Test for incorrect "double evaluation" semantics
717 test expr-20.1 {wrong brace matching} {
723 set l "\{"; set r "\}"; set q "\""
724 set cmd "expr $l$q|$q == $q$r$q$r"
725 list [catch $cmd a] $a
726 } {1 {extra characters after close-brace}}
727 test expr-20.2 {double invocation of variable traces} {
728 set exprtracecounter 0
729 proc exprtraceproc {args} {
730 upvar #0 exprtracecounter counter
731 set argc [llength $args]
732 set extraargs [lrange $args 0 [expr {$argc - 4}]]
733 set name [lindex $args [expr {$argc - 3}]]
735 if {[incr counter] % 2 == 1} {
736 set var "$counter oops [concat $extraargs]"
738 set var "$counter + [concat $extraargs]"
741 trace variable exprtracevar r [list exprtraceproc 10]
742 list [catch {expr "$exprtracevar + 20"} a] $a \
743 [catch {expr "$exprtracevar + 20"} b] $b \
744 [unset exprtracevar exprtracecounter]
745 } {1 {syntax error in expression "1 oops 10 + 20": extra tokens at end of expression} 0 32 {}}
746 test expr-20.3 {broken substitution of integer digits} {
747 # fails with 8.0.x, but not 8.1b2
748 list [set a 000; expr 0x1$a] [set a 1; expr ${a}000]
750 test expr-20.4 {proper double evaluation compilation, error case} {
751 catch {unset a}; # make sure $a doesn't exist
752 list [catch {expr 1?{$a}:0} msg] $msg
753 } {1 {can't read "a": no such variable}}
754 test expr-20.5 {proper double evaluation compilation, working case} {
758 test expr-20.6 {handling of compile error in trial compile} {
759 list [catch {expr + {[incr]}} msg] $msg
760 } {1 {wrong # args: should be "incr varName ?increment?"}}
761 test expr-20.7 {handling of compile error in runtime case} {
762 list [catch {expr + {[error foo]}} msg] $msg
765 # Test for non-numeric boolean literal handling
766 test expr-21.1 {non-numeric boolean literals} {expr false } false
767 test expr-21.2 {non-numeric boolean literals} {expr true } true
768 test expr-21.3 {non-numeric boolean literals} {expr off } off
769 test expr-21.4 {non-numeric boolean literals} {expr on } on
770 test expr-21.5 {non-numeric boolean literals} {expr no } no
771 test expr-21.6 {non-numeric boolean literals} {expr yes } yes
772 test expr-21.7 {non-numeric boolean literals} {expr !false} 1
773 test expr-21.8 {non-numeric boolean literals} {expr !true } 0
774 test expr-21.9 {non-numeric boolean literals} {expr !off } 1
775 test expr-21.10 {non-numeric boolean literals} {expr !on } 0
776 test expr-21.11 {non-numeric boolean literals} {expr !no } 1
777 test expr-21.12 {non-numeric boolean literals} {expr !yes } 0
779 # Test for non-numeric float handling.
781 # These are non-portable because strtod()-support for "Inf" and "NaN"
782 # is so wildly variable. This sucks...
783 test expr-22.1 {non-numeric floats} nonPortable {
784 list [catch {expr {NaN + 1}} msg] $msg
785 } {1 {can't use non-numeric floating-point value as operand of "+"}}
786 test expr-22.2 {non-numeric floats} nonPortable {
787 list [catch {expr {Inf + 1}} msg] $msg
788 } {1 {can't use infinite floating-point value as operand of "+"}}
789 test expr-22.3 {non-numeric floats} nonPortable {
791 list [catch {expr {$nan + 1}} msg] $msg
792 } {1 {can't use non-numeric floating-point value as operand of "+"}}
793 test expr-22.4 {non-numeric floats} nonPortable {
795 list [catch {expr {$inf + 1}} msg] $msg
796 } {1 {can't use infinite floating-point value as operand of "+"}}
797 test expr-22.5 {non-numeric floats} nonPortable {
798 list [catch {expr NaN} msg] $msg
799 } {1 {domain error: argument not in valid range}}
800 test expr-22.6 {non-numeric floats} nonPortable {
801 list [catch {expr Inf} msg] $msg
802 } {1 {floating-point value too large to represent}}
803 test expr-22.7 {non-numeric floats} nonPortable {
804 list [catch {expr {1 / NaN}} msg] $msg
805 } {1 {can't use non-numeric floating-point value as operand of "/"}}
806 test expr-22.8 {non-numeric floats} nonPortable {
807 list [catch {expr {1 / Inf}} msg] $msg
808 } {1 {can't use infinite floating-point value as operand of "/"}}
809 # Make sure [Bug 761471] stays fixed.
810 test expr-22.9 {non-numeric floats: shared object equality and NaN} nonPortable {
815 # Some compilers get this wrong; ensure that we work around it correctly
816 test expr-24.1 {expr edge cases; shifting} {expr int(5)>>31} 0
817 test expr-24.2 {expr edge cases; shifting} {expr int(5)>>63} 0
818 test expr-24.3 {expr edge cases; shifting} {expr wide(5)>>31} 0
819 test expr-24.4 {expr edge cases; shifting} {expr wide(5)>>63} 0
820 test expr-24.5 {expr edge cases; shifting} nonPortable {expr int(5)<<31} 0
821 test expr-24.6 {expr edge cases; shifting} nonPortable {expr int(5)<<63} 0
822 test expr-24.7 {expr edge cases; shifting} {expr wide(5)<<31} 10737418240
823 test expr-24.8 {expr edge cases; shifting} nonPortable {expr wide(5)<<63} -9223372036854775808
824 test expr-24.9 {expr edge cases; shifting} {expr 5>>32} 0
826 test expr-38.1 {abs of smallest 32-bit integer [Bug 1241572]} {wideIs64bit} {
827 expr {abs(int(-2147483648))}
830 test expr-46.1 {round() rounds to +-infinity} {
833 test expr-46.2 {round() rounds to +-infinity} {
836 test expr-46.3 {round() rounds to +-infinity} {
839 test expr-46.4 {round() rounds to +-infinity} {
842 test expr-46.5 {round() overflow} {
843 list [catch {expr round(9.2233720368547758e+018)} result] $result
844 } {1 {integer value too large to represent}}
845 test expr-46.6 {round() overflow} {
846 list [catch {expr round(-9.2233720368547758e+018)} result] $result
847 } {1 {integer value too large to represent}}
848 test expr-46.7 {round() bad value} {
850 list [catch {expr {round($x)}} result] $result
851 } {1 {argument to math function didn't have numeric value}}
852 test expr-46.8 {round() already an integer} {
857 test expr-46.9 {round() boundary case - 1/2 - 1 ulp} {
861 set newx [expr {$x + $bit}]
862 if { $newx == $x || $newx == 0.5 } break
864 set bit [expr { $bit / 2.0 }]
868 test expr-46.10 {round() boundary case - 1/2 + 1 ulp} {
872 set newx [expr { $x - $bit }]
873 if { $newx == $x || $newx == 0.5 } break
875 set bit [expr { $bit / 2.0 }]
879 test expr-46.11 {round() boundary case - -1/2 - 1 ulp} {
883 set newx [expr { $x + $bit }]
884 if { $newx == $x || $newx == -0.5 } break
886 set bit [expr { $bit / 2.0 }]
890 test expr-46.12 {round() boundary case - -1/2 + 1 ulp} {
894 set newx [expr { $x - $bit }]
895 if { $newx == $x || $newx == -0.5 } break
897 set bit [expr { $bit / 2.0 }]
902 test expr-46.13 {round() boundary case - round down} {
903 expr {round(2147483647 - 0.51)}
906 test expr-46.14 {round() boundary case - round up} {
907 expr {round(2147483647 - 0.50)}
910 test expr-46.15 {round() boundary case - round up to wide} {
911 expr {round(2147483647 + 0.50)}
912 } [expr {wide(2147483647) + 1}]
914 test expr-46.16 {round() boundary case - round up} {
915 expr {round(-2147483648 + 0.51)}
918 test expr-46.17 {round() boundary case - round down} {
919 expr {round(-2147483648 + 0.50)}
921 test expr-46.18 {round() boundary case - round down to wide} {
922 expr {round(-2147483648 - 0.50)}
923 } [expr {wide(-2147483648) - 1}]
926 if {[info exists a]} {
929 ::tcltest::cleanupTests