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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
# Commands covered: mapeach, 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.
# Copyright (c) 2011 Trevor Davel
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: $
if {[lsearch [namespace children] ::tcltest] == -1} {
package require tcltest
namespace import -force ::tcltest::*
}
catch {unset a}
catch {unset i}
catch {unset x}
# ----- Non-compiled operation -------------------------------------------------
# Basic "mapeach" operation (non-compiled)
test mapeach-1.1 {basic mapeach tests} {
set a {}
mapeach i {a b c d} {
set a [concat $a $i]
}
} {a {a b} {a b c} {a b c d}}
test mapeach-1.2 {basic mapeach tests} {
mapeach i {a b {{c d} e} {123 {{x}}}} {
set i
}
} {a b {{c d} e} {123 {{x}}}}
test mapeach-1.2a {basic mapeach tests} {
mapeach i {a b {{c d} e} {123 {{x}}}} {
return -level 0 $i
}
} {a b {{c d} e} {123 {{x}}}}
test mapeach-1.3 {basic mapeach tests} {catch {mapeach} msg} 1
test mapeach-1.4 {basic mapeach tests} {
catch {mapeach} msg
set msg
} {wrong # args: should be "mapeach varList list ?varList list ...? command"}
test mapeach-1.5 {basic mapeach tests} {catch {mapeach i} msg} 1
test mapeach-1.6 {basic mapeach tests} {
catch {mapeach i} msg
set msg
} {wrong # args: should be "mapeach varList list ?varList list ...? command"}
test mapeach-1.7 {basic mapeach tests} {catch {mapeach i j} msg} 1
test mapeach-1.8 {basic mapeach tests} {
catch {mapeach i j} msg
set msg
} {wrong # args: should be "mapeach varList list ?varList list ...? command"}
test mapeach-1.9 {basic mapeach tests} {catch {mapeach i j k l} msg} 1
test mapeach-1.10 {basic mapeach tests} {
catch {mapeach i j k l} msg
set msg
} {wrong # args: should be "mapeach varList list ?varList list ...? command"}
test mapeach-1.11 {basic mapeach tests} {
mapeach i {} {
set i
}
} {}
test mapeach-1.12 {basic mapeach tests} {
mapeach i {} {
return -level 0 x
}
} {}
test mapeach-1.13 {mapeach errors} {
list [catch {mapeach {{a}{b}} {1 2 3} {}} msg] $msg
} {1 {list element in braces followed by "{b}" instead of space}}
test mapeach-1.14 {mapeach errors} {
list [catch {mapeach a {{1 2}3} {}} msg] $msg
} {1 {list element in braces followed by "3" instead of space}}
catch {unset a}
test mapeach-1.15 {mapeach errors} {
catch {unset a}
set a(0) 44
list [catch {mapeach 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
"mapeach a {1 2 3} {}"}}
test mapeach-1.16 {mapeach errors} {
list [catch {mapeach {} {} {}} msg] $msg
} {1 {foreach varlist is empty}}
catch {unset a}
# Parallel "mapeach" operation (non-compiled)
test mapeach-2.1 {parallel mapeach tests} {
mapeach {a b} {1 2 3 4} {
list $b $a
}
} {{2 1} {4 3}}
test mapeach-2.2 {parallel mapeach tests} {
mapeach {a b} {1 2 3 4 5} {
list $b $a
}
} {{2 1} {4 3} {{} 5}}
test mapeach-2.3 {parallel mapeach tests} {
mapeach a {1 2 3} b {4 5 6} {
list $b $a
}
} {{4 1} {5 2} {6 3}}
test mapeach-2.4 {parallel mapeach tests} {
mapeach a {1 2 3} b {4 5 6 7 8} {
list $b $a
}
} {{4 1} {5 2} {6 3} {7 {}} {8 {}}}
test mapeach-2.5 {parallel mapeach tests} {
mapeach {a b} {a b A B aa bb} c {c C cc CC} {
list $a $b $c
}
} {{a b c} {A B C} {aa bb cc} {{} {} CC}}
test mapeach-2.6 {parallel mapeach tests} {
mapeach a {1 2 3} b {1 2 3} c {1 2 3} d {1 2 3} e {1 2 3} {
list $a$b$c$d$e
}
} {11111 22222 33333}
test mapeach-2.7 {parallel mapeach tests} {
mapeach a {} b {1 2 3} c {1 2} d {1 2 3 4} e {{1 2}} {
set x $a$b$c$d$e
}
} {{1111 2} 222 33 4}
test mapeach-2.8 {parallel mapeach tests} {
mapeach a {} b {1 2 3} c {1 2} d {1 2 3 4} e {{1 2}} {
join [list $a $b $c $d $e] .
}
} {{.1.1.1.1 2} .2.2.2. .3..3. ...4.}
test mapeach-2.9 {mapeach only sets vars if repeating loop} {
namespace eval ::mapeach_test {
set rgb {65535 0 0}
mapeach {r g b} [set rgb] {}
set ::x "r=$r, g=$g, b=$b"
}
namespace delete ::mapeach_test
set x
} {r=65535, g=0, b=0}
test mapeach-2.10 {mapeach only supports local scalar variables} {
catch { unset a }
mapeach {a(3)} {1 2 3 4} {set {a(3)}}
} {1 2 3 4}
catch { unset a }
# "mapeach" with "continue" and "break" (non-compiled)
test mapeach-3.1 {continue tests} {
mapeach i {a b c d} {
if {[string compare $i "b"] == 0} continue
set i
}
} {a c d}
test mapeach-3.2 {continue tests} {
set x 0
list [mapeach i {a b c d} {
incr x
if {[string compare $i "b"] != 0} continue
set i
}] $x
} {b 4}
test mapeach-3.3 {break tests} {
set x 0
list [mapeach i {a b c d} {
incr x
if {[string compare $i "c"] == 0} break
set i
}] $x
} {{a b} 3}
# Check for bug similar to #406709
test mapeach-3.4 {break tests} {
set a 1
mapeach b b {list [concat a; break]; incr a}
incr a
} {2}
# ----- Compiled operation ------------------------------------------------------
# Basic "mapeach" operation (compiled)
test mapeach-4.1 {basic mapeach tests} {
apply {{} {
set a {}
mapeach i {a b c d} {
set a [concat $a $i]
}
}}
} {a {a b} {a b c} {a b c d}}
test mapeach-4.2 {basic mapeach tests} {
apply {{} {
mapeach i {a b {{c d} e} {123 {{x}}}} {
set i
}
}}
} {a b {{c d} e} {123 {{x}}}}
test mapeach-4.2a {basic mapeach tests} {
apply {{} {
mapeach i {a b {{c d} e} {123 {{x}}}} {
return -level 0 $i
}
}}
} {a b {{c d} e} {123 {{x}}}}
test mapeach-4.3 {basic mapeach tests} {catch { apply {{} { mapeach }} } msg} 1
test mapeach-4.4 {basic mapeach tests} {
catch { apply {{} { mapeach }} } msg
set msg
} {wrong # args: should be "mapeach varList list ?varList list ...? command"}
test mapeach-4.5 {basic mapeach tests} {catch { apply {{} { mapeach i }} } msg} 1
test mapeach-4.6 {basic mapeach tests} {
catch { apply {{} { mapeach i }} } msg
set msg
} {wrong # args: should be "mapeach varList list ?varList list ...? command"}
test mapeach-4.7 {basic mapeach tests} {catch { apply {{} { mapeach i j }} } msg} 1
test mapeach-4.8 {basic mapeach tests} {
catch { apply {{} { mapeach i j }} } msg
set msg
} {wrong # args: should be "mapeach varList list ?varList list ...? command"}
test mapeach-4.9 {basic mapeach tests} {catch { apply {{} { mapeach i j k l }} } msg} 1
test mapeach-4.10 {basic mapeach tests} {
catch { apply {{} { mapeach i j k l }} } msg
set msg
} {wrong # args: should be "mapeach varList list ?varList list ...? command"}
test mapeach-4.11 {basic mapeach tests} {
apply {{} { mapeach i {} { set i } }}
} {}
test mapeach-4.12 {basic mapeach tests} {
apply {{} { mapeach i {} { return -level 0 x } }}
} {}
test mapeach-4.13 {mapeach errors} {
list [catch { apply {{} { mapeach {{a}{b}} {1 2 3} {} }} } msg] $msg
} {1 {list element in braces followed by "{b}" instead of space}}
test mapeach-4.14 {mapeach errors} {
list [catch { apply {{} { mapeach a {{1 2}3} {} }} } msg] $msg
} {1 {list element in braces followed by "3" instead of space}}
catch {unset a}
test mapeach-4.15 {mapeach errors} {
apply {{} {
set a(0) 44
list [catch {mapeach a {1 2 3} {}} msg o] $msg $::errorInfo
}}
} {1 {can't set "a": variable is array} {can't set "a": variable is array
while executing
"mapeach a {1 2 3} {}"}}
test mapeach-4.16 {mapeach errors} {
list [catch { apply {{} { mapeach {} {} {} }} } msg] $msg
} {1 {foreach varlist is empty}}
catch {unset a}
# Parallel "mapeach" operation (compiled)
test mapeach-5.1 {parallel mapeach tests} {
apply {{} {
mapeach {a b} {1 2 3 4} {
list $b $a
}
}}
} {{2 1} {4 3}}
test mapeach-5.2 {parallel mapeach tests} {
apply {{} {
mapeach {a b} {1 2 3 4 5} {
list $b $a
}
}}
} {{2 1} {4 3} {{} 5}}
test mapeach-5.3 {parallel mapeach tests} {
apply {{} {
mapeach a {1 2 3} b {4 5 6} {
list $b $a
}
}}
} {{4 1} {5 2} {6 3}}
test mapeach-5.4 {parallel mapeach tests} {
apply {{} {
mapeach a {1 2 3} b {4 5 6 7 8} {
list $b $a
}
}}
} {{4 1} {5 2} {6 3} {7 {}} {8 {}}}
test mapeach-5.5 {parallel mapeach tests} {
apply {{} {
mapeach {a b} {a b A B aa bb} c {c C cc CC} {
list $a $b $c
}
}}
} {{a b c} {A B C} {aa bb cc} {{} {} CC}}
test mapeach-5.6 {parallel mapeach tests} {
apply {{} {
mapeach a {1 2 3} b {1 2 3} c {1 2 3} d {1 2 3} e {1 2 3} {
list $a$b$c$d$e
}
}}
} {11111 22222 33333}
test mapeach-5.7 {parallel mapeach tests} {
apply {{} {
mapeach a {} b {1 2 3} c {1 2} d {1 2 3 4} e {{1 2}} {
set x $a$b$c$d$e
}
}}
} {{1111 2} 222 33 4}
test mapeach-5.8 {parallel mapeach tests} {
apply {{} {
mapeach a {} b {1 2 3} c {1 2} d {1 2 3 4} e {{1 2}} {
join [list $a $b $c $d $e] .
}
}}
} {{.1.1.1.1 2} .2.2.2. .3..3. ...4.}
test mapeach-5.9 {mapeach only sets vars if repeating loop} {
apply {{} {
set rgb {65535 0 0}
mapeach {r g b} [set rgb] {}
return "r=$r, g=$g, b=$b"
}}
} {r=65535, g=0, b=0}
test mapeach-5.10 {mapeach only supports local scalar variables} {
apply {{} {
mapeach {a(3)} {1 2 3 4} {set {a(3)}}
}}
} {1 2 3 4}
# "mapeach" with "continue" and "break" (compiled)
test mapeach-6.1 {continue tests} {
apply {{} {
mapeach i {a b c d} {
if {[string compare $i "b"] == 0} continue
set i
}
}}
} {a c d}
test mapeach-6.2 {continue tests} {
apply {{} {
list [mapeach i {a b c d} {
incr x
if {[string compare $i "b"] != 0} continue
set i
}] $x
}}
} {b 4}
test mapeach-6.3 {break tests} {
apply {{} {
list [mapeach i {a b c d} {
incr x
if {[string compare $i "c"] == 0} break
set i
}] $x
}}
} {{a b} 3}
# Check for bug similar to #406709
test mapeach-6.4 {break tests} {
apply {{} {
set a 1
mapeach b b {list [concat a; break]; incr a}
incr a
}}
} {2}
# ----- Special cases and bugs -------------------------------------------------
test mapeach-7.1 {compiled mapeach backward jump works correctly} {
catch {unset x}
array set x {0 zero 1 one 2 two 3 three}
lsort [apply {{arrayName} {
upvar 1 $arrayName a
mapeach member [array names a] {
list $member [set a($member)]
}
}} x]
} [lsort {{0 zero} {1 one} {2 two} {3 three}}]
test mapeach-7.2 {noncompiled mapeach and shared variable or value list objects that are converted to another type} {
catch {unset x}
mapeach {12.0} {a b c} {
set x 12.0
set x [expr $x + 1]
}
} {13.0 13.0 13.0}
# Test for incorrect "double evaluation" semantics
test mapeach-7.3 {delayed substitution of body} {
apply {{} {
set a 0
mapeach a [list 1 2 3] "
set x $a
"
set x
}}
} {0}
# Related to "foreach" test for [Bug 1189274]; crash on failure
test mapeach-7.4 {empty list handling} {
proc crash {} {
rename crash {}
set a "x y z"
set b ""
mapeach aa $a bb $b { set x "aa = $aa bb = $bb" }
}
crash
} {{aa = x bb = } {aa = y bb = } {aa = z bb = }}
# Related to [Bug 1671138]; infinite loop with empty var list in bytecompiled version
test mapeach-7.5 {compiled empty var list} {
proc foo {} {
mapeach {} x {
error "reached body"
}
}
list [catch { foo } msg] $msg
} {1 {foreach varlist is empty}}
test mapeach-7.6 {mapeach: related to "foreach" [Bug 1671087]} -setup {
proc demo {} {
set vals {1 2 3 4}
trace add variable x write {string length $vals ;# }
mapeach {x y} $vals {format $y}
}
} -body {
demo
} -cleanup {
rename demo {}
} -result {2 4}
# Huge lists must not overflow the bytecode interpreter (development bug)
test mapeach-7.7 {huge list non-compiled} {
set x [mapeach a [lrepeat 1000000 x] { set b y$a }]
list $b [llength $x] [string length $x]
} {yx 1000000 2999999}
test mapeach-7.8 {huge list compiled} {
set x [apply {{times} { mapeach a [lrepeat $times x] { set b y$a }}} 1000000]
list $b [llength $x] [string length $x]
} {yx 1000000 2999999}
test mapeach-7.9 {error then dereference loop var (dev bug)} {
catch { mapeach a 0 b {1 2 3} { error x } }
set a
} 0
test mapeach-7.9a {error then dereference loop var (dev bug)} {
catch { mapeach a 0 b {1 2 3} { incr a $b; error x } }
set a
} 1
# ----- Coroutines -------------------------------------------------------------
test mapeach-8.1 {mapeach non-compiled with coroutines} {
coroutine coro apply {{} {
set values [yield [info coroutine]]
eval mapeach i [list $values] {{ yield $i }}
}} ;# returns 'coro'
coro {a b c d e f} ;# -> a
coro 1 ;# -> b
coro 2 ;# -> c
coro 3 ;# -> d
coro 4 ;# -> e
coro 5 ;# -> f
list [coro 6] [info commands coro]
} {{1 2 3 4 5 6} {}}
test mapeach-8.2 {mapeach compiled with coroutines} {
coroutine coro apply {{} {
set values [yield [info coroutine]]
mapeach i $values { yield $i }
}} ;# returns 'coro'
coro {a b c d e f} ;# -> a
coro 1 ;# -> b
coro 2 ;# -> c
coro 3 ;# -> d
coro 4 ;# -> e
coro 5 ;# -> f
list [coro 6] [info commands coro]
} {{1 2 3 4 5 6} {}}
# cleanup
catch {unset a}
catch {unset x}
catch {rename foo {}}
::tcltest::cleanupTests
return
|