170 lines
4.8 KiB
Plaintext
170 lines
4.8 KiB
Plaintext
|
# 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.
|
||
|
# All rights reserved.
|
||
|
#
|
||
|
# Permission is hereby granted, without written agreement and without
|
||
|
# license or royalty fees, to use, copy, modify, and distribute this
|
||
|
# software and its documentation for any purpose, provided that the
|
||
|
# above copyright notice and the following two paragraphs appear in
|
||
|
# all copies of this software.
|
||
|
#
|
||
|
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
|
||
|
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
|
||
|
# OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
|
||
|
# CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
#
|
||
|
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
|
||
|
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||
|
# AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
||
|
# ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
|
||
|
# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
||
|
#
|
||
|
# $Header: /user6/ouster/tcl/tests/RCS/for.test,v 1.8 93/02/06 15:54:05 ouster Exp $ (Berkeley)
|
||
|
|
||
|
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}
|