summaryrefslogtreecommitdiffstats
path: root/tests/foreacha.test
diff options
context:
space:
mode:
Diffstat (limited to 'tests/foreacha.test')
-rw-r--r--tests/foreacha.test217
1 files changed, 217 insertions, 0 deletions
diff --git a/tests/foreacha.test b/tests/foreacha.test
new file mode 100644
index 0000000..09a90e4
--- /dev/null
+++ b/tests/foreacha.test
@@ -0,0 +1,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