-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice_test.go
More file actions
186 lines (142 loc) · 3.05 KB
/
Copy pathslice_test.go
File metadata and controls
186 lines (142 loc) · 3.05 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package easy
import (
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestMap(t *testing.T) {
t.Parallel()
type st1 struct {
a int64
b float64
}
type st2 struct {
a int64
s string
}
t.Run("empty", func(t *testing.T) {
t.Parallel()
var src []st1
converted := Map(src, func(e st1) st2 {
return st2{
a: e.a,
s: strconv.FormatInt(e.a, 10) + " " + strconv.FormatFloat(e.b, 'f', 2, 64),
}
})
require.Equal(t, []st2{}, converted)
})
t.Run("convert structs", func(t *testing.T) {
t.Parallel()
src := []st1{{a: 15, b: 25.0}, {a: 25, b: 35.0}}
converted := Map(src, func(e st1) st2 {
return st2{
a: e.a,
s: strconv.FormatInt(e.a, 10) + " " + strconv.FormatFloat(e.b, 'f', 2, 64),
}
})
require.Equal(t, []st2{
{
a: 15,
s: "15 25.00",
},
{
a: 25,
s: "25 35.00",
},
}, converted)
})
}
func TestFilter(t *testing.T) {
t.Parallel()
numbers := []int{1, 208, 54, 100, 20932, 533, 8, 211}
numbersOverOneHundred := Filter(numbers, func(n int) bool { return n > 100 })
require.Equal(t, []int{208, 20932, 533, 211}, numbersOverOneHundred)
}
func TestMapFilter(t *testing.T) {
t.Parallel()
numbers := []int{1, 208, 54, 100, 20932, 533, 8, 211}
numbersOverOneHundredStrings := MapFilter(numbers, func(n int) (string, bool) {
if n <= 100 {
return "", false
}
return strconv.FormatInt(int64(n), 10), true
})
require.Equal(t, []string{"208", "20932", "533", "211"}, numbersOverOneHundredStrings)
}
func TestContains(t *testing.T) {
t.Parallel()
numbers := []int{1, 2, 3}
require.True(t, Contains(numbers, 2))
require.False(t, Contains(numbers, 5))
}
func TestContainsFunc(t *testing.T) {
t.Parallel()
type st struct {
a int
}
s := []st{
{a: 15},
{a: 25},
}
require.True(t, ContainsFunc(s, func(e st) bool { return e.a == 25 }))
require.False(t, ContainsFunc(s, func(e st) bool { return e.a == 55 }))
}
func TestBatch(t *testing.T) {
t.Parallel()
t.Run("empty slice", func(t *testing.T) {
t.Parallel()
require.Equal(t, [][]string{}, Batch([]string{}, 10))
})
t.Run("batch size is zero", func(t *testing.T) {
t.Parallel()
s := []string{"s1", "s2"}
require.Equal(t, [][]string{s}, Batch(s, 0))
})
t.Run("one batch", func(t *testing.T) {
t.Parallel()
s := make([]int, 0, 20)
for i := range 20 {
s = append(s, i+1)
}
batches := Batch(s, 20)
require.Equal(t,
[][]int{s},
batches,
)
})
t.Run("even batch", func(t *testing.T) {
t.Parallel()
s := make([]int, 0, 20)
for i := range 20 {
s = append(s, i+1)
}
batches := Batch(s, 5)
require.Equal(t,
[][]int{
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
},
batches,
)
})
t.Run("uneven batch", func(t *testing.T) {
t.Parallel()
s := make([]int, 0, 23)
for i := range 23 {
s = append(s, i+1)
}
batches := Batch(s, 5)
require.Equal(t,
[][]int{
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23},
},
batches,
)
})
}