diff options
author | Steve Bennett <steveb@workware.net.au> | 2010-01-24 12:05:36 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2010-10-15 11:02:44 +1000 |
commit | a7335808c5725934d81dbe68247b62a6ab08bf2f (patch) | |
tree | de45f62dacf49ebabc9b89a441b6d8c3e8f14256 | |
parent | a17425e476861fde1e1ad824181f97e081740659 (diff) | |
download | jimtcl-a7335808c5725934d81dbe68247b62a6ab08bf2f.zip jimtcl-a7335808c5725934d81dbe68247b62a6ab08bf2f.tar.gz jimtcl-a7335808c5725934d81dbe68247b62a6ab08bf2f.tar.bz2 |
More expr tests and fixes
*: Add tests/expr-new.test from Tcl
*: Directly convert from int to double if possible
*: Always treat '-' in front of a number as unary minus
so that '-0x1234' works.
*: Fix expr when the variable does not exist
*: Add optional support for math functions
*: Also double/0 == Inf or -Inf
-rw-r--r-- | TODO | 12 | ||||
-rw-r--r-- | bench.tcl | 11 | ||||
-rw-r--r-- | jim.c | 153 | ||||
-rw-r--r-- | tests/expr-new.test | 582 | ||||
-rw-r--r-- | tests/expr-old.test | 15 | ||||
-rw-r--r-- | tests/perf.test | 9 |
6 files changed, 736 insertions, 46 deletions
@@ -1,9 +1,5 @@ CORE LANGUAGE FEATURES -- lrepeat -- parse foo($bar) into special tokens so that interpolation/parsing - is not required every time - CORE COMMANDS - All the missing standard core commands not related to I/O, namespaces, ... @@ -40,10 +36,6 @@ SPEED OPTIMIZATIONS the performance penality of Jim_EvalObj() overhead. In the future try to generate the calls like a JIT emitting assembler from Jim directly. -- Jim_GetDouble() should check if the object type is an integer into - a range that a double can represent without any loss, and directly - return the double value converting the integer one instead to pass - for the string repr. IMPLEMENTATION ISSUES @@ -63,10 +55,6 @@ REFERENCES SYSTEM - Add a 'call' attribute to references in order to call a given procedure if the name of a reference is used as command name. -API FUNCTIONS TO EXPORT - -- Jim_FormatString() - RANDOM THINGS TO DO ASAP - .jimrc loading, using the ENV variable @@ -160,6 +160,16 @@ proc ary n { } } +proc ary_dict n { + for {set i 0} {$i < $n} {incr i} { + dict set x $i $i + } + set last [expr {$n - 1}] + for {set j $last} {$j >= 0} {incr j -1} { + dict set y $j $x($j) + } +} + ### REPEAT ##################################################################### proc repeat {n body} { @@ -548,6 +558,7 @@ bench {heapsort} {heapsort_main} bench {sieve} {sieve 10} bench {sieve [dict]} {sieve_dict 10} bench {ary} {ary 100000} +bench {ary [dict]} {ary_dict 100000} bench {repeat} {use_repeat} bench {upvar} {upvartest} bench {nested loops} {nestedloops} @@ -95,6 +95,10 @@ #include <execinfo.h> #endif +#ifdef JIM_MATH_FUNCTIONS +#include <math.h> +#endif + /* ----------------------------------------------------------------------------- * Global variables * ---------------------------------------------------------------------------*/ @@ -371,6 +375,10 @@ int Jim_DoubleToString(char *buf, double doubleValue) * for NaN or InF */ while (*buf) { if (*buf == '.' || isalpha(*buf)) { + /* inf -> Inf, nan -> Nan */ + if (*buf == 'i' || *buf == 'n') { + *buf = toupper(*buf); + } return len; } buf++; @@ -388,7 +396,7 @@ int Jim_StringToDouble(const char *str, double *doublePtr) char *endptr; *doublePtr = strtod(str, &endptr); - if (str[0] == '\0' || endptr[0] != '\0' || (str == endptr) ) { + if (str[0] == '\0' || endptr[0] != '\0' || (str == endptr)) { return JIM_ERR; } return JIM_OK; @@ -4751,17 +4759,35 @@ int SetDoubleFromAny(Jim_Interp *interp, Jim_Obj *objPtr) double doubleValue; const char *str; - /* Get the string representation */ + /* Preserve the string representation. + * Needed so we can convert back to int without loss + */ str = Jim_GetString(objPtr, NULL); - /* Try to convert into a double */ - if (Jim_StringToDouble(str, &doubleValue) != JIM_OK) { - Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); - Jim_AppendStrings(interp, Jim_GetResult(interp), - "expected number but got '", str, "'", NULL); - return JIM_ERR; + + /* Assume a 53 bit mantissa */ +#define MIN_INT_IN_DOUBLE -(1LL << 53) +#define MAX_INT_IN_DOUBLE -(MIN_INT_IN_DOUBLE + 1) + + if (objPtr->typePtr == &intObjType + && objPtr->internalRep.wideValue >= MIN_INT_IN_DOUBLE + && objPtr->internalRep.wideValue <= MAX_INT_IN_DOUBLE + ) + { + + /* Direct conversion without loss */ + doubleValue = objPtr->internalRep.wideValue; + } + else { + /* Try to convert into a double */ + if (Jim_StringToDouble(str, &doubleValue) != JIM_OK) { + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); + Jim_AppendStrings(interp, Jim_GetResult(interp), + "expected number but got '", str, "'", NULL); + return JIM_ERR; + } + /* Free the old internal repr and set the new one. */ + Jim_FreeIntRep(interp, objPtr); } - /* Free the old internal repr and set the new one. */ - Jim_FreeIntRep(interp, objPtr); objPtr->typePtr = &doubleObjType; objPtr->internalRep.doubleValue = doubleValue; return JIM_OK; @@ -6143,6 +6169,25 @@ enum { JIM_EXPROP_FUNC_ABS, JIM_EXPROP_FUNC_DOUBLE, JIM_EXPROP_FUNC_ROUND, + +#ifdef JIM_MATH_FUNCTIONS + /* math functions from libm */ + JIM_EXPROP_FUNC_SIN, + JIM_EXPROP_FUNC_COS, + JIM_EXPROP_FUNC_TAN, + JIM_EXPROP_FUNC_ASIN, + JIM_EXPROP_FUNC_ACOS, + JIM_EXPROP_FUNC_ATAN, + JIM_EXPROP_FUNC_SINH, + JIM_EXPROP_FUNC_COSH, + JIM_EXPROP_FUNC_TANH, + JIM_EXPROP_FUNC_CEIL, + JIM_EXPROP_FUNC_FLOOR, + JIM_EXPROP_FUNC_EXP, + JIM_EXPROP_FUNC_LOG, + JIM_EXPROP_FUNC_LOG10, + JIM_EXPROP_FUNC_SQRT, +#endif }; struct expr_state { @@ -6285,6 +6330,42 @@ static int JimExprOpIntUnary(Jim_Interp *interp, struct expr_state *e) return rc; } +#ifdef JIM_MATH_FUNCTIONS +static int JimExprOpDoubleUnary(Jim_Interp *interp, struct expr_state *e) +{ + int rc; + Jim_Obj *A = expr_pop(e); + double dA, dC; + + rc = Jim_GetDouble(interp, A, &dA); + if (rc == JIM_OK) { + switch (e->opcode) { + case JIM_EXPROP_FUNC_SIN: dC = sin(dA); break; + case JIM_EXPROP_FUNC_COS: dC = cos(dA); break; + case JIM_EXPROP_FUNC_TAN: dC = tan(dA); break; + case JIM_EXPROP_FUNC_ASIN: dC=asin(dA); break; + case JIM_EXPROP_FUNC_ACOS: dC=acos(dA); break; + case JIM_EXPROP_FUNC_ATAN: dC=atan(dA); break; + case JIM_EXPROP_FUNC_SINH: dC=sinh(dA); break; + case JIM_EXPROP_FUNC_COSH: dC=cosh(dA); break; + case JIM_EXPROP_FUNC_TANH: dC=tanh(dA); break; + case JIM_EXPROP_FUNC_CEIL: dC=ceil(dA); break; + case JIM_EXPROP_FUNC_FLOOR: dC=floor(dA); break; + case JIM_EXPROP_FUNC_EXP: dC=exp(dA); break; + case JIM_EXPROP_FUNC_LOG: dC=log(dA); break; + case JIM_EXPROP_FUNC_LOG10: dC=log10(dA); break; + case JIM_EXPROP_FUNC_SQRT: dC=sqrt(dA); break; + default: abort(); + } + expr_push(e, Jim_NewDoubleObj(interp, dC)); + } + + Jim_DecrRefCount(interp, A); + + return rc; +} +#endif + /* A binary operation on two ints */ static int JimExprOpIntBin(Jim_Interp *interp, struct expr_state *e) { @@ -6378,12 +6459,12 @@ static int JimExprOpBin(Jim_Interp *interp, struct expr_state *e) intresult = 1; switch (e->opcode) { + case JIM_EXPROP_POW: wC = JimPowWide(wA,wB); break; case JIM_EXPROP_ADD: wC = wA+wB; break; case JIM_EXPROP_SUB: wC = wA-wB; break; case JIM_EXPROP_MUL: wC = wA*wB; break; case JIM_EXPROP_DIV: if (wB == 0) { - wC = 0; Jim_SetResultString(interp, "Division by zero", -1); rc = JIM_ERR; } @@ -6425,14 +6506,19 @@ static int JimExprOpBin(Jim_Interp *interp, struct expr_state *e) } switch (e->opcode) { + case JIM_EXPROP_POW: +#ifdef JIM_MATH_FUNCTIONS + dC = pow(dA,dB); +#else + rc = JIM_ERR; +#endif + break; case JIM_EXPROP_ADD: dC = dA+dB; break; case JIM_EXPROP_SUB: dC = dA-dB; break; case JIM_EXPROP_MUL: dC = dA*dB; break; case JIM_EXPROP_DIV: if (dB == 0) { - dC = 0; - Jim_SetResultString(interp, "Division by zero", -1); - rc = JIM_ERR; + dC = dA < 0 ? -INFINITY : INFINITY; } else { dC = dA/dB; @@ -6693,12 +6779,30 @@ static const struct Jim_ExprOperator Jim_ExprOperators[] = { [JIM_EXPROP_FUNC_ABS] = {"abs", 400, 1, JimExprOpNumUnary }, [JIM_EXPROP_FUNC_ROUND] = {"round", 400, 1, JimExprOpNumUnary }, +#ifdef JIM_MATH_FUNCTIONS + [JIM_EXPROP_FUNC_SIN] = {"sin", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_COS] = {"cos", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_TAN] = {"tan", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_ASIN] = {"asin", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_ACOS] = {"acos", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_ATAN] = {"atan", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_SINH] = {"sinh", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_COSH] = {"cosh", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_TANH] = {"tanh", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_CEIL] = {"ceil", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_FLOOR] = {"floor", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_EXP] = {"exp", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_LOG] = {"log", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_LOG10] = {"log10", 400, 1, JimExprOpDoubleUnary }, + [JIM_EXPROP_FUNC_SQRT] = {"sqrt", 400, 1, JimExprOpDoubleUnary }, +#endif + [JIM_EXPROP_NOT] = {"!", 300, 1, JimExprOpNumUnary }, [JIM_EXPROP_BITNOT] = {"~", 300, 1, JimExprOpIntUnary }, [JIM_EXPROP_UNARYMINUS] = {"unarymin", 300, 1, JimExprOpNumUnary }, [JIM_EXPROP_UNARYPLUS] = {"unaryplus", 300, 1, JimExprOpNumUnary }, - [JIM_EXPROP_POW] = {"**", 250, 2, JimExprOpIntBin }, + [JIM_EXPROP_POW] = {"**", 250, 2, JimExprOpBin }, [JIM_EXPROP_MUL] = {"*", 200, 2, JimExprOpBin }, [JIM_EXPROP_DIV] = {"/", 200, 2, JimExprOpBin }, @@ -6790,13 +6894,6 @@ int JimParseExpression(struct JimParserCtx *pc) else return JIM_OK; break; - case '-': - if ((pc->tt == JIM_TT_NONE || pc->tt == JIM_TT_EXPR_OPERATOR) && - isdigit(*(pc->p+1))) - return JimParseExprNumber(pc); - else - return JimParseExprOperator(pc); - break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.': return JimParseExprNumber(pc); @@ -6826,9 +6923,6 @@ int JimParseExprNumber(struct JimParserCtx *pc) pc->tstart = pc->p; pc->tline = pc->linenr; - if (*pc->p == '-') { - pc->p++; pc->len--; - } while ( isdigit(*pc->p) || (allowhex && isxdigit(*pc->p) ) || (allowdot && *pc->p == '.') @@ -6911,6 +7005,7 @@ int JimParseExprOperator(struct JimParserCtx *pc) pc->tend = pc->p + bestLen - 1; pc->p += bestLen; pc->len -= bestLen; pc->tline = pc->linenr; + pc->tt = JIM_TT_EXPR_OPERATOR; return JIM_OK; } @@ -7209,7 +7304,7 @@ int SetExprFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) int prevtt = parser.tt; if (JimParseExpression(&parser) != JIM_OK) { - Jim_SetResultString(interp, "Syntax error in expression: ", -1); + Jim_SetResultString(interp, "syntax error in expression: ", -1); Jim_AppendStrings(interp, Jim_GetResult(interp), exprText, NULL); goto err; } @@ -7409,10 +7504,16 @@ int Jim_EvalExpression(Jim_Interp *interp, Jim_Obj *exprObjPtr, case JIM_EXPROP_VARIABLE: objPtr = Jim_GetVariable(interp, expr->obj[i], JIM_ERRMSG); + if (!objPtr) { + retcode = JIM_ERR; + } break; case JIM_EXPROP_DICTSUGAR: objPtr = Jim_ExpandDictSugar(interp, expr->obj[i]); + if (!objPtr) { + retcode = JIM_ERR; + } break; case JIM_EXPROP_SUBST: diff --git a/tests/expr-new.test b/tests/expr-new.test new file mode 100644 index 0000000..c6da9fc --- /dev/null +++ b/tests/expr-new.test @@ -0,0 +1,582 @@ +# Commands covered: expr +# +# This file contains a collection of tests for one or more of the Tcl +# built-in commands. Sourcing this file into Tcl runs the tests and +# generates output for errors. No output means no errors were found. +# +# Copyright (c) 1996-1997 Sun Microsystems, Inc. +# Copyright (c) 1998-1999 by Scriptics Corporation. +# +# See the file "license.terms" for information on usage and redistribution +# of this file, and for a DISCLAIMER OF ALL WARRANTIES. +# +# RCS: @(#) $Id: expr.test,v 1.9 2000/04/10 17:18:59 ericm Exp $ + +source testing.tcl + +# procedures used below + +proc put_hello_char {c} { + global a + append a [format %c $c] + return $c +} +proc hello_world {} { + global a + set a "" + set L1 [set l0 [set h_1 [set q 0]]] + for {put_hello_char [expr [put_hello_char [expr [set h 7]*10+2]]+29]} {$l0?[put_hello_char $l0] + :!$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"])] + ?[set ll [set l0 [expr 54<<1]]]:$ll==108&&$L1<3? + [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"])]]]] + :[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 + ?[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"])]]} + expr {$L1!=1<<3?[incr q [expr ([string length "abc"]-[string length "abc"])-1]]:[set h_1 [set ll $h1]]} + } + set a +} + +proc 12days {a b c} { + global xxx + expr {1<$a?[expr {$a<3?[12days -79 -13 [string range $c [12days -87 \ + [expr 1-$b] [string range $c [12days -86 0 [string range $c 1 end]] \ + end]] end]]:1};expr {$a<$b?[12days [expr $a+1] $b $c]:3};expr {[12days \ + -94 [expr $a-27] $c]&&$a==2?$b<13?[12days 2 [expr $b+1] "%s %d %d\n"]:9 + :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' ')# \}'+\}##(!!/"] + :$a<-50?[string compare [format %c $b] [string index $c 0]]==0?[append \ + xxx [string index $c 31];scan [string index $c 31] %c x;set x] + :[12days -65 $b [string range $c 1 end]]:[12days [expr ([string compare \ + [string index $c 0] "/"]==0)+$a] $b [string range $c 1 end]]:0<$a + ?[12days 2 2 "%s"]:[string compare [string index $c 0] "/"]==0|| + [12days 0 [12days -61 [scan [string index $c 0] %c x; set x] \ + "!ek;dc i@bK'(q)-\[w\]*%n+r3#l,\{\}:\nuwloca-O;m .vpbks,fxntdCeghiry"] \ + [string range $c 1 end]]} +} +proc do_twelve_days {} { + global xxx + set xxx "" + 12days 1 1 1 + string length $xxx +} + +# start of tests + +catch {unset a b i x} + +test expr-1.1 {TclCompileExprCmd: no expression} { + list [catch {expr } msg] +} {1} +test expr-1.2 {TclCompileExprCmd: one expression word} { + expr -25 +} -25 +test expr-1.3 {TclCompileExprCmd: two expression words} { + expr -8.2 -6 +} -14.2 +test expr-1.4 {TclCompileExprCmd: five expression words} { + expr 20 - 5 +10 -7 +} 18 +test expr-1.5 {TclCompileExprCmd: quoted expression word} { + expr "0005" +} 5 +test expr-1.6 {TclCompileExprCmd: quoted expression word} { + catch {expr "0005"zxy} msg +} {1} +test expr-1.7 {TclCompileExprCmd: expression word in braces} { + expr {-0005} +} -5 +# XXX: I believe that this ought to return a string, thus -0x1234 +#test expr-1.8 {TclCompileExprCmd: expression word in braces} { +# expr {{-0x1234}} +#} -4660 +test expr-1.9 {TclCompileExprCmd: expression word in braces} { + catch {expr {-0005}foo} msg +} {1} +test expr-1.10 {TclCompileExprCmd: other expression word in braces} { + expr 4*[llength "6 2"] +} 8 +test expr-1.11 {TclCompileExprCmd: expression word terminated by ;} { + expr 4*[llength "6 2"]; +} 8 +test expr-1.12 {TclCompileExprCmd: inlined expr (in "catch") inside other catch} { + set a xxx + catch { + # Might not be a number + set a [expr 10*$a] + } +} 1 +test expr-1.13 {TclCompileExprCmd: second level of substitutions in expr not in braces with single var reference} { + set a xxx + set x 27; set bool {$x}; if $bool {set a foo} + set a +} foo +test expr-1.14 {TclCompileExprCmd: second level of substitutions in expr with comparison as top-level operator} { + set a xxx + set x 2; set b {$x}; set a [expr $b == 2] + set a +} 1 + +test expr-2.1 {TclCompileExpr: are builtin functions registered?} { + expr double(5*[llength "6 2"]) +} 10.0 +test expr-2.2 {TclCompileExpr: error in expr} { + catch {expr 2//3} msg +} {1} +test expr-2.3 {TclCompileExpr: junk after legal expr} { + catch {expr 7*[llength "a b"]foo} msg +} {1} +test expr-2.4 {TclCompileExpr: numeric expr string rep == formatted int rep} { + expr {0001} +} 1 + +test expr-3.1 {CompileCondExpr: just lor expr} {expr 3||0} 1 +test expr-3.2 {CompileCondExpr: error in lor expr} { + catch {expr x||3} msg +} {1} +test expr-3.3 {CompileCondExpr: test true arm} {expr 3>2?44:66} 44 +test expr-3.4 {CompileCondExpr: error compiling true arm} { + catch {expr 3>2?2//3:66} msg +} {1} +test expr-3.5 {CompileCondExpr: test false arm} {expr 2>3?44:66} 66 +test expr-3.6 {CompileCondExpr: error compiling false arm} { + catch {expr 2>3?44:2//3} msg +} {1} +if {0} { +test expr-3.7 {CompileCondExpr: long arms & nested cond exprs} { + puts "Note: doing test expr-3.7 which can take several minutes to run" + hello_world +} {Hello world} +catch {unset xxx} +test expr-3.8 {CompileCondExpr: long arms & nested cond exprs} { + puts "Note: doing test expr-3.8 which can take several minutes to run" + do_twelve_days +} 2358 +catch {unset xxx} +} + +test expr-4.1 {CompileLorExpr: just land expr} {expr 1.3&&3.3} 1 +test expr-4.2 {CompileLorExpr: error in land expr} { + catch {expr x&&3} msg +} {1} +test expr-4.3 {CompileLorExpr: simple lor exprs} {expr 0||1.0} 1 +test expr-4.4 {CompileLorExpr: simple lor exprs} {expr 3.0||0.0} 1 +test expr-4.5 {CompileLorExpr: simple lor exprs} {expr 0||0||1} 1 +test expr-4.6 {CompileLorExpr: error compiling lor arm} { + catch {expr 2//3||4.0} msg +} {1} +test expr-4.7 {CompileLorExpr: error compiling lor arm} { + catch {expr 1.3||2//3} msg +} {1} +test expr-4.8 {CompileLorExpr: error compiling lor arms} { + list [catch {expr {"a"||"b"}} msg] +} {1} +test expr-4.9 {CompileLorExpr: long lor arm} { + set a "abcdefghijkl" + set i 7 + 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]]} +} 1 + +test expr-5.1 {CompileLandExpr: just bitor expr} {expr 7|0x13} 23 +test expr-5.2 {CompileLandExpr: error in bitor expr} { + catch {expr x|3} msg +} {1} +test expr-5.3 {CompileLandExpr: simple land exprs} {expr 0&&1.0} 0 +test expr-5.4 {CompileLandExpr: simple land exprs} {expr 0&&0} 0 +test expr-5.5 {CompileLandExpr: simple land exprs} {expr 3.0&&1.2} 1 +test expr-5.6 {CompileLandExpr: simple land exprs} {expr 1&&1&&2} 1 +test expr-5.7 {CompileLandExpr: error compiling land arm} { + catch {expr 2//3&&4.0} msg +} {1} +test expr-5.8 {CompileLandExpr: error compiling land arm} { + catch {expr 1.3&&2//3} msg +} {1} +test expr-5.9 {CompileLandExpr: error compiling land arm} { + list [catch {expr {"a"&&"b"}} msg] +} {1} +test expr-5.10 {CompileLandExpr: long land arms} { + set a "abcdefghijkl" + set i 7 + 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]]} +} 1 + +test expr-6.1 {CompileBitXorExpr: just bitand expr} {expr 7&0x13} 3 +test expr-6.2 {CompileBitXorExpr: error in bitand expr} { + catch {expr x|3} msg +} {1} +test expr-6.3 {CompileBitXorExpr: simple bitxor exprs} {expr 7^0x13} 20 +test expr-6.4 {CompileBitXorExpr: simple bitxor exprs} {expr 3^0x10} 19 +test expr-6.5 {CompileBitXorExpr: simple bitxor exprs} {expr 0^7} 7 +test expr-6.6 {CompileBitXorExpr: simple bitxor exprs} {expr -1^7} -8 +test expr-6.7 {CompileBitXorExpr: error compiling bitxor arm} { + catch {expr 2//3|6} msg +} {1} +test expr-6.8 {CompileBitXorExpr: error compiling bitxor arm} { + catch {expr 2^x} msg +} {1} +test expr-6.9 {CompileBitXorExpr: runtime error in bitxor arm} { + list [catch {expr {24.0^3}} msg] +} {1} +test expr-6.10 {CompileBitXorExpr: runtime error in bitxor arm} { + list [catch {expr {"a"^"b"}} msg] +} {1} + +test expr-7.1 {CompileBitAndExpr: just equality expr} {expr 3==2} 0 +test expr-7.2 {CompileBitAndExpr: just equality expr} {expr 2.0==2} 1 +test expr-7.3 {CompileBitAndExpr: just equality expr} {expr 3.2!=2.2} 1 +test expr-7.4 {CompileBitAndExpr: just equality expr} {expr {"abc" == "abd"}} 0 +test expr-7.5 {CompileBitAndExpr: error in equality expr} { + catch {expr x==3} msg +} {1} +test expr-7.6 {CompileBitAndExpr: simple bitand exprs} {expr 7&0x13} 3 +test expr-7.7 {CompileBitAndExpr: simple bitand exprs} {expr 0xf2&0x53} 82 +test expr-7.8 {CompileBitAndExpr: simple bitand exprs} {expr 3&6} 2 +test expr-7.9 {CompileBitAndExpr: simple bitand exprs} {expr -1&-7} -7 +test expr-7.10 {CompileBitAndExpr: error compiling bitand arm} { + catch {expr 2//3&6} msg +} {1} +test expr-7.11 {CompileBitAndExpr: error compiling bitand arm} { + catch {expr 2&x} msg +} {1} +test expr-7.12 {CompileBitAndExpr: runtime error in bitand arm} { + list [catch {expr {24.0&3}} msg] +} {1} +test expr-7.13 {CompileBitAndExpr: runtime error in bitand arm} { + list [catch {expr {"a"&"b"}} msg] +} {1} + +test expr-8.1 {CompileEqualityExpr: just relational expr} {expr 3>=2} 1 +test expr-8.2 {CompileEqualityExpr: just relational expr} {expr 2<=2.1} 1 +test expr-8.3 {CompileEqualityExpr: just relational expr} {expr 3.2>"2.2"} 1 +test expr-8.4 {CompileEqualityExpr: just relational expr} {expr {"0y"<"0x12"}} 0 +test expr-8.5 {CompileEqualityExpr: error in relational expr} { + catch {expr x>3} msg +} {1} +test expr-8.6 {CompileEqualityExpr: simple equality exprs} {expr 7==0x13} 0 +test expr-8.7 {CompileEqualityExpr: simple equality exprs} {expr -0xf2!=0x53} 1 +test expr-8.8 {CompileEqualityExpr: simple equality exprs} {expr {"12398712938788234-1298379" != ""}} 1 +test expr-8.9 {CompileEqualityExpr: simple equality exprs} {expr -1!="abc"} 1 +test expr-8.10 {CompileEqualityExpr: error compiling equality arm} { + catch {expr 2//3==6} msg +} {1} +test expr-8.11 {CompileEqualityExpr: error compiling equality arm} { + catch {expr 2!=x} msg +} {1} + + +test expr-9.1 {CompileRelationalExpr: just shift expr} {expr 3<<2} 12 +test expr-9.2 {CompileRelationalExpr: just shift expr} {expr 0xff>>2} 63 +test expr-9.3 {CompileRelationalExpr: just shift expr} {expr -1>>2} -1 +test expr-9.4 {CompileRelationalExpr: just shift expr} {expr {1<<3}} 8 + +# The following test is different for 32-bit versus 64-bit +# architectures because LONG_MIN is different + +if {0x80000000 > 0} { + test expr-9.5 {CompileRelationalExpr: shift expr producing LONG_MIN} { + expr {1<<63} + } -9223372036854775808 +} else { + test expr-9.5 {CompileRelationalExpr: shift expr producing LONG_MIN} { + expr {1<<31} + } -2147483648 +} +test expr-9.6 {CompileRelationalExpr: error in shift expr} { + catch {expr x>>3} msg +} {1} +test expr-9.7 {CompileRelationalExpr: simple relational exprs} {expr 0xff>=+0x3} 1 +test expr-9.8 {CompileRelationalExpr: simple relational exprs} {expr -0xf2<0x3} 1 +test expr-9.9 {CompileRelationalExpr: error compiling relational arm} { + catch {expr 2//3>6} msg +} {1} +test expr-9.10 {CompileRelationalExpr: error compiling relational arm} { + catch {expr 2<x} msg +} {1} + +test expr-10.1 {CompileShiftExpr: just add expr} {expr 4+-2} 2 +test expr-10.2 {CompileShiftExpr: just add expr} {expr 0xff-2} 253 +test expr-10.3 {CompileShiftExpr: just add expr} {expr -1--2} 1 +test expr-10.4 {CompileShiftExpr: just add expr} {expr 1-0123} -82 +test expr-10.5 {CompileShiftExpr: error in add expr} { + catch {expr x+3} msg +} {1} +test expr-10.6 {CompileShiftExpr: simple shift exprs} {expr 0xff>>0x3} 31 +test expr-10.7 {CompileShiftExpr: simple shift exprs} {expr -0xf2<<0x3} -1936 +test expr-10.8 {CompileShiftExpr: error compiling shift arm} { + catch {expr 2//3>>6} msg +} {1} +test expr-10.9 {CompileShiftExpr: error compiling shift arm} { + catch {expr 2<<x} msg +} {1} +test expr-10.10 {CompileShiftExpr: runtime error} { + list [catch {expr {24.0>>43}} msg] +} {1} +test expr-10.11 {CompileShiftExpr: runtime error} { + list [catch {expr {"a"<<"b"}} msg] +} {1} + +test expr-11.1 {CompileAddExpr: just multiply expr} {expr 4*-2} -8 +test expr-11.2 {CompileAddExpr: just multiply expr} {expr 0xff%2} 1 +test expr-11.3 {CompileAddExpr: just multiply expr} {expr -1/2} -1 +test expr-11.4 {CompileAddExpr: just multiply expr} {expr 7891%0123} 6 +test expr-11.5 {CompileAddExpr: error in multiply expr} { + catch {expr x*3} msg +} {1} +test expr-11.6 {CompileAddExpr: simple add exprs} {expr 0xff++0x3} 258 +test expr-11.7 {CompileAddExpr: simple add exprs} {expr -0xf2--0x3} -239 +test expr-11.8 {CompileAddExpr: error compiling add arm} { + catch {expr 2//3+6} msg +} {1} +test expr-11.9 {CompileAddExpr: error compiling add arm} { + catch {expr 2-x} msg +} {1} +test expr-11.10 {CompileAddExpr: runtime error} { + list [catch {expr {24.0+"xx"}} msg] +} {1} +test expr-11.11 {CompileAddExpr: runtime error} { + list [catch {expr {"a"-"b"}} msg] +} {1} +test expr-11.12 {CompileAddExpr: runtime error} { + list [catch {expr {3/0}} msg] +} {1} +test expr-11.13 {CompileAddExpr: divide by zero} { + expr {2.3/0.0} +} {Inf} +test expr-11.14 {CompileAddExpr: divide by zero} { + expr {-2.3/0.0} +} {-Inf} + +test expr-12.1 {CompileMultiplyExpr: just unary expr} {expr ~4} -5 +test expr-12.2 {CompileMultiplyExpr: just unary expr} {expr --5} 5 +test expr-12.3 {CompileMultiplyExpr: just unary expr} {expr !27} 0 +test expr-12.4 {CompileMultiplyExpr: just unary expr} {expr ~0xff00ff} -16711936 +test expr-12.5 {CompileMultiplyExpr: error in unary expr} { + catch {expr ~x} msg +} {1} +test expr-12.6 {CompileMultiplyExpr: simple multiply exprs} {expr 0xff*0x3} 765 +test expr-12.7 {CompileMultiplyExpr: simple multiply exprs} {expr -0xf2%-0x3} -2 +test expr-12.8 {CompileMultiplyExpr: error compiling multiply arm} { + catch {expr 2*3%%6} msg +} {1} +test expr-12.9 {CompileMultiplyExpr: error compiling multiply arm} { + catch {expr 2*x} msg +} {1} +test expr-12.10 {CompileMultiplyExpr: runtime error} { + list [catch {expr {24.0*"xx"}} msg] +} {1} +test expr-12.11 {CompileMultiplyExpr: runtime error} { + list [catch {expr {"a"/"b"}} msg] +} {1} + +test expr-13.1 {CompileUnaryExpr: unary exprs} {expr -0xff} -255 +test expr-13.2 {CompileUnaryExpr: unary exprs} {expr +000123} 83 +test expr-13.3 {CompileUnaryExpr: unary exprs} {expr +--++36} 36 +test expr-13.4 {CompileUnaryExpr: unary exprs} {expr !2} 0 +test expr-13.5 {CompileUnaryExpr: unary exprs} {expr +--+-62.0} -62.0 +test expr-13.6 {CompileUnaryExpr: unary exprs} {expr !0.0} 1 +test expr-13.7 {CompileUnaryExpr: unary exprs} {expr !0xef} 0 +test expr-13.8 {CompileUnaryExpr: error compiling unary expr} { + catch {expr ~x} msg +} {1} +test expr-13.9 {CompileUnaryExpr: error compiling unary expr} { + catch {expr !1.x} msg +} {1} +test expr-13.10 {CompileUnaryExpr: runtime error} { + list [catch {expr {~"xx"}} msg] +} {1} +test expr-13.11 {CompileUnaryExpr: runtime error} { + list [catch {expr ~4.0} msg] +} {1} +test expr-13.12 {CompileUnaryExpr: just primary expr} {expr 0x123} 291 +test expr-13.13 {CompileUnaryExpr: just primary expr} { + set a 27 + expr $a +} 27 +test expr-13.14 {CompileUnaryExpr: just primary expr} { + expr double(27) +} 27.0 +test expr-13.15 {CompileUnaryExpr: just primary expr} {expr "123"} 123 +test expr-13.16 {CompileUnaryExpr: error in primary expr} { + catch {expr [set]} msg +} {1} +test expr-14.1 {CompilePrimaryExpr: literal primary} {expr 1} 1 +test expr-14.2 {CompilePrimaryExpr: literal primary} {expr 123} 123 +test expr-14.3 {CompilePrimaryExpr: literal primary} {expr 0xff} 255 +test expr-14.4 {CompilePrimaryExpr: literal primary} {expr 00010} 8 +test expr-14.5 {CompilePrimaryExpr: literal primary} {expr 62.0} 62.0 +test expr-14.6 {CompilePrimaryExpr: literal primary} { + expr 3.1400000 +} 3.14 +test expr-14.7 {CompilePrimaryExpr: literal primary} {expr {{abcde}<{abcdef}}} 1 +test expr-14.8 {CompilePrimaryExpr: literal primary} {expr {{abc\ +def} < {abcdef}}} 1 +test expr-14.9 {CompilePrimaryExpr: literal primary} {expr {{abc\tde} > {abc\tdef}}} 0 +test expr-14.10 {CompilePrimaryExpr: literal primary} {expr {{123}}} 123 +test expr-14.11 {CompilePrimaryExpr: var reference primary} { + set i 789 + list [expr {$i}] [expr $i] +} {789 789} +test expr-14.12 {CompilePrimaryExpr: var reference primary} { + set i {789} ;# test expr's aggressive conversion to numeric semantics + list [expr {$i}] [expr $i] +} {789 789} +test expr-14.13 {CompilePrimaryExpr: var reference primary} { + catch {unset a} + set a(foo) foo + set a(bar) bar + set a(123) 123 + set result "" + lappend result [expr $a(123)] [expr {$a(bar)<$a(foo)}] + catch {unset a} + set result +} {123 1} +test expr-14.14 {CompilePrimaryExpr: var reference primary} { + set i 123 ;# test "$var.0" floating point conversion hack + list [expr $i] [expr $i.0] [expr $i.0/12.0] +} {123 123.0 10.25} +test expr-14.15 {CompilePrimaryExpr: var reference primary} { + set i 123 + catch {expr $i.2} msg + set msg +} 123.2 +test expr-14.16 {CompilePrimaryExpr: error compiling var reference primary} { + catch {expr {$a(foo}} msg +} {1} +test expr-14.18 {CompilePrimaryExpr: quoted string primary} { + expr "21" +} 21 +test expr-14.19 {CompilePrimaryExpr: quoted string primary} { + set i 123 + set x 456 + expr "$i+$x" +} 579 +test expr-14.20 {CompilePrimaryExpr: quoted string primary} { + set i 3 + set x 6 + expr 2+"$i.$x" +} 5.6 +test expr-14.21 {CompilePrimaryExpr: error in quoted string primary} { + catch {expr "[set]"} msg +} {1} +test expr-14.22 {CompilePrimaryExpr: subcommand primary} { + expr {[set i 123; set i]} +} 123 +test expr-14.23 {CompilePrimaryExpr: error in subcommand primary} { + catch {expr {[set]}} msg +} {1} +test expr-14.24 {CompilePrimaryExpr: error in subcommand primary} { + catch {expr {[set blah}} msg +} {1} +test expr-14.28 {CompilePrimaryExpr: subexpression primary} { + expr 2+(3*4) +} 14 +test expr-14.29 {CompilePrimaryExpr: error in subexpression primary} { + catch {expr 2+(3*[set])} msg +} {1} +test expr-14.30 {CompilePrimaryExpr: missing paren in subexpression primary} { + catch {expr 2+(3*(4+5)} msg +} {1} +test expr-14.31 {CompilePrimaryExpr: just var ref in subexpression primary} { + set i "5+10" + list "[expr $i] == 15" "[expr ($i)] == 15" "[eval expr ($i)] == 15" +} {{15 == 15} {15 == 15} {15 == 15}} +test expr-14.32 {CompilePrimaryExpr: unexpected token} { + catch {expr @} msg +} {1} + +test expr-15.2 {CompileMathFuncCall: unknown math function} { + catch {expr whazzathuh(1)} msg +} {1} + +test expr-16.1 {GetToken: checks whether integer token starting with "0x" (e.g., "0x$") is invalid} { + catch {unset a} + set a(VALUE) ff15 + set i 123 + if {[expr 0x$a(VALUE)] & 16} { + set i {} + } + set i +} {} +test expr-16.2 {GetToken: check for string literal in braces} { + expr {{1}} +} {1} + +# Check "expr" and computed command names. + +test expr-17.1 {expr and computed command names} { + set i 0 + set z expr + $z 1+2 +} 3 + +# Check correct conversion of operands to numbers: If the string looks like +# an integer, convert to integer. Otherwise, if the string looks like a +# double, convert to double. + +test expr-18.1 {expr and conversion of operands to numbers} { + set x [lindex 11 0] + catch {expr int($x)} + expr {$x} +} 11 +test expr-18.2 {whitespace strings should not be == 0 (buggy strtod)} { + expr {" "} +} { } + +# Check "expr" and interpreter result object resetting before appending +# an error msg during evaluation of exprs not in {}s + +test expr-19.1 {expr and interpreter result object resetting} { + proc p {} { + set t 10.0 + set x 2.0 + set dx 0.2 + set f {$dx-$x/10} + set g {-$x/5} + set center 1.0 + set x [expr $x-$center] + set dx [expr $dx+$g] + set x [expr $x+$f+$center] + set x [expr $x+$f+$center] + set y [expr round($x)] + } + p +} 3 + +catch {unset a} + +# Test for incorrect "double evaluation" semantics + +#XXX: Jim doesn't care about missing braces +#test expr-20.1 {wrong brace matching} { +# catch {unset l} +# catch {unset r} +# catch {unset q} +# catch {unset cmd} +# catch {unset a} +# set l "\{"; set r "\}"; set q "\"" +# set cmd "expr $l$q|$q == $q$r$q$r" +# catch $cmd a +#} {1} +test expr-20.3 {broken substitution of integer digits} { + # fails with 8.0.x, but not 8.1b2 + list [set a 000; expr 0x1$a] [set a 1; expr ${a}000] +} {4096 1000} +test expr-20.4 {proper double evaluation compilation, error case} { + catch {unset a}; # make sure $a doesn't exist + list [catch {expr 1?{$a}:0} msg] +} {1} +test expr-20.5 {proper double evaluation compilation, working case} { + set a yellow + expr 1?{$a}:0 +} yellow +test expr-20.6 {handling of compile error in trial compile} { + list [catch {expr + {[incr]}} msg] +} {1} +test expr-20.7 {handling of compile error in runtime case} { + list [catch {expr + {[error foo]}} msg] +} {1} + +# cleanup +if {[info exists a]} { + unset a +} + +testreport diff --git a/tests/expr-old.test b/tests/expr-old.test index 41c6139..d038144 100644 --- a/tests/expr-old.test +++ b/tests/expr-old.test @@ -432,8 +432,8 @@ test expr-old-26.9 {error conditions} { list [catch {expr 2%0} msg] } {1} test expr-old-26.10 {error conditions} { - list [catch {expr 2.0/0.0} msg] -} {1} + expr 2.0/0.0 +} {Inf} test expr-old-26.11 {error conditions} { list [catch {expr 2#} msg] } {1} @@ -814,11 +814,12 @@ test expr-old-36.10 {ExprLooksLikeInt procedure} { } # test for [Bug #542588] -test expr-old-36.11 {ExprLooksLikeInt procedure} { - # define a "too large integer"; this one works also for 64bit arith - set x 665802003400000000000000 - list [catch {expr {$x+1}} msg] $msg -} {1 {can't use integer value too large to represent as operand of "+"}} +# XXX: Can't rely on overflow checking +#test expr-old-36.11 {ExprLooksLikeInt procedure} { +# # define a "too large integer"; this one works also for 64bit arith +# set x 665802003400000000000000 +# list [catch {expr {$x+1}} msg] $msg +#} {1 {can't use integer value too large to represent as operand of "+"}} # Special test for Pentium arithmetic bug of 1994: diff --git a/tests/perf.test b/tests/perf.test index e792c96..83b13ae 100644 --- a/tests/perf.test +++ b/tests/perf.test @@ -27,6 +27,12 @@ proc set_var_dict_sugar {} { } } +proc set_var_dict {} { + set b b + for {set i 0} {$i < $::iterations} {incr i} { + dict set a $b $i + } +} proc read_file {file} { set f [open $file] @@ -111,14 +117,15 @@ close $f bench "set dictsugar" {set_dict_sugar} bench "set var dictsugar" {set_var_dict_sugar} +bench "set var dict" {set_var_dict} # Read once before testing perf read_file test.in bench "read file" {read_file test.in} bench "read file split" {read_file_split test.in} +bench "foreach: simple" {read_file_split_assign_foreach_simple test.in} bench "foreach: direct dictsugar" {read_file_split_assign_foreach test.in} bench "foreach: dict cmd" {read_file_split_assign_foreach_dict test.in} bench "foreach: assign to dictsugar" {read_file_split_assign_foreach_dictsugar test.in} -bench "foreach: simple" {read_file_split_assign_foreach_simple test.in} bench "foreach: assign to dictsugar via lindex" {read_file_split_assign_lindex test.in} file delete test.in |