-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator_test.go
104 lines (99 loc) · 2.43 KB
/
iterator_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package stream
import (
"fmt"
"reflect"
"testing"
)
func TestIterators(t *testing.T) {
for _, tc := range []struct {
iterator iterator[string]
create func() iterator[string]
limit int
expect []string
}{
{
iterator: sliceIterator[string]([]string{"a", "b", "c"}),
limit: -1,
expect: []string{"begin", "accept(a)", "accept(b)", "accept(c)", "end"},
},
{
iterator: sliceIterator[string]([]string{"a", "b", "c"}),
limit: 0,
expect: []string{"begin", "end"},
},
{
iterator: sliceIterator[string]([]string{"a", "b", "c"}),
limit: 1,
expect: []string{"begin", "accept(a)", "end"},
},
{
iterator: generatorIterator[string](func() string { return "a" }),
limit: 0,
expect: []string{"begin", "end"},
},
{
iterator: generatorIterator[string](func() string { return "a" }),
limit: 3,
expect: []string{"begin", "accept(a)", "accept(a)", "accept(a)", "end"},
},
{
iterator: &seedIterator[string]{"a", func(s string) string { return s + "b" }},
limit: 0,
expect: []string{"begin", "end"},
},
{
iterator: &seedIterator[string]{"a", func(s string) string { return s + "b" }},
limit: 3,
expect: []string{"begin", "accept(a)", "accept(ab)", "accept(abb)", "end"},
},
{
iterator: &whileIterator[string]{func() bool { return false }, func() string { return "a" }},
limit: -1,
expect: []string{"begin", "end"},
},
{
create: func() iterator[string] {
counter := 3
return &whileIterator[string]{
func() bool {
return counter > 0
}, func() (s string) {
s = fmt.Sprintf("%d", counter)
counter--
return
},
}
},
limit: -1,
expect: []string{"begin", "accept(3)", "accept(2)", "accept(1)", "end"},
},
{
create: func() iterator[string] {
counter := 3
return &whileIterator[string]{
func() bool {
return counter > 0
}, func() (s string) {
s = fmt.Sprintf("%d", counter)
counter--
return
},
}
},
limit: 2,
expect: []string{"begin", "accept(3)", "accept(2)", "end"},
},
} {
t.Run(fmt.Sprintf("%#v %d", tc.iterator, tc.limit), func(t *testing.T) {
it := tc.iterator
if tc.create != nil {
it = tc.create()
}
s := &testSink[string]{limit: tc.limit}
it.copyInto(s)
if !reflect.DeepEqual(tc.expect, s.log) {
t.Errorf("wrong result, expected: %v, got: %v", tc.expect, s.log)
}
})
}
}