-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs_test.go
122 lines (99 loc) · 2.47 KB
/
funcs_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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package collfunc_test
import (
"fmt"
"testing"
"github.com/moshenahmias/collfunc"
)
func TestCompareEqual(t *testing.T) {
first := []int{0, 1, 2, 3}
second := []int{0, 1, 2, 3}
cmp := collfunc.Compare[int](first, second)
if cmp != 0 {
t.Fatal("unexpected result", cmp)
}
}
func TestCompareFirstGreater(t *testing.T) {
first := []int{0, 1, 3, 4}
second := []int{0, 1, 2, 3}
cmp := collfunc.Compare[int](first, second)
if cmp >= 0 {
t.Fatal("unexpected result", cmp)
}
}
func TestCompareSecondGreater(t *testing.T) {
first := []int{0, 1, 2, 3}
second := []int{0, 1, 3, 4}
cmp := collfunc.Compare[int](first, second)
if cmp <= 0 {
t.Fatal("unexpected result", cmp)
}
}
func TestZip(t *testing.T) {
first := []int{0, 2, 4, 6}
second := []int{1, 3, 5, 7}
if collfunc.Compare[int](collfunc.Zip[int](first, second), []int{0, 1, 2, 3, 4, 5, 6, 7}) != 0 {
t.FailNow()
}
}
func TestConcat(t *testing.T) {
first := []int{0, 1, 2, 3}
second := []int{4, 5, 6, 7}
if collfunc.Compare[int](collfunc.Concat[int]([]int{}, first, []int{}, second, []int{}), []int{0, 1, 2, 3, 4, 5, 6, 7}) != 0 {
t.FailNow()
}
}
func TestIn(t *testing.T) {
if !collfunc.In(2, []int{0, 1, 2, 3}) {
t.FailNow()
}
if collfunc.In(6, []int{0, 1, 2, 3}) {
t.FailNow()
}
}
func TestFilter(t *testing.T) {
if collfunc.Compare[int](collfunc.Filter([]int{0, 1, 2, 3, 4, 5, 6, 7}, func(i int) bool { return i%2 == 0 }), []int{0, 2, 4, 6}) != 0 {
t.FailNow()
}
}
func TestMap(t *testing.T) {
if collfunc.Compare[int](collfunc.Map([]int{0, 1, 2, 3, 4, 5, 6, 7}, func(i int) int { return i + 1 }), []int{1, 2, 3, 4, 5, 6, 7, 8}) != 0 {
t.FailNow()
}
}
func TestEnumerate(t *testing.T) {
iter := collfunc.Enumerate[int]([]int{0, 1, 2, 3, 4, 5, 6, 7})
for i, v, ok := iter(); ok; i, v, ok = iter() {
if i != v {
t.FailNow()
}
}
}
func TestSink(t *testing.T) {
if collfunc.Compare[string](
collfunc.Sink(func(values ...int) string { return fmt.Sprint(values[0] + values[1]) },
[]int{0, 1, 2},
[]int{3, 4, 5}),
[]string{"3", "5", "7"}) != 0 {
t.FailNow()
}
}
func TestAccumulate(t *testing.T) {
if collfunc.Accumulate([]int{0, 1, 2}, func(cur, mem int) int { return cur + mem }) != 3 {
t.FailNow()
}
}
func TestOne(t *testing.T) {
if collfunc.One[int]([]int{3, 2, 1}) != 3 {
t.FailNow()
}
}
func TestOneEndOfIteration(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.FailNow()
}
}()
if collfunc.One[int](collfunc.Empty[int]()) != 3 {
t.FailNow()
}
}