aboutsummaryrefslogtreecommitdiff
path: root/vendor/x11iraf/obm/Tcl/tests/for.test
blob: 2fafcc5ac74905fcb7c98d2ddca85b832b05b0a8 (plain) (blame)
1
2
3
4
5
6
7
8
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
# 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}