-
Notifications
You must be signed in to change notification settings - Fork 2
/
order_test.go
373 lines (335 loc) · 7.38 KB
/
order_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package order
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
var intFn = By(func(a, b int) int { return a - b })
type cmp1 struct{ v int }
func (c cmp1) Compare(other cmp1) int { return c.v - other.v }
type cmp2 struct{ v int }
func (c *cmp2) Compare(other *cmp2) int { return c.v - other.v }
func TestConvertTypes(t *testing.T) {
t.Parallel()
type myStr string
var m1, m2 myStr
var s1, s2 string
tests := []struct{ a, b interface{} }{
{s1, s2},
{&s1, s2},
{&s1, &s2},
{m1, m2},
{&m1, m2},
{&m1, &m2},
{s1, m2},
{s1, &m2},
{&s1, m2},
{&s1, &m2},
{cmp1{1}, cmp1{1}},
{&cmp2{1}, &cmp2{1}},
{&cmp1{1}, cmp1{1}},
}
test := func(t *testing.T, a, b interface{}) {
t.Run(name2(a, b), func(t *testing.T) {
assert.True(t, Is(a).Equal(b))
assert.True(t, Is(a).GreaterEqual(b))
assert.True(t, Is(a).LessEqual(b))
assert.False(t, Is(a).Greater(a))
assert.False(t, Is(a).Less(a))
})
}
for _, tt := range tests {
test(t, tt.a, tt.b)
test(t, tt.b, tt.a)
}
}
func TestReversed(t *testing.T) {
t.Parallel()
c := intFn.Reversed()
assert.False(t, c.Is(1).Greater(0))
assert.False(t, c.Is(1).Greater(1))
assert.True(t, c.Is(1).Greater(2))
}
func TestSort(t *testing.T) {
t.Parallel()
got := []int{2, 3, 1}
Sort(got)
assert.Equal(t, []int{1, 2, 3}, got)
}
func TestSortStable(t *testing.T) {
t.Parallel()
intp := func(i int) *int { return &i }
got := []*int{intp(2), intp(2), intp(1)}
want := []*int{got[2], got[0], got[1]}
SortStable(got)
// Check the actual pointers and not the pointer values.
for i := range want {
if want[i] != got[i] {
t.Errorf("Element %d differs", i)
}
}
}
func TestSearch(t *testing.T) {
t.Parallel()
tests := []struct {
name string
slice []int
value int
want int
}{
{
name: "empty slice",
slice: []int{},
value: 1,
want: -1,
},
{
name: "odd size slice, middle value",
slice: []int{1, 2, 3},
value: 2,
want: 1,
},
{
name: "odd size slice, first value",
slice: []int{1, 2, 3},
value: 1,
want: 0,
},
{
name: "odd size slice, last value",
slice: []int{1, 2, 3},
value: 3,
want: 2,
},
{
name: "odd size slice, not found",
slice: []int{1, 2, 3},
value: 4,
want: -1,
},
{
name: "even size slice, middle value",
slice: []int{1, 2, 3, 4},
value: 2,
want: 1,
},
{
name: "even size slice, first value",
slice: []int{1, 2, 3, 4},
value: 1,
want: 0,
},
{
name: "even size slice, last value",
slice: []int{1, 2, 3, 4},
value: 4,
want: 3,
},
{
name: "even size slice, not found",
slice: []int{1, 2, 3, 4},
value: 5,
want: -1,
},
{
name: "not found within the slice",
slice: []int{1, 2, 3, 5},
value: 4,
want: -1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Search(tt.slice, tt.value)
assert.Equal(t, tt.want, got)
})
}
}
func TestIsSorted(t *testing.T) {
t.Parallel()
tests := []struct {
name string
slice []int
wantSorted bool
wantStrictSorted bool
}{
{
name: "empty slice",
slice: []int{},
wantSorted: true,
wantStrictSorted: true,
},
{
name: "one element",
slice: []int{1},
wantSorted: true,
wantStrictSorted: true,
},
{
name: "increasing",
slice: []int{1, 5, 5},
wantSorted: true,
wantStrictSorted: false,
},
{
name: "strictly increasing",
slice: []int{1, 5, 10},
wantSorted: true,
wantStrictSorted: true,
},
{
name: "constant",
slice: []int{1, 1, 1},
wantSorted: true,
wantStrictSorted: false,
},
{
name: "decreasing",
slice: []int{10, 5, 5},
wantSorted: false,
wantStrictSorted: false,
},
{
name: "strictly decreasing",
slice: []int{10, 5, 1},
wantSorted: false,
wantStrictSorted: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.wantSorted, IsSorted(tt.slice))
assert.Equal(t, tt.wantStrictSorted, IsStrictSorted(tt.slice))
})
}
}
func TestMinMax(t *testing.T) {
t.Parallel()
tests := []struct {
name string
slice []int
wantMinI int
wantMaxI int
}{
{
name: "empty slice",
slice: []int{},
wantMinI: -1,
wantMaxI: -1,
},
{
name: "single value",
slice: []int{1},
wantMinI: 0,
wantMaxI: 0,
},
{
name: "get the first minmum/maximum",
slice: []int{1, 1, 2, 2},
wantMinI: 0,
wantMaxI: 2,
},
{
name: "slice not sorted",
slice: []int{3, 1, 2},
wantMinI: 1,
wantMaxI: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotMinI, gotMaxI := MinMax(tt.slice)
assert.Equal(t, tt.wantMinI, gotMinI)
assert.Equal(t, tt.wantMaxI, gotMaxI)
})
}
}
func TestBy_invalidFn(t *testing.T) {
t.Parallel()
tests := []struct {
name string
fns []interface{}
}{
{
name: "no functions",
fns: []interface{}{},
},
{
name: "not a function",
fns: []interface{}{true},
},
{
name: "1 arg",
fns: []interface{}{func(a int) int { return 0 }},
},
{
name: "3 arg",
fns: []interface{}{func(a, b, c int) int { return 0 }},
},
{
name: "arg types differ",
fns: []interface{}{func(a int, b string) int { return 0 }},
},
{
name: "arg invalid types",
fns: []interface{}{func(a, b func()) int { return 0 }},
},
{
name: "1st arg invalid types",
fns: []interface{}{func(a func(), b int) int { return 0 }},
},
{
name: "2nd arg invalid types",
fns: []interface{}{func(a int, b func()) int { return 0 }},
},
{
name: "no return values",
fns: []interface{}{func(a, b int) {}},
},
{
name: "2 return values",
fns: []interface{}{func(a, b int) (int, int) { return 0, 0 }},
},
{
name: "invalid return value type",
fns: []interface{}{func(a, b int) bool { return false }},
},
{
name: "functions type mismatch",
fns: []interface{}{
func(a, b int) int { return 0 },
func(a, b bool) int { return 0 },
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Panics(t, func() { By(tt.fns...) })
})
}
}
func TestInvalidArgs(t *testing.T) {
t.Parallel()
fns := []func(v interface{}){
func(v interface{}) { intFn.Sort(v) },
func(v interface{}) { intFn.SortStable(v) },
func(v interface{}) { intFn.Search(v, 1) },
func(v interface{}) { intFn.IsSorted(v) },
func(v interface{}) { intFn.IsStrictSorted(v) },
func(v interface{}) { intFn.MinMax(v) },
func(v interface{}) { intFn.Select(v, 0) },
}
for _, fn := range fns {
t.Run("not a slice", func(t *testing.T) { assert.Panics(t, func() { fn(1) }) })
t.Run("slice of wrong type", func(t *testing.T) { assert.Panics(t, func() { fn([]bool{true}) }) })
t.Run("slice of invalid type", func(t *testing.T) { assert.Panics(t, func() { fn([]func(){func() {}}) }) })
}
// Other wrong arguments
// Search invalid value type.
assert.Panics(t, func() { intFn.Search([]int{}, true) })
// Select K out of bounds.
assert.Panics(t, func() { Select([]int{1}, -1) })
assert.Panics(t, func() { Select([]int{1}, 1) })
assert.Panics(t, func() { Select([]int{}, 0) })
}
func name2(a, b interface{}) string { return fmt.Sprintf("%v(%T)/%v(%T)", a, a, b, b) }