sl@0: # Commands covered: expr sl@0: # sl@0: # This file contains a collection of tests for one or more of the Tcl sl@0: # built-in commands. Sourcing this file into Tcl runs the tests and sl@0: # generates output for errors. No output means no errors were found. sl@0: # sl@0: # Copyright (c) 1996-1997 Sun Microsystems, Inc. sl@0: # Copyright (c) 1998-2000 by Scriptics Corporation. sl@0: # sl@0: # See the file "license.terms" for information on usage and redistribution sl@0: # of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: # sl@0: # RCS: @(#) $Id: expr.test,v 1.17.2.12 2006/03/23 16:40:32 dgp Exp $ sl@0: sl@0: if {[lsearch [namespace children] ::tcltest] == -1} { sl@0: package require tcltest sl@0: namespace import -force ::tcltest::* sl@0: } sl@0: sl@0: testConstraint registeredMathFuncs [expr { sl@0: ([catch {expr T1()} msg] != 1) || ($msg ne {unknown math function "T1"}) sl@0: }] sl@0: sl@0: testConstraint wideIs64bit [expr {(0x80000000 > 0) && (0x8000000000000000 < 0)}] sl@0: # procedures used below sl@0: sl@0: proc put_hello_char {c} { sl@0: global a sl@0: append a [format %c $c] sl@0: return $c sl@0: } sl@0: proc hello_world {} { sl@0: global a sl@0: set a "" sl@0: set L1 [set l0 [set h_1 [set q 0]]] sl@0: for {put_hello_char [expr [put_hello_char [expr [set h 7]*10+2]]+29]} {$l0?[put_hello_char $l0] sl@0: :!$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"])] sl@0: ?[set ll [set l0 [expr 54<<1]]]:$ll==108&&$L1<3? sl@0: [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"])]]]] sl@0: :[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 sl@0: ?[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"])]]} sl@0: expr {$L1!=1<<3?[incr q [expr ([string length "abc"]-[string length "abc"])-1]]:[set h_1 [set ll $h1]]} sl@0: } sl@0: set a sl@0: } sl@0: sl@0: proc 12days {a b c} { sl@0: global xxx sl@0: expr {1<$a?[expr {$a<3?[12days -79 -13 [string range $c [12days -87 \ sl@0: [expr 1-$b] [string range $c [12days -86 0 [string range $c 1 end]] \ sl@0: end]] end]]:1};expr {$a<$b?[12days [expr $a+1] $b $c]:3};expr {[12days \ sl@0: -94 [expr $a-27] $c]&&$a==2?$b<13?[12days 2 [expr $b+1] "%s %d %d\n"]:9 sl@0: :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' ')# \}'+\}##(!!/"] sl@0: :$a<-50?[string compare [format %c $b] [string index $c 0]]==0?[append \ sl@0: xxx [string index $c 31];scan [string index $c 31] %c x;set x] sl@0: :[12days -65 $b [string range $c 1 end]]:[12days [expr ([string compare \ sl@0: [string index $c 0] "/"]==0)+$a] $b [string range $c 1 end]]:0<$a sl@0: ?[12days 2 2 "%s"]:[string compare [string index $c 0] "/"]==0|| sl@0: [12days 0 [12days -61 [scan [string index $c 0] %c x; set x] \ sl@0: "!ek;dc i@bK'(q)-\[w\]*%n+r3#l,\{\}:\nuwloca-O;m .vpbks,fxntdCeghiry"] \ sl@0: [string range $c 1 end]]} sl@0: } sl@0: proc do_twelve_days {} { sl@0: global xxx sl@0: set xxx "" sl@0: 12days 1 1 1 sl@0: string length $xxx sl@0: } sl@0: sl@0: # start of tests sl@0: sl@0: catch {unset a b i x} sl@0: sl@0: test expr-1.1 {TclCompileExprCmd: no expression} { sl@0: list [catch {expr } msg] $msg sl@0: } {1 {wrong # args: should be "expr arg ?arg ...?"}} sl@0: test expr-1.2 {TclCompileExprCmd: one expression word} { sl@0: expr -25 sl@0: } -25 sl@0: test expr-1.3 {TclCompileExprCmd: two expression words} { sl@0: expr -8.2 -6 sl@0: } -14.2 sl@0: test expr-1.4 {TclCompileExprCmd: five expression words} { sl@0: expr 20 - 5 +10 -7 sl@0: } 18 sl@0: test expr-1.5 {TclCompileExprCmd: quoted expression word} { sl@0: expr "0005" sl@0: } 5 sl@0: test expr-1.6 {TclCompileExprCmd: quoted expression word} { sl@0: catch {expr "0005"zxy} msg sl@0: set msg sl@0: } {extra characters after close-quote} sl@0: test expr-1.7 {TclCompileExprCmd: expression word in braces} { sl@0: expr {-0005} sl@0: } -5 sl@0: test expr-1.8 {TclCompileExprCmd: expression word in braces} { sl@0: expr {{-0x1234}} sl@0: } -4660 sl@0: test expr-1.9 {TclCompileExprCmd: expression word in braces} { sl@0: catch {expr {-0005}foo} msg sl@0: set msg sl@0: } {extra characters after close-brace} sl@0: test expr-1.10 {TclCompileExprCmd: other expression word in braces} { sl@0: expr 4*[llength "6 2"] sl@0: } 8 sl@0: test expr-1.11 {TclCompileExprCmd: expression word terminated by ;} { sl@0: expr 4*[llength "6 2"]; sl@0: } 8 sl@0: test expr-1.12 {TclCompileExprCmd: inlined expr (in "catch") inside other catch} { sl@0: set a xxx sl@0: catch { sl@0: # Might not be a number sl@0: set a [expr 10*$a] sl@0: } sl@0: } 1 sl@0: test expr-1.13 {TclCompileExprCmd: second level of substitutions in expr not in braces with single var reference} { sl@0: set a xxx sl@0: set x 27; set bool {$x}; if $bool {set a foo} sl@0: set a sl@0: } foo sl@0: test expr-1.14 {TclCompileExprCmd: second level of substitutions in expr with comparison as top-level operator} { sl@0: set a xxx sl@0: set x 2; set b {$x}; set a [expr $b == 2] sl@0: set a sl@0: } 1 sl@0: test expr-1.15 {TclCompileExprCmd: second level of substitutions in expr with comparison as top-level operator} { sl@0: set a xxx sl@0: set x 2; set b {$x}; set a [expr $b eq 2] sl@0: set a sl@0: } 1 sl@0: sl@0: test expr-2.1 {TclCompileExpr: are builtin functions registered?} { sl@0: expr double(5*[llength "6 2"]) sl@0: } 10.0 sl@0: test expr-2.2 {TclCompileExpr: error in expr} { sl@0: catch {expr 2**3} msg sl@0: set msg sl@0: } {syntax error in expression "2**3": unexpected operator *} sl@0: test expr-2.3 {TclCompileExpr: junk after legal expr} { sl@0: catch {expr 7*[llength "a b"]foo} msg sl@0: set msg sl@0: } {syntax error in expression "7*2foo": extra tokens at end of expression} sl@0: test expr-2.4 {TclCompileExpr: numeric expr string rep == formatted int rep} { sl@0: expr {0001} sl@0: } 1 sl@0: sl@0: test expr-3.1 {CompileCondExpr: just lor expr} {expr 3||0} 1 sl@0: test expr-3.2 {CompileCondExpr: error in lor expr} { sl@0: catch {expr x||3} msg sl@0: set msg sl@0: } {syntax error in expression "x||3": variable references require preceding $} sl@0: test expr-3.3 {CompileCondExpr: test true arm} {expr 3>2?44:66} 44 sl@0: test expr-3.4 {CompileCondExpr: error compiling true arm} { sl@0: catch {expr 3>2?2**3:66} msg sl@0: set msg sl@0: } {syntax error in expression "3>2?2**3:66": unexpected operator *} sl@0: test expr-3.5 {CompileCondExpr: test false arm} {expr 2>3?44:66} 66 sl@0: test expr-3.6 {CompileCondExpr: error compiling false arm} { sl@0: catch {expr 2>3?44:2**3} msg sl@0: set msg sl@0: } {syntax error in expression "2>3?44:2**3": unexpected operator *} sl@0: test expr-3.7 {CompileCondExpr: long arms & nested cond exprs} {unixOnly nonPortable} { sl@0: puts "Note: doing test expr-3.7 which can take several minutes to run" sl@0: hello_world sl@0: } {Hello world} sl@0: catch {unset xxx} sl@0: test expr-3.8 {CompileCondExpr: long arms & nested cond exprs} {unixOnly nonPortable} { sl@0: puts "Note: doing test expr-3.8 which can take several minutes to run" sl@0: do_twelve_days sl@0: } 2358 sl@0: catch {unset xxx} sl@0: sl@0: test expr-4.1 {CompileLorExpr: just land expr} {expr 1.3&&3.3} 1 sl@0: test expr-4.2 {CompileLorExpr: error in land expr} { sl@0: catch {expr x&&3} msg sl@0: set msg sl@0: } {syntax error in expression "x&&3": variable references require preceding $} sl@0: test expr-4.3 {CompileLorExpr: simple lor exprs} {expr 0||1.0} 1 sl@0: test expr-4.4 {CompileLorExpr: simple lor exprs} {expr 3.0||0.0} 1 sl@0: test expr-4.5 {CompileLorExpr: simple lor exprs} {expr 0||0||1} 1 sl@0: test expr-4.6 {CompileLorExpr: error compiling lor arm} { sl@0: catch {expr 2**3||4.0} msg sl@0: set msg sl@0: } {syntax error in expression "2**3||4.0": unexpected operator *} sl@0: test expr-4.7 {CompileLorExpr: error compiling lor arm} { sl@0: catch {expr 1.3||2**3} msg sl@0: set msg sl@0: } {syntax error in expression "1.3||2**3": unexpected operator *} sl@0: test expr-4.8 {CompileLorExpr: error compiling lor arms} { sl@0: list [catch {expr {"a"||"b"}} msg] $msg sl@0: } {1 {expected boolean value but got "a"}} sl@0: test expr-4.9 {CompileLorExpr: long lor arm} { sl@0: set a "abcdefghijkl" sl@0: set i 7 sl@0: 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]]} sl@0: } 1 sl@0: sl@0: test expr-5.1 {CompileLandExpr: just bitor expr} {expr 7|0x13} 23 sl@0: test expr-5.2 {CompileLandExpr: error in bitor expr} { sl@0: catch {expr x|3} msg sl@0: set msg sl@0: } {syntax error in expression "x|3": variable references require preceding $} sl@0: test expr-5.3 {CompileLandExpr: simple land exprs} {expr 0&&1.0} 0 sl@0: test expr-5.4 {CompileLandExpr: simple land exprs} {expr 0&&0} 0 sl@0: test expr-5.5 {CompileLandExpr: simple land exprs} {expr 3.0&&1.2} 1 sl@0: test expr-5.6 {CompileLandExpr: simple land exprs} {expr 1&&1&&2} 1 sl@0: test expr-5.7 {CompileLandExpr: error compiling land arm} { sl@0: catch {expr 2**3&&4.0} msg sl@0: set msg sl@0: } {syntax error in expression "2**3&&4.0": unexpected operator *} sl@0: test expr-5.8 {CompileLandExpr: error compiling land arm} { sl@0: catch {expr 1.3&&2**3} msg sl@0: set msg sl@0: } {syntax error in expression "1.3&&2**3": unexpected operator *} sl@0: test expr-5.9 {CompileLandExpr: error compiling land arm} { sl@0: list [catch {expr {"a"&&"b"}} msg] $msg sl@0: } {1 {expected boolean value but got "a"}} sl@0: test expr-5.10 {CompileLandExpr: long land arms} { sl@0: set a "abcdefghijkl" sl@0: set i 7 sl@0: 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]]} sl@0: } 1 sl@0: sl@0: test expr-6.1 {CompileBitXorExpr: just bitand expr} {expr 7&0x13} 3 sl@0: test expr-6.2 {CompileBitXorExpr: error in bitand expr} { sl@0: catch {expr x|3} msg sl@0: set msg sl@0: } {syntax error in expression "x|3": variable references require preceding $} sl@0: test expr-6.3 {CompileBitXorExpr: simple bitxor exprs} {expr 7^0x13} 20 sl@0: test expr-6.4 {CompileBitXorExpr: simple bitxor exprs} {expr 3^0x10} 19 sl@0: test expr-6.5 {CompileBitXorExpr: simple bitxor exprs} {expr 0^7} 7 sl@0: test expr-6.6 {CompileBitXorExpr: simple bitxor exprs} {expr -1^7} -8 sl@0: test expr-6.7 {CompileBitXorExpr: error compiling bitxor arm} { sl@0: catch {expr 2**3|6} msg sl@0: set msg sl@0: } {syntax error in expression "2**3|6": unexpected operator *} sl@0: test expr-6.8 {CompileBitXorExpr: error compiling bitxor arm} { sl@0: catch {expr 2^x} msg sl@0: set msg sl@0: } {syntax error in expression "2^x": variable references require preceding $} sl@0: test expr-6.9 {CompileBitXorExpr: runtime error in bitxor arm} { sl@0: list [catch {expr {24.0^3}} msg] $msg sl@0: } {1 {can't use floating-point value as operand of "^"}} sl@0: test expr-6.10 {CompileBitXorExpr: runtime error in bitxor arm} { sl@0: list [catch {expr {"a"^"b"}} msg] $msg sl@0: } {1 {can't use non-numeric string as operand of "^"}} sl@0: sl@0: test expr-7.1 {CompileBitAndExpr: just equality expr} {expr 3==2} 0 sl@0: test expr-7.2 {CompileBitAndExpr: just equality expr} {expr 2.0==2} 1 sl@0: test expr-7.3 {CompileBitAndExpr: just equality expr} {expr 3.2!=2.2} 1 sl@0: test expr-7.4 {CompileBitAndExpr: just equality expr} {expr {"abc" == "abd"}} 0 sl@0: test expr-7.5 {CompileBitAndExpr: error in equality expr} { sl@0: catch {expr x==3} msg sl@0: set msg sl@0: } {syntax error in expression "x==3": variable references require preceding $} sl@0: test expr-7.6 {CompileBitAndExpr: simple bitand exprs} {expr 7&0x13} 3 sl@0: test expr-7.7 {CompileBitAndExpr: simple bitand exprs} {expr 0xf2&0x53} 82 sl@0: test expr-7.8 {CompileBitAndExpr: simple bitand exprs} {expr 3&6} 2 sl@0: test expr-7.9 {CompileBitAndExpr: simple bitand exprs} {expr -1&-7} -7 sl@0: test expr-7.10 {CompileBitAndExpr: error compiling bitand arm} { sl@0: catch {expr 2**3&6} msg sl@0: set msg sl@0: } {syntax error in expression "2**3&6": unexpected operator *} sl@0: test expr-7.11 {CompileBitAndExpr: error compiling bitand arm} { sl@0: catch {expr 2&x} msg sl@0: set msg sl@0: } {syntax error in expression "2&x": variable references require preceding $} sl@0: test expr-7.12 {CompileBitAndExpr: runtime error in bitand arm} { sl@0: list [catch {expr {24.0&3}} msg] $msg sl@0: } {1 {can't use floating-point value as operand of "&"}} sl@0: test expr-7.13 {CompileBitAndExpr: runtime error in bitand arm} { sl@0: list [catch {expr {"a"&"b"}} msg] $msg sl@0: } {1 {can't use non-numeric string as operand of "&"}} sl@0: test expr-7.14 {CompileBitAndExpr: equality expr} {expr 3eq2} 0 sl@0: test expr-7.18 {CompileBitAndExpr: equality expr} {expr {"abc" eq "abd"}} 0 sl@0: test expr-7.20 {CompileBitAndExpr: error in equality expr} { sl@0: catch {expr xne3} msg sl@0: set msg sl@0: } {syntax error in expression "xne3": variable references require preceding $} sl@0: sl@0: test expr-8.1 {CompileEqualityExpr: just relational expr} {expr 3>=2} 1 sl@0: test expr-8.2 {CompileEqualityExpr: just relational expr} {expr 2<=2.1} 1 sl@0: test expr-8.3 {CompileEqualityExpr: just relational expr} {expr 3.2>"2.2"} 1 sl@0: test expr-8.4 {CompileEqualityExpr: just relational expr} {expr {"0y"<"0x12"}} 0 sl@0: test expr-8.5 {CompileEqualityExpr: error in relational expr} { sl@0: catch {expr x>3} msg sl@0: set msg sl@0: } {syntax error in expression "x>3": variable references require preceding $} sl@0: test expr-8.6 {CompileEqualityExpr: simple equality exprs} {expr 7==0x13} 0 sl@0: test expr-8.7 {CompileEqualityExpr: simple equality exprs} {expr -0xf2!=0x53} 1 sl@0: test expr-8.8 {CompileEqualityExpr: simple equality exprs} {expr {"12398712938788234-1298379" != ""}} 1 sl@0: test expr-8.9 {CompileEqualityExpr: simple equality exprs} {expr -1!="abc"} 1 sl@0: test expr-8.10 {CompileEqualityExpr: error compiling equality arm} { sl@0: catch {expr 2**3==6} msg sl@0: set msg sl@0: } {syntax error in expression "2**3==6": unexpected operator *} sl@0: test expr-8.11 {CompileEqualityExpr: error compiling equality arm} { sl@0: catch {expr 2!=x} msg sl@0: set msg sl@0: } {syntax error in expression "2!=x": variable references require preceding $} sl@0: test expr-8.12 {CompileBitAndExpr: equality expr} {expr {"a"eq"a"}} 1 sl@0: test expr-8.13 {CompileBitAndExpr: equality expr} {expr {"\374" eq "ü"}} 1 sl@0: test expr-8.14 {CompileBitAndExpr: equality expr} {expr 3eq2} 0 sl@0: test expr-8.15 {CompileBitAndExpr: equality expr} {expr 2.0eq2} 0 sl@0: test expr-8.16 {CompileBitAndExpr: equality expr} {expr 3.2ne2.2} 1 sl@0: test expr-8.17 {CompileBitAndExpr: equality expr} {expr 01eq1} 0 sl@0: test expr-8.18 {CompileBitAndExpr: equality expr} {expr {"abc" eq "abd"}} 0 sl@0: test expr-8.19 {CompileBitAndExpr: equality expr} {expr {"abc" ne "abd"}} 1 sl@0: test expr-8.20 {CompileBitAndExpr: error in equality expr} { sl@0: catch {expr x ne3} msg sl@0: set msg sl@0: } {syntax error in expression "x ne3": variable references require preceding $} sl@0: test expr-8.21 {CompileBitAndExpr: error in equality expr} { sl@0: # These should be ""ed to avoid the error sl@0: catch {expr a eq b} msg sl@0: set msg sl@0: } {syntax error in expression "a eq b": variable references require preceding $} sl@0: sl@0: test expr-9.1 {CompileRelationalExpr: just shift expr} {expr 3<<2} 12 sl@0: test expr-9.2 {CompileRelationalExpr: just shift expr} {expr 0xff>>2} 63 sl@0: test expr-9.3 {CompileRelationalExpr: just shift expr} {expr -1>>2} -1 sl@0: test expr-9.4 {CompileRelationalExpr: just shift expr} {expr {1<<3}} 8 sl@0: sl@0: # The following test is different for 32-bit versus 64-bit sl@0: # architectures because LONG_MIN is different sl@0: sl@0: if {0x80000000 > 0} { sl@0: test expr-9.5 {CompileRelationalExpr: shift expr producing LONG_MIN} {nonPortable} { sl@0: expr {1<<63} sl@0: } -9223372036854775808 sl@0: } else { sl@0: test expr-9.5 {CompileRelationalExpr: shift expr producing LONG_MIN} {nonPortable} { sl@0: expr {1<<31} sl@0: } -2147483648 sl@0: } sl@0: test expr-9.6 {CompileRelationalExpr: error in shift expr} { sl@0: catch {expr x>>3} msg sl@0: set msg sl@0: } {syntax error in expression "x>>3": variable references require preceding $} sl@0: test expr-9.7 {CompileRelationalExpr: simple relational exprs} {expr 0xff>=+0x3} 1 sl@0: test expr-9.8 {CompileRelationalExpr: simple relational exprs} {expr -0xf2<0x3} 1 sl@0: test expr-9.9 {CompileRelationalExpr: error compiling relational arm} { sl@0: catch {expr 2**3>6} msg sl@0: set msg sl@0: } {syntax error in expression "2**3>6": unexpected operator *} sl@0: test expr-9.10 {CompileRelationalExpr: error compiling relational arm} { sl@0: catch {expr 2>0x3} 31 sl@0: test expr-10.7 {CompileShiftExpr: simple shift exprs} {expr -0xf2<<0x3} -1936 sl@0: test expr-10.8 {CompileShiftExpr: error compiling shift arm} { sl@0: catch {expr 2**3>>6} msg sl@0: set msg sl@0: } {syntax error in expression "2**3>>6": unexpected operator *} sl@0: test expr-10.9 {CompileShiftExpr: error compiling shift arm} { sl@0: catch {expr 2<>43}} msg] $msg sl@0: } {1 {can't use floating-point value as operand of ">>"}} sl@0: test expr-10.11 {CompileShiftExpr: runtime error} { sl@0: list [catch {expr {"a"<<"b"}} msg] $msg sl@0: } {1 {can't use non-numeric string as operand of "<<"}} sl@0: sl@0: test expr-11.1 {CompileAddExpr: just multiply expr} {expr 4*-2} -8 sl@0: test expr-11.2 {CompileAddExpr: just multiply expr} {expr 0xff%2} 1 sl@0: test expr-11.3 {CompileAddExpr: just multiply expr} {expr -1/2} -1 sl@0: test expr-11.4 {CompileAddExpr: just multiply expr} {expr 7891%0123} 6 sl@0: test expr-11.5 {CompileAddExpr: error in multiply expr} { sl@0: catch {expr x*3} msg sl@0: set msg sl@0: } {syntax error in expression "x*3": variable references require preceding $} sl@0: test expr-11.6 {CompileAddExpr: simple add exprs} {expr 0xff++0x3} 258 sl@0: test expr-11.7 {CompileAddExpr: simple add exprs} {expr -0xf2--0x3} -239 sl@0: test expr-11.8 {CompileAddExpr: error compiling add arm} { sl@0: catch {expr 2**3+6} msg sl@0: set msg sl@0: } {syntax error in expression "2**3+6": unexpected operator *} sl@0: test expr-11.9 {CompileAddExpr: error compiling add arm} { sl@0: catch {expr 2-x} msg sl@0: set msg sl@0: } {syntax error in expression "2-x": variable references require preceding $} sl@0: test expr-11.10 {CompileAddExpr: runtime error} { sl@0: list [catch {expr {24.0+"xx"}} msg] $msg sl@0: } {1 {can't use non-numeric string as operand of "+"}} sl@0: test expr-11.11 {CompileAddExpr: runtime error} { sl@0: list [catch {expr {"a"-"b"}} msg] $msg sl@0: } {1 {can't use non-numeric string as operand of "-"}} sl@0: test expr-11.12 {CompileAddExpr: runtime error} { sl@0: list [catch {expr {3/0}} msg] $msg sl@0: } {1 {divide by zero}} sl@0: test expr-11.13 {CompileAddExpr: runtime error} { sl@0: list [catch {expr {2.3/0.0}} msg] $msg sl@0: } {1 {divide by zero}} sl@0: sl@0: test expr-12.1 {CompileMultiplyExpr: just unary expr} {expr ~4} -5 sl@0: test expr-12.2 {CompileMultiplyExpr: just unary expr} {expr --5} 5 sl@0: test expr-12.3 {CompileMultiplyExpr: just unary expr} {expr !27} 0 sl@0: test expr-12.4 {CompileMultiplyExpr: just unary expr} {expr ~0xff00ff} -16711936 sl@0: test expr-12.5 {CompileMultiplyExpr: error in unary expr} { sl@0: catch {expr ~x} msg sl@0: set msg sl@0: } {syntax error in expression "~x": variable references require preceding $} sl@0: test expr-12.6 {CompileMultiplyExpr: simple multiply exprs} {expr 0xff*0x3} 765 sl@0: test expr-12.7 {CompileMultiplyExpr: simple multiply exprs} {expr -0xf2%-0x3} -2 sl@0: test expr-12.8 {CompileMultiplyExpr: error compiling multiply arm} { sl@0: catch {expr 2*3%%6} msg sl@0: set msg sl@0: } {syntax error in expression "2*3%%6": unexpected operator %} sl@0: test expr-12.9 {CompileMultiplyExpr: error compiling multiply arm} { sl@0: catch {expr 2*x} msg sl@0: set msg sl@0: } {syntax error in expression "2*x": variable references require preceding $} sl@0: test expr-12.10 {CompileMultiplyExpr: runtime error} { sl@0: list [catch {expr {24.0*"xx"}} msg] $msg sl@0: } {1 {can't use non-numeric string as operand of "*"}} sl@0: test expr-12.11 {CompileMultiplyExpr: runtime error} { sl@0: list [catch {expr {"a"/"b"}} msg] $msg sl@0: } {1 {can't use non-numeric string as operand of "/"}} sl@0: sl@0: test expr-13.1 {CompileUnaryExpr: unary exprs} {expr -0xff} -255 sl@0: test expr-13.2 {CompileUnaryExpr: unary exprs} {expr +000123} 83 sl@0: test expr-13.3 {CompileUnaryExpr: unary exprs} {expr +--++36} 36 sl@0: test expr-13.4 {CompileUnaryExpr: unary exprs} {expr !2} 0 sl@0: test expr-13.5 {CompileUnaryExpr: unary exprs} {expr +--+-62.0} -62.0 sl@0: test expr-13.6 {CompileUnaryExpr: unary exprs} {expr !0.0} 1 sl@0: test expr-13.7 {CompileUnaryExpr: unary exprs} {expr !0xef} 0 sl@0: test expr-13.8 {CompileUnaryExpr: error compiling unary expr} { sl@0: catch {expr ~x} msg sl@0: set msg sl@0: } {syntax error in expression "~x": variable references require preceding $} sl@0: test expr-13.9 {CompileUnaryExpr: error compiling unary expr} { sl@0: catch {expr !1.x} msg sl@0: set msg sl@0: } {syntax error in expression "!1.x": extra tokens at end of expression} sl@0: test expr-13.10 {CompileUnaryExpr: runtime error} { sl@0: list [catch {expr {~"xx"}} msg] $msg sl@0: } {1 {can't use non-numeric string as operand of "~"}} sl@0: test expr-13.11 {CompileUnaryExpr: runtime error} { sl@0: list [catch {expr ~4.0} msg] $msg sl@0: } {1 {can't use floating-point value as operand of "~"}} sl@0: test expr-13.12 {CompileUnaryExpr: just primary expr} {expr 0x123} 291 sl@0: test expr-13.13 {CompileUnaryExpr: just primary expr} { sl@0: set a 27 sl@0: expr $a sl@0: } 27 sl@0: test expr-13.14 {CompileUnaryExpr: just primary expr} { sl@0: expr double(27) sl@0: } 27.0 sl@0: test expr-13.15 {CompileUnaryExpr: just primary expr} {expr "123"} 123 sl@0: test expr-13.16 {CompileUnaryExpr: error in primary expr} { sl@0: catch {expr [set]} msg sl@0: set msg sl@0: } {wrong # args: should be "set varName ?newValue?"} sl@0: test expr-13.17 {CompileUnaryExpr: negating non-numeric boolean literals} { sl@0: set a1 yes; set a0 no; set b1 true; set b0 false sl@0: list [expr {!$a1}] [expr {!$a0}] [expr {!$b1}] [expr {!$b0}] sl@0: } {0 1 0 1} sl@0: sl@0: test expr-14.1 {CompilePrimaryExpr: literal primary} {expr 1} 1 sl@0: test expr-14.2 {CompilePrimaryExpr: literal primary} {expr 123} 123 sl@0: test expr-14.3 {CompilePrimaryExpr: literal primary} {expr 0xff} 255 sl@0: test expr-14.4 {CompilePrimaryExpr: literal primary} {expr 00010} 8 sl@0: test expr-14.5 {CompilePrimaryExpr: literal primary} {expr 62.0} 62.0 sl@0: test expr-14.6 {CompilePrimaryExpr: literal primary} { sl@0: expr 3.1400000 sl@0: } 3.14 sl@0: test expr-14.7 {CompilePrimaryExpr: literal primary} {expr {{abcde}<{abcdef}}} 1 sl@0: test expr-14.8 {CompilePrimaryExpr: literal primary} {expr {{abc\ sl@0: def} < {abcdef}}} 1 sl@0: test expr-14.9 {CompilePrimaryExpr: literal primary} {expr {{abc\tde} > {abc\tdef}}} 0 sl@0: test expr-14.10 {CompilePrimaryExpr: literal primary} {expr {{123}}} 123 sl@0: test expr-14.11 {CompilePrimaryExpr: var reference primary} { sl@0: set i 789 sl@0: list [expr {$i}] [expr $i] sl@0: } {789 789} sl@0: test expr-14.12 {CompilePrimaryExpr: var reference primary} { sl@0: set i {789} ;# test expr's aggressive conversion to numeric semantics sl@0: list [expr {$i}] [expr $i] sl@0: } {789 789} sl@0: test expr-14.13 {CompilePrimaryExpr: var reference primary} { sl@0: catch {unset a} sl@0: set a(foo) foo sl@0: set a(bar) bar sl@0: set a(123) 123 sl@0: set result "" sl@0: lappend result [expr $a(123)] [expr {$a(bar)<$a(foo)}] sl@0: catch {unset a} sl@0: set result sl@0: } {123 1} sl@0: test expr-14.14 {CompilePrimaryExpr: var reference primary} { sl@0: set i 123 ;# test "$var.0" floating point conversion hack sl@0: list [expr $i] [expr $i.0] [expr $i.0/12.0] sl@0: } {123 123.0 10.25} sl@0: test expr-14.15 {CompilePrimaryExpr: var reference primary} { sl@0: set i 123 sl@0: catch {expr $i.2} msg sl@0: set msg sl@0: } 123.2 sl@0: test expr-14.16 {CompilePrimaryExpr: error compiling var reference primary} { sl@0: catch {expr {$a(foo}} msg sl@0: set errorInfo sl@0: } {missing ) sl@0: while compiling sl@0: "expr {$a(foo}"} sl@0: test expr-14.17 {CompilePrimaryExpr: string primary that looks like var ref} { sl@0: expr $ sl@0: } $ sl@0: test expr-14.18 {CompilePrimaryExpr: quoted string primary} { sl@0: expr "21" sl@0: } 21 sl@0: test expr-14.19 {CompilePrimaryExpr: quoted string primary} { sl@0: set i 123 sl@0: set x 456 sl@0: expr "$i+$x" sl@0: } 579 sl@0: test expr-14.20 {CompilePrimaryExpr: quoted string primary} { sl@0: set i 3 sl@0: set x 6 sl@0: expr 2+"$i.$x" sl@0: } 5.6 sl@0: test expr-14.21 {CompilePrimaryExpr: error in quoted string primary} { sl@0: catch {expr "[set]"} msg sl@0: set msg sl@0: } {wrong # args: should be "set varName ?newValue?"} sl@0: test expr-14.22 {CompilePrimaryExpr: subcommand primary} { sl@0: expr {[set i 123; set i]} sl@0: } 123 sl@0: test expr-14.23 {CompilePrimaryExpr: error in subcommand primary} { sl@0: catch {expr {[set]}} msg sl@0: set errorInfo sl@0: } {wrong # args: should be "set varName ?newValue?" sl@0: while compiling sl@0: "set" sl@0: while compiling sl@0: "expr {[set]}"} sl@0: test expr-14.24 {CompilePrimaryExpr: error in subcommand primary} { sl@0: catch {expr {[set i}} msg sl@0: set errorInfo sl@0: } {missing close-bracket sl@0: while compiling sl@0: "expr {[set i}"} sl@0: test expr-14.25 {CompilePrimaryExpr: math function primary} { sl@0: format %.6g [expr exp(1.0)] sl@0: } 2.71828 sl@0: test expr-14.26 {CompilePrimaryExpr: math function primary} { sl@0: format %.6g [expr pow(2.0+0.1,3.0+0.1)] sl@0: } 9.97424 sl@0: test expr-14.27 {CompilePrimaryExpr: error in math function primary} { sl@0: catch {expr sinh::(2.0)} msg sl@0: set errorInfo sl@0: } {syntax error in expression "sinh::(2.0)": expected parenthesis enclosing function arguments sl@0: while compiling sl@0: "expr sinh::(2.0)"} sl@0: test expr-14.28 {CompilePrimaryExpr: subexpression primary} { sl@0: expr 2+(3*4) sl@0: } 14 sl@0: test expr-14.29 {CompilePrimaryExpr: error in subexpression primary} { sl@0: catch {expr 2+(3*[set])} msg sl@0: set errorInfo sl@0: } {wrong # args: should be "set varName ?newValue?" sl@0: while compiling sl@0: "set" sl@0: while compiling sl@0: "expr 2+(3*[set])"} sl@0: test expr-14.30 {CompilePrimaryExpr: missing paren in subexpression primary} { sl@0: catch {expr 2+(3*(4+5)} msg sl@0: set errorInfo sl@0: } {syntax error in expression "2+(3*(4+5)": looking for close parenthesis sl@0: while compiling sl@0: "expr 2+(3*(4+5)"} sl@0: test expr-14.31 {CompilePrimaryExpr: just var ref in subexpression primary} { sl@0: set i "5+10" sl@0: list "[expr $i] == 15" "[expr ($i)] == 15" "[eval expr ($i)] == 15" sl@0: } {{15 == 15} {15 == 15} {15 == 15}} sl@0: test expr-14.32 {CompilePrimaryExpr: unexpected token} { sl@0: catch {expr @} msg sl@0: set errorInfo sl@0: } {syntax error in expression "@": character not legal in expressions sl@0: while compiling sl@0: "expr @"} sl@0: sl@0: test expr-15.1 {CompileMathFuncCall: missing parenthesis} { sl@0: catch {expr sinh2.0)} msg sl@0: set errorInfo sl@0: } {syntax error in expression "sinh2.0)": variable references require preceding $ sl@0: while compiling sl@0: "expr sinh2.0)"} sl@0: test expr-15.2 {CompileMathFuncCall: unknown math function} { sl@0: catch {expr whazzathuh(1)} msg sl@0: set errorInfo sl@0: } {unknown math function "whazzathuh" sl@0: while compiling sl@0: "expr whazzathuh(1)"} sl@0: test expr-15.3 {CompileMathFuncCall: too many arguments} { sl@0: catch {expr sin(1,2,3)} msg sl@0: set errorInfo sl@0: } {too many arguments for math function sl@0: while compiling sl@0: "expr sin(1,2,3)"} sl@0: test expr-15.4 {CompileMathFuncCall: ')' found before last required arg} { sl@0: catch {expr sin()} msg sl@0: set errorInfo sl@0: } {too few arguments for math function sl@0: while compiling sl@0: "expr sin()"} sl@0: test expr-15.5 {CompileMathFuncCall: too few arguments} { sl@0: catch {expr pow(1)} msg sl@0: set errorInfo sl@0: } {too few arguments for math function sl@0: while compiling sl@0: "expr pow(1)"} sl@0: test expr-15.6 {CompileMathFuncCall: missing ')'} { sl@0: catch {expr sin(1} msg sl@0: set errorInfo sl@0: } {syntax error in expression "sin(1": missing close parenthesis at end of function call sl@0: while compiling sl@0: "expr sin(1"} sl@0: test expr-15.7 {CompileMathFuncCall: call registered math function} {registeredMathFuncs} { sl@0: expr 2*T1() sl@0: } 246 sl@0: test expr-15.8 {CompileMathFuncCall: call registered math function} {registeredMathFuncs} { sl@0: expr T2()*3 sl@0: } 1035 sl@0: test expr-15.9 {CompileMathFuncCall: call registered math function} {registeredMathFuncs} { sl@0: expr T3(21, 37) sl@0: } 37 sl@0: test expr-15.10 {CompileMathFuncCall: call registered math function} {registeredMathFuncs} { sl@0: expr T3(21.2, 37) sl@0: } 37.0 sl@0: test expr-15.11 {CompileMathFuncCall: call registered math function} {registeredMathFuncs} { sl@0: expr T3(-21.2, -17.5) sl@0: } -17.5 sl@0: test expr-15.12 {ExprCallMathFunc: call registered math function} {registeredMathFuncs} { sl@0: expr T3(21, wide(37)) sl@0: } 37 sl@0: test expr=15.13 {ExprCallMathFunc: call registered math function} {registeredMathFuncs} { sl@0: expr T3(wide(21), 37) sl@0: } 37 sl@0: test expr=15.14 {ExprCallMathFunc: call registered math function} {registeredMathFuncs} { sl@0: expr T3(wide(21), wide(37)) sl@0: } 37 sl@0: test expr-15.15 {ExprCallMathFunc: call registered math function} {registeredMathFuncs} { sl@0: expr T3(21.0, wide(37)) sl@0: } 37.0 sl@0: test expr=15.16 {ExprCallMathFunc: call registered math function} {registeredMathFuncs} { sl@0: expr T3(wide(21), 37.0) sl@0: } 37.0 sl@0: sl@0: test expr-16.1 {GetToken: checks whether integer token starting with "0x" (e.g., "0x$") is invalid} { sl@0: catch {unset a} sl@0: set a(VALUE) ff15 sl@0: set i 123 sl@0: if {[expr 0x$a(VALUE)] & 16} { sl@0: set i {} sl@0: } sl@0: set i sl@0: } {} sl@0: test expr-16.2 {GetToken: check for string literal in braces} { sl@0: expr {{1}} sl@0: } {1} sl@0: sl@0: # Check "expr" and computed command names. sl@0: sl@0: test expr-17.1 {expr and computed command names} { sl@0: set i 0 sl@0: set z expr sl@0: $z 1+2 sl@0: } 3 sl@0: sl@0: # Check correct conversion of operands to numbers: If the string looks like sl@0: # an integer, convert to integer. Otherwise, if the string looks like a sl@0: # double, convert to double. sl@0: sl@0: test expr-18.1 {expr and conversion of operands to numbers} { sl@0: set x [lindex 11 0] sl@0: catch {expr int($x)} sl@0: expr {$x} sl@0: } 11 sl@0: test expr-18.2 {whitespace strings should not be == 0 (buggy strtod)} { sl@0: expr {" "} sl@0: } { } sl@0: sl@0: # Check "expr" and interpreter result object resetting before appending sl@0: # an error msg during evaluation of exprs not in {}s sl@0: sl@0: test expr-19.1 {expr and interpreter result object resetting} { sl@0: proc p {} { sl@0: set t 10.0 sl@0: set x 2.0 sl@0: set dx 0.2 sl@0: set f {$dx-$x/10} sl@0: set g {-$x/5} sl@0: set center 1.0 sl@0: set x [expr $x-$center] sl@0: set dx [expr $dx+$g] sl@0: set x [expr $x+$f+$center] sl@0: set x [expr $x+$f+$center] sl@0: set y [expr round($x)] sl@0: } sl@0: p sl@0: } 3 sl@0: sl@0: # Test for incorrect "double evaluation" semantics sl@0: sl@0: test expr-20.1 {wrong brace matching} { sl@0: catch {unset l} sl@0: catch {unset r} sl@0: catch {unset q} sl@0: catch {unset cmd} sl@0: catch {unset a} sl@0: set l "\{"; set r "\}"; set q "\"" sl@0: set cmd "expr $l$q|$q == $q$r$q$r" sl@0: list [catch $cmd a] $a sl@0: } {1 {extra characters after close-brace}} sl@0: test expr-20.2 {double invocation of variable traces} { sl@0: set exprtracecounter 0 sl@0: proc exprtraceproc {args} { sl@0: upvar #0 exprtracecounter counter sl@0: set argc [llength $args] sl@0: set extraargs [lrange $args 0 [expr {$argc - 4}]] sl@0: set name [lindex $args [expr {$argc - 3}]] sl@0: upvar 1 $name var sl@0: if {[incr counter] % 2 == 1} { sl@0: set var "$counter oops [concat $extraargs]" sl@0: } else { sl@0: set var "$counter + [concat $extraargs]" sl@0: } sl@0: } sl@0: trace variable exprtracevar r [list exprtraceproc 10] sl@0: list [catch {expr "$exprtracevar + 20"} a] $a \ sl@0: [catch {expr "$exprtracevar + 20"} b] $b \ sl@0: [unset exprtracevar exprtracecounter] sl@0: } {1 {syntax error in expression "1 oops 10 + 20": extra tokens at end of expression} 0 32 {}} sl@0: test expr-20.3 {broken substitution of integer digits} { sl@0: # fails with 8.0.x, but not 8.1b2 sl@0: list [set a 000; expr 0x1$a] [set a 1; expr ${a}000] sl@0: } {4096 1000} sl@0: test expr-20.4 {proper double evaluation compilation, error case} { sl@0: catch {unset a}; # make sure $a doesn't exist sl@0: list [catch {expr 1?{$a}:0} msg] $msg sl@0: } {1 {can't read "a": no such variable}} sl@0: test expr-20.5 {proper double evaluation compilation, working case} { sl@0: set a yellow sl@0: expr 1?{$a}:0 sl@0: } yellow sl@0: test expr-20.6 {handling of compile error in trial compile} { sl@0: list [catch {expr + {[incr]}} msg] $msg sl@0: } {1 {wrong # args: should be "incr varName ?increment?"}} sl@0: test expr-20.7 {handling of compile error in runtime case} { sl@0: list [catch {expr + {[error foo]}} msg] $msg sl@0: } {1 foo} sl@0: sl@0: # Test for non-numeric boolean literal handling sl@0: test expr-21.1 {non-numeric boolean literals} {expr false } false sl@0: test expr-21.2 {non-numeric boolean literals} {expr true } true sl@0: test expr-21.3 {non-numeric boolean literals} {expr off } off sl@0: test expr-21.4 {non-numeric boolean literals} {expr on } on sl@0: test expr-21.5 {non-numeric boolean literals} {expr no } no sl@0: test expr-21.6 {non-numeric boolean literals} {expr yes } yes sl@0: test expr-21.7 {non-numeric boolean literals} {expr !false} 1 sl@0: test expr-21.8 {non-numeric boolean literals} {expr !true } 0 sl@0: test expr-21.9 {non-numeric boolean literals} {expr !off } 1 sl@0: test expr-21.10 {non-numeric boolean literals} {expr !on } 0 sl@0: test expr-21.11 {non-numeric boolean literals} {expr !no } 1 sl@0: test expr-21.12 {non-numeric boolean literals} {expr !yes } 0 sl@0: sl@0: # Test for non-numeric float handling. sl@0: # sl@0: # These are non-portable because strtod()-support for "Inf" and "NaN" sl@0: # is so wildly variable. This sucks... sl@0: test expr-22.1 {non-numeric floats} nonPortable { sl@0: list [catch {expr {NaN + 1}} msg] $msg sl@0: } {1 {can't use non-numeric floating-point value as operand of "+"}} sl@0: test expr-22.2 {non-numeric floats} nonPortable { sl@0: list [catch {expr {Inf + 1}} msg] $msg sl@0: } {1 {can't use infinite floating-point value as operand of "+"}} sl@0: test expr-22.3 {non-numeric floats} nonPortable { sl@0: set nan NaN sl@0: list [catch {expr {$nan + 1}} msg] $msg sl@0: } {1 {can't use non-numeric floating-point value as operand of "+"}} sl@0: test expr-22.4 {non-numeric floats} nonPortable { sl@0: set inf Inf sl@0: list [catch {expr {$inf + 1}} msg] $msg sl@0: } {1 {can't use infinite floating-point value as operand of "+"}} sl@0: test expr-22.5 {non-numeric floats} nonPortable { sl@0: list [catch {expr NaN} msg] $msg sl@0: } {1 {domain error: argument not in valid range}} sl@0: test expr-22.6 {non-numeric floats} nonPortable { sl@0: list [catch {expr Inf} msg] $msg sl@0: } {1 {floating-point value too large to represent}} sl@0: test expr-22.7 {non-numeric floats} nonPortable { sl@0: list [catch {expr {1 / NaN}} msg] $msg sl@0: } {1 {can't use non-numeric floating-point value as operand of "/"}} sl@0: test expr-22.8 {non-numeric floats} nonPortable { sl@0: list [catch {expr {1 / Inf}} msg] $msg sl@0: } {1 {can't use infinite floating-point value as operand of "/"}} sl@0: # Make sure [Bug 761471] stays fixed. sl@0: test expr-22.9 {non-numeric floats: shared object equality and NaN} nonPortable { sl@0: set x NaN sl@0: expr {$x == $x} sl@0: } 0 sl@0: sl@0: # Some compilers get this wrong; ensure that we work around it correctly sl@0: test expr-24.1 {expr edge cases; shifting} {expr int(5)>>31} 0 sl@0: test expr-24.2 {expr edge cases; shifting} {expr int(5)>>63} 0 sl@0: test expr-24.3 {expr edge cases; shifting} {expr wide(5)>>31} 0 sl@0: test expr-24.4 {expr edge cases; shifting} {expr wide(5)>>63} 0 sl@0: test expr-24.5 {expr edge cases; shifting} nonPortable {expr int(5)<<31} 0 sl@0: test expr-24.6 {expr edge cases; shifting} nonPortable {expr int(5)<<63} 0 sl@0: test expr-24.7 {expr edge cases; shifting} {expr wide(5)<<31} 10737418240 sl@0: test expr-24.8 {expr edge cases; shifting} nonPortable {expr wide(5)<<63} -9223372036854775808 sl@0: test expr-24.9 {expr edge cases; shifting} {expr 5>>32} 0 sl@0: sl@0: test expr-38.1 {abs of smallest 32-bit integer [Bug 1241572]} {wideIs64bit} { sl@0: expr {abs(int(-2147483648))} sl@0: } 2147483648 sl@0: sl@0: test expr-46.1 {round() rounds to +-infinity} { sl@0: expr round(0.5) sl@0: } 1 sl@0: test expr-46.2 {round() rounds to +-infinity} { sl@0: expr round(1.5) sl@0: } 2 sl@0: test expr-46.3 {round() rounds to +-infinity} { sl@0: expr round(-0.5) sl@0: } -1 sl@0: test expr-46.4 {round() rounds to +-infinity} { sl@0: expr round(-1.5) sl@0: } -2 sl@0: test expr-46.5 {round() overflow} { sl@0: list [catch {expr round(9.2233720368547758e+018)} result] $result sl@0: } {1 {integer value too large to represent}} sl@0: test expr-46.6 {round() overflow} { sl@0: list [catch {expr round(-9.2233720368547758e+018)} result] $result sl@0: } {1 {integer value too large to represent}} sl@0: test expr-46.7 {round() bad value} { sl@0: set x trash sl@0: list [catch {expr {round($x)}} result] $result sl@0: } {1 {argument to math function didn't have numeric value}} sl@0: test expr-46.8 {round() already an integer} { sl@0: set x 123456789012 sl@0: incr x sl@0: expr round($x) sl@0: } 123456789013 sl@0: test expr-46.9 {round() boundary case - 1/2 - 1 ulp} { sl@0: set x 0.25 sl@0: set bit 0.125 sl@0: while 1 { sl@0: set newx [expr {$x + $bit}] sl@0: if { $newx == $x || $newx == 0.5 } break sl@0: set x $newx sl@0: set bit [expr { $bit / 2.0 }] sl@0: } sl@0: expr {round($x)} sl@0: } 0 sl@0: test expr-46.10 {round() boundary case - 1/2 + 1 ulp} { sl@0: set x 0.75 sl@0: set bit 0.125 sl@0: while 1 { sl@0: set newx [expr { $x - $bit }] sl@0: if { $newx == $x || $newx == 0.5 } break sl@0: set x $newx sl@0: set bit [expr { $bit / 2.0 }] sl@0: } sl@0: expr {round($x)} sl@0: } 1 sl@0: test expr-46.11 {round() boundary case - -1/2 - 1 ulp} { sl@0: set x -0.75 sl@0: set bit 0.125 sl@0: while 1 { sl@0: set newx [expr { $x + $bit }] sl@0: if { $newx == $x || $newx == -0.5 } break sl@0: set x $newx sl@0: set bit [expr { $bit / 2.0 }] sl@0: } sl@0: expr {round($x)} sl@0: } -1 sl@0: test expr-46.12 {round() boundary case - -1/2 + 1 ulp} { sl@0: set x -0.25 sl@0: set bit 0.125 sl@0: while 1 { sl@0: set newx [expr { $x - $bit }] sl@0: if { $newx == $x || $newx == -0.5 } break sl@0: set x $newx sl@0: set bit [expr { $bit / 2.0 }] sl@0: } sl@0: expr {round($x)} sl@0: } 0 sl@0: sl@0: test expr-46.13 {round() boundary case - round down} { sl@0: expr {round(2147483647 - 0.51)} sl@0: } 2147483646 sl@0: sl@0: test expr-46.14 {round() boundary case - round up} { sl@0: expr {round(2147483647 - 0.50)} sl@0: } 2147483647 sl@0: sl@0: test expr-46.15 {round() boundary case - round up to wide} { sl@0: expr {round(2147483647 + 0.50)} sl@0: } [expr {wide(2147483647) + 1}] sl@0: sl@0: test expr-46.16 {round() boundary case - round up} { sl@0: expr {round(-2147483648 + 0.51)} sl@0: } -2147483647 sl@0: sl@0: test expr-46.17 {round() boundary case - round down} { sl@0: expr {round(-2147483648 + 0.50)} sl@0: } -2147483648 sl@0: test expr-46.18 {round() boundary case - round down to wide} { sl@0: expr {round(-2147483648 - 0.50)} sl@0: } [expr {wide(-2147483648) - 1}] sl@0: sl@0: # cleanup sl@0: if {[info exists a]} { sl@0: unset a sl@0: } sl@0: ::tcltest::cleanupTests sl@0: return