os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/if.test
First public contribution.
3 # This file contains a collection of tests for one or more of the Tcl
4 # built-in commands. Sourcing this file into Tcl runs the tests and
5 # generates output for errors. No output means no errors were found.
7 # Copyright (c) 1996 Sun Microsystems, Inc.
8 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 # See the file "license.terms" for information on usage and redistribution
11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 # RCS: @(#) $Id: if.test,v 1.7 2001/12/04 15:36:29 dkf Exp $
15 if {[lsearch [namespace children] ::tcltest] == -1} {
16 package require tcltest
17 namespace import -force ::tcltest::*
20 # Basic "if" operation.
23 test if-1.1 {TclCompileIfCmd: missing if/elseif test} {
24 list [catch {if} msg] $msg
25 } {1 {wrong # args: no expression after "if" argument}}
26 test if-1.2 {TclCompileIfCmd: error in if/elseif test} {
27 list [catch {if {[error "error in condition"]} foo} msg] $msg
28 } {1 {error in condition}}
29 test if-1.3 {TclCompileIfCmd: error in if/elseif test} {
30 list [catch {if {1+}} msg] $msg $errorInfo
31 } {1 {syntax error in expression "1+": premature end of expression} {syntax error in expression "1+": premature end of expression
32 ("if" test expression)
35 test if-1.4 {TclCompileIfCmd: if/elseif test in braces} {
40 test if-1.5 {TclCompileIfCmd: if/elseif test not in braces} {
45 test if-1.6 {TclCompileIfCmd: multiline test expr} {
47 if {($tcl_platform(platform) != "foobar1") && \
48 ($tcl_platform(platform) != "foobar2")} {set a 3} else {set a 4}
51 test if-1.7 {TclCompileIfCmd: "then" after if/elseif test} {
56 test if-1.8 {TclCompileIfCmd: keyword other than "then" after if/elseif test} {
58 catch {if 1<2 therefore {set a 1}} msg
60 } {invalid command name "therefore"}
61 test if-1.9 {TclCompileIfCmd: missing "then" body} {
63 catch {if 1<2 then} msg
65 } {wrong # args: no script following "then" argument}
66 test if-1.10 {TclCompileIfCmd: error in "then" body} {
68 list [catch {if {$a!="xxx"} then {set}} msg] $msg $errorInfo
69 } {1 {wrong # args: should be "set varName ?newValue?"} {wrong # args: should be "set varName ?newValue?"
72 ("if" then script line 1)
74 "if {$a!="xxx"} then {set}"}}
75 test if-1.11 {TclCompileIfCmd: error in "then" body} {
76 list [catch {if 2 then {[error "error in then clause"]}} msg] $msg
77 } {1 {error in then clause}}
78 test if-1.12 {TclCompileIfCmd: "then" body in quotes} {
83 test if-1.13 {TclCompileIfCmd: computed "then" body} {
88 set x2 {; append a x2}
93 test if-1.14 {TclCompileIfCmd: taking proper branch} {
98 test if-1.15 {TclCompileIfCmd: taking proper branch} {
103 test if-1.16 {TclCompileIfCmd: test jumpFalse instruction replacement after long "then" body} {
108 while {$a != "xxx"} {
111 if {[string compare $a "bar"] < 0} {
115 if {[string compare $a "bar"] < 0} {
119 if {[string compare $a "bar"] < 0} {
123 if {[string compare $a "bar"] < 0} {
131 while {$a != "xxx"} {
134 if {[string compare $a "bar"] < 0} {
138 if {[string compare $a "bar"] < 0} {
142 if {[string compare $a "bar"] < 0} {
146 if {[string compare $a "bar"] < 0} {
157 test if-1.17 {TclCompileIfCmd: if/elseif test in quotes} {
159 list [catch {if {"0 < 3"} {set a 1}} msg] $msg
160 } {1 {expected boolean value but got "0 < 3"}}
163 test if-2.1 {TclCompileIfCmd: "elseif" after if/elseif test} {
165 if 3>4 {set a 1} elseif 1 {set a 2}
168 # Since "else" is optional, the "elwood" below is treated as a command.
169 # But then there shouldn't be any additional argument words for the "if".
170 test if-2.2 {TclCompileIfCmd: keyword other than "elseif"} {
172 catch {if 1<2 {set a 1} elwood {set a 2}} msg
174 } {wrong # args: extra words after "else" clause in "if" command}
175 test if-2.3 {TclCompileIfCmd: missing expression after "elseif"} {
177 catch {if 1<2 {set a 1} elseif} msg
179 } {wrong # args: no expression after "elseif" argument}
180 test if-2.4 {TclCompileIfCmd: error in expression after "elseif"} {
182 list [catch {if 3>4 {set a 1} elseif {1>}} msg] $msg $errorInfo
183 } {1 {syntax error in expression "1>": premature end of expression} {syntax error in expression "1>": premature end of expression
184 ("if" test expression)
186 "if 3>4 {set a 1} elseif {1>}"}}
187 test if-2.5 {TclCompileIfCmd: test jumpFalse instruction replacement after long "elseif" body} {
192 while {$a != "xxx"} {
195 if {[string compare $a "bar"] < 0} {
199 if {[string compare $a "bar"] < 0} {
203 if {[string compare $a "bar"] < 0} {
207 if {[string compare $a "bar"] < 0} {
215 while {$a != "xxx"} {
218 if {[string compare $a "bar"] < 0} {
222 if {[string compare $a "bar"] < 0} {
226 if {[string compare $a "bar"] < 0} {
230 if {[string compare $a "bar"] < 0} {
238 } elseif 1<2 then { #; this if arm should be taken
240 while {$a != "xxx"} {
243 if {[string compare $a "bar"] < 0} {
247 if {[string compare $a "bar"] < 0} {
251 if {[string compare $a "bar"] < 0} {
255 if {[string compare $a "bar"] < 0} {
263 while {$a != "xxx"} {
266 if {[string compare $a "bar"] < 0} {
270 if {[string compare $a "bar"] < 0} {
274 if {[string compare $a "bar"] < 0} {
278 if {[string compare $a "bar"] < 0} {
290 test if-3.1 {TclCompileIfCmd: "else" clause} {
292 if 3>4 {set a 1} elseif {$a == "foo"} {set a 2} else {set a 3}
295 # Since "else" is optional, the "elsex" below is treated as a command.
296 # But then there shouldn't be any additional argument words for the "if".
297 test if-3.2 {TclCompileIfCmd: keyword other than "else"} {
299 catch {if 1<2 then {set a 1} elsex {set a 2}} msg
301 } {wrong # args: extra words after "else" clause in "if" command}
302 test if-3.3 {TclCompileIfCmd: missing body after "else"} {
304 catch {if 2<1 {set a 1} else} msg
306 } {wrong # args: no script following "else" argument}
307 test if-3.4 {TclCompileIfCmd: error compiling body after "else"} {
309 catch {if 2<1 {set a 1} else {set}} msg
311 } {wrong # args: should be "set varName ?newValue?"
314 ("if" else script line 1)
316 "if 2<1 {set a 1} else {set}"}
317 test if-3.5 {TclCompileIfCmd: extra arguments after "else" argument} {
319 catch {if 2<1 {set a 1} else {set a 2} or something} msg
321 } {wrong # args: extra words after "else" clause in "if" command}
322 # The following test also checks whether contained loops and other
323 # commands are properly relocated because a short jump must be replaced
324 # by a "long distance" one.
325 test if-3.6 {TclCompileIfCmd: test jumpFalse instruction replacement after long "else" clause} {
330 while {$a != "xxx"} {
333 if {[string compare $a "bar"] < 0} {
337 if {[string compare $a "bar"] < 0} {
341 if {[string compare $a "bar"] < 0} {
345 if {[string compare $a "bar"] < 0} {
353 while {$a != "xxx"} {
356 if {[string compare $a "bar"] < 0} {
360 if {[string compare $a "bar"] < 0} {
364 if {[string compare $a "bar"] < 0} {
368 if {[string compare $a "bar"] < 0} {
376 } elseif 1==2 then { #; this if arm should be taken
378 while {$a != "xxx"} {
381 if {[string compare $a "bar"] < 0} {
385 if {[string compare $a "bar"] < 0} {
389 if {[string compare $a "bar"] < 0} {
393 if {[string compare $a "bar"] < 0} {
401 while {$a != "xxx"} {
404 if {[string compare $a "bar"] < 0} {
408 if {[string compare $a "bar"] < 0} {
412 if {[string compare $a "bar"] < 0} {
416 if {[string compare $a "bar"] < 0} {
426 while {$a != "xxx"} {
429 if {[string compare $a "bar"] < 0} {
433 if {[string compare $a "bar"] < 0} {
437 if {[string compare $a "bar"] < 0} {
441 if {[string compare $a "bar"] < 0} {
449 while {$a != "xxx"} {
452 if {[string compare $a "bar"] < 0} {
456 if {[string compare $a "bar"] < 0} {
460 if {[string compare $a "bar"] < 0} {
464 if {[string compare $a "bar"] < 0} {
476 test if-4.1 {TclCompileIfCmd: "if" command result} {
478 set a [if 3<4 {set i 27}]
481 test if-4.2 {TclCompileIfCmd: "if" command result} {
483 set a [if 3>4 {set i 27}]
486 test if-4.3 {TclCompileIfCmd: "if" command result} {
488 set a [if 0 {set i 1} elseif 1 {set i 2}]
491 test if-4.4 {TclCompileIfCmd: "if" command result} {
493 set a [if 0 {set i 1} elseif 0 {set i 2} elseif 2>5 {set i 3} else {set i 4}]
496 test if-4.5 {TclCompileIfCmd: return value} {
497 if 0 then {set a 22; concat abc} elseif 1 {concat def} {concat ghi}
500 # Check "if" and computed command names.
503 test if-5.1 {if cmd with computed command names: missing if/elseif test} {
505 list [catch {$z} msg] $msg
506 } {1 {wrong # args: no expression after "if" argument}}
508 test if-5.2 {if cmd with computed command names: error in if/elseif test} {
510 list [catch {$z {[error "error in condition"]} foo} msg] $msg
511 } {1 {error in condition}}
512 test if-5.3 {if cmd with computed command names: error in if/elseif test} {
514 list [catch {$z {1+}} msg] $msg $errorInfo
515 } {1 {syntax error in expression "1+": premature end of expression} {syntax error in expression "1+": premature end of expression
518 test if-5.4 {if cmd with computed command names: if/elseif test in braces} {
524 test if-5.5 {if cmd with computed command names: if/elseif test not in braces} {
530 test if-5.6 {if cmd with computed command names: multiline test expr} {
533 $z {($tcl_platform(platform) != "foobar1") && \
534 ($tcl_platform(platform) != "foobar2")} {set a 3} else {set a 4}
537 test if-5.7 {if cmd with computed command names: "then" after if/elseif test} {
540 $z 4>3 then {set a 1}
543 test if-5.8 {if cmd with computed command names: keyword other than "then" after if/elseif test} {
546 catch {$z 1<2 therefore {set a 1}} msg
548 } {invalid command name "therefore"}
549 test if-5.9 {if cmd with computed command names: missing "then" body} {
552 catch {$z 1<2 then} msg
554 } {wrong # args: no script following "then" argument}
555 test if-5.10 {if cmd with computed command names: error in "then" body} {
558 list [catch {$z {$a!="xxx"} then {set}} msg] $msg $errorInfo
559 } {1 {wrong # args: should be "set varName ?newValue?"} {wrong # args: should be "set varName ?newValue?"
563 "$z {$a!="xxx"} then {set}"}}
564 test if-5.11 {if cmd with computed command names: error in "then" body} {
566 list [catch {$z 2 then {[error "error in then clause"]}} msg] $msg
567 } {1 {error in then clause}}
568 test if-5.12 {if cmd with computed command names: "then" body in quotes} {
571 $z 27>17 "append a x"
574 test if-5.13 {if cmd with computed command names: computed "then" body} {
580 set x2 {; append a x2}
585 test if-5.14 {if cmd with computed command names: taking proper branch} {
591 test if-5.15 {if cmd with computed command names: taking proper branch} {
597 test if-5.16 {if cmd with computed command names: test jumpFalse instruction replacement after long "then" body} {
603 while {$a != "xxx"} {
606 $z {[string compare $a "bar"] < 0} {
610 $z {[string compare $a "bar"] < 0} {
614 $z {[string compare $a "bar"] < 0} {
618 $z {[string compare $a "bar"] < 0} {
626 while {$a != "xxx"} {
629 $z {[string compare $a "bar"] < 0} {
633 $z {[string compare $a "bar"] < 0} {
637 $z {[string compare $a "bar"] < 0} {
641 $z {[string compare $a "bar"] < 0} {
652 test if-5.17 {if cmd with computed command names: if/elseif test in quotes} {
655 list [catch {$z {"0 < 3"} {set a 1}} msg] $msg
656 } {1 {expected boolean value but got "0 < 3"}}
659 test if-6.1 {if cmd with computed command names: "elseif" after if/elseif test} {
662 $z 3>4 {set a 1} elseif 1 {set a 2}
665 # Since "else" is optional, the "elwood" below is treated as a command.
666 # But then there shouldn't be any additional argument words for the "if".
667 test if-6.2 {if cmd with computed command names: keyword other than "elseif"} {
670 catch {$z 1<2 {set a 1} elwood {set a 2}} msg
672 } {wrong # args: extra words after "else" clause in "if" command}
673 test if-6.3 {if cmd with computed command names: missing expression after "elseif"} {
676 catch {$z 1<2 {set a 1} elseif} msg
678 } {wrong # args: no expression after "elseif" argument}
679 test if-6.4 {if cmd with computed command names: error in expression after "elseif"} {
682 list [catch {$z 3>4 {set a 1} elseif {1>}} msg] $msg $errorInfo
683 } {1 {syntax error in expression "1>": premature end of expression} {syntax error in expression "1>": premature end of expression
685 "$z 3>4 {set a 1} elseif {1>}"}}
686 test if-6.5 {if cmd with computed command names: test jumpFalse instruction replacement after long "elseif" body} {
692 while {$a != "xxx"} {
695 $z {[string compare $a "bar"] < 0} {
699 $z {[string compare $a "bar"] < 0} {
703 $z {[string compare $a "bar"] < 0} {
707 $z {[string compare $a "bar"] < 0} {
715 while {$a != "xxx"} {
718 $z {[string compare $a "bar"] < 0} {
722 $z {[string compare $a "bar"] < 0} {
726 $z {[string compare $a "bar"] < 0} {
730 $z {[string compare $a "bar"] < 0} {
738 } elseif 1<2 then { #; this if arm should be taken
740 while {$a != "xxx"} {
743 $z {[string compare $a "bar"] < 0} {
747 $z {[string compare $a "bar"] < 0} {
751 $z {[string compare $a "bar"] < 0} {
755 $z {[string compare $a "bar"] < 0} {
763 while {$a != "xxx"} {
766 $z {[string compare $a "bar"] < 0} {
770 $z {[string compare $a "bar"] < 0} {
774 $z {[string compare $a "bar"] < 0} {
778 $z {[string compare $a "bar"] < 0} {
790 test if-7.1 {if cmd with computed command names: "else" clause} {
793 $z 3>4 {set a 1} elseif {$a == "foo"} {set a 2} else {set a 3}
796 # Since "else" is optional, the "elsex" below is treated as a command.
797 # But then there shouldn't be any additional argument words for the "if".
798 test if-7.2 {if cmd with computed command names: keyword other than "else"} {
801 catch {$z 1<2 then {set a 1} elsex {set a 2}} msg
803 } {wrong # args: extra words after "else" clause in "if" command}
804 test if-7.3 {if cmd with computed command names: missing body after "else"} {
807 catch {$z 2<1 {set a 1} else} msg
809 } {wrong # args: no script following "else" argument}
810 test if-7.4 {if cmd with computed command names: error compiling body after "else"} {
813 catch {$z 2<1 {set a 1} else {set}} msg
815 } {wrong # args: should be "set varName ?newValue?"
819 "$z 2<1 {set a 1} else {set}"}
820 test if-7.5 {if cmd with computed command names: extra arguments after "else" argument} {
823 catch {$z 2<1 {set a 1} else {set a 2} or something} msg
825 } {wrong # args: extra words after "else" clause in "if" command}
826 # The following test also checks whether contained loops and other
827 # commands are properly relocated because a short jump must be replaced
828 # by a "long distance" one.
829 test if-7.6 {if cmd with computed command names: test jumpFalse instruction replacement after long "else" clause} {
835 while {$a != "xxx"} {
838 $z {[string compare $a "bar"] < 0} {
842 $z {[string compare $a "bar"] < 0} {
846 $z {[string compare $a "bar"] < 0} {
850 $z {[string compare $a "bar"] < 0} {
858 while {$a != "xxx"} {
861 $z {[string compare $a "bar"] < 0} {
865 $z {[string compare $a "bar"] < 0} {
869 $z {[string compare $a "bar"] < 0} {
873 $z {[string compare $a "bar"] < 0} {
881 } elseif 1==2 then { #; this if arm should be taken
883 while {$a != "xxx"} {
886 $z {[string compare $a "bar"] < 0} {
890 $z {[string compare $a "bar"] < 0} {
894 $z {[string compare $a "bar"] < 0} {
898 $z {[string compare $a "bar"] < 0} {
906 while {$a != "xxx"} {
909 $z {[string compare $a "bar"] < 0} {
913 $z {[string compare $a "bar"] < 0} {
917 $z {[string compare $a "bar"] < 0} {
921 $z {[string compare $a "bar"] < 0} {
931 while {$a != "xxx"} {
934 $z {[string compare $a "bar"] < 0} {
938 $z {[string compare $a "bar"] < 0} {
942 $z {[string compare $a "bar"] < 0} {
946 $z {[string compare $a "bar"] < 0} {
954 while {$a != "xxx"} {
957 $z {[string compare $a "bar"] < 0} {
961 $z {[string compare $a "bar"] < 0} {
965 $z {[string compare $a "bar"] < 0} {
969 $z {[string compare $a "bar"] < 0} {
981 test if-8.1 {if cmd with computed command names: "if" command result} {
984 set a [$z 3<4 {set i 27}]
987 test if-8.2 {if cmd with computed command names: "if" command result} {
990 set a [$z 3>4 {set i 27}]
993 test if-8.3 {if cmd with computed command names: "if" command result} {
996 set a [$z 0 {set i 1} elseif 1 {set i 2}]
999 test if-8.4 {if cmd with computed command names: "if" command result} {
1002 set a [$z 0 {set i 1} elseif 0 {set i 2} elseif 2>5 {set i 3} else {set i 4}]
1005 test if-8.5 {if cmd with computed command names: return value} {
1007 $z 0 then {set a 22; concat abc} elseif 1 {concat def} {concat ghi}
1010 test if-9.1 {if cmd with namespace qualifiers} {
1014 # Test for incorrect "double evaluation semantics"
1016 test if-10.1 {delayed substitution of then body} {
1019 # this is not compiled
1020 $if {[incr j] == 1} "
1023 # this will be compiled
1033 test if-10.2 {delayed substitution of elseif expression} {
1036 # this is not compiled
1037 $if {[incr j] == 0} {
1039 } elseif "$j == 1" {
1040 set result badelseif
1044 # this will be compiled
1047 if {[incr j] == 0} {
1049 } elseif "$j == 1" {
1050 set result badelseif
1058 test if-10.3 {delayed substitution of elseif body} {
1061 # this is not compiled
1062 $if {[incr j] == 0} {
1067 # this will be compiled
1070 if {[incr j] == 0} {
1078 test if-10.4 {delayed substitution of else body} {
1080 if {[incr j] == 0} {
1087 test if-10.5 {substituted control words} {
1088 set then then; proc then {} {return badthen}
1089 set else else; proc else {} {return badelse}
1090 set elseif elseif; proc elseif {} {return badelseif}
1091 list [catch {if 1 $then {if 0 {} $elseif 1 {if 0 {} $else {list ok}}}} a] $a
1093 test if-10.6 {double invocation of variable traces} {
1094 set iftracecounter 0
1095 proc iftraceproc {args} {
1096 upvar #0 iftracecounter counter
1097 set argc [llength $args]
1098 set extraargs [lrange $args 0 [expr {$argc - 4}]]
1099 set name [lindex $args [expr {$argc - 3}]]
1101 if {[incr counter] % 2 == 1} {
1102 set var "$counter oops [concat $extraargs]"
1104 set var "$counter + [concat $extraargs]"
1107 trace variable iftracevar r [list iftraceproc 10]
1108 list [catch {if "$iftracevar + 20" {}} a] $a \
1109 [catch {if "$iftracevar + 20" {}} b] $b \
1110 [unset iftracevar iftracecounter]
1111 } {1 {syntax error in expression "1 oops 10 + 20": extra tokens at end of expression} 0 {} {}}
1114 ::tcltest::cleanupTests