1 # This file contains tests for the tclExecute.c source file. Tests appear
2 # in the same order as the C code that they test. The set of tests is
3 # currently incomplete since it currently includes only new tests for
4 # code changed for the addition of Tcl namespaces. Other execution-
5 # related tests appear in several other test files including
6 # namespace.test, basic.test, eval.test, for.test, etc.
8 # Sourcing this file into Tcl runs the tests and generates output for
9 # errors. No output means no errors were found.
11 # Copyright (c) 1997 Sun Microsystems, Inc.
12 # Copyright (c) 1998-1999 by Scriptics Corporation.
14 # See the file "license.terms" for information on usage and redistribution
15 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
19 if {[lsearch [namespace children] ::tcltest] == -1} {
20 package require tcltest
21 namespace import -force ::tcltest::*
24 catch {eval namespace delete [namespace children :: test_ns_*]}
30 set ::tcltest::testConstraints(testobj) \
31 [expr {[info commands testobj] != {} \
32 && [info commands testdoubleobj] != {} \
33 && [info commands teststringobj] != {} \
34 && [info commands testobj] != {}}]
36 # Tests for the omnibus TclExecuteByteCode function:
38 # INST_DONE not tested
39 # INST_PUSH1 not tested
40 # INST_PUSH4 not tested
43 # INST_CONCAT1 not tested
44 # INST_INVOKE_STK4 not tested
45 # INST_INVOKE_STK1 not tested
46 # INST_EVAL_STK not tested
47 # INST_EXPR_STK not tested
51 test execute-1.1 {TclExecuteByteCode, INST_LOAD_SCALAR1, small opnd} {
58 test execute-1.2 {TclExecuteByteCode, INST_LOAD_SCALAR1, large opnd} {
61 for {set i 0} {$i < 129} {incr i} {
62 append body "set x$i x\n"
72 test execute-1.3 {TclExecuteByteCode, INST_LOAD_SCALAR1, error} {
78 list [catch {foo} msg] $msg
79 } {1 {can't read "x": no such variable}}
84 test execute-2.1 {TclExecuteByteCode, INST_LOAD_SCALAR4, simple case} {
86 for {set i 0} {$i < 256} {incr i} {
87 append body "set x$i x\n"
97 test execute-2.2 {TclExecuteByteCode, INST_LOAD_SCALAR4, error} {
99 for {set i 0} {$i < 256} {incr i} {
100 append body "set x$i x\n"
109 list [catch {foo} msg] $msg
110 } {1 {can't read "y": no such variable}}
113 # INST_LOAD_SCALAR_STK not tested
114 # INST_LOAD_ARRAY4 not tested
115 # INST_LOAD_ARRAY1 not tested
116 # INST_LOAD_ARRAY_STK not tested
117 # INST_LOAD_STK not tested
118 # INST_STORE_SCALAR4 not tested
119 # INST_STORE_SCALAR1 not tested
120 # INST_STORE_SCALAR_STK not tested
121 # INST_STORE_ARRAY4 not tested
122 # INST_STORE_ARRAY1 not tested
123 # INST_STORE_ARRAY_STK not tested
124 # INST_STORE_STK not tested
125 # INST_INCR_SCALAR1 not tested
126 # INST_INCR_SCALAR_STK not tested
127 # INST_INCR_STK not tested
128 # INST_INCR_ARRAY1 not tested
129 # INST_INCR_ARRAY_STK not tested
130 # INST_INCR_SCALAR1_IMM not tested
131 # INST_INCR_SCALAR_STK_IMM not tested
132 # INST_INCR_STK_IMM not tested
133 # INST_INCR_ARRAY1_IMM not tested
134 # INST_INCR_ARRAY_STK_IMM not tested
135 # INST_JUMP1 not tested
136 # INST_JUMP4 not tested
137 # INST_JUMP_TRUE4 not tested
138 # INST_JUMP_TRUE1 not tested
139 # INST_JUMP_FALSE4 not tested
140 # INST_JUMP_FALSE1 not tested
141 # INST_LOR not tested
142 # INST_LAND not tested
144 # INST_NEQ not tested
149 # INST_MOD not tested
150 # INST_LSHIFT not tested
151 # INST_RSHIFT not tested
152 # INST_BITOR not tested
153 # INST_BITXOR not tested
154 # INST_BITAND not tested
156 # INST_ADD is partially tested:
157 test execute-3.1 {TclExecuteByteCode, INST_ADD, op1 is int} {testobj} {
158 set x [testintobj set 0 1]
161 test execute-3.2 {TclExecuteByteCode, INST_ADD, op1 is double} {testobj} {
162 set x [testdoubleobj set 0 1]
165 test execute-3.3 {TclExecuteByteCode, INST_ADD, op1 is double with string} {testobj} {
166 set x [testintobj set 0 1]
167 testobj convert 0 double
170 test execute-3.4 {TclExecuteByteCode, INST_ADD, op1 is string int} {testobj} {
171 set x [teststringobj set 0 1]
174 test execute-3.5 {TclExecuteByteCode, INST_ADD, op1 is string double} {testobj} {
175 set x [teststringobj set 0 1.0]
178 test execute-3.6 {TclExecuteByteCode, INST_ADD, op1 is non-numeric} {testobj} {
179 set x [teststringobj set 0 foo]
180 list [catch {expr {$x + 1}} msg] $msg
181 } {1 {can't use non-numeric string as operand of "+"}}
182 test execute-3.7 {TclExecuteByteCode, INST_ADD, op2 is int} {testobj} {
183 set x [testintobj set 0 1]
186 test execute-3.8 {TclExecuteByteCode, INST_ADD, op2 is double} {testobj} {
187 set x [testdoubleobj set 0 1]
190 test execute-3.9 {TclExecuteByteCode, INST_ADD, op2 is double with string} {testobj} {
191 set x [testintobj set 0 1]
192 testobj convert 0 double
195 test execute-3.10 {TclExecuteByteCode, INST_ADD, op2 is string int} {testobj} {
196 set x [teststringobj set 0 1]
199 test execute-3.11 {TclExecuteByteCode, INST_ADD, op2 is string double} {testobj} {
200 set x [teststringobj set 0 1.0]
203 test execute-3.12 {TclExecuteByteCode, INST_ADD, op2 is non-numeric} {testobj} {
204 set x [teststringobj set 0 foo]
205 list [catch {expr {1 + $x}} msg] $msg
206 } {1 {can't use non-numeric string as operand of "+"}}
208 # INST_SUB is partially tested:
209 test execute-3.13 {TclExecuteByteCode, INST_SUB, op1 is int} {testobj} {
210 set x [testintobj set 0 1]
213 test execute-3.14 {TclExecuteByteCode, INST_SUB, op1 is double} {testobj} {
214 set x [testdoubleobj set 0 1]
217 test execute-3.15 {TclExecuteByteCode, INST_SUB, op1 is double with string} {testobj} {
218 set x [testintobj set 0 1]
219 testobj convert 0 double
222 test execute-3.16 {TclExecuteByteCode, INST_SUB, op1 is string int} {testobj} {
223 set x [teststringobj set 0 1]
226 test execute-3.17 {TclExecuteByteCode, INST_SUB, op1 is string double} {testobj} {
227 set x [teststringobj set 0 1.0]
230 test execute-3.18 {TclExecuteByteCode, INST_SUB, op1 is non-numeric} {testobj} {
231 set x [teststringobj set 0 foo]
232 list [catch {expr {$x - 1}} msg] $msg
233 } {1 {can't use non-numeric string as operand of "-"}}
234 test execute-3.19 {TclExecuteByteCode, INST_SUB, op2 is int} {testobj} {
235 set x [testintobj set 0 1]
238 test execute-3.20 {TclExecuteByteCode, INST_SUB, op2 is double} {testobj} {
239 set x [testdoubleobj set 0 1]
242 test execute-3.21 {TclExecuteByteCode, INST_SUB, op2 is double with string} {testobj} {
243 set x [testintobj set 0 1]
244 testobj convert 0 double
247 test execute-3.22 {TclExecuteByteCode, INST_SUB, op2 is string int} {testobj} {
248 set x [teststringobj set 0 1]
251 test execute-3.23 {TclExecuteByteCode, INST_SUB, op2 is string double} {testobj} {
252 set x [teststringobj set 0 1.0]
255 test execute-3.24 {TclExecuteByteCode, INST_SUB, op2 is non-numeric} {testobj} {
256 set x [teststringobj set 0 foo]
257 list [catch {expr {1 - $x}} msg] $msg
258 } {1 {can't use non-numeric string as operand of "-"}}
260 # INST_MULT is partially tested:
261 test execute-3.25 {TclExecuteByteCode, INST_MULT, op1 is int} {testobj} {
262 set x [testintobj set 1 1]
265 test execute-3.26 {TclExecuteByteCode, INST_MULT, op1 is double} {testobj} {
266 set x [testdoubleobj set 1 2.0]
269 test execute-3.27 {TclExecuteByteCode, INST_MULT, op1 is double with string} {testobj} {
270 set x [testintobj set 1 2]
271 testobj convert 1 double
274 test execute-3.28 {TclExecuteByteCode, INST_MULT, op1 is string int} {testobj} {
275 set x [teststringobj set 1 1]
278 test execute-3.29 {TclExecuteByteCode, INST_MULT, op1 is string double} {testobj} {
279 set x [teststringobj set 1 1.0]
282 test execute-3.30 {TclExecuteByteCode, INST_MULT, op1 is non-numeric} {testobj} {
283 set x [teststringobj set 1 foo]
284 list [catch {expr {$x * 1}} msg] $msg
285 } {1 {can't use non-numeric string as operand of "*"}}
286 test execute-3.31 {TclExecuteByteCode, INST_MULT, op2 is int} {testobj} {
287 set x [testintobj set 1 1]
290 test execute-3.32 {TclExecuteByteCode, INST_MULT, op2 is double} {testobj} {
291 set x [testdoubleobj set 1 2.0]
294 test execute-3.33 {TclExecuteByteCode, INST_MULT, op2 is double with string} {testobj} {
295 set x [testintobj set 1 2]
296 testobj convert 1 double
299 test execute-3.34 {TclExecuteByteCode, INST_MULT, op2 is string int} {testobj} {
300 set x [teststringobj set 1 1]
303 test execute-3.35 {TclExecuteByteCode, INST_MULT, op2 is string double} {testobj} {
304 set x [teststringobj set 1 1.0]
307 test execute-3.36 {TclExecuteByteCode, INST_MULT, op2 is non-numeric} {testobj} {
308 set x [teststringobj set 1 foo]
309 list [catch {expr {1 * $x}} msg] $msg
310 } {1 {can't use non-numeric string as operand of "*"}}
312 # INST_DIV is partially tested:
313 test execute-3.37 {TclExecuteByteCode, INST_DIV, op1 is int} {testobj} {
314 set x [testintobj set 1 1]
317 test execute-3.38 {TclExecuteByteCode, INST_DIV, op1 is double} {testobj} {
318 set x [testdoubleobj set 1 2.0]
321 test execute-3.39 {TclExecuteByteCode, INST_DIV, op1 is double with string} {testobj} {
322 set x [testintobj set 1 2]
323 testobj convert 1 double
326 test execute-3.40 {TclExecuteByteCode, INST_DIV, op1 is string int} {testobj} {
327 set x [teststringobj set 1 1]
330 test execute-3.41 {TclExecuteByteCode, INST_DIV, op1 is string double} {testobj} {
331 set x [teststringobj set 1 1.0]
334 test execute-3.42 {TclExecuteByteCode, INST_DIV, op1 is non-numeric} {testobj} {
335 set x [teststringobj set 1 foo]
336 list [catch {expr {$x / 1}} msg] $msg
337 } {1 {can't use non-numeric string as operand of "/"}}
338 test execute-3.43 {TclExecuteByteCode, INST_DIV, op2 is int} {testobj} {
339 set x [testintobj set 1 1]
342 test execute-3.44 {TclExecuteByteCode, INST_DIV, op2 is double} {testobj} {
343 set x [testdoubleobj set 1 1.0]
346 test execute-3.45 {TclExecuteByteCode, INST_DIV, op2 is double with string} {testobj} {
347 set x [testintobj set 1 1]
348 testobj convert 1 double
351 test execute-3.46 {TclExecuteByteCode, INST_DIV, op2 is string int} {testobj} {
352 set x [teststringobj set 1 1]
355 test execute-3.47 {TclExecuteByteCode, INST_DIV, op2 is string double} {testobj} {
356 set x [teststringobj set 1 1.0]
359 test execute-3.48 {TclExecuteByteCode, INST_DIV, op2 is non-numeric} {testobj} {
360 set x [teststringobj set 1 foo]
361 list [catch {expr {1 / $x}} msg] $msg
362 } {1 {can't use non-numeric string as operand of "/"}}
364 # INST_UPLUS is partially tested:
365 test execute-3.49 {TclExecuteByteCode, INST_UPLUS, op is int} {testobj} {
366 set x [testintobj set 1 1]
369 test execute-3.50 {TclExecuteByteCode, INST_UPLUS, op is double} {testobj} {
370 set x [testdoubleobj set 1 1.0]
373 test execute-3.51 {TclExecuteByteCode, INST_UPLUS, op is double with string} {testobj} {
374 set x [testintobj set 1 1]
375 testobj convert 1 double
378 test execute-3.52 {TclExecuteByteCode, INST_UPLUS, op is string int} {testobj} {
379 set x [teststringobj set 1 1]
382 test execute-3.53 {TclExecuteByteCode, INST_UPLUS, op is string double} {testobj} {
383 set x [teststringobj set 1 1.0]
386 test execute-3.54 {TclExecuteByteCode, INST_UPLUS, op is non-numeric} {testobj} {
387 set x [teststringobj set 1 foo]
388 list [catch {expr {+ $x}} msg] $msg
389 } {1 {can't use non-numeric string as operand of "+"}}
391 # INST_UMINUS is partially tested:
392 test execute-3.55 {TclExecuteByteCode, INST_UMINUS, op is int} {testobj} {
393 set x [testintobj set 1 1]
396 test execute-3.56 {TclExecuteByteCode, INST_UMINUS, op is double} {testobj} {
397 set x [testdoubleobj set 1 1.0]
400 test execute-3.57 {TclExecuteByteCode, INST_UMINUS, op is double with string} {testobj} {
401 set x [testintobj set 1 1]
402 testobj convert 1 double
405 test execute-3.58 {TclExecuteByteCode, INST_UMINUS, op is string int} {testobj} {
406 set x [teststringobj set 1 1]
409 test execute-3.59 {TclExecuteByteCode, INST_UMINUS, op is string double} {testobj} {
410 set x [teststringobj set 1 1.0]
413 test execute-3.60 {TclExecuteByteCode, INST_UMINUS, op is non-numeric} {testobj} {
414 set x [teststringobj set 1 foo]
415 list [catch {expr {- $x}} msg] $msg
416 } {1 {can't use non-numeric string as operand of "-"}}
418 # INST_LNOT is partially tested:
419 test execute-3.61 {TclExecuteByteCode, INST_LNOT, op is int} {testobj} {
420 set x [testintobj set 1 2]
423 test execute-3.62 {TclExecuteByteCode, INST_LNOT, op is int} {testobj} {
424 set x [testintobj set 1 0]
427 test execute-3.63 {TclExecuteByteCode, INST_LNOT, op is double} {testobj} {
428 set x [testdoubleobj set 1 1.0]
431 test execute-3.64 {TclExecuteByteCode, INST_LNOT, op is double} {testobj} {
432 set x [testdoubleobj set 1 0.0]
435 test execute-3.65 {TclExecuteByteCode, INST_LNOT, op is double with string} {testobj} {
436 set x [testintobj set 1 1]
437 testobj convert 1 double
440 test execute-3.66 {TclExecuteByteCode, INST_LNOT, op is double with string} {testobj} {
441 set x [testintobj set 1 0]
442 testobj convert 1 double
445 test execute-3.67 {TclExecuteByteCode, INST_LNOT, op is string int} {testobj} {
446 set x [teststringobj set 1 1]
449 test execute-3.68 {TclExecuteByteCode, INST_LNOT, op is string int} {testobj} {
450 set x [teststringobj set 1 0]
453 test execute-3.69 {TclExecuteByteCode, INST_LNOT, op is string double} {testobj} {
454 set x [teststringobj set 1 1.0]
457 test execute-3.70 {TclExecuteByteCode, INST_LNOT, op is string double} {testobj} {
458 set x [teststringobj set 1 0.0]
461 test execute-3.71 {TclExecuteByteCode, INST_LNOT, op is non-numeric} {testobj} {
462 set x [teststringobj set 1 foo]
463 list [catch {expr {! $x}} msg] $msg
464 } {1 {can't use non-numeric string as operand of "!"}}
466 # INST_BITNOT not tested
467 # INST_CALL_BUILTIN_FUNC1 not tested
468 # INST_CALL_FUNC1 not tested
470 # INST_TRY_CVT_TO_NUMERIC is partially tested:
471 test execute-3.72 {TclExecuteByteCode, INST_TRY_CVT_TO_NUMERIC, op is int} {testobj} {
472 set x [testintobj set 1 1]
475 test execute-3.73 {TclExecuteByteCode, INST_TRY_CVT_TO_NUMERIC, op is double} {testobj} {
476 set x [testdoubleobj set 1 1.0]
479 test execute-3.74 {TclExecuteByteCode, INST_TRY_CVT_TO_NUMERIC, op is double with string} {testobj} {
480 set x [testintobj set 1 1]
481 testobj convert 1 double
484 test execute-3.75 {TclExecuteByteCode, INST_TRY_CVT_TO_NUMERIC, op is string int} {testobj} {
485 set x [teststringobj set 1 1]
488 test execute-3.76 {TclExecuteByteCode, INST_TRY_CVT_TO_NUMERIC, op is string double} {testobj} {
489 set x [teststringobj set 1 1.0]
492 test execute-3.77 {TclExecuteByteCode, INST_TRY_CVT_TO_NUMERIC, op is non-numeric} {testobj} {
493 set x [teststringobj set 1 foo]
497 # INST_BREAK not tested
498 # INST_CONTINUE not tested
499 # INST_FOREACH_START4 not tested
500 # INST_FOREACH_STEP4 not tested
501 # INST_BEGIN_CATCH4 not tested
502 # INST_END_CATCH not tested
503 # INST_PUSH_RESULT not tested
504 # INST_PUSH_RETURN_CODE not tested
506 test execute-4.1 {Tcl_GetCommandFromObj, convert to tclCmdNameType} {
507 catch {eval namespace delete [namespace children :: test_ns_*]}
510 namespace eval test_ns_1 {
511 namespace export cmd1
512 proc cmd1 {args} {return "cmd1: $args"}
513 proc cmd2 {args} {return "cmd2: $args"}
515 namespace eval test_ns_1::test_ns_2 {
516 namespace import ::test_ns_1::*
520 list [namespace which -command ${x}${y}cmd1] \
521 [catch {namespace which -command ${x}${y}cmd2} msg] $msg \
522 [catch {namespace which -command ${x}${y}:cmd2} msg] $msg
523 } {::test_ns_1::test_ns_2::cmd1 0 {} 0 {}}
524 test execute-4.2 {Tcl_GetCommandFromObj, check if cached tclCmdNameType is invalid} {
525 catch {eval namespace delete [namespace children :: test_ns_*]}
526 catch {rename foo ""}
531 namespace eval test_ns_1 {
533 return [namespace which -command foo]
537 lappend l [test_ns_1::whichFoo]
538 namespace eval test_ns_1 {
540 return "namespace foo"
543 lappend l [test_ns_1::whichFoo]
545 } {::foo ::test_ns_1::foo}
546 test execute-4.3 {Tcl_GetCommandFromObj, command never found} {
547 catch {eval namespace delete [namespace children :: test_ns_*]}
548 catch {rename foo ""}
549 namespace eval test_ns_1 {
551 return "namespace foo"
554 namespace eval test_ns_1 {
556 return "namespace foo"
559 list [namespace eval test_ns_1 {namespace which -command foo}] \
560 [rename test_ns_1::foo ""] \
561 [catch {namespace eval test_ns_1 {namespace which -command foo}} msg] $msg
562 } {::test_ns_1::foo {} 0 {}}
564 test execute-5.1 {SetCmdNameFromAny, set cmd name to empty heap string if NULL} {
565 catch {eval namespace delete [namespace children :: test_ns_*]}
567 proc {} {} {return {}}
574 test execute-6.1 {UpdateStringOfCmdName: called for duplicate of empty cmdName object} {
587 catch {eval namespace delete [namespace children :: test_ns_*]}
588 catch {rename foo ""}
591 catch {rename { } ""}
595 ::tcltest::cleanupTests