-
Notifications
You must be signed in to change notification settings - Fork 2
/
seq_test.go
373 lines (313 loc) · 8 KB
/
seq_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 seq
import (
. "testing"
"github.com/stretchr/testify/assert"
)
// Tests the FirstRest, Size, and ToSlice methods of a Seq
func testSeqGen(t *T, s Seq, ints []interface{}) Seq {
intsl := uint64(len(ints))
for i := range ints {
assertSaneList(ToList(s), t)
assert.Equal(t, intsl-uint64(i), Size(s))
assert.Equal(t, ints[i:], ToSlice(s))
first, rest, ok := s.FirstRest()
assert.Equal(t, true, ok)
assert.Equal(t, ints[i], first)
s = rest
}
return s
}
// Tests the FirstRest, Size, and ToSlice methods of an unordered Seq
func testSeqNoOrderGen(t *T, s Seq, ints []interface{}) Seq {
intsl := uint64(len(ints))
m := map[interface{}]bool{}
for i := range ints {
m[ints[i]] = true
}
for i := range ints {
assertSaneList(ToList(s), t)
assert.Equal(t, intsl-uint64(i), Size(s))
assertSeqContentsNoOrderMap(t, m, s)
first, rest, ok := s.FirstRest()
assert.Equal(t, true, ok)
_, ok = m[first]
assert.Equal(t, true, ok)
delete(m, first)
s = rest
}
return s
}
// Test reversing a Seq
func TestReverse(t *T) {
// Normal case
intl := []interface{}{3, 2, 1}
l := NewList(intl...)
nl := Reverse(l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, []interface{}{1, 2, 3}, ToSlice(nl))
// Degenerate case
l = NewList()
nl = Reverse(l)
assert.Equal(t, 0, Size(l))
assert.Equal(t, 0, Size(nl))
}
func testMapGen(t *T, mapFn func(func(interface{}) interface{}, Seq) Seq) {
fn := func(n interface{}) interface{} {
return n.(int) + 1
}
// Normal case
intl := []interface{}{1, 2, 3}
l := NewList(intl...)
nl := mapFn(fn, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, []interface{}{2, 3, 4}, ToSlice(nl))
// Degenerate case
l = NewList()
nl = mapFn(fn, l)
assert.Equal(t, 0, Size(l))
assert.Equal(t, 0, Size(nl))
}
// Test mapping over a Seq
func TestMap(t *T) {
testMapGen(t, Map)
}
// Test lazily mapping over a Seq
func TestLMap(t *T) {
testMapGen(t, LMap)
}
// Test reducing over a Seq
func TestReduce(t *T) {
fn := func(acc, el interface{}) (interface{}, bool) {
return acc.(int) + el.(int), false
}
// Normal case
intl := []interface{}{1, 2, 3, 4}
l := NewList(intl...)
r := Reduce(fn, 0, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, 10, r)
// Short-circuit case
fns := func(acc, el interface{}) (interface{}, bool) {
return acc.(int) + el.(int), el.(int) > 2
}
r = Reduce(fns, 0, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, 6, r)
// Degenerate case
l = NewList()
r = Reduce(fn, 0, l)
assert.Equal(t, 0, Size(l))
assert.Equal(t, 0, r)
}
// Test the Any function
func TestAny(t *T) {
fn := func(el interface{}) bool {
return el.(int) > 3
}
// Value found case
intl := []interface{}{1, 2, 3, 4}
l := NewList(intl...)
r, ok := Any(fn, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, 4, r)
assert.Equal(t, true, ok)
// Value not found case
intl = []interface{}{1, 2, 3}
l = NewList(intl...)
r, ok = Any(fn, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, nil, r)
assert.Equal(t, false, ok)
// Degenerate case
l = NewList()
r, ok = Any(fn, l)
assert.Equal(t, 0, Size(l))
assert.Equal(t, nil, r)
assert.Equal(t, false, ok)
}
// Test the All function
func TestAll(t *T) {
fn := func(el interface{}) bool {
return el.(int) > 3
}
// All match case
intl := []interface{}{4, 5, 6}
l := NewList(intl...)
ok := All(fn, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, true, ok)
// Not all match case
intl = []interface{}{3, 4, 2, 5}
l = NewList(intl...)
ok = All(fn, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, false, ok)
// Degenerate case
l = NewList()
ok = All(fn, l)
assert.Equal(t, 0, Size(l))
assert.Equal(t, true, ok)
}
func testFilterGen(t *T, filterFn func(func(interface{}) bool, Seq) Seq) {
fn := func(el interface{}) bool {
return el.(int)%2 != 0
}
// Normal case
intl := []interface{}{1, 2, 3, 4, 5}
l := NewList(intl...)
r := filterFn(fn, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, []interface{}{1, 3, 5}, ToSlice(r))
// Degenerate cases
l = NewList()
r = filterFn(fn, l)
assert.Equal(t, 0, Size(l))
assert.Equal(t, 0, Size(r))
}
// Test the Filter function
func TestFilter(t *T) {
testFilterGen(t, Filter)
}
// Test the lazy Filter function
func TestLFilter(t *T) {
testFilterGen(t, LFilter)
}
// Test Flatten-ing of a Seq
func TestFlatten(t *T) {
// Normal case
intl1 := []interface{}{0, 1, 2}
intl2 := []interface{}{3, 4, 5}
l1 := NewList(intl1...)
l2 := NewList(intl2...)
blank := NewList()
intl := []interface{}{-1, l1, l2, 6, blank, 7}
l := NewList(intl...)
nl := Flatten(l)
assert.Equal(t, intl1, ToSlice(l1))
assert.Equal(t, intl2, ToSlice(l2))
assert.Equal(t, 0, Size(blank))
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, []interface{}{-1, 0, 1, 2, 3, 4, 5, 6, 7}, ToSlice(nl))
// Degenerate case
nl = Flatten(blank)
assert.Equal(t, 0, Size(blank))
assert.Equal(t, 0, Size(nl))
}
func testTakeGen(t *T, takeFn func(uint64, Seq) Seq) {
// Normal case
intl := []interface{}{0, 1, 2, 3, 4}
l := NewList(intl...)
nl := takeFn(3, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, []interface{}{0, 1, 2}, ToSlice(nl))
// Edge cases
nl = takeFn(5, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, intl, ToSlice(nl))
nl = takeFn(6, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, intl, ToSlice(nl))
// Degenerate cases
empty := NewList()
nl = takeFn(1, empty)
assert.Equal(t, 0, Size(empty))
assert.Equal(t, 0, Size(nl))
nl = takeFn(0, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, 0, Size(nl))
}
// Test taking from a Seq
func TestTake(t *T) {
testTakeGen(t, Take)
}
// Test lazily taking from a Seq
func TestLTake(t *T) {
testTakeGen(t, LTake)
}
func testTakeWhileGen(t *T, takeWhileFn func(func(interface{}) bool, Seq) Seq) {
pred := func(el interface{}) bool {
return el.(int) < 3
}
// Normal case
intl := []interface{}{0, 1, 2, 3, 4, 5}
l := NewList(intl...)
nl := takeWhileFn(pred, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, []interface{}{0, 1, 2}, ToSlice(nl))
// Edge cases
intl = []interface{}{5, 5, 5}
l = NewList(intl...)
nl = takeWhileFn(pred, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, 0, Size(nl))
intl = []interface{}{0, 1, 2}
l = NewList(intl...)
nl = takeWhileFn(pred, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, []interface{}{0, 1, 2}, ToSlice(nl))
// Degenerate case
l = NewList()
nl = takeWhileFn(pred, l)
assert.Equal(t, 0, Size(l))
assert.Equal(t, 0, Size(nl))
}
// Test taking from a Seq until a given condition
func TestTakeWhile(t *T) {
testTakeWhileGen(t, TakeWhile)
}
// Test lazily taking from a Seq until a given condition
func TestLTakeWhile(t *T) {
testTakeWhileGen(t, LTakeWhile)
}
// Test dropping from a Seq
func TestDrop(t *T) {
// Normal case
intl := []interface{}{0, 1, 2, 3, 4}
l := NewList(intl...)
nl := Drop(3, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, []interface{}{3, 4}, ToSlice(nl))
// Edge cases
nl = Drop(5, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, 0, Size(nl))
nl = Drop(6, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, 0, Size(nl))
// Degenerate cases
empty := NewList()
nl = Drop(1, empty)
assert.Equal(t, 0, Size(empty))
assert.Equal(t, 0, Size(nl))
nl = Drop(0, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, intl, ToSlice(nl))
}
// Test dropping from a Seq until a given condition
func TestDropWhile(t *T) {
pred := func(el interface{}) bool {
return el.(int) < 3
}
// Normal case
intl := []interface{}{0, 1, 2, 3, 4, 5}
l := NewList(intl...)
nl := DropWhile(pred, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, []interface{}{3, 4, 5}, ToSlice(nl))
// Edge cases
intl = []interface{}{5, 5, 5}
l = NewList(intl...)
nl = DropWhile(pred, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, intl, ToSlice(nl))
intl = []interface{}{0, 1, 2}
l = NewList(intl...)
nl = DropWhile(pred, l)
assert.Equal(t, intl, ToSlice(l))
assert.Equal(t, 0, Size(nl))
// Degenerate case
l = NewList()
nl = DropWhile(pred, l)
assert.Equal(t, 0, Size(l))
assert.Equal(t, 0, Size(nl))
}