blob: a4b652a9fe107e8594b42d221676e787d48e24a8 (
plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# Commands covered: foreach, 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-1997 Sun Microsystems, Inc.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
if {[lsearch [namespace children] ::tcltest] == -1} {
package require tcltest
namespace import -force ::tcltest::*
}
catch {unset a}
catch {unset x}
# Basic "foreach" operation.
test foreach-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 foreach-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 foreach-1.3 {basic foreach tests} {catch {foreach} msg} 1
test foreach-1.4 {basic foreach tests} {
catch {foreach} msg
set msg
} {wrong # args: should be "foreach varList list ?varList list ...? command"}
test foreach-1.5 {basic foreach tests} {catch {foreach i} msg} 1
test foreach-1.6 {basic foreach tests} {
catch {foreach i} msg
set msg
} {wrong # args: should be "foreach varList list ?varList list ...? command"}
test foreach-1.7 {basic foreach tests} {catch {foreach i j} msg} 1
test foreach-1.8 {basic foreach tests} {
catch {foreach i j} msg
set msg
} {wrong # args: should be "foreach varList list ?varList list ...? command"}
test foreach-1.9 {basic foreach tests} {catch {foreach i j k l} msg} 1
test foreach-1.10 {basic foreach tests} {
catch {foreach i j k l} msg
set msg
} {wrong # args: should be "foreach varList list ?varList list ...? command"}
test foreach-1.11 {basic foreach tests} {
set a {}
foreach i {} {
set a [concat $a $i]
}
set a
} {}
test foreach-1.12 {foreach errors} {
list [catch {foreach {{a}{b}} {1 2 3} {}} msg] $msg
} {1 {list element in braces followed by "{b}" instead of space}}
test foreach-1.13 {foreach errors} {
list [catch {foreach a {{1 2}3} {}} msg] $msg
} {1 {list element in braces followed by "3" instead of space}}
catch {unset a}
test foreach-1.14 {foreach errors} {
catch {unset a}
set a(0) 44
list [catch {foreach a {1 2 3} {}} msg o] $msg $::errorInfo
} {1 {can't set "a": variable is array} {can't set "a": variable is array
(setting foreach loop variable "a")
invoked from within
"foreach a {1 2 3} {}"}}
test foreach-1.15 {foreach errors} {
list [catch {foreach {} {} {}} msg] $msg
} {1 {foreach varlist is empty}}
catch {unset a}
test foreach-2.1 {parallel foreach tests} {
set x {}
foreach {a b} {1 2 3 4} {
append x $b $a
}
set x
} {2143}
test foreach-2.2 {parallel foreach tests} {
set x {}
foreach {a b} {1 2 3 4 5} {
append x $b $a
}
set x
} {21435}
test foreach-2.3 {parallel foreach tests} {
set x {}
foreach a {1 2 3} b {4 5 6} {
append x $b $a
}
set x
} {415263}
test foreach-2.4 {parallel foreach tests} {
set x {}
foreach a {1 2 3} b {4 5 6 7 8} {
append x $b $a
}
set x
} {41526378}
test foreach-2.5 {parallel foreach tests} {
set x {}
foreach {a b} {a b A B aa bb} c {c C cc CC} {
append x $a $b $c
}
set x
} {abcABCaabbccCC}
test foreach-2.6 {parallel foreach tests} {
set x {}
foreach a {1 2 3} b {1 2 3} c {1 2 3} d {1 2 3} e {1 2 3} {
append x $a $b $c $d $e
}
set x
} {111112222233333}
test foreach-2.7 {parallel foreach tests} {
set x {}
foreach a {} b {1 2 3} c {1 2} d {1 2 3 4} e {{1 2}} {
append x $a $b $c $d $e
}
set x
} {1111 2222334}
test foreach-2.8 {foreach only sets vars if repeating loop} {
proc foo {} {
set rgb {65535 0 0}
foreach {r g b} [set rgb] {}
return "r=$r, g=$g, b=$b"
}
foo
} {r=65535, g=0, b=0}
test foreach-2.9 {foreach only supports local scalar variables} {
proc foo {} {
set x {}
foreach {a(3)} {1 2 3 4} {lappend x [set {a(3)}]}
set x
}
foo
} {1 2 3 4}
test foreach-3.1 {compiled foreach backward jump works correctly} {
catch {unset x}
proc foo {arrayName} {
upvar 1 $arrayName a
set l {}
foreach member [array names a] {
lappend l [list $member [set a($member)]]
}
return $l
}
array set x {0 zero 1 one 2 two 3 three}
lsort [foo x]
} [lsort {{0 zero} {1 one} {2 two} {3 three}}]
test foreach-4.1 {noncompiled foreach and shared variable or value list objects that are converted to another type} {
catch {unset x}
foreach {12.0} {a b c} {
set x 12.0
set x [expr $x + 1]
}
set x
} 13.0
# Check "continue".
test foreach-5.1 {continue tests} {catch continue} 4
test foreach-5.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 foreach-5.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 foreach-5.4 {continue tests} {catch {continue foo} msg} 1
test foreach-5.5 {continue tests} {
catch {continue foo} msg
set msg
} {wrong # args: should be "continue"}
# Check "break".
test foreach-6.1 {break tests} {catch break} 3
test foreach-6.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 foreach-6.3 {break tests} {catch {break foo} msg} 1
test foreach-6.4 {break tests} {
catch {break foo} msg
set msg
} {wrong # args: should be "break"}
# Check for bug #406709
test foreach-6.5 {break tests} {
proc a {} {
set a 1
foreach b b {list [concat a; break]; incr a}
incr a
}
a
} {2}
# Test for incorrect "double evaluation" semantics
test foreach-7.1 {delayed substitution of body} {
proc foo {} {
set a 0
foreach a [list 1 2 3] "
set x $a
"
set x
}
foo
} {0}
# Test for [Bug 1189274]; crash on failure
test foreach-8.1 {empty list handling} {
proc crash {} {
rename crash {}
set a "x y z"
set b ""
foreach aa $a bb $b { set x "aa = $aa bb = $bb" }
}
crash
} {}
# [Bug 1671138]; infinite loop with empty var list in bytecompiled version
test foreach-9.1 {compiled empty var list} {
proc foo {} {
foreach {} x {
error "reached body"
}
}
list [catch { foo } msg] $msg
} {1 {foreach varlist is empty}}
test foreach-10.1 {foreach: [Bug 1671087]} -setup {
proc demo {} {
set vals {1 2 3 4}
trace add variable x write {string length $vals ;# }
foreach {x y} $vals {format $y}
}
} -body {
demo
} -cleanup {
rename demo {}
} -result {}
# cleanup
catch {unset a}
catch {unset x}
catch {rename foo {}}
::tcltest::cleanupTests
return
|