-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchain_test.go
59 lines (52 loc) · 1.87 KB
/
chain_test.go
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
package itertools
import (
. "github.com/go-playground/assert/v2"
optionext "github.com/go-playground/pkg/v5/values/option"
"testing"
)
func TestChain(t *testing.T) {
// Test sort
slice := []int{0, 1, 2, 3}
iterChain := WrapSlice(slice).Iter().Chain(WrapSlice(slice).IntoIter())
Equal(t, iterChain.Next(), optionext.Some(0))
Equal(t, iterChain.Next(), optionext.Some(1))
Equal(t, iterChain.Next(), optionext.Some(2))
Equal(t, iterChain.Next(), optionext.Some(3))
Equal(t, iterChain.Next(), optionext.Some(0))
Equal(t, iterChain.Next(), optionext.Some(1))
Equal(t, iterChain.Next(), optionext.Some(2))
Equal(t, iterChain.Next(), optionext.Some(3))
Equal(t, iterChain.Next(), optionext.None[int]())
iterChain = Chain[int](WrapSlice(slice).IntoIter(), WrapSlice(slice).IntoIter()).Iter()
Equal(t, iterChain.Next(), optionext.Some(0))
Equal(t, iterChain.Next(), optionext.Some(1))
Equal(t, iterChain.Next(), optionext.Some(2))
Equal(t, iterChain.Next(), optionext.Some(3))
Equal(t, iterChain.Next(), optionext.Some(0))
Equal(t, iterChain.Next(), optionext.Some(1))
Equal(t, iterChain.Next(), optionext.Some(2))
Equal(t, iterChain.Next(), optionext.Some(3))
Equal(t, iterChain.Next(), optionext.None[int]())
// Test same Iterator[T] but different underlying iterator types
fi := &fakeIterator{max: 3}
si := WrapSlice(slice).IntoIter()
iter := Chain[int](fi, si)
Equal(t, iter.Next(), optionext.Some(2))
Equal(t, iter.Next(), optionext.Some(1))
Equal(t, iter.Next(), optionext.Some(0))
Equal(t, iter.Next(), optionext.Some(0))
Equal(t, iter.Next(), optionext.Some(1))
Equal(t, iter.Next(), optionext.Some(2))
Equal(t, iter.Next(), optionext.Some(3))
Equal(t, iter.Next(), optionext.None[int]())
}
type fakeIterator struct {
max int
}
func (f *fakeIterator) Next() optionext.Option[int] {
f.max--
if f.max < 0 {
return optionext.None[int]()
}
return optionext.Some(f.max)
}