-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccumulate_test.go
39 lines (32 loc) · 904 Bytes
/
accumulate_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
package iterium
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func add(a, b int) int {
return a + b
}
func mul(a, b int) int {
return a * b
}
func TestAccumulate(t *testing.T) {
accumulateAdd := Accumulate(New(1, 2, 3, 4, 5), add)
if slice, err := accumulateAdd.Slice(); assert.Nil(t, err) {
assert.Exactly(t, []int{1, 3, 6, 10, 15}, slice)
}
accumulateMul := Accumulate(New(1, 2, 3, 4, 5), mul)
if slice, err := accumulateMul.Slice(); assert.Nil(t, err) {
assert.Exactly(t, []int{1, 2, 6, 24, 120}, slice)
}
}
func mergeAcc(first, second string) string {
return fmt.Sprintf("%s-%s", first, second)
}
func TestAccumulateString(t *testing.T) {
data := New("A", "B", "C", "D")
accumulateString := Accumulate(data, mergeAcc)
if slice, err := accumulateString.Slice(); assert.Nil(t, err) {
assert.Exactly(t, []string{"A", "A-B", "A-B-C", "A-B-C-D"}, slice)
}
}