forked from samber/lo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_test.go
216 lines (169 loc) · 4.6 KB
/
find_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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package lo
import (
"fmt"
"math/rand"
"sort"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestIndexOf(t *testing.T) {
is := assert.New(t)
result1 := IndexOf([]int{0, 1, 2, 1, 2, 3}, 2)
result2 := IndexOf([]int{0, 1, 2, 1, 2, 3}, 6)
is.Equal(result1, 2)
is.Equal(result2, -1)
}
func TestLastIndexOf(t *testing.T) {
is := assert.New(t)
result1 := LastIndexOf([]int{0, 1, 2, 1, 2, 3}, 2)
result2 := LastIndexOf([]int{0, 1, 2, 1, 2, 3}, 6)
is.Equal(result1, 4)
is.Equal(result2, -1)
}
func TestFind(t *testing.T) {
is := assert.New(t)
result1, ok1 := Find([]string{"a", "b", "c", "d"}, func(i string) bool {
return i == "b"
})
result2, ok2 := Find([]string{"foobar"}, func(i string) bool {
return i == "b"
})
is.Equal(ok1, true)
is.Equal(result1, "b")
is.Equal(ok2, false)
is.Equal(result2, "")
}
func TestFindIndexOf(t *testing.T) {
is := assert.New(t)
item1, index1, ok1 := FindIndexOf([]string{"a", "b", "c", "d", "b"}, func(i string) bool {
return i == "b"
})
item2, index2, ok2 := FindIndexOf([]string{"foobar"}, func(i string) bool {
return i == "b"
})
is.Equal(item1, "b")
is.Equal(ok1, true)
is.Equal(index1, 1)
is.Equal(item2, "")
is.Equal(ok2, false)
is.Equal(index2, -1)
}
func TestFindLastIndexOf(t *testing.T) {
is := assert.New(t)
item1, index1, ok1 := FindLastIndexOf([]string{"a", "b", "c", "d", "b"}, func(i string) bool {
return i == "b"
})
item2, index2, ok2 := FindLastIndexOf([]string{"foobar"}, func(i string) bool {
return i == "b"
})
is.Equal(item1, "b")
is.Equal(ok1, true)
is.Equal(index1, 4)
is.Equal(item2, "")
is.Equal(ok2, false)
is.Equal(index2, -1)
}
func TestFindOrElse(t *testing.T) {
is := assert.New(t)
result1 := FindOrElse([]string{"a", "b", "c", "d"}, "x", func(i string) bool {
return i == "b"
})
result2 := FindOrElse([]string{"foobar"}, "x", func(i string) bool {
return i == "b"
})
is.Equal(result1, "b")
is.Equal(result2, "x")
}
func TestMin(t *testing.T) {
is := assert.New(t)
result1 := Min([]int{1, 2, 3})
result2 := Min([]int{3, 2, 1})
result3 := Min([]int{})
is.Equal(result1, 1)
is.Equal(result2, 1)
is.Equal(result3, 0)
}
func TestMinBy(t *testing.T) {
is := assert.New(t)
result1 := MinBy([]string{"s1", "string2", "s3"}, func(item string, min string) bool {
return len(item) < len(min)
})
result2 := MinBy([]string{"string1", "string2", "s3"}, func(item string, min string) bool {
return len(item) < len(min)
})
result3 := MinBy([]string{}, func(item string, min string) bool {
return len(item) < len(min)
})
is.Equal(result1, "s1")
is.Equal(result2, "s3")
is.Equal(result3, "")
}
func TestMax(t *testing.T) {
is := assert.New(t)
result1 := Max([]int{1, 2, 3})
result2 := Max([]int{3, 2, 1})
result3 := Max([]int{})
is.Equal(result1, 3)
is.Equal(result2, 3)
is.Equal(result3, 0)
}
func TestMaxBy(t *testing.T) {
is := assert.New(t)
result1 := MaxBy([]string{"s1", "string2", "s3"}, func(item string, max string) bool {
return len(item) > len(max)
})
result2 := MaxBy([]string{"string1", "string2", "s3"}, func(item string, max string) bool {
return len(item) > len(max)
})
result3 := MaxBy([]string{}, func(item string, max string) bool {
return len(item) > len(max)
})
is.Equal(result1, "string2")
is.Equal(result2, "string1")
is.Equal(result3, "")
}
func TestLast(t *testing.T) {
is := assert.New(t)
result1, err1 := Last([]int{1, 2, 3})
result2, err2 := Last([]int{})
is.Equal(result1, 3)
is.Equal(err1, nil)
is.Equal(result2, 0)
is.Equal(err2, fmt.Errorf("last: cannot extract the last element of an empty slice"))
}
func TestNth(t *testing.T) {
is := assert.New(t)
result1, err1 := Nth([]int{0, 1, 2, 3}, 2)
result2, err2 := Nth([]int{0, 1, 2, 3}, -2)
result3, err3 := Nth([]int{0, 1, 2, 3}, 42)
result4, err4 := Nth([]int{}, 0)
result5, err5 := Nth([]int{42}, 0)
is.Equal(result1, 2)
is.Equal(err1, nil)
is.Equal(result2, 2)
is.Equal(err2, nil)
is.Equal(result3, 0)
is.Equal(err3, fmt.Errorf("nth: 42 out of slice bounds"))
is.Equal(result4, 0)
is.Equal(err4, fmt.Errorf("nth: 0 out of slice bounds"))
is.Equal(result5, 42)
is.Equal(err5, nil)
}
func TestSample(t *testing.T) {
is := assert.New(t)
rand.Seed(time.Now().UnixNano())
result1 := Sample([]string{"a", "b", "c"})
result2 := Sample([]string{})
is.True(Contains([]string{"a", "b", "c"}, result1))
is.Equal(result2, "")
}
func TestSamples(t *testing.T) {
is := assert.New(t)
rand.Seed(time.Now().UnixNano())
result1 := Samples([]string{"a", "b", "c"}, 3)
result2 := Samples([]string{}, 3)
sort.Strings(result1)
is.Equal(result1, []string{"a", "b", "c"})
is.Equal(result2, []string{})
}