diff options
author | David Symonds <dsymonds@golang.org> | 2009-04-20 18:13:14 -0700 |
---|---|---|
committer | David Symonds <dsymonds@golang.org> | 2009-04-20 18:13:14 -0700 |
commit | a0761e334aba87375c842ace499102c4622ecee9 (patch) | |
tree | 17208a76059f814a5b3a2b6db9a5a0836af13257 /src/lib/container/iterable_test.go | |
parent | 3d8743c037811e3957aacd05e7573938b8fa0297 (diff) | |
download | golang-a0761e334aba87375c842ace499102c4622ecee9.tar.gz |
Move iterable package to usr/dsymonds/.
R=r
APPROVED=r
DELTA=598 (330 added, 266 deleted, 2 changed)
OCL=27627
CL=27649
Diffstat (limited to 'src/lib/container/iterable_test.go')
-rw-r--r-- | src/lib/container/iterable_test.go | 129 |
1 files changed, 0 insertions, 129 deletions
diff --git a/src/lib/container/iterable_test.go b/src/lib/container/iterable_test.go deleted file mode 100644 index ceb1de6e4..000000000 --- a/src/lib/container/iterable_test.go +++ /dev/null @@ -1,129 +0,0 @@ -// 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 ( - "container/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 adder(acc interface {}, n interface {}) interface {} { - return acc.(int) + n.(int) -} - -// 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) { - 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 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 - } - for i := range res { - 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 TestInject(t *testing.T) { - res := Inject(oneToFive, 0, adder); - if res.(int) != 15 { - t.Errorf("Inject(oneToFive, 0, adder) = %v, want 15", res) - } -} - -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 }) -} |