Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/foreach.test @ 68

Last change on this file since 68 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 6.9 KB
Line 
1# Commands covered:  foreach, continue, break
2#
3# This file contains a collection of tests for one or more of the Tcl
4# built-in commands.  Sourcing this file into Tcl runs the tests and
5# generates output for errors.  No output means no errors were found.
6#
7# Copyright (c) 1991-1993 The Regents of the University of California.
8# Copyright (c) 1994-1997 Sun Microsystems, Inc.
9#
10# See the file "license.terms" for information on usage and redistribution
11# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12#
13# RCS: @(#) $Id: foreach.test,v 1.14 2008/03/14 17:43:25 dgp Exp $
14
15if {[lsearch [namespace children] ::tcltest] == -1} {
16    package require tcltest
17    namespace import -force ::tcltest::*
18}
19
20catch {unset a}
21catch {unset x}
22
23# Basic "foreach" operation.
24
25test foreach-1.1 {basic foreach tests} {
26    set a {}
27    foreach i {a b c d} {
28        set a [concat $a $i]
29    }
30    set a
31} {a b c d}
32test foreach-1.2 {basic foreach tests} {
33    set a {}
34    foreach i {a b {{c d} e} {123 {{x}}}} {
35        set a [concat $a $i]
36    }
37    set a
38} {a b {c d} e 123 {{x}}}
39test foreach-1.3 {basic foreach tests} {catch {foreach} msg} 1
40test foreach-1.4 {basic foreach tests} {
41    catch {foreach} msg
42    set msg
43} {wrong # args: should be "foreach varList list ?varList list ...? command"}
44test foreach-1.5 {basic foreach tests} {catch {foreach i} msg} 1
45test foreach-1.6 {basic foreach tests} {
46    catch {foreach i} msg
47    set msg
48} {wrong # args: should be "foreach varList list ?varList list ...? command"}
49test foreach-1.7 {basic foreach tests} {catch {foreach i j} msg} 1
50test foreach-1.8 {basic foreach tests} {
51    catch {foreach i j} msg
52    set msg
53} {wrong # args: should be "foreach varList list ?varList list ...? command"}
54test foreach-1.9 {basic foreach tests} {catch {foreach i j k l} msg} 1
55test foreach-1.10 {basic foreach tests} {
56    catch {foreach i j k l} msg
57    set msg
58} {wrong # args: should be "foreach varList list ?varList list ...? command"}
59test foreach-1.11 {basic foreach tests} {
60    set a {}
61    foreach i {} {
62        set a [concat $a $i]
63    }
64    set a
65} {}
66test foreach-1.12 {foreach errors} {
67    list [catch {foreach {{a}{b}} {1 2 3} {}} msg] $msg
68} {1 {list element in braces followed by "{b}" instead of space}}
69test foreach-1.13 {foreach errors} {
70    list [catch {foreach a {{1 2}3} {}} msg] $msg
71} {1 {list element in braces followed by "3" instead of space}}
72catch {unset a}
73test foreach-1.14 {foreach errors} {
74    catch {unset a}
75    set a(0) 44
76    list [catch {foreach a {1 2 3} {}} msg o] $msg $::errorInfo
77} {1 {can't set "a": variable is array} {can't set "a": variable is array
78    (setting foreach loop variable "a")
79    invoked from within
80"foreach a {1 2 3} {}"}}
81test foreach-1.15 {foreach errors} {
82    list [catch {foreach {} {} {}} msg] $msg
83} {1 {foreach varlist is empty}}
84catch {unset a}
85
86test foreach-2.1 {parallel foreach tests} {
87    set x {}
88    foreach {a b} {1 2 3 4} {
89        append x $b $a
90    }
91    set x
92} {2143}
93test foreach-2.2 {parallel foreach tests} {
94    set x {}
95    foreach {a b} {1 2 3 4 5} {
96        append x $b $a
97    }
98    set x
99} {21435}
100test foreach-2.3 {parallel foreach tests} {
101    set x {}
102    foreach a {1 2 3} b {4 5 6} {
103        append x $b $a
104    }
105    set x
106} {415263}
107test foreach-2.4 {parallel foreach tests} {
108    set x {}
109    foreach a {1 2 3} b {4 5 6 7 8} {
110        append x $b $a
111    }
112    set x
113} {41526378}
114test foreach-2.5 {parallel foreach tests} {
115    set x {}
116    foreach {a b} {a b A B aa bb} c {c C cc CC} {
117        append x $a $b $c
118    }
119    set x
120} {abcABCaabbccCC}
121test foreach-2.6 {parallel foreach tests} {
122    set x {}
123    foreach a {1 2 3} b {1 2 3} c {1 2 3} d {1 2 3} e {1 2 3} {
124        append x $a $b $c $d $e
125    }
126    set x
127} {111112222233333}
128test foreach-2.7 {parallel foreach tests} {
129    set x {}
130    foreach a {} b {1 2 3} c {1 2} d {1 2 3 4} e {{1 2}} {
131        append x $a $b $c $d $e
132    }
133    set x
134} {1111 2222334}
135test foreach-2.8 {foreach only sets vars if repeating loop} {
136    proc foo {} {
137        set rgb {65535 0 0}
138        foreach {r g b} [set rgb] {}
139        return "r=$r, g=$g, b=$b"
140    }
141    foo
142} {r=65535, g=0, b=0}
143test foreach-2.9 {foreach only supports local scalar variables} {
144    proc foo {} {
145        set x {}
146        foreach {a(3)} {1 2 3 4} {lappend x [set {a(3)}]}
147        set x
148    }
149    foo
150} {1 2 3 4}
151
152test foreach-3.1 {compiled foreach backward jump works correctly} {
153    catch {unset x}
154    proc foo {arrayName} {
155        upvar 1 $arrayName a
156        set l {}
157        foreach member [array names a] {
158            lappend l [list $member [set a($member)]]
159        }
160        return $l
161    }
162    array set x {0 zero 1 one 2 two 3 three}
163    lsort [foo x]
164} [lsort {{0 zero} {1 one} {2 two} {3 three}}]
165
166test foreach-4.1 {noncompiled foreach and shared variable or value list objects that are converted to another type} {
167    catch {unset x}
168    foreach {12.0} {a b c} {
169        set x 12.0
170        set x [expr $x + 1]
171    }
172    set x
173} 13.0
174
175# Check "continue".
176
177test foreach-5.1 {continue tests} {catch continue} 4
178test foreach-5.2 {continue tests} {
179    set a {}
180    foreach i {a b c d} {
181        if {[string compare $i "b"] == 0} continue
182        set a [concat $a $i]
183    }
184    set a
185} {a c d}
186test foreach-5.3 {continue tests} {
187    set a {}
188    foreach i {a b c d} {
189        if {[string compare $i "b"] != 0} continue
190        set a [concat $a $i]
191    }
192    set a
193} {b}
194test foreach-5.4 {continue tests} {catch {continue foo} msg} 1
195test foreach-5.5 {continue tests} {
196    catch {continue foo} msg
197    set msg
198} {wrong # args: should be "continue"}
199
200# Check "break".
201
202test foreach-6.1 {break tests} {catch break} 3
203test foreach-6.2 {break tests} {
204    set a {}
205    foreach i {a b c d} {
206        if {[string compare $i "c"] == 0} break
207        set a [concat $a $i]
208    }
209    set a
210} {a b}
211test foreach-6.3 {break tests} {catch {break foo} msg} 1
212test foreach-6.4 {break tests} {
213    catch {break foo} msg
214    set msg
215} {wrong # args: should be "break"}
216# Check for bug #406709
217test foreach-6.5 {break tests} {
218    proc a {} {
219        set a 1
220        foreach b b {list [concat a; break]; incr a}
221        incr a
222    }
223    a
224} {2}
225
226# Test for incorrect "double evaluation" semantics
227test foreach-7.1 {delayed substitution of body} {
228    proc foo {} {
229       set a 0
230       foreach a [list 1 2 3] "
231           set x $a
232       "
233       set x
234    }
235    foo
236} {0}
237
238# Test for [Bug 1189274]; crash on failure
239test foreach-8.1 {empty list handling} {
240    proc crash {} {
241        rename crash {}
242        set a "x y z"
243        set b ""
244        foreach aa $a bb $b { set x "aa = $aa bb = $bb" }
245    }
246    crash
247} {}
248
249# [Bug 1671138]; infinite loop with empty var list in bytecompiled version
250test foreach-9.1 {compiled empty var list} {
251    proc foo {} {
252        foreach {} x {
253            error "reached body"
254        }
255    }
256    list [catch { foo } msg] $msg
257} {1 {foreach varlist is empty}}
258
259test foreach-10.1 {foreach: [Bug 1671087]} -setup {
260    proc demo {} {
261        set vals {1 2 3 4}
262        trace add variable x write {string length $vals ;# }
263        foreach {x y} $vals {format $y}
264    }
265} -body {
266    demo
267} -cleanup {
268    rename demo {}
269} -result {}
270
271# cleanup
272catch {unset a}
273catch {unset x}
274::tcltest::cleanupTests
275return
Note: See TracBrowser for help on using the repository browser.