-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcycle_test.go
63 lines (48 loc) · 1.04 KB
/
cycle_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
package iterium
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestCycle(t *testing.T) {
cycle := Cycle(New(1, 2, 3))
assert.Exactly(t, true, cycle.IsInfinite())
var dump []int
for {
value, err := cycle.Next()
if len(dump) >= 9 {
assert.ErrorContains(t, err, "stop iteration")
break
}
dump = append(dump, value)
if len(dump) == 9 {
cycle.Close()
}
}
expected := []int{1, 2, 3, 1, 2, 3, 1, 2, 3}
assert.Exactly(t, expected, dump)
}
func TestCycleWithInfiniteIter(t *testing.T) {
countNumbers := Count(50, 1)
cycle := Cycle(countNumbers)
var dump []int
for {
value, err := cycle.Next()
if len(dump) >= 5 {
assert.ErrorContains(t, err, "stop iteration")
break
}
dump = append(dump, value)
if len(dump) == 5 {
cycle.Close()
}
}
expected := []int{50, 51, 52, 53, 54}
assert.Exactly(t, expected, dump)
}
func TestCycleEmptySlice(t *testing.T) {
repeat := Repeat(1, 5)
repeat.Close()
cycle := Cycle(repeat)
_, err := cycle.Next()
assert.Exactly(t, err, stopIterationErr)
}