-
Notifications
You must be signed in to change notification settings - Fork 0
/
fold_test.go
62 lines (54 loc) · 1.16 KB
/
fold_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
package iterator
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReduce(t *testing.T) {
cases := []struct {
iter Iterator[int]
expected int
err error
}{
{Empty[int](), 0, ErrNoItems},
{Once(1), 1, nil},
{Limit[int](5)(Ascending(0, 1)), 10, nil},
}
for i := range cases {
sum, err := Reduce(cases[i].iter, func(_ int, item, sum int) (int, error) {
return sum + item, nil
})
require.ErrorIs(t, err, cases[i].err)
assert.Equal(t, cases[i].expected, sum)
}
}
func TestSum(t *testing.T) {
cases := []struct {
iter Iterator[int]
expected int
}{
{Empty[int](), 0},
{Once(1), 1},
{Limit[int](5)(Ascending(0, 1)), 10},
}
for i := range cases {
sum, err := Sum(cases[i].iter)
require.NoError(t, err)
assert.Equal(t, cases[i].expected, sum)
}
}
func TestString(t *testing.T) {
cases := []struct {
iter Iterator[int]
expected string
}{
{Empty[int](), ""},
{Once(1), "1"},
{Limit[int](5)(Ascending(0, 1)), "01234"},
}
for i := range cases {
str, err := String(cases[i].iter)
require.NoError(t, err)
assert.Equal(t, cases[i].expected, str)
}
}