Tcl Source Code

Changes On Branch tip-526
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch tip-526 Excluding Merge-Ins

This is equivalent to a diff from 6c90dbed50 to a0038b00c5

2022-10-14
20:55
Merge 8.7 check-in: 59a78c3597 user: jan.nijtmans tags: trunk, main
14:36
rebase to 9.0 Leaf check-in: a0038b00c5 user: jan.nijtmans tags: tip-526
14:34
merge trunk check-in: cb72aedc1b user: dgp tags: dgp-refactor
14:32
merge trunk check-in: 213e11b1f5 user: dgp tags: novem
13:46
Merge 9.0 Closed-Leaf check-in: 0d826c0652 user: jan.nijtmans tags: tip-637
13:37
Merge 8.7 check-in: 6c90dbed50 user: jan.nijtmans tags: trunk, main
13:36
Fix [7505fac5bd]: new iocmd.test failures check-in: ae98efef7f user: jan.nijtmans tags: core-8-branch
2022-10-13
21:12
Merge 8.7 check-in: ab240e1a47 user: jan.nijtmans tags: trunk, main
2021-01-28
12:11
Restore accidently overwritten testcases check-in: cbda4839f6 user: jan.nijtmans tags: tip-526

Changes to doc/expr.n.

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43






44
45
46
47
48
49
50
.TH expr n 8.5 Tcl "Tcl Built-In Commands"
.so man.macros
.BS
'\" Note:  do not modify the .SH NAME line immediately below!
.SH NAME
expr \- Evaluate an expression
.SH SYNOPSIS
\fBexpr \fIarg \fR?\fIarg arg ...\fR?
.BE
.SH DESCRIPTION
.PP
Concatenates \fIarg\fRs, separated by a space, into an expression, and evaluates
that expression, returning its value.
The operators permitted in an expression include a subset of
the operators permitted in C expressions.  For those operators
common to both Tcl and C, Tcl applies the same meaning and precedence
as the corresponding C operators.
The value of an expression is often a numeric result, either an integer or a
floating-point value, but may also be a non-numeric value.
For example, the expression
.PP
.CS
\fBexpr\fR 8.2 + 6
.CE
.PP
evaluates to 14.2.
Expressions differ from C expressions in the way that
operands are specified.  Expressions also support
non-numeric operands, string comparisons, and some
additional operators not found in C.
.PP
When the result of expression is an integer, it is in decimal form, and when
the result is a floating-point number, it is in the form produced by the
\fB%g\fR format specifier of \fBformat\fR.
.PP






.VS "TIP 582"
At any point in the expression except within double quotes or braces, \fB#\fR
is the beginning of a comment, which lasts to the end of the line or
the end of the expression, whichever comes first.
.VE "TIP 582"
.SS OPERANDS
.PP







|



<
|









|












>
>
>
>
>
>







9
10
11
12
13
14
15
16
17
18
19

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
.TH expr n 8.5 Tcl "Tcl Built-In Commands"
.so man.macros
.BS
'\" Note:  do not modify the .SH NAME line immediately below!
.SH NAME
expr \- Evaluate an expression
.SH SYNOPSIS
\fBexpr \fIexpression\fR
.BE
.SH DESCRIPTION
.PP

Evaluates the expression, \fIexpression\fR, returning its value.
The operators permitted in an expression include a subset of
the operators permitted in C expressions.  For those operators
common to both Tcl and C, Tcl applies the same meaning and precedence
as the corresponding C operators.
The value of an expression is often a numeric result, either an integer or a
floating-point value, but may also be a non-numeric value.
For example, the expression
.PP
.CS
\fBexpr\fR {8.2 + 6}
.CE
.PP
evaluates to 14.2.
Expressions differ from C expressions in the way that
operands are specified.  Expressions also support
non-numeric operands, string comparisons, and some
additional operators not found in C.
.PP
When the result of expression is an integer, it is in decimal form, and when
the result is a floating-point number, it is in the form produced by the
\fB%g\fR format specifier of \fBformat\fR.
.PP
.VS "9.0, TIP 526"
Unlike with prior versions of Tcl, from Tcl 9.0 onwards, the \fBexpr\fR
command only accepts a single argument instead of concatenating an arbitrary
number of arguments to form an expression.
See \fBCOMPATIBILITY WITH TCL 8.*\fR below for details.
.VE "9.0, TIP 526"
.VS "TIP 582"
At any point in the expression except within double quotes or braces, \fB#\fR
is the beginning of a comment, which lasts to the end of the line or
the end of the expression, whichever comes first.
.VE "TIP 582"
.SS OPERANDS
.PP
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
\fBexpr\fR {$v?[a]:[b]}
.CE
.PP
only one of \fB[a]\fR or \fB[b]\fR is evaluated,
depending on the value of \fB$v\fR.  This is not true of the normal Tcl parser,
so it is normally recommended to enclose the arguments to \fBexpr\fR in braces.
Without braces, as in
\fBexpr\fR $v ? [a] : [b]
both \fB[a]\fR and \fB[b]\fR are evaluated before \fBexpr\fR is even called.
.PP
For more details on the results
produced by each operator, see the documentation for C.
.SS "MATH FUNCTIONS"
.PP
A mathematical function such as \fBsin($x)\fR is replaced with a call to an ordinary







|







283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
\fBexpr\fR {$v?[a]:[b]}
.CE
.PP
only one of \fB[a]\fR or \fB[b]\fR is evaluated,
depending on the value of \fB$v\fR.  This is not true of the normal Tcl parser,
so it is normally recommended to enclose the arguments to \fBexpr\fR in braces.
Without braces, as in
\fBexpr\fR $v?[a]:[b]
both \fB[a]\fR and \fB[b]\fR are evaluated before \fBexpr\fR is even called.
.PP
For more details on the results
produced by each operator, see the documentation for C.
.SS "MATH FUNCTIONS"
.PP
A mathematical function such as \fBsin($x)\fR is replaced with a call to an ordinary
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
.CE
.PP
both return 1.25.
A floating-point result can be distinguished from an integer result by the
presence of either
.QW \fB.\fR
or
.QW \fBe\fR
.PP
. For example,
.PP
.CS
\fBexpr\fR {20.0/5.0}
.CE
.PP
returns \fB4.0\fR, not \fB4\fR.
.SH "PERFORMANCE CONSIDERATIONS"
.PP
Where an expression contains syntax that Tcl would otherwise perform
substitutions on, enclosing an expression in braces or otherwise quoting it
so that it's a static value allows the Tcl compiler to generate bytecode for
the expression, resulting in better speed and smaller storage requirements.
This also avoids issues that can arise if Tcl is allowed to perform
substitution on the value before \fBexpr\fR is called.
.PP
In the following example, the value of the expression is 11 because the Tcl parser first
substitutes \fB$b\fR and \fBexpr\fR then substitutes \fB$a\fR as part
of evaluating the expression
.QW "$a + 2*4" .
Enclosing the
expression in braces would result in a syntax error as \fB$b\fR does
not evaluate to a numeric value.
.PP
.CS
set a 3
set b {$a + 2}
\fBexpr\fR $b*4







|

|






|












|







369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
.CE
.PP
both return 1.25.
A floating-point result can be distinguished from an integer result by the
presence of either
.QW \fB.\fR
or
.QW \fBe\fR .
.PP
For example,
.PP
.CS
\fBexpr\fR {20.0/5.0}
.CE
.PP
returns \fB4.0\fR, not \fB4\fR.
.SH "PERFORMANCE AND COMPATIBILITY"
.PP
Where an expression contains syntax that Tcl would otherwise perform
substitutions on, enclosing an expression in braces or otherwise quoting it
so that it's a static value allows the Tcl compiler to generate bytecode for
the expression, resulting in better speed and smaller storage requirements.
This also avoids issues that can arise if Tcl is allowed to perform
substitution on the value before \fBexpr\fR is called.
.PP
In the following example, the value of the expression is 11 because the Tcl parser first
substitutes \fB$b\fR and \fBexpr\fR then substitutes \fB$a\fR as part
of evaluating the expression
.QW "$a + 2*4" .
Simply enclosing the
expression in braces would result in a syntax error as \fB$b\fR does
not evaluate to a numeric value.
.PP
.CS
set a 3
set b {$a + 2}
\fBexpr\fR $b*4
409
410
411
412
413
414
415
416
417

418
419
420
421
422




423










424
425
426



























427
428
429

430

431
432
433
434


435
436
437
438
439
440
441
Most expressions are not formed at runtime, but are literal strings or contain
substitutions that don't introduce other substitutions.  To allow the bytecode
compiler to work with an expression as a string literal at compilation time,
ensure that it contains no substitutions or that it is enclosed in braces or
otherwise quoted to prevent Tcl from performing substitutions, allowing
\fBexpr\fR to perform them instead.
.PP
If it is necessary to include a non-constant expression string within the
wider context of an otherwise-constant expression, the most efficient

technique is to put the varying part inside a recursive \fBexpr\fR, as this at
least allows for the compilation of the outer part, though it does mean that
the varying part must itself be evaluated as a separate expression. Thus, in
this example the result is 20 and the outer expression benefits from fully
cached bytecode compilation.




.PP










.CS
set a 3
set b {$a + 2}



























\fBexpr\fR {[\fBexpr\fR $b] * 4}
.CE
.PP

In general, you should enclose your expression in braces wherever possible,

and where not possible, the argument to \fBexpr\fR should be an expression
defined elsewhere as simply as possible. It is usually more efficient and
safer to use other techniques (e.g., the commands in the \fBtcl::mathop\fR
namespace) than it is to do complex expression generation.


.SH EXAMPLES
.PP
A numeric comparison whose result is 1:
.PP
.CS
\fBexpr\fR {"0x03" > "2"}
.CE







|
|
>
|
<
|
|
|
>
>
>
>

>
>
>
>
>
>
>
>
>
>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



>
|
>
|
<
<
|
>
>







414
415
416
417
418
419
420
421
422
423
424

425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479


480
481
482
483
484
485
486
487
488
489
Most expressions are not formed at runtime, but are literal strings or contain
substitutions that don't introduce other substitutions.  To allow the bytecode
compiler to work with an expression as a string literal at compilation time,
ensure that it contains no substitutions or that it is enclosed in braces or
otherwise quoted to prevent Tcl from performing substitutions, allowing
\fBexpr\fR to perform them instead.
.PP
In general, you should enclose your expression in braces wherever possible,
and where not possible, the argument to \fBexpr\fR should be an expression
defined elsewhere as simply as possible. It is usually more efficient and
safer to use other techniques (e.g., the commands in the \fBtcl::mathop\fR

namespace) than it is to do complex expression generation. Storing a
complex expression in a variable and using that with \fBexpr\fR is a
case which the bytecode compiler also supports well, provided that
expression is always evaluated in the same stack context (though the
expression could be conveyed across procedure calls and through
\fBuplevel\fR calls in between).
.SS "COMPATIBILITY WITH TCL 8.*"
.PP
.VS "9.0, TIP 526"
In Tcl 8 and older, the \fBexpr\fR command would concatenate multiple
arguments to form the expression; if this behavior is desired in Tcl 9.0
onwards, explicit use of \fBconcat\fR is required (this is also compatible
with earlier versions of Tcl) or a double-quoted argument could be used (as
whitespace is not generally important in expressions). Note that this style of
programming is often a source of bugs, including security bugs, so it is
\fInot recommended\fR. For example, instead of this version (supported in Tcl
8 and before):
.PP
.CS
set a 3
set b {$a + 2}
\fBexpr\fR $b * 4
.CE
.PP
you might write:
.PP
.CS
# ...
\fBexpr\fR [concat $b * 4]
.CE
.PP
or:
.PP
.CS
# ...
\fBexpr\fR "$b * 4"
.CE
.PP
Both of the above cases substitute \fB$b\fR prior to evaluating the
expression, and do the building of the expression dynamically making it
difficult to avoid compiling the expression on every call to \fBexpr\fR.
.PP
However, some use cases that \fIare\fR supported may be preferable, such as
embedding the use of \fBexpr\fR with a variable argument inside an otherwise
constant expression:
.PP
.CS
# ...
\fBexpr\fR {[\fBexpr\fR $b] * 4}
.CE
.PP
This changes what value is calculated (in this case, producing \fB20\fR rather
than \fB11\fR) because they force the inner expression to be evaluated
independently of the outer one; such changes may actually fix more bugs than
they cause! Note that this allows the outer expression to be fully compiled,


and enables compilation of the inner expression too (which can be cached in
some cases).
.VE "9.0, TIP 526"
.SH EXAMPLES
.PP
A numeric comparison whose result is 1:
.PP
.CS
\fBexpr\fR {"0x03" > "2"}
.CE

Changes to generic/tclCmdAH.c.

971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
    TCL_UNUSED(void *),
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    Tcl_Obj *resultPtr, *objPtr;

    if (objc < 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "arg ?arg ...?");
	return TCL_ERROR;
    }

    TclNewObj(resultPtr);
    Tcl_IncrRefCount(resultPtr);
    if (objc == 2) {
	objPtr = objv[1];
	TclNRAddCallback(interp, ExprCallback, resultPtr, NULL, NULL, NULL);
    } else {
	objPtr = Tcl_ConcatObj(objc-1, objv+1);
	TclNRAddCallback(interp, ExprCallback, resultPtr, objPtr, NULL, NULL);
    }

    return Tcl_NRExprObj(interp, objPtr, resultPtr);
}

static int
ExprCallback(
    void *data[],
    Tcl_Interp *interp,
    int result)
{
    Tcl_Obj *resultPtr = (Tcl_Obj *)data[0];
    Tcl_Obj *objPtr = (Tcl_Obj *)data[1];

    if (objPtr != NULL) {
	Tcl_DecrRefCount(objPtr);
    }

    if (result == TCL_OK) {
	Tcl_SetObjResult(interp, resultPtr);
    }
    Tcl_DecrRefCount(resultPtr);
    return result;
}







|
|





<
|
|
<
<
<
<











<
<
<
<
<







971
972
973
974
975
976
977
978
979
980
981
982
983
984

985
986




987
988
989
990
991
992
993
994
995
996
997





998
999
1000
1001
1002
1003
1004
    TCL_UNUSED(void *),
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    Tcl_Obj *resultPtr, *objPtr;

    if (objc != 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "expression");
	return TCL_ERROR;
    }

    TclNewObj(resultPtr);
    Tcl_IncrRefCount(resultPtr);

    objPtr = objv[1];
    TclNRAddCallback(interp, ExprCallback, resultPtr, NULL, NULL, NULL);





    return Tcl_NRExprObj(interp, objPtr, resultPtr);
}

static int
ExprCallback(
    void *data[],
    Tcl_Interp *interp,
    int result)
{
    Tcl_Obj *resultPtr = (Tcl_Obj *)data[0];






    if (result == TCL_OK) {
	Tcl_SetObjResult(interp, resultPtr);
    }
    Tcl_DecrRefCount(resultPtr);
    return result;
}

Changes to generic/tclCompCmds.c.

2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
    Tcl_Parse *parsePtr,	/* Points to a parse structure for the command
				 * created by Tcl_ParseCommand. */
    TCL_UNUSED(Command *),
    CompileEnv *envPtr)		/* Holds resulting instructions. */
{
    Tcl_Token *firstWordPtr;

    if (parsePtr->numWords == 1) {
	return TCL_ERROR;
    }

    /*
     * TIP #280: Use the per-word line information of the current command.
     */








|







2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
    Tcl_Parse *parsePtr,	/* Points to a parse structure for the command
				 * created by Tcl_ParseCommand. */
    TCL_UNUSED(Command *),
    CompileEnv *envPtr)		/* Holds resulting instructions. */
{
    Tcl_Token *firstWordPtr;

    if (parsePtr->numWords != 2) {
	return TCL_ERROR;
    }

    /*
     * TIP #280: Use the per-word line information of the current command.
     */

Changes to tests/compExpr-old.test.

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

# start of tests

catch {unset a b i x}

test compExpr-old-1.1 {TclCompileExprCmd: no expression} {
    list [catch {expr  } msg] $msg
} {1 {wrong # args: should be "expr arg ?arg ...?"}}
test compExpr-old-1.2 {TclCompileExprCmd: one expression word} {
    expr -25
} -25
test compExpr-old-1.3 {TclCompileExprCmd: two expression words} {
    expr -8.2   -6
} -14.2
test compExpr-old-1.4 {TclCompileExprCmd: five expression words} {
    expr 20 - 5 +10 -7
} 18
test compExpr-old-1.5 {TclCompileExprCmd: quoted expression word} {
    expr "0005"
} 5
test compExpr-old-1.6 {TclCompileExprCmd: quoted expression word} {
    catch {expr "0005"zxy} msg
    set msg
} {extra characters after close-quote}







|



<
<
<
<
<
|







130
131
132
133
134
135
136
137
138
139
140





141
142
143
144
145
146
147
148

# start of tests

catch {unset a b i x}

test compExpr-old-1.1 {TclCompileExprCmd: no expression} {
    list [catch {expr  } msg] $msg
} {1 {wrong # args: should be "expr expression"}}
test compExpr-old-1.2 {TclCompileExprCmd: one expression word} {
    expr -25
} -25





# 1.3 and 1.4 no longer apply
test compExpr-old-1.5 {TclCompileExprCmd: quoted expression word} {
    expr "0005"
} 5
test compExpr-old-1.6 {TclCompileExprCmd: quoted expression word} {
    catch {expr "0005"zxy} msg
    set msg
} {extra characters after close-quote}
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
test compExpr-old-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 compExpr-old-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 compExpr-old-2.1 {TclCompileExpr: are builtin functions registered?} {
    expr double(5*[llength "6 2"])
} 10.0
test compExpr-old-2.2 {TclCompileExpr: error in expr} -body {







|







172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
test compExpr-old-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 compExpr-old-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 compExpr-old-2.1 {TclCompileExpr: are builtin functions registered?} {
    expr double(5*[llength "6 2"])
} 10.0
test compExpr-old-2.2 {TclCompileExpr: error in expr} -body {

Changes to tests/expr-old.test.

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
	}
    }
}
testConstraint ieeeFloatingPoint [testIEEE]

# First, test all of the integer operators individually.

test expr-old-1.1 {integer operators} {expr -4} -4
test expr-old-1.2 {integer operators} {expr -(1+4)} -5
test expr-old-1.3 {integer operators} {expr ~3} -4
test expr-old-1.4 {integer operators} {expr !2} 0
test expr-old-1.5 {integer operators} {expr !0} 1
test expr-old-1.6 {integer operators} {expr 4*6} 24
test expr-old-1.7 {integer operators} {expr 36/12} 3
test expr-old-1.8 {integer operators} {expr 27/4} 6
test expr-old-1.9 {integer operators} {expr 27%4} 3
test expr-old-1.10 {integer operators} {expr 2+2} 4
test expr-old-1.11 {integer operators} {expr 2-6} -4
test expr-old-1.12 {integer operators} {expr 1<<3} 8
test expr-old-1.13 {integer operators} {expr 0xff>>2} 63
test expr-old-1.14 {integer operators} {expr -1>>2} -1
test expr-old-1.15 {integer operators} {expr 3>2} 1
test expr-old-1.16 {integer operators} {expr 2>2} 0
test expr-old-1.17 {integer operators} {expr 1>2} 0
test expr-old-1.18 {integer operators} {expr 3<2} 0
test expr-old-1.19 {integer operators} {expr 2<2} 0
test expr-old-1.20 {integer operators} {expr 1<2} 1
test expr-old-1.21 {integer operators} {expr 3>=2} 1
test expr-old-1.22 {integer operators} {expr 2>=2} 1
test expr-old-1.23 {integer operators} {expr 1>=2} 0
test expr-old-1.24 {integer operators} {expr 3<=2} 0
test expr-old-1.25 {integer operators} {expr 2<=2} 1
test expr-old-1.26 {integer operators} {expr 1<=2} 1
test expr-old-1.27 {integer operators} {expr 3==2} 0
test expr-old-1.28 {integer operators} {expr 2==2} 1
test expr-old-1.29 {integer operators} {expr 3!=2} 1
test expr-old-1.30 {integer operators} {expr 2!=2} 0
test expr-old-1.31 {integer operators} {expr 7&0x13} 3
test expr-old-1.32 {integer operators} {expr 7^0x13} 20
test expr-old-1.33 {integer operators} {expr 7|0x13} 23
test expr-old-1.34 {integer operators} {expr 0&&1} 0
test expr-old-1.35 {integer operators} {expr 0&&0} 0
test expr-old-1.36 {integer operators} {expr 1&&3} 1
test expr-old-1.37 {integer operators} {expr 0||1} 1
test expr-old-1.38 {integer operators} {expr 3||0} 1
test expr-old-1.39 {integer operators} {expr 0||0} 0
test expr-old-1.40 {integer operators} {expr 3>2?44:66} 44
test expr-old-1.41 {integer operators} {expr 2>3?44:66} 66
test expr-old-1.42 {integer operators} {expr 36/5} 7
test expr-old-1.43 {integer operators} {expr 36%5} 1
test expr-old-1.44 {integer operators} {expr -36/5} -8
test expr-old-1.45 {integer operators} {expr -36%5} 4
test expr-old-1.46 {integer operators} {expr 36/-5} -8
test expr-old-1.47 {integer operators} {expr 36%-5} -4
test expr-old-1.48 {integer operators} {expr -36/-5} 7
test expr-old-1.49 {integer operators} {expr -36%-5} -1
test expr-old-1.50 {integer operators} {expr +36} 36
test expr-old-1.51 {integer operators} {expr +--++36} 36
test expr-old-1.52 {integer operators} {expr +36%+5} 1
test expr-old-1.53 {integer operators} {
    unset -nocomplain x

    set x yes
    list [expr {1 && $x}] [expr {$x && 1}] \
         [expr {0 || $x}] [expr {$x || 0}]
} {1 1 1 1}

# Check the floating-point operators individually, along with
# automatic conversion to integers where needed.

test expr-old-2.1 {floating-point operators} {expr -4.2} -4.2
test expr-old-2.2 {floating-point operators} {expr -(1.125+4.25)} -5.375
test expr-old-2.3 {floating-point operators} {expr +5.7} 5.7
test expr-old-2.4 {floating-point operators} {expr +--+-62.0} -62.0
test expr-old-2.5 {floating-point operators} {expr !2.1} 0
test expr-old-2.6 {floating-point operators} {expr !0.0} 1
test expr-old-2.7 {floating-point operators} {expr 4.2*6.3} 26.46
test expr-old-2.8 {floating-point operators} {expr 36.0/12.0} 3.0
test expr-old-2.9 {floating-point operators} {expr 27/4.0} 6.75
test expr-old-2.10 {floating-point operators} {expr 2.3+2.1} 4.4
test expr-old-2.11 {floating-point operators} {expr 2.3-6.5} -4.2
test expr-old-2.12 {floating-point operators} {expr 3.1>2.1} 1
test expr-old-2.13 {floating-point operators} {expr {2.1 > 2.1}} 0
test expr-old-2.14 {floating-point operators} {expr 1.23>2.34e+1} 0
test expr-old-2.15 {floating-point operators} {expr 3.45<2.34} 0
test expr-old-2.16 {floating-point operators} {expr 0.002e3<--200e-2} 0
test expr-old-2.17 {floating-point operators} {expr 1.1<2.1} 1
test expr-old-2.18 {floating-point operators} {expr 3.1>=2.2} 1
test expr-old-2.19 {floating-point operators} {expr 2.345>=2.345} 1
test expr-old-2.20 {floating-point operators} {expr 1.1>=2.2} 0
test expr-old-2.21 {floating-point operators} {expr 3.0<=2.0} 0
test expr-old-2.22 {floating-point operators} {expr 2.2<=2.2} 1
test expr-old-2.23 {floating-point operators} {expr 2.2<=2.2001} 1
test expr-old-2.24 {floating-point operators} {expr 3.2==2.2} 0
test expr-old-2.25 {floating-point operators} {expr 2.2==2.2} 1
test expr-old-2.26 {floating-point operators} {expr 3.2!=2.2} 1
test expr-old-2.27 {floating-point operators} {expr 2.2!=2.2} 0
test expr-old-2.28 {floating-point operators} {expr 0.0&&0.0} 0
test expr-old-2.29 {floating-point operators} {expr 0.0&&1.3} 0
test expr-old-2.30 {floating-point operators} {expr 1.3&&0.0} 0
test expr-old-2.31 {floating-point operators} {expr 1.3&&3.3} 1
test expr-old-2.32 {floating-point operators} {expr 0.0||0.0} 0
test expr-old-2.33 {floating-point operators} {expr 0.0||1.3} 1
test expr-old-2.34 {floating-point operators} {expr 1.3||0.0} 1
test expr-old-2.35 {floating-point operators} {expr 3.3||0.0} 1
test expr-old-2.36 {floating-point operators} {expr 3.3>2.3?44.3:66.3} 44.3
test expr-old-2.37 {floating-point operators} {expr 2.3>3.3?44.3:66.3} 66.3
test expr-old-2.38 {floating-point operators} {
    list [catch {expr 028.1 + 09.2} msg] $msg
} {0 37.3}

# Operators that aren't legal on floating-point numbers

test expr-old-3.1 {illegal floating-point operations} {
    list [catch {expr ~4.0} msg] $msg
} {1 {can't use floating-point value "4.0" as operand of "~"}}
test expr-old-3.2 {illegal floating-point operations} {
    list [catch {expr 27%4.0} msg] $msg
} {1 {can't use floating-point value "4.0" as operand of "%"}}
test expr-old-3.3 {illegal floating-point operations} {
    list [catch {expr 27.0%4} msg] $msg
} {1 {can't use floating-point value "27.0" as operand of "%"}}
test expr-old-3.4 {illegal floating-point operations} {
    list [catch {expr 1.0<<3} msg] $msg
} {1 {can't use floating-point value "1.0" as operand of "<<"}}
test expr-old-3.5 {illegal floating-point operations} {
    list [catch {expr 3<<1.0} msg] $msg
} {1 {can't use floating-point value "1.0" as operand of "<<"}}
test expr-old-3.6 {illegal floating-point operations} {
    list [catch {expr 24.0>>3} msg] $msg
} {1 {can't use floating-point value "24.0" as operand of ">>"}}
test expr-old-3.7 {illegal floating-point operations} {
    list [catch {expr 24>>3.0} msg] $msg
} {1 {can't use floating-point value "3.0" as operand of ">>"}}
test expr-old-3.8 {illegal floating-point operations} {
    list [catch {expr 24&3.0} msg] $msg
} {1 {can't use floating-point value "3.0" as operand of "&"}}
test expr-old-3.9 {illegal floating-point operations} {
    list [catch {expr 24.0|3} msg] $msg
} {1 {can't use floating-point value "24.0" as operand of "|"}}
test expr-old-3.10 {illegal floating-point operations} {
    list [catch {expr 24.0^3} msg] $msg
} {1 {can't use floating-point value "24.0" as operand of "^"}}

# Check the string operators individually.

test expr-old-4.1 {string operators} {expr {"abc" > "def"}} 0
test expr-old-4.2 {string operators} {expr {"def" > "def"}} 0
test expr-old-4.3 {string operators} {expr {"g" > "def"}} 1
test expr-old-4.4 {string operators} {expr {"abc" < "abd"}} 1







|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

>



|




|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|




|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|







83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
	}
    }
}
testConstraint ieeeFloatingPoint [testIEEE]

# First, test all of the integer operators individually.

test expr-old-1.1 {integer operators} {expr {-4}} -4
test expr-old-1.2 {integer operators} {expr {-(1 + 4)}} -5
test expr-old-1.3 {integer operators} {expr {~3}} -4
test expr-old-1.4 {integer operators} {expr {!2}} 0
test expr-old-1.5 {integer operators} {expr {!0}} 1
test expr-old-1.6 {integer operators} {expr {4 * 6}} 24
test expr-old-1.7 {integer operators} {expr {36 / 12}} 3
test expr-old-1.8 {integer operators} {expr {27 / 4}} 6
test expr-old-1.9 {integer operators} {expr {27 % 4}} 3
test expr-old-1.10 {integer operators} {expr {2 + 2}} 4
test expr-old-1.11 {integer operators} {expr {2 - 6}} -4
test expr-old-1.12 {integer operators} {expr {1 << 3}} 8
test expr-old-1.13 {integer operators} {expr {0xff >> 2}} 63
test expr-old-1.14 {integer operators} {expr {-1 >> 2}} -1
test expr-old-1.15 {integer operators} {expr {3 > 2}} 1
test expr-old-1.16 {integer operators} {expr {2 > 2}} 0
test expr-old-1.17 {integer operators} {expr {1 > 2}} 0
test expr-old-1.18 {integer operators} {expr {3 < 2}} 0
test expr-old-1.19 {integer operators} {expr {2 < 2}} 0
test expr-old-1.20 {integer operators} {expr {1 < 2}} 1
test expr-old-1.21 {integer operators} {expr {3 >= 2}} 1
test expr-old-1.22 {integer operators} {expr {2 >= 2}} 1
test expr-old-1.23 {integer operators} {expr {1 >= 2}} 0
test expr-old-1.24 {integer operators} {expr {3 <= 2}} 0
test expr-old-1.25 {integer operators} {expr {2 <= 2}} 1
test expr-old-1.26 {integer operators} {expr {1 <= 2}} 1
test expr-old-1.27 {integer operators} {expr {3 == 2}} 0
test expr-old-1.28 {integer operators} {expr {2 == 2}} 1
test expr-old-1.29 {integer operators} {expr {3 != 2}} 1
test expr-old-1.30 {integer operators} {expr {2 != 2}} 0
test expr-old-1.31 {integer operators} {expr {7 & 0x13}} 3
test expr-old-1.32 {integer operators} {expr {7 ^ 0x13}} 20
test expr-old-1.33 {integer operators} {expr {7 | 0x13}} 23
test expr-old-1.34 {integer operators} {expr {0 && 1}} 0
test expr-old-1.35 {integer operators} {expr {0 && 0}} 0
test expr-old-1.36 {integer operators} {expr {1 && 3}} 1
test expr-old-1.37 {integer operators} {expr {0 || 1}} 1
test expr-old-1.38 {integer operators} {expr {3 || 0}} 1
test expr-old-1.39 {integer operators} {expr {0 || 0}} 0
test expr-old-1.40 {integer operators} {expr {3>2 ? 44 : 66}} 44
test expr-old-1.41 {integer operators} {expr {2>3 ? 44 : 66}} 66
test expr-old-1.42 {integer operators} {expr {36 / 5}} 7
test expr-old-1.43 {integer operators} {expr {36 % 5}} 1
test expr-old-1.44 {integer operators} {expr {-36 / 5}} -8
test expr-old-1.45 {integer operators} {expr {-36 % 5}} 4
test expr-old-1.46 {integer operators} {expr {36 / -5}} -8
test expr-old-1.47 {integer operators} {expr {36 % -5}} -4
test expr-old-1.48 {integer operators} {expr {-36 / -5}} 7
test expr-old-1.49 {integer operators} {expr {-36 % -5}} -1
test expr-old-1.50 {integer operators} {expr {+36}} 36
test expr-old-1.51 {integer operators} {expr {+--++36}} 36
test expr-old-1.52 {integer operators} {expr {+36%+5}} 1
test expr-old-1.53 {integer operators} -setup {
    unset -nocomplain x
} -body {
    set x yes
    list [expr {1 && $x}] [expr {$x && 1}] \
         [expr {0 || $x}] [expr {$x || 0}]
} -result {1 1 1 1}

# Check the floating-point operators individually, along with
# automatic conversion to integers where needed.

test expr-old-2.1 {floating-point operators} {expr {-4.2}} -4.2
test expr-old-2.2 {floating-point operators} {expr {-(1.125 + 4.25)}} -5.375
test expr-old-2.3 {floating-point operators} {expr {+5.7}} 5.7
test expr-old-2.4 {floating-point operators} {expr {+--+-62.0}} -62.0
test expr-old-2.5 {floating-point operators} {expr {!2.1}} 0
test expr-old-2.6 {floating-point operators} {expr {!0.0}} 1
test expr-old-2.7 {floating-point operators} {expr {4.2 * 6.3}} 26.46
test expr-old-2.8 {floating-point operators} {expr {36.0 / 12.0}} 3.0
test expr-old-2.9 {floating-point operators} {expr {27 / 4.0}} 6.75
test expr-old-2.10 {floating-point operators} {expr {2.3 + 2.1}} 4.4
test expr-old-2.11 {floating-point operators} {expr {2.3 - 6.5}} -4.2
test expr-old-2.12 {floating-point operators} {expr {3.1 > 2.1}} 1
test expr-old-2.13 {floating-point operators} {expr {2.1 > 2.1}} 0
test expr-old-2.14 {floating-point operators} {expr {1.23 > 2.34e+1}} 0
test expr-old-2.15 {floating-point operators} {expr {3.45 < 2.34}} 0
test expr-old-2.16 {floating-point operators} {expr {0.002e3<--200e-2}} 0
test expr-old-2.17 {floating-point operators} {expr {1.1 < 2.1}} 1
test expr-old-2.18 {floating-point operators} {expr {3.1 >= 2.2}} 1
test expr-old-2.19 {floating-point operators} {expr {2.345 >= 2.345}} 1
test expr-old-2.20 {floating-point operators} {expr {1.1 >= 2.2}} 0
test expr-old-2.21 {floating-point operators} {expr {3.0 <= 2.0}} 0
test expr-old-2.22 {floating-point operators} {expr {2.2 <= 2.2}} 1
test expr-old-2.23 {floating-point operators} {expr {2.2 <= 2.2001}} 1
test expr-old-2.24 {floating-point operators} {expr {3.2 == 2.2}} 0
test expr-old-2.25 {floating-point operators} {expr {2.2 == 2.2}} 1
test expr-old-2.26 {floating-point operators} {expr {3.2 != 2.2}} 1
test expr-old-2.27 {floating-point operators} {expr {2.2 != 2.2}} 0
test expr-old-2.28 {floating-point operators} {expr {0.0 && 0.0}} 0
test expr-old-2.29 {floating-point operators} {expr {0.0 && 1.3}} 0
test expr-old-2.30 {floating-point operators} {expr {1.3 && 0.0}} 0
test expr-old-2.31 {floating-point operators} {expr {1.3 && 3.3}} 1
test expr-old-2.32 {floating-point operators} {expr {0.0 || 0.0}} 0
test expr-old-2.33 {floating-point operators} {expr {0.0 || 1.3}} 1
test expr-old-2.34 {floating-point operators} {expr {1.3 || 0.0}} 1
test expr-old-2.35 {floating-point operators} {expr {3.3 || 0.0}} 1
test expr-old-2.36 {floating-point operators} {expr {3.3>2.3 ? 44.3 : 66.3}} 44.3
test expr-old-2.37 {floating-point operators} {expr {2.3>3.3 ? 44.3 : 66.3}} 66.3
test expr-old-2.38 {floating-point operators} {
    list [catch {expr {028.1 + 09.2}} msg] $msg
} {0 37.3}

# Operators that aren't legal on floating-point numbers

test expr-old-3.1 {illegal floating-point operations} -returnCodes error -body {
    expr {~4.0}
} -result {can't use floating-point value "4.0" as operand of "~"}
test expr-old-3.2 {illegal floating-point operations} -returnCodes error -body {
    expr {27 % 4.0}
} -result {can't use floating-point value "4.0" as operand of "%"}
test expr-old-3.3 {illegal floating-point operations} -returnCodes error -body {
    expr {27.0 % 4}
} -result {can't use floating-point value "27.0" as operand of "%"}
test expr-old-3.4 {illegal floating-point operations} -returnCodes error -body {
    expr {1.0 << 3}
} -result {can't use floating-point value "1.0" as operand of "<<"}
test expr-old-3.5 {illegal floating-point operations} -returnCodes error -body {
    expr {3 << 1.0}
} -result {can't use floating-point value "1.0" as operand of "<<"}
test expr-old-3.6 {illegal floating-point operations} -returnCodes error -body {
    expr {24.0 >> 3}
} -result {can't use floating-point value "24.0" as operand of ">>"}
test expr-old-3.7 {illegal floating-point operations} -returnCodes error -body {
    expr {24 >> 3.0}
} -result {can't use floating-point value "3.0" as operand of ">>"}
test expr-old-3.8 {illegal floating-point operations} -returnCodes error -body {
    expr {24 & 3.0}
} -result {can't use floating-point value "3.0" as operand of "&"}
test expr-old-3.9 {illegal floating-point operations} -returnCodes error -body {
    expr {24.0 | 3}
} -result {can't use floating-point value "24.0" as operand of "|"}
test expr-old-3.10 {illegal floating-point operations} -returnCodes error -body {
    expr {24.0 ^ 3}
} -result {can't use floating-point value "24.0" as operand of "^"}

# Check the string operators individually.

test expr-old-4.1 {string operators} {expr {"abc" > "def"}} 0
test expr-old-4.2 {string operators} {expr {"def" > "def"}} 0
test expr-old-4.3 {string operators} {expr {"g" > "def"}} 1
test expr-old-4.4 {string operators} {expr {"abc" < "abd"}} 1
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
test expr-old-4.29 {string operators} {expr {"0" == "+"}} 0
test expr-old-4.30 {string operators} {expr {"0" == "-"}} 0
test expr-old-4.31 {string operators} {expr {1?"foo":"bar"}} foo
test expr-old-4.32 {string operators} {expr {0?"foo":"bar"}} bar

# Operators that aren't legal on string operands.

test expr-old-5.1 {illegal string operations} {
    list [catch {expr {-"a"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "-"}}
test expr-old-5.2 {illegal string operations} {
    list [catch {expr {+"a"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "+"}}
test expr-old-5.3 {illegal string operations} {
    list [catch {expr {~"a"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "~"}}
test expr-old-5.4 {illegal string operations} {
    list [catch {expr {!"a"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "!"}}
test expr-old-5.5 {illegal string operations} {
    list [catch {expr {"a"*"b"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "*"}}
test expr-old-5.6 {illegal string operations} {
    list [catch {expr {"a"/"b"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "/"}}
test expr-old-5.7 {illegal string operations} {
    list [catch {expr {"a"%"b"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "%"}}
test expr-old-5.8 {illegal string operations} {
    list [catch {expr {"a"+"b"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "+"}}
test expr-old-5.9 {illegal string operations} {
    list [catch {expr {"a"-"b"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "-"}}
test expr-old-5.10 {illegal string operations} {
    list [catch {expr {"a"<<"b"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "<<"}}
test expr-old-5.11 {illegal string operations} {
    list [catch {expr {"a">>"b"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of ">>"}}
test expr-old-5.12 {illegal string operations} {
    list [catch {expr {"a"&"b"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "&"}}
test expr-old-5.13 {illegal string operations} {
    list [catch {expr {"a"^"b"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "^"}}
test expr-old-5.14 {illegal string operations} {
    list [catch {expr {"a"|"b"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "|"}}
test expr-old-5.15 {illegal string operations} {
    list [catch {expr {"a"&&"b"}} msg] $msg
} {1 {expected boolean value but got "a"}}
test expr-old-5.16 {illegal string operations} {
    list [catch {expr {"a"||"b"}} msg] $msg
} {1 {expected boolean value but got "a"}}
test expr-old-5.17 {illegal string operations} {
    list [catch {expr {"a"?4:2}} msg] $msg
} {1 {expected boolean value but got "a"}}

# Check precedence pairwise.

test expr-old-6.1 {precedence checks} {expr -~3} 4
test expr-old-6.2 {precedence checks} {expr -!3} 0
test expr-old-6.3 {precedence checks} {expr -~0} 1

test expr-old-7.1 {precedence checks} {expr 2*4/6} 1
test expr-old-7.2 {precedence checks} {expr 24/6*3} 12
test expr-old-7.3 {precedence checks} {expr 24/6/2} 2

test expr-old-8.1 {precedence checks} {expr -2+4} 2
test expr-old-8.2 {precedence checks} {expr -2-4} -6
test expr-old-8.3 {precedence checks} {expr +2-4} -2

test expr-old-9.1 {precedence checks} {expr 2*3+4} 10
test expr-old-9.2 {precedence checks} {expr 8/2+4} 8
test expr-old-9.3 {precedence checks} {expr 8%3+4} 6
test expr-old-9.4 {precedence checks} {expr 2*3-1} 5
test expr-old-9.5 {precedence checks} {expr 8/2-1} 3
test expr-old-9.6 {precedence checks} {expr 8%3-1} 1

test expr-old-10.1 {precedence checks} {expr 6-3-2} 1

test expr-old-11.1 {precedence checks} {expr 7+1>>2} 2
test expr-old-11.2 {precedence checks} {expr 7+1<<2} 32
test expr-old-11.3 {precedence checks} {expr 7>>3-2} 3
test expr-old-11.4 {precedence checks} {expr 7<<3-2} 14

test expr-old-12.1 {precedence checks} {expr 6>>1>4} 0
test expr-old-12.2 {precedence checks} {expr 6>>1<2} 0
test expr-old-12.3 {precedence checks} {expr 6>>1>=3} 1
test expr-old-12.4 {precedence checks} {expr 6>>1<=2} 0
test expr-old-12.5 {precedence checks} {expr 6<<1>5} 1
test expr-old-12.6 {precedence checks} {expr 6<<1<5} 0
test expr-old-12.7 {precedence checks} {expr 5<=6<<1} 1
test expr-old-12.8 {precedence checks} {expr 5>=6<<1} 0

test expr-old-13.1 {precedence checks} {expr 2<3<4} 1
test expr-old-13.2 {precedence checks} {expr 0<4>2} 0
test expr-old-13.3 {precedence checks} {expr 4>2<1} 0
test expr-old-13.4 {precedence checks} {expr 4>3>2} 0
test expr-old-13.5 {precedence checks} {expr 4>3>=2} 0
test expr-old-13.6 {precedence checks} {expr 4>=3>2} 0
test expr-old-13.7 {precedence checks} {expr 4>=3>=2} 0
test expr-old-13.8 {precedence checks} {expr 0<=4>=2} 0
test expr-old-13.9 {precedence checks} {expr 4>=2<=0} 0
test expr-old-13.10 {precedence checks} {expr 2<=3<=4} 1

test expr-old-14.1 {precedence checks} {expr 1==4>3} 1
test expr-old-14.2 {precedence checks} {expr 0!=4>3} 1
test expr-old-14.3 {precedence checks} {expr 1==3<4} 1
test expr-old-14.4 {precedence checks} {expr 0!=3<4} 1
test expr-old-14.5 {precedence checks} {expr 1==4>=3} 1
test expr-old-14.6 {precedence checks} {expr 0!=4>=3} 1
test expr-old-14.7 {precedence checks} {expr 1==3<=4} 1
test expr-old-14.8 {precedence checks} {expr 0!=3<=4} 1
test expr-old-14.9 {precedence checks} {expr 1eq4>3} 1
test expr-old-14.10 {precedence checks} {expr 0ne4>3} 1
test expr-old-14.11 {precedence checks} {expr 1eq3<4} 1
test expr-old-14.12 {precedence checks} {expr 0ne3<4} 1
test expr-old-14.13 {precedence checks} {expr 1eq4>=3} 1
test expr-old-14.14 {precedence checks} {expr 0ne4>=3} 1
test expr-old-14.15 {precedence checks} {expr 1eq3<=4} 1
test expr-old-14.16 {precedence checks} {expr 0ne3<=4} 1

test expr-old-15.1 {precedence checks} {expr 1==3==3} 0
test expr-old-15.2 {precedence checks} {expr 3==3!=2} 1
test expr-old-15.3 {precedence checks} {expr 2!=3==3} 0
test expr-old-15.4 {precedence checks} {expr 2!=1!=1} 0
test expr-old-15.5 {precedence checks} {expr 1eq3eq3} 0
test expr-old-15.6 {precedence checks} {expr 3eq3ne2} 1
test expr-old-15.7 {precedence checks} {expr 2ne3eq3} 0
test expr-old-15.8 {precedence checks} {expr 2ne1ne1} 0

test expr-old-16.1 {precedence checks} {expr 2&3eq2} 0
test expr-old-16.2 {precedence checks} {expr 1&3ne3} 0
test expr-old-16.3 {precedence checks} {expr 2&3eq2} 0
test expr-old-16.4 {precedence checks} {expr 1&3ne3} 0

test expr-old-17.1 {precedence checks} {expr 7&3^0x10} 19
test expr-old-17.2 {precedence checks} {expr 7^0x10&3} 7

test expr-old-18.1 {precedence checks} {expr 7^0x10|3} 23
test expr-old-18.2 {precedence checks} {expr 7|0x10^3} 23

test expr-old-19.1 {precedence checks} {expr 7|3&&1} 1
test expr-old-19.2 {precedence checks} {expr 1&&3|7} 1
test expr-old-19.3 {precedence checks} {expr 0&&1||1} 1
test expr-old-19.4 {precedence checks} {expr 1||1&&0} 1

test expr-old-20.1 {precedence checks} {expr 1||0?3:4} 3
test expr-old-20.2 {precedence checks} {expr 1?0:4||1} 0
test expr-old-20.3 {precedence checks} {expr 1?2:0?3:4} 2
test expr-old-20.4 {precedence checks} {expr 0?2:0?3:4} 4
test expr-old-20.5 {precedence checks} {expr 1?2?3:4:0} 3
test expr-old-20.6 {precedence checks} {expr 0?2?3:4:0} 0

# Parentheses.

test expr-old-21.1 {parenthesization} {expr (2+4)*6} 36
test expr-old-21.2 {parenthesization} {expr (1?0:4)||1} 1
test expr-old-21.3 {parenthesization} {expr +(3-4)} -1

# Embedded commands and variable names.

set a 16
test expr-old-22.1 {embedded variables} {expr {2*$a}} 32
test expr-old-22.2 {embedded variables} {
    set x -5
    set y 10
    expr {$x + $y}
} {5}
test expr-old-22.3 {embedded variables} {
    set x "  -5"
    set y "  +10"
    expr {$x + $y}
} {5}
test expr-old-22.4 {embedded commands and variables} {expr {[set a] - 14}} 2
test expr-old-22.5 {embedded commands and variables} {
    list [catch {expr {12 - [bad_command_name]}} msg] $msg
} {1 {invalid command name "bad_command_name"}}

# Double-quotes and things inside them.

test expr-old-23.1 {double quotes} {expr {"abc"}} abc
test expr-old-23.2 {double quotes} {
    set a 189
    expr {"$a.bc"}







|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|



|
|
|

|
|
|

|
|
|

|
|
|
|
|
|

|

|
|
|
|

|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|

|
|
|
|

|
|

|
|

|
|
|
|

|
|
|
|
|
|



|
|
|
















|
|
|







257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
test expr-old-4.29 {string operators} {expr {"0" == "+"}} 0
test expr-old-4.30 {string operators} {expr {"0" == "-"}} 0
test expr-old-4.31 {string operators} {expr {1?"foo":"bar"}} foo
test expr-old-4.32 {string operators} {expr {0?"foo":"bar"}} bar

# Operators that aren't legal on string operands.

test expr-old-5.1 {illegal string operations} -returnCodes error -body {
    expr {-"a"}
} -result {can't use non-numeric string "a" as operand of "-"}
test expr-old-5.2 {illegal string operations} -returnCodes error -body {
    expr {+"a"}
} -result {can't use non-numeric string "a" as operand of "+"}
test expr-old-5.3 {illegal string operations} -returnCodes error -body {
    expr {~"a"}
} -result {can't use non-numeric string "a" as operand of "~"}
test expr-old-5.4 {illegal string operations} -returnCodes error -body {
    expr {!"a"}
} -result {can't use non-numeric string "a" as operand of "!"}
test expr-old-5.5 {illegal string operations} -returnCodes error -body {
    expr {"a" * "b"}
} -result {can't use non-numeric string "a" as operand of "*"}
test expr-old-5.6 {illegal string operations} -returnCodes error -body {
    expr {"a" / "b"}
} -result {can't use non-numeric string "a" as operand of "/"}
test expr-old-5.7 {illegal string operations} -returnCodes error -body {
    expr {"a" % "b"}
} -result {can't use non-numeric string "a" as operand of "%"}
test expr-old-5.8 {illegal string operations} -returnCodes error -body {
    expr {"a" + "b"}
} -result {can't use non-numeric string "a" as operand of "+"}
test expr-old-5.9 {illegal string operations} -returnCodes error -body {
    expr {"a" - "b"}
} -result {can't use non-numeric string "a" as operand of "-"}
test expr-old-5.10 {illegal string operations} -returnCodes error -body {
    expr {"a" << "b"}
} -result {can't use non-numeric string "a" as operand of "<<"}
test expr-old-5.11 {illegal string operations} -returnCodes error -body {
    expr {"a" >> "b"}
} -result {can't use non-numeric string "a" as operand of ">>"}
test expr-old-5.12 {illegal string operations} -returnCodes error -body {
    expr {"a" & "b"}
} -result {can't use non-numeric string "a" as operand of "&"}
test expr-old-5.13 {illegal string operations} -returnCodes error -body {
    expr {"a" ^ "b"}
} -result {can't use non-numeric string "a" as operand of "^"}
test expr-old-5.14 {illegal string operations} -returnCodes error -body {
    expr {"a" | "b"}
} -result {can't use non-numeric string "a" as operand of "|"}
test expr-old-5.15 {illegal string operations} -returnCodes error -body {
    expr {"a" && "b"}
} -result {expected boolean value but got "a"}
test expr-old-5.16 {illegal string operations} -returnCodes error -body {
    expr {"a" || "b"}
} -result {expected boolean value but got "a"}
test expr-old-5.17 {illegal string operations} -returnCodes error -body {
    expr {"a" ? 4 : 2}
} -result {expected boolean value but got "a"}

# Check precedence pairwise.

test expr-old-6.1 {precedence checks} {expr {-~3}} 4
test expr-old-6.2 {precedence checks} {expr {-!3}} 0
test expr-old-6.3 {precedence checks} {expr {-~0}} 1

test expr-old-7.1 {precedence checks} {expr {2 * 4 / 6}} 1
test expr-old-7.2 {precedence checks} {expr {24 / 6 * 3}} 12
test expr-old-7.3 {precedence checks} {expr {24 / 6 / 2}} 2

test expr-old-8.1 {precedence checks} {expr {-2+4}} 2
test expr-old-8.2 {precedence checks} {expr {-2-4}} -6
test expr-old-8.3 {precedence checks} {expr {+2-4}} -2

test expr-old-9.1 {precedence checks} {expr {2*3+4}} 10
test expr-old-9.2 {precedence checks} {expr {8/2+4}} 8
test expr-old-9.3 {precedence checks} {expr {8%3+4}} 6
test expr-old-9.4 {precedence checks} {expr {2*3-1}} 5
test expr-old-9.5 {precedence checks} {expr {8/2-1}} 3
test expr-old-9.6 {precedence checks} {expr {8%3-1}} 1

test expr-old-10.1 {precedence checks} {expr {6-3-2}} 1

test expr-old-11.1 {precedence checks} {expr {7+1>>2}} 2
test expr-old-11.2 {precedence checks} {expr {7+1<<2}} 32
test expr-old-11.3 {precedence checks} {expr {7>>3-2}} 3
test expr-old-11.4 {precedence checks} {expr {7<<3-2}} 14

test expr-old-12.1 {precedence checks} {expr {6>>1>4}} 0
test expr-old-12.2 {precedence checks} {expr {6>>1<2}} 0
test expr-old-12.3 {precedence checks} {expr {6>>1>=3}} 1
test expr-old-12.4 {precedence checks} {expr {6>>1<=2}} 0
test expr-old-12.5 {precedence checks} {expr {6<<1>5}} 1
test expr-old-12.6 {precedence checks} {expr {6<<1<5}} 0
test expr-old-12.7 {precedence checks} {expr {5<=6<<1}} 1
test expr-old-12.8 {precedence checks} {expr {5>=6<<1}} 0

test expr-old-13.1 {precedence checks} {expr {2<3<4}} 1
test expr-old-13.2 {precedence checks} {expr {0<4>2}} 0
test expr-old-13.3 {precedence checks} {expr {4>2<1}} 0
test expr-old-13.4 {precedence checks} {expr {4>3>2}} 0
test expr-old-13.5 {precedence checks} {expr {4>3>=2}} 0
test expr-old-13.6 {precedence checks} {expr {4>=3>2}} 0
test expr-old-13.7 {precedence checks} {expr {4>=3>=2}} 0
test expr-old-13.8 {precedence checks} {expr {0<=4>=2}} 0
test expr-old-13.9 {precedence checks} {expr {4>=2<=0}} 0
test expr-old-13.10 {precedence checks} {expr {2<=3<=4}} 1

test expr-old-14.1 {precedence checks} {expr {1==4>3}} 1
test expr-old-14.2 {precedence checks} {expr {0!=4>3}} 1
test expr-old-14.3 {precedence checks} {expr {1==3<4}} 1
test expr-old-14.4 {precedence checks} {expr {0!=3<4}} 1
test expr-old-14.5 {precedence checks} {expr {1==4>=3}} 1
test expr-old-14.6 {precedence checks} {expr {0!=4>=3}} 1
test expr-old-14.7 {precedence checks} {expr {1==3<=4}} 1
test expr-old-14.8 {precedence checks} {expr {0!=3<=4}} 1
test expr-old-14.9 {precedence checks} {expr {1eq4>3}} 1
test expr-old-14.10 {precedence checks} {expr {0ne4>3}} 1
test expr-old-14.11 {precedence checks} {expr {1eq3<4}} 1
test expr-old-14.12 {precedence checks} {expr {0ne3<4}} 1
test expr-old-14.13 {precedence checks} {expr {1eq4>=3}} 1
test expr-old-14.14 {precedence checks} {expr {0ne4>=3}} 1
test expr-old-14.15 {precedence checks} {expr {1eq3<=4}} 1
test expr-old-14.16 {precedence checks} {expr {0ne3<=4}} 1

test expr-old-15.1 {precedence checks} {expr {1==3==3}} 0
test expr-old-15.2 {precedence checks} {expr {3==3!=2}} 1
test expr-old-15.3 {precedence checks} {expr {2!=3==3}} 0
test expr-old-15.4 {precedence checks} {expr {2!=1!=1}} 0
test expr-old-15.5 {precedence checks} {expr {1eq3eq3}} 0
test expr-old-15.6 {precedence checks} {expr {3eq3ne2}} 1
test expr-old-15.7 {precedence checks} {expr {2ne3eq3}} 0
test expr-old-15.8 {precedence checks} {expr {2ne1ne1}} 0

test expr-old-16.1 {precedence checks} {expr {2&3eq2}} 0
test expr-old-16.2 {precedence checks} {expr {1&3ne3}} 0
test expr-old-16.3 {precedence checks} {expr {2&3eq2}} 0
test expr-old-16.4 {precedence checks} {expr {1&3ne3}} 0

test expr-old-17.1 {precedence checks} {expr {7&3^0x10}} 19
test expr-old-17.2 {precedence checks} {expr {7^0x10&3}} 7

test expr-old-18.1 {precedence checks} {expr {7^0x10|3}} 23
test expr-old-18.2 {precedence checks} {expr {7|0x10^3}} 23

test expr-old-19.1 {precedence checks} {expr {7|3&&1}} 1
test expr-old-19.2 {precedence checks} {expr {1&&3|7}} 1
test expr-old-19.3 {precedence checks} {expr {0&&1||1}} 1
test expr-old-19.4 {precedence checks} {expr {1||1&&0}} 1

test expr-old-20.1 {precedence checks} {expr {1||0?3:4}} 3
test expr-old-20.2 {precedence checks} {expr {1?0:4||1}} 0
test expr-old-20.3 {precedence checks} {expr {1?2:0?3:4}} 2
test expr-old-20.4 {precedence checks} {expr {0?2:0?3:4}} 4
test expr-old-20.5 {precedence checks} {expr {1?2?3:4:0}} 3
test expr-old-20.6 {precedence checks} {expr {0?2?3:4:0}} 0

# Parentheses.

test expr-old-21.1 {parenthesization} {expr {(2+4)*6}} 36
test expr-old-21.2 {parenthesization} {expr {(1?0:4)||1}} 1
test expr-old-21.3 {parenthesization} {expr {+(3-4)}} -1

# Embedded commands and variable names.

set a 16
test expr-old-22.1 {embedded variables} {expr {2*$a}} 32
test expr-old-22.2 {embedded variables} {
    set x -5
    set y 10
    expr {$x + $y}
} {5}
test expr-old-22.3 {embedded variables} {
    set x "  -5"
    set y "  +10"
    expr {$x + $y}
} {5}
test expr-old-22.4 {embedded commands and variables} {expr {[set a] - 14}} 2
test expr-old-22.5 {embedded commands and variables} -returnCodes error -body {
    expr {12 - [bad_command_name]}
} -result {invalid command name "bad_command_name"}

# Double-quotes and things inside them.

test expr-old-23.1 {double quotes} {expr {"abc"}} abc
test expr-old-23.2 {double quotes} {
    set a 189
    expr {"$a.bc"}
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553



554
555
556
557
558
559
560
561
562
} {1 Testing}
test expr-old-23.8 {double quotes} {
    list [catch {expr {"12398712938788234-1298379" != ""}} msg] $msg
} {0 1}

# Numbers in various bases.

test expr-old-24.1 {numbers in different bases} {expr 0x20} 32
test expr-old-24.2 {numbers in different bases} {expr 0o15} 13

# Conversions between various data types.

test expr-old-25.1 {type conversions} {expr 2+2.5} 4.5
test expr-old-25.2 {type conversions} {expr 2.5+2} 4.5
test expr-old-25.3 {type conversions} {expr 2-2.5} -0.5
test expr-old-25.4 {type conversions} {expr 2/2.5} 0.8
test expr-old-25.5 {type conversions} {expr 2>2.5} 0
test expr-old-25.6 {type conversions} {expr 2.5>2} 1
test expr-old-25.7 {type conversions} {expr 2<2.5} 1
test expr-old-25.8 {type conversions} {expr 2>=2.5} 0
test expr-old-25.9 {type conversions} {expr 2<=2.5} 1
test expr-old-25.10 {type conversions} {expr 2==2.5} 0
test expr-old-25.11 {type conversions} {expr 2!=2.5} 1
test expr-old-25.12 {type conversions} {expr 2>"ab"} 0
test expr-old-25.13 {type conversions} {expr {2>" "}} 1
test expr-old-25.14 {type conversions} {expr {"24.1a" > 24.1}} 1
test expr-old-25.15 {type conversions} {expr {24.1 > "24.1a"}} 0
test expr-old-25.16 {type conversions} {expr 2+2.5} 4.5
test expr-old-25.17 {type conversions} {expr 2+2.5} 4.5
test expr-old-25.18 {type conversions} {expr 2.0e2} 200.0
test expr-old-25.19 {type conversions} {expr 2.0e15} 2000000000000000.0
test expr-old-25.20 {type conversions} {expr 10.0} 10.0

# Various error conditions.

test expr-old-26.1 {error conditions} {
    list [catch {expr 2+"a"} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "+"}}
test expr-old-26.2 {error conditions} -body {
    expr 2+4*
} -returnCodes error -match glob -result *
test expr-old-26.3 {error conditions} -body {
    expr 2+4*(
} -returnCodes error -match glob -result *
unset -nocomplain _non_existent_
test expr-old-26.4 {error conditions} {
    list [catch {expr 2+$_non_existent_} msg] $msg
} {1 {can't read "_non_existent_": no such variable}}
set a xx
test expr-old-26.5 {error conditions} {
    list [catch {expr {2+$a}} msg] $msg
} {1 {can't use non-numeric string "xx" as operand of "+"}}
test expr-old-26.6 {error conditions} {
    list [catch {expr {2+[set a]}} msg] $msg
} {1 {can't use non-numeric string "xx" as operand of "+"}}
test expr-old-26.7 {error conditions} -body {
    expr {2+(4}
} -returnCodes error -match glob -result *
test expr-old-26.8 {error conditions} {
    list [catch {expr 2/0} msg] $msg $errorCode
} {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
test expr-old-26.9 {error conditions} {
    list [catch {expr 2%0} msg] $msg $errorCode
} {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
test expr-old-26.10a {error conditions} !ieeeFloatingPoint {
    list [catch {expr 2.0/0.0} msg] $msg $errorCode
} {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
test expr-old-26.10b {error conditions} ieeeFloatingPoint {
    list [catch {expr 2.0/0.0} msg] $msg
} {0 Inf}
test expr-old-26.11 {error conditions} -body {
    expr 2`
} -returnCodes error -match glob -result *
test expr-old-26.12 {error conditions} -body {
    expr a.b
} -returnCodes error -match glob -result *
test expr-old-26.13 {error conditions} {
    list [catch {expr {"a"/"b"}} msg] $msg
} {1 {can't use non-numeric string "a" as operand of "/"}}
test expr-old-26.14 {error conditions} -body {
    expr 2:3
} -returnCodes error -match glob -result *
test expr-old-26.15 {error conditions} -body {
    expr a@b
} -returnCodes error -match glob -result *
test expr-old-26.16 {error conditions} {
    list [catch {expr a[b} msg] $msg
} {1 {missing close-bracket}}
test expr-old-26.17 {error conditions} -body {
    expr a`b
} -returnCodes error -match glob -result *
test expr-old-26.18 {error conditions} -body {
    expr \"a\"\{b
} -returnCodes error -match glob -result *
test expr-old-26.19 {error conditions} -body {
    expr a
} -returnCodes error -match glob -result *
test expr-old-26.20 {error conditions} {



    list [catch expr msg] $msg
} {1 {wrong # args: should be "expr arg ?arg ...?"}}

# Cancelled evaluation.

test expr-old-27.1 {cancelled evaluation} {
    set a 1
    expr {0&&[set a 2]}
    set a







|
|



|
|
|
|
|
|
|
|
|
|
|
|



|
|
|
|
|



|
|
|

|


|


|
|
|

|
|
|
|
|
|




|


|


|


|





|

|
|
|

|


|

|
|
|

|







|
>
>
>
|
|







457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
} {1 Testing}
test expr-old-23.8 {double quotes} {
    list [catch {expr {"12398712938788234-1298379" != ""}} msg] $msg
} {0 1}

# Numbers in various bases.

test expr-old-24.1 {numbers in different bases} {expr {0x20}} 32
test expr-old-24.2 {numbers in different bases} {expr {0o15}} 13

# Conversions between various data types.

test expr-old-25.1 {type conversions} {expr {2+2.5}} 4.5
test expr-old-25.2 {type conversions} {expr {2.5+2}} 4.5
test expr-old-25.3 {type conversions} {expr {2-2.5}} -0.5
test expr-old-25.4 {type conversions} {expr {2/2.5}} 0.8
test expr-old-25.5 {type conversions} {expr {2>2.5}} 0
test expr-old-25.6 {type conversions} {expr {2.5>2}} 1
test expr-old-25.7 {type conversions} {expr {2<2.5}} 1
test expr-old-25.8 {type conversions} {expr {2>=2.5}} 0
test expr-old-25.9 {type conversions} {expr {2<=2.5}} 1
test expr-old-25.10 {type conversions} {expr {2==2.5}} 0
test expr-old-25.11 {type conversions} {expr {2!=2.5}} 1
test expr-old-25.12 {type conversions} {expr {2>"ab"}} 0
test expr-old-25.13 {type conversions} {expr {2>" "}} 1
test expr-old-25.14 {type conversions} {expr {"24.1a" > 24.1}} 1
test expr-old-25.15 {type conversions} {expr {24.1 > "24.1a"}} 0
test expr-old-25.16 {type conversions} {expr {2+2.5}} 4.5
test expr-old-25.17 {type conversions} {expr {2+2.5}} 4.5
test expr-old-25.18 {type conversions} {expr {2.0e2}} 200.0
test expr-old-25.19 {type conversions} {expr {2.0e15}} 2000000000000000.0
test expr-old-25.20 {type conversions} {expr {10.0}} 10.0

# Various error conditions.

test expr-old-26.1 {error conditions} -returnCodes error -body {
    expr {2 + "a"}
} -result {can't use non-numeric string "a" as operand of "+"}
test expr-old-26.2 {error conditions} -body {
    expr {2 + 4 *}
} -returnCodes error -match glob -result *
test expr-old-26.3 {error conditions} -body {
    expr {2 + 4 * (}
} -returnCodes error -match glob -result *
unset -nocomplain _non_existent_
test expr-old-26.4 {error conditions} -returnCodes error -body {
    expr {2 + $_non_existent_}
} -result {can't read "_non_existent_": no such variable}
set a xx
test expr-old-26.5 {error conditions} -returnCodes error -body {
    expr {2+$a}
} -result {can't use non-numeric string "xx" as operand of "+"}
test expr-old-26.6 {error conditions} -returnCodes error -body {
    expr {2+[set a]}
} -result {can't use non-numeric string "xx" as operand of "+"}
test expr-old-26.7 {error conditions} -body {
    expr {2+(4}
} -returnCodes error -match glob -result *
test expr-old-26.8 {error conditions} {
    list [catch {expr {2 / 0}} msg] $msg $errorCode
} {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
test expr-old-26.9 {error conditions} {
    list [catch {expr {2 % 0}} msg] $msg $errorCode
} {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
test expr-old-26.10a {error conditions} !ieeeFloatingPoint {
    list [catch {expr {2.0 / 0.0}} msg] $msg $errorCode
} {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
test expr-old-26.10b {error conditions} ieeeFloatingPoint {
    list [catch {expr {2.0 / 0.0}} msg] $msg
} {0 Inf}
test expr-old-26.11 {error conditions} -body {
    expr 2`
} -returnCodes error -match glob -result *
test expr-old-26.12 {error conditions} -body {
    expr {a.b}
} -returnCodes error -match glob -result *
test expr-old-26.13 {error conditions} -returnCodes error -body {
    expr {"a"/"b"}
} -result {can't use non-numeric string "a" as operand of "/"}
test expr-old-26.14 {error conditions} -body {
    expr {2:3}
} -returnCodes error -match glob -result *
test expr-old-26.15 {error conditions} -body {
    expr {a@b}
} -returnCodes error -match glob -result *
test expr-old-26.16 {error conditions} -returnCodes error -body {
    expr "\$a+\[b"
} -match glob -result {missing close-bracket*}
test expr-old-26.17 {error conditions} -body {
    expr {a`b}
} -returnCodes error -match glob -result *
test expr-old-26.18 {error conditions} -body {
    expr \"a\"\{b
} -returnCodes error -match glob -result *
test expr-old-26.19 {error conditions} -body {
    expr a
} -returnCodes error -match glob -result *
test expr-old-26.20 {error conditions} -returnCodes error -body {
    expr
} -result {wrong # args: should be "expr expression"}
test expr-old-26.21 {error conditions} -returnCodes error -body {
    expr +1 +2
} -result {wrong # args: should be "expr expression"}

# Cancelled evaluation.

test expr-old-27.1 {cancelled evaluation} {
    set a 1
    expr {0&&[set a 2]}
    set a
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
test expr-old-30.2 {long values} {
    set a "000000000000000000000000000000"
    set a "$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a${a}5"
    expr $a
} 5

# Expressions spanning multiple arguments

test expr-old-31.1 {multiple arguments to expr command} {
    expr 4 + ( 6 *12) -3
} 73
test expr-old-31.2 {multiple arguments to expr command} -body {
    expr 2 + (3 + 4
} -returnCodes error -match glob -result *
test expr-old-31.3 {multiple arguments to expr command} -body {
    expr 2 + 3 +
} -returnCodes error -match glob -result *
test expr-old-31.4 {multiple arguments to expr command} -body {
    expr 2 + 3 )
} -returnCodes error -match glob -result *

# Math functions

test expr-old-32.1 {math functions in expressions} {
    format %.6g [expr acos(0.5)]
} {1.0472}
test expr-old-32.2 {math functions in expressions} {
    format %.6g [expr asin(0.5)]
} {0.523599}
test expr-old-32.3 {math functions in expressions} {
    format %.6g [expr atan(1.0)]
} {0.785398}
test expr-old-32.4 {math functions in expressions} {
    format %.6g [expr atan2(2.0, 2.0)]
} {0.785398}
test expr-old-32.5 {math functions in expressions} {
    format %.6g [expr ceil(1.999)]
} {2}
test expr-old-32.6 {math functions in expressions} {
    format %.6g [expr cos(.1)]
} {0.995004}
test expr-old-32.7 {math functions in expressions} {
    format %.6g [expr cosh(.1)]
} {1.005}
test expr-old-32.8 {math functions in expressions} {
    format %.6g [expr exp(1.0)]
} {2.71828}
test expr-old-32.9 {math functions in expressions} {
    format %.6g [expr floor(2.000)]
} {2}
test expr-old-32.10 {math functions in expressions} {
    format %.6g [expr floor(2.001)]
} {2}
test expr-old-32.11 {math functions in expressions} {
    format %.6g [expr fmod(7.3, 3.2)]
} {0.9}
test expr-old-32.12 {math functions in expressions} {
    format %.6g [expr hypot(3.0, 4.0)]
} {5}
test expr-old-32.13 {math functions in expressions} {
    format %.6g [expr log(2.8)]
} {1.02962}
test expr-old-32.14 {math functions in expressions} {
    format %.6g [expr log10(2.8)]
} {0.447158}
test expr-old-32.15 {math functions in expressions} {
    format %.6g [expr pow(2.1, 3.1)]
} {9.97424}
test expr-old-32.16 {math functions in expressions} {
    format %.6g [expr sin(.1)]
} {0.0998334}
test expr-old-32.17 {math functions in expressions} {
    format %.6g [expr sinh(.1)]
} {0.100167}
test expr-old-32.18 {math functions in expressions} {
    format %.6g [expr sqrt(2.0)]
} {1.41421}
test expr-old-32.19 {math functions in expressions} {
    format %.6g [expr tan(0.8)]
} {1.02964}
test expr-old-32.20 {math functions in expressions} {
    format %.6g [expr tanh(0.8)]
} {0.664037}
test expr-old-32.21 {math functions in expressions} {
    format %.6g [expr abs(-1.8)]
} {1.8}
test expr-old-32.22 {math functions in expressions} {
    expr abs(10.0)
} {10.0}
test expr-old-32.23 {math functions in expressions} {
    format %.6g [expr abs(-4)]
} {4}
test expr-old-32.24 {math functions in expressions} {
    format %.6g [expr abs(66)]
} {66}

test expr-old-32.25a {math functions in expressions} {
    expr abs(0x8000000000000000)
} [expr 1<<63]

test expr-old-32.25b {math functions in expressions} {
    expr abs(0x80000000)
} 2147483648

test expr-old-32.26 {math functions in expressions} {
    expr double(1)
} {1.0}
test expr-old-32.27 {math functions in expressions} {
    expr double(1.1)
} {1.1}
test expr-old-32.28 {math functions in expressions} {
    expr int(1)
} {1}
test expr-old-32.29 {math functions in expressions} {
    expr int(1.4)
} {1}
test expr-old-32.30 {math functions in expressions} {
    expr int(1.6)
} {1}
test expr-old-32.31 {math functions in expressions} {
    expr int(-1.4)
} {-1}
test expr-old-32.32 {math functions in expressions} {
    expr int(-1.6)
} {-1}
test expr-old-32.33 {math functions in expressions} {
    expr int(1e60)
} 999999999999999949387135297074018866963645011013410073083904
test expr-old-32.34 {math functions in expressions} {
    expr int(-1e60)
} -999999999999999949387135297074018866963645011013410073083904
test expr-old-32.35 {math functions in expressions} {
    expr round(1.49)
} {1}
test expr-old-32.36 {math functions in expressions} {
    expr round(1.51)
} {2}
test expr-old-32.37 {math functions in expressions} {
    expr round(-1.49)
} {-1}
test expr-old-32.38 {math functions in expressions} {
    expr round(-1.51)
} {-2}
test expr-old-32.39 {math functions in expressions} {
    expr round(1e60)
} 999999999999999949387135297074018866963645011013410073083904
test expr-old-32.40 {math functions in expressions} {
    expr round(-1e60)
} -999999999999999949387135297074018866963645011013410073083904
test expr-old-32.41 {math functions in expressions} {
    list [catch {expr pow(1.0 + 3.0 - 2, .8 * 5)} msg] $msg
} {0 16.0}
test expr-old-32.42 {math functions in expressions} {
    list [catch {expr hypot(5*.8,3)} msg] $msg
} {0 5.0}
test expr-old-32.45 {math functions in expressions} {
    expr (0 <= rand()) && (rand() < 1)
} {1}
test expr-old-32.46 {math functions in expressions} -body {
    list [catch {expr rand(24)} msg] $msg
} -match glob -result {1 {too many arguments for math function*}}
test expr-old-32.47 {math functions in expressions} -body {
    list [catch {expr srand()} msg] $msg
} -match glob -result {1 {not enough arguments for math function*}}
test expr-old-32.48 {math functions in expressions} -body {
    expr srand(3.79)
} -returnCodes error -match glob -result *
test expr-old-32.49 {math functions in expressions} -body {
    expr srand("")
} -returnCodes error -match glob -result *
test expr-old-32.50 {math functions in expressions} {
    set result [expr round(srand(12345) * 1000)]
    for {set i 0} {$i < 10} {incr i} {
	lappend result [expr round(rand() * 1000)]
    }
    set result
} {97 834 948 36 12 51 766 585 914 784 333}
test expr-old-32.51 {math functions in expressions} -body {
    expr {srand([lindex "6ty" 0])}
} -returnCodes error -match glob -result *
test expr-old-32.52 {math functions in expressions} {
    expr {srand(int(1<<37)) < 1}
} {1}
test expr-old-32.53 {math functions in expressions} {
    expr {srand((1<<31) - 1) > 0}
} {1}

test expr-old-33.1 {conversions and fancy args to math functions} {
    expr hypot ( 3 , 4 )
} 5.0
test expr-old-33.2 {conversions and fancy args to math functions} {
    expr hypot ( (2.0+1.0) , 4 )
} 5.0
test expr-old-33.3 {conversions and fancy args to math functions} {
    expr hypot ( 3 , (3.0 + 1.0) )
} 5.0
test expr-old-33.4 {conversions and fancy args to math functions} {
    format %.6g [expr cos(acos(0.1))]
} 0.1

test expr-old-34.1 {errors in math functions} -body {
    list [catch {expr func_2(1.0)} msg] $msg
} -match glob -result {1 {* "*func_2"}}
test expr-old-34.2 {errors in math functions} -body {
    expr func|(1.0)
} -returnCodes error -match glob -result *
test expr-old-34.3 {errors in math functions} {
    list [catch {expr {hypot("a b", 2.0)}} msg] $msg
} {1 {expected floating-point number but got "a b"}}
test expr-old-34.4 {errors in math functions} -body {
    expr hypot(1.0 2.0)
} -returnCodes error -match glob -result *
test expr-old-34.5 {errors in math functions} -body {
    expr hypot(1.0, 2.0
} -returnCodes error -match glob -result *
test expr-old-34.6 {errors in math functions} -body {
    expr hypot(1.0 ,
} -returnCodes error -match glob -result *
test expr-old-34.7 {errors in math functions} -body {
    list [catch {expr hypot(1.0)} msg] $msg
} -match glob -result {1 {not enough arguments for math function*}}
test expr-old-34.8 {errors in math functions} -body {
    list [catch {expr hypot(1.0, 2.0, 3.0)} msg] $msg
} -match glob -result {1 {too many arguments for math function*}}
test expr-old-34.9 {errors in math functions} {
    list [catch {expr acos(-2.0)} msg] $msg $errorCode
} {1 {domain error: argument not in valid range} {ARITH DOMAIN {domain error: argument not in valid range}}}
test expr-old-34.10 {errors in math functions} {
    list [catch {expr pow(-3, 1000001)} msg] $msg
} {0 -Inf}
test expr-old-34.11a {errors in math functions} !ieeeFloatingPoint {
    list [catch {expr pow(3, 1000001)} msg] $msg $errorCode
} {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}}
test expr-old-34.11b {errors in math functions} ieeeFloatingPoint {
    list [catch {expr pow(3, 1000001)} msg] $msg
} {0 Inf}
test expr-old-34.12a {errors in math functions} !ieeeFloatingPoint {
    list [catch {expr -14.0*exp(100000)} msg] $msg $errorCode
} {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}}
test expr-old-34.12b {errors in math functions} ieeeFloatingPoint {
    list [catch {expr -14.0*exp(100000)} msg] $msg
} {0 -Inf}
test expr-old-34.13 {errors in math functions} {
    expr wide(1.0e30)
} 5076964154930102272
test expr-old-34.14 {errors in math functions} {
    expr wide(-1.0e30)
} -5076964154930102272
test expr-old-34.15 {errors in math functions} {
    expr round(1.0e30)
} 1000000000000000019884624838656
test expr-old-34.16 {errors in math functions} {
    expr round(-1.0e30)
} -1000000000000000019884624838656

test expr-old-36.1 {ExprLooksLikeInt procedure} -body {
    expr 0o289
} -returnCodes error -match glob -result {*invalid octal number*}
test expr-old-36.2 {ExprLooksLikeInt procedure} {
    set x 0o289
    list [catch {expr {$x+1}} msg] $msg
} {1 {can't use non-numeric string "0o289" as operand of "+"}}
test expr-old-36.3 {ExprLooksLikeInt procedure} {
    list [catch {expr 0289.1} msg] $msg
} {0 289.1}
test expr-old-36.4 {ExprLooksLikeInt procedure} {
    set x 0289.1
    list [catch {expr {$x+1}} msg] $msg
} {0 290.1}
test expr-old-36.5 {ExprLooksLikeInt procedure} {
    set x {  +22}
    list [catch {expr {$x+1}} msg] $msg
} {0 23}
test expr-old-36.6 {ExprLooksLikeInt procedure} {
    set x {	-22}
    list [catch {expr {$x+1}} msg] $msg
} {0 -21}
test expr-old-36.7 {ExprLooksLikeInt procedure} {
    list [catch {expr nan} msg] $msg
} {1 {domain error: argument not in valid range}}
test expr-old-36.8 {ExprLooksLikeInt procedure} {
    list [catch {expr 78e1} msg] $msg
} {0 780.0}
test expr-old-36.9 {ExprLooksLikeInt procedure} {
    list [catch {expr 24E1} msg] $msg
} {0 240.0}
test expr-old-36.10 {ExprLooksLikeInt procedure} -body {
    expr 78e
} -returnCodes error -match glob -result *

# 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
    expr {$x+1}
} 665802003400000000000001

# tests for [Bug #587140]
test expr-old-36.12 {ExprLooksLikeInt procedure} {
    set x "10;"
    list [catch {expr {$x+1}} msg] $msg
} {1 {can't use non-numeric string "10;" as operand of "+"}}
test expr-old-36.13 {ExprLooksLikeInt procedure} {
    set x " +"
    list [catch {expr {$x+1}} msg] $msg
} {1 {can't use non-numeric string " +" as operand of "+"}}
test expr-old-36.14 {ExprLooksLikeInt procedure} {
    set x "123456789012345678901234567890 "
    expr {$x+1}
} 123456789012345678901234567891
test expr-old-36.15 {ExprLooksLikeInt procedure} {
    set x "0o99 "
    list [catch {expr {$x+1}} msg] $msg
} {1 {can't use non-numeric string "0o99 " as operand of "+"}}
test expr-old-36.16 {ExprLooksLikeInt procedure} {
    set x " 0xffffffffffffffffffffffffffffffffffffff  "
    expr {$x+1}
} [expr 0x100000000000000000000000000000000000000]

test expr-old-37.1 {Check that Tcl_ExprLong doesn't modify interpreter result if no error} testexprlong {
    testexprlong 4+1
} {This is a result: 5}
#Check for [Bug 1109484]
test expr-old-37.2 {Tcl_ExprLong handles wide ints gracefully} testexprlong {
    testexprlong wide(1)+2







|
<
<
<
<
<
<
<
<
<
<
<
<




|


|


|


|


|


|


|


|


|


|


|


|


|


|


|


|


|


|


|


|


|


|


|


|



|
|


|



|


|


|


|


|


|


|


|


|


|


|


|


|


|


|


|


|


|








|


|


|

|














|


|


|


|


|
|
|

|

|
|
|

|


|


|





|


|


|


|


|


|


|


|


|


|


|



|

|

|
|

|
|


|
|


|
|


|
|
|
|
|

|
|

|
|

|










|

|
|
|

|
|


|

|

|
|


|
|







696
697
698
699
700
701
702
703












704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
test expr-old-30.2 {long values} {
    set a "000000000000000000000000000000"
    set a "$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a${a}5"
    expr $a
} 5

# Expressions spanning multiple arguments
# TIP 526: expr-old-31.* now irrelevant













# Math functions

test expr-old-32.1 {math functions in expressions} {
    format %.6g [expr {acos(0.5)}]
} {1.0472}
test expr-old-32.2 {math functions in expressions} {
    format %.6g [expr {asin(0.5)}]
} {0.523599}
test expr-old-32.3 {math functions in expressions} {
    format %.6g [expr {atan(1.0)}]
} {0.785398}
test expr-old-32.4 {math functions in expressions} {
    format %.6g [expr {atan2(2.0, 2.0)}]
} {0.785398}
test expr-old-32.5 {math functions in expressions} {
    format %.6g [expr {ceil(1.999)}]
} {2}
test expr-old-32.6 {math functions in expressions} {
    format %.6g [expr {cos(.1)}]
} {0.995004}
test expr-old-32.7 {math functions in expressions} {
    format %.6g [expr {cosh(.1)}]
} {1.005}
test expr-old-32.8 {math functions in expressions} {
    format %.6g [expr {exp(1.0)}]
} {2.71828}
test expr-old-32.9 {math functions in expressions} {
    format %.6g [expr {floor(2.000)}]
} {2}
test expr-old-32.10 {math functions in expressions} {
    format %.6g [expr {floor(2.001)}]
} {2}
test expr-old-32.11 {math functions in expressions} {
    format %.6g [expr {fmod(7.3, 3.2)}]
} {0.9}
test expr-old-32.12 {math functions in expressions} {
    format %.6g [expr {hypot(3.0, 4.0)}]
} {5}
test expr-old-32.13 {math functions in expressions} {
    format %.6g [expr {log(2.8)}]
} {1.02962}
test expr-old-32.14 {math functions in expressions} {
    format %.6g [expr {log10(2.8)}]
} {0.447158}
test expr-old-32.15 {math functions in expressions} {
    format %.6g [expr {pow(2.1, 3.1)}]
} {9.97424}
test expr-old-32.16 {math functions in expressions} {
    format %.6g [expr {sin(.1)}]
} {0.0998334}
test expr-old-32.17 {math functions in expressions} {
    format %.6g [expr {sinh(.1)}]
} {0.100167}
test expr-old-32.18 {math functions in expressions} {
    format %.6g [expr {sqrt(2.0)}]
} {1.41421}
test expr-old-32.19 {math functions in expressions} {
    format %.6g [expr {tan(0.8)}]
} {1.02964}
test expr-old-32.20 {math functions in expressions} {
    format %.6g [expr {tanh(0.8)}]
} {0.664037}
test expr-old-32.21 {math functions in expressions} {
    format %.6g [expr {abs(-1.8)}]
} {1.8}
test expr-old-32.22 {math functions in expressions} {
    expr {abs(10.0)}
} {10.0}
test expr-old-32.23 {math functions in expressions} {
    format %.6g [expr {abs(-4)}]
} {4}
test expr-old-32.24 {math functions in expressions} {
    format %.6g [expr {abs(66)}]
} {66}

test expr-old-32.25a {math functions in expressions} {
    expr {abs(0x8000000000000000)}
} [expr {1 << 63}]

test expr-old-32.25b {math functions in expressions} {
    expr {abs(0x80000000)}
} 2147483648

test expr-old-32.26 {math functions in expressions} {
    expr {double(1)}
} {1.0}
test expr-old-32.27 {math functions in expressions} {
    expr {double(1.1)}
} {1.1}
test expr-old-32.28 {math functions in expressions} {
    expr {int(1)}
} {1}
test expr-old-32.29 {math functions in expressions} {
    expr {int(1.4)}
} {1}
test expr-old-32.30 {math functions in expressions} {
    expr {int(1.6)}
} {1}
test expr-old-32.31 {math functions in expressions} {
    expr {int(-1.4)}
} {-1}
test expr-old-32.32 {math functions in expressions} {
    expr {int(-1.6)}
} {-1}
test expr-old-32.33 {math functions in expressions} {
    expr {int(1e60)}
} 999999999999999949387135297074018866963645011013410073083904
test expr-old-32.34 {math functions in expressions} {
    expr {int(-1e60)}
} -999999999999999949387135297074018866963645011013410073083904
test expr-old-32.35 {math functions in expressions} {
    expr {round(1.49)}
} {1}
test expr-old-32.36 {math functions in expressions} {
    expr {round(1.51)}
} {2}
test expr-old-32.37 {math functions in expressions} {
    expr {round(-1.49)}
} {-1}
test expr-old-32.38 {math functions in expressions} {
    expr {round(-1.51)}
} {-2}
test expr-old-32.39 {math functions in expressions} {
    expr {round(1e60)}
} 999999999999999949387135297074018866963645011013410073083904
test expr-old-32.40 {math functions in expressions} {
    expr {round(-1e60)}
} -999999999999999949387135297074018866963645011013410073083904
test expr-old-32.41 {math functions in expressions} {
    list [catch {expr {pow(1.0 + 3.0 - 2, .8 * 5)}} msg] $msg
} {0 16.0}
test expr-old-32.42 {math functions in expressions} {
    list [catch {expr {hypot(5*.8, 3)}} msg] $msg
} {0 5.0}
test expr-old-32.45 {math functions in expressions} {
    expr {(0 <= rand()) && (rand() < 1)}
} {1}
test expr-old-32.46 {math functions in expressions} -body {
    list [catch {expr rand(24)} msg] $msg
} -match glob -result {1 {too many arguments for math function*}}
test expr-old-32.47 {math functions in expressions} -body {
    list [catch {expr srand()} msg] $msg
} -match glob -result {1 {not enough arguments for math function*}}
test expr-old-32.48 {math functions in expressions} -body {
    expr {srand(3.79)}
} -returnCodes error -match glob -result *
test expr-old-32.49 {math functions in expressions} -body {
    expr {srand("")}
} -returnCodes error -match glob -result *
test expr-old-32.50 {math functions in expressions} {
    set result [expr {round(srand(12345) * 1000)}]
    for {set i 0} {$i < 10} {incr i} {
	lappend result [expr {round(rand() * 1000)}]
    }
    set result
} {97 834 948 36 12 51 766 585 914 784 333}
test expr-old-32.51 {math functions in expressions} -body {
    expr {srand([lindex "6ty" 0])}
} -returnCodes error -match glob -result *
test expr-old-32.52 {math functions in expressions} {
    expr {srand(int(1<<37)) < 1}
} {1}
test expr-old-32.53 {math functions in expressions} {
    expr {srand((1<<31) - 1) > 0}
} {1}

test expr-old-33.1 {conversions and fancy args to math functions} {
    expr {hypot ( 3 , 4 )}
} 5.0
test expr-old-33.2 {conversions and fancy args to math functions} {
    expr {hypot ( (2.0+1.0) , 4 )}
} 5.0
test expr-old-33.3 {conversions and fancy args to math functions} {
    expr {hypot ( 3 , (3.0 + 1.0) )}
} 5.0
test expr-old-33.4 {conversions and fancy args to math functions} {
    format %.6g [expr {cos(acos(0.1))}]
} 0.1

test expr-old-34.1 {errors in math functions} -returnCodes error -body {
    expr {func_2(1.0)}
} -match glob -result {* "*func_2"}
test expr-old-34.2 {errors in math functions} -body {
    expr {func|(1.0)}
} -returnCodes error -match glob -result *
test expr-old-34.3 {errors in math functions} -returnCodes error -body {
    expr {hypot("a b", 2.0)}
} -result {expected floating-point number but got "a b"}
test expr-old-34.4 {errors in math functions} -body {
    expr {hypot(1.0 2.0)}
} -returnCodes error -match glob -result *
test expr-old-34.5 {errors in math functions} -body {
    expr {hypot(1.0, 2.0}
} -returnCodes error -match glob -result *
test expr-old-34.6 {errors in math functions} -body {
    expr {hypot(1.0 ,}
} -returnCodes error -match glob -result *
test expr-old-34.7 {errors in math functions} -body {
    list [catch {expr hypot(1.0)} msg] $msg
} -match glob -result {1 {not enough arguments for math function*}}
test expr-old-34.8 {errors in math functions} -body {
    list [catch {expr {hypot(1.0, 2.0, 3.0)}} msg] $msg
} -match glob -result {1 {too many arguments for math function*}}
test expr-old-34.9 {errors in math functions} {
    list [catch {expr {acos(-2.0)}} msg] $msg $errorCode
} {1 {domain error: argument not in valid range} {ARITH DOMAIN {domain error: argument not in valid range}}}
test expr-old-34.10 {errors in math functions} {
    list [catch {expr {pow(-3, 1000001)}} msg] $msg
} {0 -Inf}
test expr-old-34.11a {errors in math functions} !ieeeFloatingPoint {
    list [catch {expr {pow(3, 1000001)}} msg] $msg $errorCode
} {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}}
test expr-old-34.11b {errors in math functions} ieeeFloatingPoint {
    list [catch {expr {pow(3, 1000001)}} msg] $msg
} {0 Inf}
test expr-old-34.12a {errors in math functions} !ieeeFloatingPoint {
    list [catch {expr {-14.0 * exp(100000)}} msg] $msg $errorCode
} {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}}
test expr-old-34.12b {errors in math functions} ieeeFloatingPoint {
    list [catch {expr {-14.0 * exp(100000)}} msg] $msg
} {0 -Inf}
test expr-old-34.13 {errors in math functions} {
    expr {wide(1.0e30)}
} 5076964154930102272
test expr-old-34.14 {errors in math functions} {
    expr {wide(-1.0e30)}
} -5076964154930102272
test expr-old-34.15 {errors in math functions} {
    expr {round(1.0e30)}
} 1000000000000000019884624838656
test expr-old-34.16 {errors in math functions} {
    expr {round(-1.0e30)}
} -1000000000000000019884624838656

test expr-old-36.1 {ExprLooksLikeInt procedure} -body {
    expr {0o289}
} -returnCodes error -match glob -result {*invalid octal number*}
test expr-old-36.2 {ExprLooksLikeInt procedure} -returnCodes error -body {
    set x 0o289
    expr {$x + 1}
} -result {can't use non-numeric string "0o289" as operand of "+"}
test expr-old-36.3 {ExprLooksLikeInt procedure} {
    expr {0289.1}
} 289.1
test expr-old-36.4 {ExprLooksLikeInt procedure} {
    set x 0289.1
    expr {$x + 1}
} 290.1
test expr-old-36.5 {ExprLooksLikeInt procedure} {
    set x {  +22}
    expr {$x+1}
} 23
test expr-old-36.6 {ExprLooksLikeInt procedure} {
    set x {	-22}
    expr {$x + 1}
} -21
test expr-old-36.7 {ExprLooksLikeInt procedure} -returnCodes error -body {
    expr {nan}
} -result {domain error: argument not in valid range}
test expr-old-36.8 {ExprLooksLikeInt procedure} {
    expr {78e1}
} 780.0
test expr-old-36.9 {ExprLooksLikeInt procedure} {
    expr {24E1}
} 240.0
test expr-old-36.10 {ExprLooksLikeInt procedure} -body {
    expr {78e}
} -returnCodes error -match glob -result *

# 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
    expr {$x+1}
} 665802003400000000000001

# tests for [Bug #587140]
test expr-old-36.12 {ExprLooksLikeInt procedure} -returnCodes error -body {
    set x "10;"
    expr {$x + 1}
} -result {can't use non-numeric string "10;" as operand of "+"}
test expr-old-36.13 {ExprLooksLikeInt procedure} -returnCodes error -body {
    set x " +"
    expr {$x + 1}
} -result {can't use non-numeric string " +" as operand of "+"}
test expr-old-36.14 {ExprLooksLikeInt procedure} {
    set x "123456789012345678901234567890 "
    expr {$x + 1}
} 123456789012345678901234567891
test expr-old-36.15 {ExprLooksLikeInt procedure} -returnCodes error -body {
    set x "0o99 "
    expr {$x + 1}
} -result {can't use non-numeric string "0o99 " as operand of "+"}
test expr-old-36.16 {ExprLooksLikeInt procedure} {
    set x " 0xffffffffffffffffffffffffffffffffffffff  "
    expr {$x + 1}
} [expr {0x100000000000000000000000000000000000000}]

test expr-old-37.1 {Check that Tcl_ExprLong doesn't modify interpreter result if no error} testexprlong {
    testexprlong 4+1
} {This is a result: 5}
#Check for [Bug 1109484]
test expr-old-37.2 {Tcl_ExprLong handles wide ints gracefully} testexprlong {
    testexprlong wide(1)+2
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
#
# Test for bug #908375: rounding numbers that do not fit in a
# long but do fit in a wide
#

test expr-old-39.1 {Rounding with wide result} {
    set x 1.0e10
    set y [expr $x + 0.1]
    catch {
	set x [list [expr {$x == round($y)}] [expr $x == -round(-$y)]]
    }
    set x
} {1 1}
unset -nocomplain x y

#
# TIP #255 min and max math functions







|

|







1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
#
# Test for bug #908375: rounding numbers that do not fit in a
# long but do fit in a wide
#

test expr-old-39.1 {Rounding with wide result} {
    set x 1.0e10
    set y [expr {$x + 0.1}]
    catch {
	set x [list [expr {$x == round($y)}] [expr {$x == -round(-$y)}]]
    }
    set x
} {1 1}
unset -nocomplain x y

#
# TIP #255 min and max math functions
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
test expr-old-40.5 {min math function} -body {
    expr {min("a", 0)}
} -returnCodes error -match glob -result *
test expr-old-40.6 {min math function} -body {
    expr {min(300, "0xFF")}
} -result 255
test expr-old-40.7 {min math function} -body {
    expr min(1[string repeat 0 10000], 1e300)
} -result 1e+300
test expr-old-40.8 {min math function} -body {
    expr {min(0, "a")}
} -returnCodes error -match glob -result *

test expr-old-41.1 {max math function} -body {
    expr {max(0)}







|







1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
test expr-old-40.5 {min math function} -body {
    expr {min("a", 0)}
} -returnCodes error -match glob -result *
test expr-old-40.6 {min math function} -body {
    expr {min(300, "0xFF")}
} -result 255
test expr-old-40.7 {min math function} -body {
    expr {min("1[string repeat 0 10000]", 1e300)}
} -result 1e+300
test expr-old-40.8 {min math function} -body {
    expr {min(0, "a")}
} -returnCodes error -match glob -result *

test expr-old-41.1 {max math function} -body {
    expr {max(0)}
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
test expr-old-41.5 {max math function} -body {
    expr {max("a", 0)}
} -returnCodes error -match glob -result *
test expr-old-41.6 {max math function} -body {
    expr {max(200, "0xFF")}
} -result 255
test expr-old-41.7 {max math function} -body {
    expr max(1[string repeat 0 10000], 1e300)
} -result 1[string repeat 0 10000]
test expr-old-41.8 {max math function} -body {
    expr {max(0, "a")}
} -returnCodes error -match glob -result *

# Special test for Pentium arithmetic bug of 1994:








|







1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
test expr-old-41.5 {max math function} -body {
    expr {max("a", 0)}
} -returnCodes error -match glob -result *
test expr-old-41.6 {max math function} -body {
    expr {max(200, "0xFF")}
} -result 255
test expr-old-41.7 {max math function} -body {
    expr {max("1[string repeat 0 10000]", 1e300)}
} -result 1[string repeat 0 10000]
test expr-old-41.8 {max math function} -body {
    expr {max(0, "a")}
} -returnCodes error -match glob -result *

# Special test for Pentium arithmetic bug of 1994:

Changes to tests/expr.test.

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    return $result
}

# start of tests

catch {unset a b i x}

test expr-1.1 {TclCompileExprCmd: no expression} {
    list [catch {expr  } msg] $msg
} {1 {wrong # args: should be "expr arg ?arg ...?"}}
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
    set msg
} {extra characters after close-quote}







|
|
|

|
|
|
|
<
|
<
|







133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

148

149
150
151
152
153
154
155
156
    return $result
}

# start of tests

catch {unset a b i x}

test expr-1.1 {TclCompileExprCmd: no expression} -body {
    expr
} -returnCodes error -result {wrong # args: should be "expr expression"}
test expr-1.2 {TclCompileExprCmd: one expression word} {
    expr +25
} 25
test expr-1.3 {TclCompileExprCmd: two arguments} -body {
    expr +1 +2

} -returnCodes error -result {wrong # args: should be "expr expression"}

# 1.4 no longer applies
test expr-1.5 {TclCompileExprCmd: quoted expression word} {
    expr "0005"
} 5
test expr-1.6 {TclCompileExprCmd: quoted expression word} {
    catch {expr "0005"zxy} msg
    set msg
} {extra characters after close-quote}
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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-1.15 {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 eq 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} -body {







|




|







180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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-1.15 {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 eq 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} -body {
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
    list [catch {expr 1?{$a}:0} msg] $msg
} {1 {can't read "a": no such variable}}
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] $msg
} {1 {wrong # args: should be "incr varName ?increment?"}}
test expr-20.7 {handling of compile error in runtime case} {
    list [catch {expr + {[error foo]}} msg] $msg
} {1 foo}

# Test for non-numeric boolean literal handling
test expr-21.1 	{non-numeric boolean literals} {expr false } false
test expr-21.2 	{non-numeric boolean literals} {expr true  } true
test expr-21.3 	{non-numeric boolean literals} {expr off   } off
test expr-21.4 	{non-numeric boolean literals} {expr on    } on







|


|







790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
    list [catch {expr 1?{$a}:0} msg] $msg
} {1 {can't read "a": no such variable}}
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] $msg
} {1 {wrong # args: should be "incr varName ?increment?"}}
test expr-20.7 {handling of compile error in runtime case} {
    list [catch {expr {+[error foo]}} msg] $msg
} {1 foo}

# Test for non-numeric boolean literal handling
test expr-21.1 	{non-numeric boolean literals} {expr false } false
test expr-21.2 	{non-numeric boolean literals} {expr true  } true
test expr-21.3 	{non-numeric boolean literals} {expr off   } off
test expr-21.4 	{non-numeric boolean literals} {expr on    } on
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
	test expr-31.$i.5.$j {boolean conversion} {
	    expr bool("[string range $s 0 $j]")
	} 0
	incr j
    }
    incr i
}
test expr-31.3.4.0 {boolean conversion} {expr bool(n)} 0
test expr-31.3.5.0 {boolean conversion} {expr bool("n")} 0
test expr-31.4.4.0 {boolean conversion} {expr bool(f)} 0
test expr-31.4.5.0 {boolean conversion} {expr bool("f")} 0
test expr-31.6  {boolean conversion} {expr bool(-1 + 1)} 0
test expr-31.7  {boolean conversion} {expr bool(0 + 1)} 1
test expr-31.8  {boolean conversion} {expr bool(0.0)} 0
test expr-31.9  {boolean conversion} {expr bool(0x0)} 0
test expr-31.10 {boolean conversion} {expr bool(wide(0))} 0
test expr-31.11 {boolean conversion} {expr bool(5.0)} 1
test expr-31.12 {boolean conversion} {expr bool(5)} 1
test expr-31.13 {boolean conversion} {expr bool(0x5)} 1
test expr-31.14 {boolean conversion} {expr bool(wide(5))} 1
test expr-31.15 {boolean conversion} -body {
    expr bool("fred")
} -returnCodes error -match glob -result *

test expr-32.1 {expr mod basics} {
    set mod_nums [list \
        {-3 1} {-3 2} {-3 3} {-3 4} {-3 5} \
        {-3 -1} {-3 -2} {-3 -3} {-3 -4} {-3 -5} \
        {-2 1} {-2 2} {-2 3} {-2 4} {-2 5} \







|
|
|
|
|
|
|
|
|
|
|
|
|

|







5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
	test expr-31.$i.5.$j {boolean conversion} {
	    expr bool("[string range $s 0 $j]")
	} 0
	incr j
    }
    incr i
}
test expr-31.3.4.0 {boolean conversion} {expr {bool(n)}} 0
test expr-31.3.5.0 {boolean conversion} {expr {bool("n")}} 0
test expr-31.4.4.0 {boolean conversion} {expr {bool(f)}} 0
test expr-31.4.5.0 {boolean conversion} {expr {bool("f")}} 0
test expr-31.6  {boolean conversion} {expr {bool(-1 + 1)}} 0
test expr-31.7  {boolean conversion} {expr {bool(0 + 1)}} 1
test expr-31.8  {boolean conversion} {expr {bool(0.0)}} 0
test expr-31.9  {boolean conversion} {expr {bool(0x0)}} 0
test expr-31.10 {boolean conversion} {expr {bool(wide(0))}} 0
test expr-31.11 {boolean conversion} {expr {bool(5.0)}} 1
test expr-31.12 {boolean conversion} {expr {bool(5)}} 1
test expr-31.13 {boolean conversion} {expr {bool(0x5)}} 1
test expr-31.14 {boolean conversion} {expr {bool(wide(5))}} 1
test expr-31.15 {boolean conversion} -body {
    expr {bool("fred")}
} -returnCodes error -match glob -result *

test expr-32.1 {expr mod basics} {
    set mod_nums [list \
        {-3 1} {-3 2} {-3 3} {-3 4} {-3 5} \
        {-3 -1} {-3 -2} {-3 -3} {-3 -4} {-3 -5} \
        {-2 1} {-2 2} {-2 3} {-2 4} {-2 5} \