summaryrefslogtreecommitdiff
path: root/src/lib/container/iterable_test.go
diff options
context:
space:
mode:
authorDavid Symonds <dsymonds@golang.org>2009-04-08 21:50:40 -0700
committerDavid Symonds <dsymonds@golang.org>2009-04-08 21:50:40 -0700
commit2c1e6fa390276aea0f1961d4b64eb4678ab0dd33 (patch)
treef30d536eed86a95ea547855309aac5f86156857b /src/lib/container/iterable_test.go
parente048e6229e071df4d02de0a7047df226b31ad5cf (diff)
downloadgolang-2c1e6fa390276aea0f1961d4b64eb4678ab0dd33.tar.gz
Add new functions to the iterable package:
- Filter - Find - Partition R=rsc APPROVED=rsc DELTA=117 (92 added, 17 deleted, 8 changed) OCL=27135 CL=27240
Diffstat (limited to 'src/lib/container/iterable_test.go')
-rw-r--r--src/lib/container/iterable_test.go59
1 files changed, 50 insertions, 9 deletions
diff --git a/src/lib/container/iterable_test.go b/src/lib/container/iterable_test.go
index 9c7d29110..702ebe861 100644
--- a/src/lib/container/iterable_test.go
+++ b/src/lib/container/iterable_test.go
@@ -43,6 +43,17 @@ func addOne(n interface {}) interface {} {
return n.(int) + 1
}
+// A stream of the natural numbers: 0, 1, 2, 3, ...
+type integerStream struct {}
+func (i integerStream) Iter() <-chan interface {} {
+ ch := make(chan interface {});
+ go func() {
+ for i := 0; ; i++ {
+ ch <- i
+ }
+ }();
+ return ch
+}
func TestAll(t *testing.T) {
if !All(oneToFive, isPositive) {
@@ -53,7 +64,6 @@ func TestAll(t *testing.T) {
}
}
-
func TestAny(t *testing.T) {
if Any(oneToFive, isNegative) {
t.Error("Any(oneToFive, isNegative) == true")
@@ -63,16 +73,47 @@ func TestAny(t *testing.T) {
}
}
-
-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))
+func assertArraysAreEqual(t *testing.T, res []interface {}, expected []int) {
+ if len(res) != len(expected) {
+ t.Errorf("len(res) = %v, want %v", len(res), len(expected));
+ goto missing
}
- 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])
+ if v := res[i].(int); v != expected[i] {
+ t.Errorf("res[%v] = %v, want %v", i, v, expected[i]);
+ goto missing
}
}
+ return;
+missing:
+ t.Errorf("res = %v\nwant %v", res, expected);
+}
+
+func TestFilter(t *testing.T) {
+ ints := integerStream{};
+ moreInts := Filter(ints, isAbove3).Iter();
+ res := make([]interface {}, 3);
+ for i := 0; i < 3; i++ {
+ res[i] = <-moreInts;
+ }
+ assertArraysAreEqual(t, res, []int{ 4, 5, 6 })
+}
+
+func TestFind(t *testing.T) {
+ ints := integerStream{};
+ first := Find(ints, isAbove3);
+ if first.(int) != 4 {
+ t.Errorf("Find(ints, isAbove3) = %v, want 4", first)
+ }
+}
+
+func TestMap(t *testing.T) {
+ res := Data(Map(Map(oneToFive, doubler), addOne));
+ assertArraysAreEqual(t, res, []int{ 3, 5, 7, 9, 11 })
+}
+
+func TestPartition(t *testing.T) {
+ ti, fi := Partition(oneToFive, isEven);
+ assertArraysAreEqual(t, Data(ti), []int{ 2, 4 });
+ assertArraysAreEqual(t, Data(fi), []int{ 1, 3, 5 })
}