-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoldright_test.go
66 lines (61 loc) · 1.61 KB
/
foldright_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
package slices_test
import (
"testing"
"github.com/dosadczuk/go-slices"
"github.com/google/go-cmp/cmp"
)
func TestFoldRight(t *testing.T) {
tt := map[string]struct {
// input
values []string
initial string
operation func(string, string) string
// assert
want string
}{
"empty slice": {
values: nil, // zero value
initial: "", // zero value
operation: func(_ string, _ string) string { return "" },
want: "",
},
"empty slice and initial value": {
values: nil, // zero value
initial: "order:",
operation: func(_ string, _ string) string { return "" },
want: "order:",
},
"slice with value": {
values: []string{"1"},
initial: "",
operation: func(acc string, val string) string { return acc + val },
want: "1",
},
"slice with value and initial value": {
values: []string{"1"},
initial: "order:",
operation: func(acc string, val string) string { return acc + val },
want: "order:1",
},
"slice with values": {
values: []string{"1", "2", "3", "4", "5"},
initial: "",
operation: func(acc string, val string) string { return acc + val },
want: "54321",
},
"slice with values and initial value": {
values: []string{"1", "2", "3", "4", "5"},
initial: "order:",
operation: func(acc string, val string) string { return acc + val },
want: "order:54321",
},
}
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
have := slices.FoldRight(tc.values, tc.initial, tc.operation)
if !cmp.Equal(tc.want, have) {
t.Error(cmp.Diff(tc.want, have))
}
})
}
}