summaryrefslogtreecommitdiff
path: root/src/lib/container/iterable_test.go
diff options
context:
space:
mode:
authorDavid Symonds <dsymonds@golang.org>2009-04-05 22:40:40 -0700
committerDavid Symonds <dsymonds@golang.org>2009-04-05 22:40:40 -0700
commit459936e9bf8e03e26b5de91356588dc2020cca7f (patch)
treef2092936c6bc81b30463c75166f9151ff4b947b8 /src/lib/container/iterable_test.go
parenteaa9156ff73f91f666aa6df5cdd85269a556f05b (diff)
downloadgolang-459936e9bf8e03e26b5de91356588dc2020cca7f.tar.gz
Add an Iterable package with handy functions like All, Any and Map.
Add a Data method to vector.Vector. R=r,rsc APPROVED=rsc DELTA=173 (170 added, 0 deleted, 3 changed) OCL=26980 CL=27098
Diffstat (limited to 'src/lib/container/iterable_test.go')
-rw-r--r--src/lib/container/iterable_test.go78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/lib/container/iterable_test.go b/src/lib/container/iterable_test.go
new file mode 100644
index 000000000..9c7d29110
--- /dev/null
+++ b/src/lib/container/iterable_test.go
@@ -0,0 +1,78 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package iterable
+
+import (
+ "iterable";
+ "testing";
+)
+
+type IntArray []int;
+
+func (arr IntArray) Iter() <-chan interface {} {
+ ch := make(chan interface {});
+ go func() {
+ for i, x := range arr {
+ ch <- x
+ }
+ close(ch)
+ }();
+ return ch
+}
+
+var oneToFive IntArray = []int{ 1, 2, 3, 4, 5 };
+
+func isNegative(n interface {}) bool {
+ return n.(int) < 0
+}
+func isPositive(n interface {}) bool {
+ return n.(int) > 0
+}
+func isAbove3(n interface {}) bool {
+ return n.(int) > 3
+}
+func isEven(n interface {}) bool {
+ return n.(int) % 2 == 0
+}
+func doubler(n interface {}) interface {} {
+ return n.(int) * 2
+}
+func addOne(n interface {}) interface {} {
+ return n.(int) + 1
+}
+
+
+func TestAll(t *testing.T) {
+ if !All(oneToFive, isPositive) {
+ t.Error("All(oneToFive, isPositive) == false")
+ }
+ if All(oneToFive, isAbove3) {
+ t.Error("All(oneToFive, isAbove3) == true")
+ }
+}
+
+
+func TestAny(t *testing.T) {
+ if Any(oneToFive, isNegative) {
+ t.Error("Any(oneToFive, isNegative) == true")
+ }
+ if !Any(oneToFive, isEven) {
+ t.Error("Any(oneToFive, isEven) == false")
+ }
+}
+
+
+func TestMap(t *testing.T) {
+ res := Data(Map(Map(oneToFive, doubler), addOne));
+ if len(res) != len(oneToFive) {
+ t.Fatal("len(res) = %v, want %v", len(res), len(oneToFive))
+ }
+ expected := []int{ 3, 5, 7, 9, 11 };
+ for i := range res {
+ if res[i].(int) != expected[i] {
+ t.Errorf("res[%v] = %v, want %v", i, res[i], expected[i])
+ }
+ }
+}