summaryrefslogtreecommitdiffstats
path: root/tests/foreacha.test
blob: 09a90e47cef4b4dc928f14fdd9c9cf4c7df3e2a7 (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
# 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 "foreacha" operation (non-compiled) ------------------------------

test foreacha-1.1 {basic foreacha tests (non-compiled) - foldl/reduce with initial value} {
    set x {}
    set c [foreacha a 0 b {1 2 3 4} { lappend x $a ; incr a $b }]
    list $a $b $c $x
} {10 4 10 {0 1 3 6}}

test foreacha-1.2 {basic foreacha tests (non-compiled) - foldl/reduce without initial value} {
    set x {}
    set c [foreacha {a b} {1 2 3 4 5 6} { lappend x $a ; incr a $b }]
    list $a $b $c $x
} {21 6 21 {1 3 6 10 15}}

test foreacha-1.3 {basic foreacha tests (non-compiled) - filter} {
    foreacha a {} b {1 2 3 4 5 6} { if { ($b % 2)==0 } { lappend a $b } }
} {2 4 6}

test foreacha-1.3.1 {basic foreacha tests (non-compiled) - filter (via continue)} {
    foreacha a {} b {1 2 3 4 5 6} { if { ($b % 2)==0 } continue; lappend a $b }
} {1 3 5}

test foreacha-1.4 {basic foreacha tests (non-compiled) - map} {
    foreacha a {} b {1 2 3 4 5 6} { lappend a [lrepeat $b $b] }
} {1 {2 2} {3 3 3} {4 4 4 4} {5 5 5 5 5} {6 6 6 6 6 6}}

test foreacha-1.5 {basic foreacha tests (non-compiled) - prefix (via break)} {
    foreacha a {} b {1 2 3 4 5 6} { if { $b > 4 } break; lappend a $b }
} {1 2 3 4}

test foreacha-1.6 {basic foreacha tests (non-compiled) - accumulator doesn't iterate} {
    set x {}
    set b [foreacha a {1 2 3 4} { lappend x $a }]
    list $a $b $x
} {1 1 1}

test foreacha-1.7 {basic foreacha tests (non-compiled) - accumulator doesn't iterate} {
    set x {}
    set c [foreacha a {1 2 3 4} b 0 { lappend x $a $b ; append a $b ; append b $a }]
    list $a $b $c $x
} {10 010 10 {1 0}}

test foreacha-1.8 {basic foreacha tests (non-compiled) - huge list} {
    foreacha {a b} [lsearch -all [lrepeat 1000000 x] x] { incr a $b }
} 499999500000

test foreacha-1.9 {basic foreacha tests (non-compiled) - spaghetti} {
    foreacha {a b} [foreacha a {} {b c} [lsearch -all [lrepeat 1000 x] x] {
	lappend a [expr { $b * $c }]
    }] {
	incr a $b
    }
} 166416500

test foreacha-1.9.1 {basic foreacha tests (non-compiled) - spaghetti with mapeach} {
    foreacha {a b} [mapeach {b c} [lsearch -all [lrepeat 1000 x] x] {
	expr { $b * $c }
    }] {
	incr a $b
    }
} 166416500

test foreacha-1.10 {basic foreacha tests (non-compiled) - nested} {
    foreacha {a b} [lsearch -all [lrepeat 1000 x] x] {
	incr a [foreacha c 10 d [lrepeat $b $b] { incr c $b }]
    }
} 332843490

test foreacha-1.10.1 {basic foreacha tests (non-compiled) - nested with loop var collision} {
    foreacha {a b} [lsearch -all [lrepeat 1000 x] x] {
	foreacha a 10 b [lrepeat $b $b] { incr a $b }
    }
} 998011

test foreacha-1.10.2 {basic foreacha tests (non-compiled) - nested, inner non-compiled} {
    foreacha {a b} [lsearch -all [lrepeat 1000 x] x] {
	incr a [eval foreacha c 10 d [list [lrepeat $b $b] { incr c $b }]]
    }
} 332843490


# ----- Basic "foreacha" operation (compiled) ----------------------------------

test foreacha-2.1 {basic foreacha tests (compiled) - foldl/reduce with initial value} {
    apply {{} {
	set x {}
	set c [foreacha a 0 b {1 2 3 4} { lappend x $a ; incr a $b }]
	list $a $b $c $x
    }}
} {10 4 10 {0 1 3 6}}

test foreacha-2.2 {basic foreacha tests (compiled) - foldl/reduce without initial value} {
    apply {{} {
	set x {}
	set c [foreacha {a b} {1 2 3 4 5 6} { lappend x $a ; incr a $b }]
	list $a $b $c $x
    }}
} {21 6 21 {1 3 6 10 15}}

test foreacha-2.3 {basic foreacha tests (compiled) - filter} {
    apply {{} {
	foreacha a {} b {1 2 3 4 5 6} { if { ($b % 2)==0 } { lappend a $b } }
    }}
} {2 4 6}

test foreacha-2.3.1 {basic foreacha tests (non-compiled) - filter (via continue)} {
    apply {{} {
	foreacha a {} b {1 2 3 4 5 6} { if { ($b % 2)==0 } continue; lappend a $b }
    }}
} {1 3 5}

test foreacha-2.4 {basic foreacha tests (compiled) - map} {
    apply {{} {
	foreacha a {} b {1 2 3 4 5 6} { lappend a [lrepeat $b $b] }
    }}
} {1 {2 2} {3 3 3} {4 4 4 4} {5 5 5 5 5} {6 6 6 6 6 6}}

test foreacha-2.5 {basic foreacha tests (non-compiled) - prefix (via break)} {
    apply {{} {
	foreacha a {} b {1 2 3 4 5 6} { if { $b > 4 } break; lappend a $b }
    }}
} {1 2 3 4}

test foreacha-2.6 {basic foreacha tests (compiled) - accumulator doesn't iterate} {
    apply {{} {
	set x {}
	set b [foreacha a {1 2 3 4} { lappend x $a }]
	list $a $b $x
    }}
} {1 1 1}

test foreacha-2.7 {basic foreacha tests (compiled) - accumulator doesn't iterate} {
    apply {{} {
	set x {}
	set c [foreacha a {1 2 3 4} b 0 { lappend x $a $b ; append a $b ; append b $a }]
	list $a $b $c $x
    }}
} {10 010 10 {1 0}}

test foreacha-2.8 {basic foreacha tests (compiled) - huge list} {
    apply {{} {
	foreacha {a b} [lsearch -all [lrepeat 1000000 x] x] { incr a $b }
    }}
} 499999500000

test foreacha-2.9 {basic foreacha tests (compiled) - spaghetti} {
    apply {{} {
	foreacha {a b} [foreacha a {} {b c} [lsearch -all [lrepeat 1000 x] x] {
	    lappend a [expr { $b * $c }]
	}] {
	    incr a $b
	}
    }}
} 166416500

test foreacha-2.9.1 {basic foreacha tests (compiled) - spaghetti with mapeach} {
    apply {{} {
	foreacha {a b} [mapeach {b c} [lsearch -all [lrepeat 1000 x] x] {
	    expr { $b * $c }
	}] {
	    incr a $b
	}
    }}
} 166416500

test foreacha-2.10 {basic foreacha tests (compiled) - nested} {
    apply {{} {
	foreacha {a b} [lsearch -all [lrepeat 1000 x] x] {
	    incr a [foreacha c 10 d [lrepeat $b $b] { incr c $b }]
	}
    }}
} 332843490

test foreacha-2.10.1 {basic foreacha tests (compiled) - nested with loop var collision} {
    apply {{} {
	foreacha {a b} [lsearch -all [lrepeat 1000 x] x] {
	    foreacha a 10 b [lrepeat $b $b] { incr a $b }
	}
    }}
} 998011

test foreacha-2.10.2 {basic foreacha tests (compiled) - nested, inner non-compiled} {
    apply {{} {
	foreacha {a b} [lsearch -all [lrepeat 1000 x] x] {
	    incr a [eval foreacha c 10 d [list [lrepeat $b $b] { incr c $b }]]
	}
    }}
} 332843490



# cleanup
catch {unset a}
catch {unset x}
catch {rename foo {}}
::tcltest::cleanupTests
return