# Commands covered: foreach, for, continue, break # # This file contains a collection of tests for one or more of the Tcl # built-in commands. Sourcing this file into Tcl runs the tests and # generates output for errors. No output means no errors were found. # # Copyright (c) 1991-1993 The Regents of the University of California. # Copyright (c) 1994 Sun Microsystems, Inc. # # See the file "license.terms" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # # @(#) for.test 1.9 94/12/17 16:19:57 if {[string compare test [info procs test]] == 1} then {source defs} # Basic "foreach" operation. test for-1.1 {basic foreach tests} { set a {} foreach i {a b c d} { set a [concat $a $i] } set a } {a b c d} test for-1.2 {basic foreach tests} { set a {} foreach i {a b {{c d} e} {123 {{x}}}} { set a [concat $a $i] } set a } {a b {c d} e 123 {{x}}} test for-1.3 {basic foreach tests} {catch {foreach} msg} 1 test for-1.4 {basic foreach tests} { catch {foreach} msg set msg } {wrong # args: should be "foreach varName list command"} test for-1.5 {basic foreach tests} {catch {foreach i} msg} 1 test for-1.6 {basic foreach tests} { catch {foreach i} msg set msg } {wrong # args: should be "foreach varName list command"} test for-1.7 {basic foreach tests} {catch {foreach i j} msg} 1 test for-1.8 {basic foreach tests} { catch {foreach i j} msg set msg } {wrong # args: should be "foreach varName list command"} test for-1.9 {basic foreach tests} {catch {foreach i j k l} msg} 1 test for-1.10 {basic foreach tests} { catch {foreach i j k l} msg set msg } {wrong # args: should be "foreach varName list command"} test for-1.11 {basic foreach tests} { set a {} foreach i {} { set a [concat $a $i] } set a } {} test for-1.11 {foreach errors} { catch {unset a} set a(0) 44 list [catch {foreach a {1 2 3} {}} msg] $msg } {1 {couldn't set loop variable}} catch {unset a} # Check "continue". test for-2.1 {continue tests} {catch continue} 4 test for-2.2 {continue tests} { set a {} foreach i {a b c d} { if {[string compare $i "b"] == 0} continue set a [concat $a $i] } set a } {a c d} test for-2.3 {continue tests} { set a {} foreach i {a b c d} { if {[string compare $i "b"] != 0} continue set a [concat $a $i] } set a } {b} test for-2.4 {continue tests} {catch {continue foo} msg} 1 test for-2.5 {continue tests} { catch {continue foo} msg set msg } {wrong # args: should be "continue"} # Check "break". test for-3.1 {break tests} {catch break} 3 test for-3.2 {break tests} { set a {} foreach i {a b c d} { if {[string compare $i "c"] == 0} break set a [concat $a $i] } set a } {a b} test for-3.3 {break tests} {catch {break foo} msg} 1 test for-3.4 {break tests} { catch {break foo} msg set msg } {wrong # args: should be "break"} # Check "for" and its use of continue and break. test for-4.1 {for tests} { set a {} for {set i 1} {$i<6} {set i [expr $i+1]} { set a [concat $a $i] } set a } {1 2 3 4 5} test for-4.2 {for tests} { set a {} for {set i 1} {$i<6} {set i [expr $i+1]} { if $i==4 continue set a [concat $a $i] } set a } {1 2 3 5} test for-4.3 {for tests} { set a {} for {set i 1} {$i<6} {set i [expr $i+1]} { if $i==4 break set a [concat $a $i] } set a } {1 2 3} test for-4.4 {for tests} {catch {for 1 2 3} msg} 1 test for-4.5 {for tests} { catch {for 1 2 3} msg set msg } {wrong # args: should be "for start test next command"} test for-4.6 {for tests} {catch {for 1 2 3 4 5} msg} 1 test for-4.7 {for tests} { catch {for 1 2 3 4 5} msg set msg } {wrong # args: should be "for start test next command"} test for-4.8 {for tests} { set a {xyz} for {set i 1} {$i<6} {set i [expr $i+1]} {} set a } xyz test for-4.9 {for tests} { set a {} for {set i 1} {$i<6} {set i [expr $i+1]; if $i==4 break} { set a [concat $a $i] } set a } {1 2 3}