-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeat_test.go
More file actions
104 lines (93 loc) · 2.8 KB
/
Copy pathrepeat_test.go
File metadata and controls
104 lines (93 loc) · 2.8 KB
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 iterator
import (
"testing"
)
func TestRepeat(t *testing.T) {
cases := []struct {
iter Iterator[int]
from, to int
times int
expected []int
}{
{Range(1, 4, 1), 0, 1, -1, []int{1, 2, 3, 4}},
{Range(1, 4, 1), 0, 1, 2, []int{1, 1, 1, 2, 3, 4}},
{Range(1, 4, 1), 3, 4, 2, []int{1, 2, 3, 4, 4, 4}},
{Range(1, 4, 1), 1, 2, 1, []int{1, 2, 2, 3, 4}},
{Range(1, 4, 1), 1, 2, 2, []int{1, 2, 2, 2, 3, 4}},
{Range(1, 4, 1), 1, 3, 2, []int{1, 2, 3, 2, 3, 2, 3, 4}},
{Range(1, 4, 1), 0, 4, 1, []int{1, 2, 3, 4, 1, 2, 3, 4}},
{Range(1, 4, 1), 1, 1, 2, []int{1, 2, 3, 4}},
{Range(1, 4, 1), 0, 1, 0, []int{1, 2, 3, 4}},
{Range(1, 4, 1), 1, 0, 1, []int{1, 1, 2, 3, 4}},
{Range(1, 4, 1), 0, 10, 2, []int{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}},
{Once(1), 0, 1, 1, []int{1, 1}},
{Once(1), 0, 10, 1, []int{1, 1}},
{Empty[int](), 0, 1, 1, []int{}},
}
for i := range cases {
checkIteratorEqual(t, Repeat[int](cases[i].from, cases[i].to, cases[i].times)(cases[i].iter), cases[i].expected)
}
}
func TestCycle(t *testing.T) {
cases := []struct {
iter Iterator[int]
times int
expected []int
}{
{Range(1, 4, 1), 1, []int{1, 2, 3, 4, 1, 2, 3, 4}},
{Range(1, 4, 1), 2, []int{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}},
{Once(1), 3, []int{1, 1, 1, 1}},
{Empty[int](), 4, []int{}},
}
for i := range cases {
checkIteratorEqual(t, Cycle[int](cases[i].times)(cases[i].iter), cases[i].expected)
}
}
func TestBoomerang(t *testing.T) {
cases := []struct {
iter Iterator[int]
times int
expected []int
}{
{Range(1, 4, 1), 1, []int{1, 2, 3, 4, 4, 3, 2, 1}},
{Range(1, 4, 1), 2, []int{1, 2, 3, 4, 4, 3, 2, 1, 1, 2, 3, 4}},
{Once(1), 3, []int{1, 1, 1, 1}},
{Empty[int](), 4, []int{}},
}
for i := range cases {
checkIteratorEqual(t, Boomerang[int](cases[i].times)(cases[i].iter), cases[i].expected)
}
}
func TestEcho(t *testing.T) {
cases := []struct {
iter Iterator[int]
times int
expected []int
}{
{Range(1, 4, 1), -1, []int{1, 2, 3, 4}},
{Range(1, 4, 1), 0, []int{1, 2, 3, 4}},
{Range(1, 4, 1), 1, []int{1, 1, 2, 2, 3, 3, 4, 4}},
{Range(1, 4, 1), 3, []int{1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4}},
{Once(1), 0, []int{1}},
{Once(1), 3, []int{1, 1, 1, 1}},
{Empty[int](), 0, []int{}},
{Empty[int](), 10, []int{}},
}
for i := range cases {
checkIteratorEqual(t, Echo[int](cases[i].times)(cases[i].iter), cases[i].expected)
}
}
func TestEchoFunc(t *testing.T) {
cases := []struct {
iter Iterator[int]
expected []int
}{
{Range(0, 4, 1), []int{0, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4}},
{Range(1, 4, 1), []int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4}},
{Once(1), []int{1}},
{Empty[int](), []int{}},
}
for i := range cases {
checkIteratorEqual(t, EchoFunc(func(_ int, i int) (int, error) { return i - 1, nil })(cases[i].iter), cases[i].expected)
}
}