forked from samber/lo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice_test.go
161 lines (119 loc) · 3.36 KB
/
slice_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package lo
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFilter(t *testing.T) {
is := assert.New(t)
r1 := Filter[int]([]int{1, 2, 3, 4}, func(x int, _ int) bool {
return x%2 == 0
})
is.Equal(r1, []int{2, 4})
r2 := Filter[string]([]string{"", "foo", "", "bar", ""}, func(x string, _ int) bool {
return len(x) > 0
})
is.Equal(r2, []string{"foo", "bar"})
}
func TestMap(t *testing.T) {
is := assert.New(t)
result1 := Map[int, string]([]int{1, 2, 3, 4}, func(x int, _ int) string {
return "Hello"
})
result2 := Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
return strconv.FormatInt(x, 10)
})
is.Equal(len(result1), 4)
is.Equal(len(result2), 4)
is.Equal(result1, []string{"Hello", "Hello", "Hello", "Hello"})
is.Equal(result2, []string{"1", "2", "3", "4"})
}
func TestReduce(t *testing.T) {
is := assert.New(t)
result1 := Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int {
return agg + item
}, 0)
result2 := Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int {
return agg + item
}, 10)
is.Equal(result1, 10)
is.Equal(result2, 20)
}
func TestUniq(t *testing.T) {
is := assert.New(t)
result1 := Uniq[int]([]int{1, 2, 2, 1})
is.Equal(len(result1), 2)
is.Equal(result1, []int{1, 2})
}
func TestUniqBy(t *testing.T) {
is := assert.New(t)
result1 := UniqBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
return i % 3
})
is.Equal(len(result1), 3)
is.Equal(result1, []int{0, 1, 2})
}
func TestGroupBy(t *testing.T) {
is := assert.New(t)
result1 := GroupBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
return i % 3
})
is.Equal(len(result1), 3)
is.Equal(result1, map[int][]int{
0: []int{0, 3},
1: []int{1, 4},
2: []int{2, 5},
})
}
func TestChunk(t *testing.T) {
is := assert.New(t)
result1 := Chunk[int]([]int{0, 1, 2, 3, 4, 5}, 2)
result2 := Chunk[int]([]int{0, 1, 2, 3, 4, 5, 6}, 2)
result3 := Chunk[int]([]int{}, 2)
result4 := Chunk[int]([]int{0}, 2)
is.Equal(result1, [][]int{{0, 1}, {2, 3}, {4, 5}})
is.Equal(result2, [][]int{{0, 1}, {2, 3}, {4, 5}, {6}})
is.Equal(result3, [][]int{})
is.Equal(result4, [][]int{{0}})
}
func TestFlatten(t *testing.T) {
is := assert.New(t)
result1 := Flatten[int]([][]int{{0, 1}, {2, 3, 4, 5}})
is.Equal(result1, []int{0, 1, 2, 3, 4, 5})
}
func TestShuffle(t *testing.T) {
is := assert.New(t)
result1 := Shuffle[int]([]int{0, 1, 2, 3, 4, 5})
result2 := Shuffle[int]([]int{})
is.NotEqual(result1, []int{0, 1, 2, 3, 4, 5})
is.Equal(result2, []int{})
}
func TestReverse(t *testing.T) {
is := assert.New(t)
result1 := Reverse[int]([]int{0, 1, 2, 3, 4, 5})
result2 := Reverse[int]([]int{0, 1, 2, 3, 4, 5, 6})
result3 := Reverse[int]([]int{})
is.Equal(result1, []int{5, 4, 3, 2, 1, 0})
is.Equal(result2, []int{6, 5, 4, 3, 2, 1, 0})
is.Equal(result3, []int{})
}
type foo struct {
bar string
}
func (f foo) Clone() foo {
return foo{f.bar}
}
func TestFill(t *testing.T) {
is := assert.New(t)
result1 := Fill[foo]([]foo{foo{"a"}, foo{"a"}}, foo{"b"})
result2 := Fill[foo]([]foo{}, foo{"a"})
is.Equal(result1, []foo{foo{"b"}, foo{"b"}})
is.Equal(result2, []foo{})
}
func TestToMap(t *testing.T) {
is := assert.New(t)
result1 := ToMap[int, string]([]string{"a", "aa", "aaa"}, func(str string) int {
return len(str)
})
is.Equal(result1, map[int]string{1: "a", 2: "aa", 3: "aaa"})
}