-
Notifications
You must be signed in to change notification settings - Fork 437
/
IterableTest.kt
484 lines (422 loc) · 15.6 KB
/
IterableTest.kt
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package arrow.core
import arrow.core.test.UnitSpec
import arrow.core.test.generators.either
import arrow.core.test.generators.functionAToB
import arrow.core.test.generators.option
import arrow.typeclasses.Semigroup
import io.kotest.matchers.collections.shouldBeEmpty
import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.should
import io.kotest.property.Arb
import io.kotest.matchers.shouldBe
import io.kotest.property.arbitrary.boolean
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.orNull
import io.kotest.property.arbitrary.string
import kotlin.math.max
import kotlin.math.min
class IterableTest : UnitSpec() {
init {
"traverse Either stack-safe" {
// also verifies result order and execution order (l to r)
val acc = mutableListOf<Int>()
val res = (0..20_000).traverse { a ->
acc.add(a)
Either.Right(a)
}
res shouldBe Either.Right(acc)
res shouldBe Either.Right((0..20_000).toList())
}
"traverse Either short-circuit" {
checkAll(Arb.list(Arb.int())) { ints ->
val acc = mutableListOf<Int>()
val evens = ints.traverse {
if (it % 2 == 0) {
acc.add(it)
Either.Right(it)
} else Either.Left(it)
}
acc shouldBe ints.takeWhile { it % 2 == 0 }
when (evens) {
is Either.Right -> evens.value shouldBe ints
is Either.Left -> evens.value shouldBe ints.first { it % 2 != 0 }
}
}
}
"sequenceEither should be consistent with traverse Either" {
checkAll(Arb.list(Arb.int())) { ints ->
ints.map { it.right() }.sequence() shouldBe ints.traverse { it.right() }
}
}
"traverse Result stack-safe" {
// also verifies result order and execution order (l to r)
val acc = mutableListOf<Int>()
val res = (0..20_000).traverse { a ->
acc.add(a)
Result.success(a)
}
res shouldBe Result.success(acc)
res shouldBe Result.success((0..20_000).toList())
}
"traverse Result short-circuit" {
checkAll(Arb.list(Arb.int())) { ints ->
val acc = mutableListOf<Int>()
val evens = ints.traverse {
if (it % 2 == 0) {
acc.add(it)
Result.success(it)
} else Result.failure(RuntimeException())
}
acc shouldBe ints.takeWhile { it % 2 == 0 }
evens.fold(
{ it shouldBe ints },
{ }
)
}
}
"sequence Result should be consistent with traverse Result" {
checkAll(Arb.list(Arb.int())) { ints ->
ints.map { Result.success(it) }.sequence() shouldBe ints.traverse { Result.success(it) }
}
}
"traverse Option is stack-safe" {
// also verifies result order and execution order (l to r)
val acc = mutableListOf<Int>()
val res = (0..20_000).traverse { a: Int ->
acc.add(a)
Some(a)
}
res shouldBe Some(acc)
res shouldBe Some((0..20_000).toList())
}
"traverse Option short-circuits" {
checkAll(Arb.list(Arb.int())) { ints ->
val acc = mutableListOf<Int>()
val evens = ints.traverse {
(it % 2 == 0).maybe {
acc.add(it)
it
}
}
acc shouldBe ints.takeWhile { it % 2 == 0 }
evens.fold({ Unit }) { it shouldBe ints }
}
}
"sequence Option yields some when all entries in the list are some" {
checkAll(Arb.list(Arb.int())) { ints ->
val evens = ints.map { (it % 2 == 0).maybe { it } }.sequence()
evens.fold({ Unit }) { it shouldBe ints }
}
}
"sequence Option should be consistent with traverse Option" {
checkAll(Arb.list(Arb.int())) { ints ->
ints.map { Some(it) }.sequence() shouldBe ints.traverse { Some(it) }
}
}
"traverse Nullable is stack-safe" {
// also verifies result order and execution order (l to r)
val acc = mutableListOf<Int?>()
val res = (0..20_000).traverse { a: Int ->
acc.add(a)
a
}
res.shouldNotBeNull() shouldBe acc
res.shouldNotBeNull() shouldBe (0..20_000).toList()
}
"traverse Nullable short-circuits" {
checkAll(Arb.list(Arb.int())) { ints ->
val acc = mutableListOf<Int>()
val evens = ints.traverse {
if (it % 2 == 0) {
acc.add(it)
it
} else {
null
}
}
val expected = ints.takeWhile { it % 2 == 0 }
acc shouldBe expected
if (ints.any { it % 2 != 0 }) {
evens.shouldBeNull()
} else {
evens.shouldNotBeNull() shouldContainExactly expected
}
}
}
"sequence Nullable yields some when all entries in the list are not null" {
checkAll(Arb.list(Arb.int())) { ints ->
val evens = ints.map { if (it % 2 == 0) it else null }.sequence()
val expected = ints.takeWhile { it % 2 == 0 }
if (ints.any { it % 2 != 0 }) {
evens.shouldBeNull()
} else {
evens.shouldNotBeNull() shouldContainExactly ints.takeWhile { it % 2 == 0 }
}
}
}
"sequence Nullable should be consistent with travers Nullable" {
checkAll(Arb.list(Arb.int())) { ints ->
ints.map { it as Int? }.sequence() shouldBe ints.traverse { it as Int? }
}
}
"traverse Validated stack-safe" {
// also verifies result order and execution order (l to r)
val acc = mutableListOf<Int>()
val res = (0..20_000).traverse(Semigroup.string()) {
acc.add(it)
Validated.Valid(it)
}
res shouldBe Validated.Valid(acc)
res shouldBe Validated.Valid((0..20_000).toList())
}
"traverse Validated acumulates" {
checkAll(Arb.list(Arb.int())) { ints ->
val res: ValidatedNel<Int, List<Int>> =
ints.map { i -> if (i % 2 == 0) Valid(i) else Invalid(nonEmptyListOf(i)) }
.sequence()
val expected: ValidatedNel<Int, List<Int>> = ints.filterNot { it % 2 == 0 }
.toNonEmptyListOrNull()?.invalid() ?: Valid(ints.filter { it % 2 == 0 })
res shouldBe expected
}
}
"sequence Validated should be consistent with traverse Validated" {
checkAll(Arb.list(Arb.int())) { ints ->
ints.map { it.valid() }.sequence(Semigroup.string()) shouldBe
ints.traverse(Semigroup.string()) { it.valid() }
}
}
"sequence Either traverse Nullable interoperate - and proof map + sequence equality with traverse" {
checkAll(
Arb.list(Arb.int()),
Arb.functionAToB<Int, Either<String, Int>?>(Arb.either(Arb.string(), Arb.int()).orNull())
) { ints, f ->
val res: Either<String, List<Int>>? =
ints.traverse(f)?.sequence()
val expected: Either<String, List<Int>>? =
ints.map(f).sequence()?.sequence()
res shouldBe expected
}
}
"zip3" {
checkAll(Arb.list(Arb.int()), Arb.list(Arb.int()), Arb.list(Arb.int())) { a, b, c ->
val result = a.zip(b, c, ::Triple)
val expected = a.zip(b, ::Pair).zip(c) { (a, b), c -> Triple(a, b, c) }
result shouldBe expected
}
}
"zip4" {
checkAll(Arb.list(Arb.int()), Arb.list(Arb.int()), Arb.list(Arb.int()), Arb.list(Arb.int())) { a, b, c, d ->
val result = a.zip(b, c, d, ::Tuple4)
val expected = a.zip(b, ::Pair)
.zip(c) { (a, b), c -> Triple(a, b, c) }
.zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }
result shouldBe expected
}
}
"zip5" {
checkAll(
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int())
) { a, b, c, d, e ->
val result = a.zip(b, c, d, e, ::Tuple5)
val expected = a.zip(b, ::Pair)
.zip(c) { (a, b), c -> Triple(a, b, c) }
.zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }
.zip(e) { (a, b, c, d), e -> Tuple5(a, b, c, d, e) }
result shouldBe expected
}
}
"zip6" {
checkAll(
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int())
) { a, b, c, d, e, f ->
val result = a.zip(b, c, d, e, f, ::Tuple6)
val expected = a.zip(b, ::Pair)
.zip(c) { (a, b), c -> Triple(a, b, c) }
.zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }
.zip(e) { (a, b, c, d), e -> Tuple5(a, b, c, d, e) }
.zip(f) { (a, b, c, d, e), f -> Tuple6(a, b, c, d, e, f) }
result shouldBe expected
}
}
"zip7" {
checkAll(
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int())
) { a, b, c, d, e, f, g ->
val result = a.zip(b, c, d, e, f, g, ::Tuple7)
val expected = a.zip(b, ::Pair)
.zip(c) { (a, b), c -> Triple(a, b, c) }
.zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }
.zip(e) { (a, b, c, d), e -> Tuple5(a, b, c, d, e) }
.zip(f) { (a, b, c, d, e), f -> Tuple6(a, b, c, d, e, f) }
.zip(g) { (a, b, c, d, e, f), g -> Tuple7(a, b, c, d, e, f, g) }
result shouldBe expected
}
}
"zip8" {
checkAll(
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int())
) { a, b, c, d, e, f, g, h ->
val result = a.zip(b, c, d, e, f, g, h, ::Tuple8)
val expected = a.zip(b, ::Pair)
.zip(c) { (a, b), c -> Triple(a, b, c) }
.zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }
.zip(e) { (a, b, c, d), e -> Tuple5(a, b, c, d, e) }
.zip(f) { (a, b, c, d, e), f -> Tuple6(a, b, c, d, e, f) }
.zip(g) { (a, b, c, d, e, f), g -> Tuple7(a, b, c, d, e, f, g) }
.zip(h) { (a, b, c, d, e, f, g), h -> Tuple8(a, b, c, d, e, f, g, h) }
result shouldBe expected
}
}
"zip9" {
checkAll(
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int())
) { a, b, c, d, e, f, g, h, i ->
val result = a.zip(b, c, d, e, f, g, h, i, ::Tuple9)
val expected = a.zip(b, ::Pair)
.zip(c) { (a, b), c -> Triple(a, b, c) }
.zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }
.zip(e) { (a, b, c, d), e -> Tuple5(a, b, c, d, e) }
.zip(f) { (a, b, c, d, e), f -> Tuple6(a, b, c, d, e, f) }
.zip(g) { (a, b, c, d, e, f), g -> Tuple7(a, b, c, d, e, f, g) }
.zip(h) { (a, b, c, d, e, f, g), h -> Tuple8(a, b, c, d, e, f, g, h) }
.zip(i) { (a, b, c, d, e, f, g, h), i -> Tuple9(a, b, c, d, e, f, g, h, i) }
result shouldBe expected
}
}
"zip10" {
checkAll(
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int()),
Arb.list(Arb.int())
) { a, b, c, d, e, f, g, h, i, j ->
val result = a.zip(b, c, d, e, f, g, h, i, j, ::Tuple10)
val expected = a.zip(b, ::Pair)
.zip(c) { (a, b), c -> Triple(a, b, c) }
.zip(d) { (a, b, c), d -> Tuple4(a, b, c, d) }
.zip(e) { (a, b, c, d), e -> Tuple5(a, b, c, d, e) }
.zip(f) { (a, b, c, d, e), f -> Tuple6(a, b, c, d, e, f) }
.zip(g) { (a, b, c, d, e, f), g -> Tuple7(a, b, c, d, e, f, g) }
.zip(h) { (a, b, c, d, e, f, g), h -> Tuple8(a, b, c, d, e, f, g, h) }
.zip(i) { (a, b, c, d, e, f, g, h), i -> Tuple9(a, b, c, d, e, f, g, h, i) }
.zip(j) { (a, b, c, d, e, f, g, h, i), j -> Tuple10(a, b, c, d, e, f, g, h, i, j) }
result shouldBe expected
}
}
"can align lists with different lengths" {
checkAll(Arb.list(Arb.boolean()), Arb.list(Arb.boolean())) { a, b ->
a.align(b).size shouldBe max(a.size, b.size)
}
checkAll(Arb.list(Arb.boolean()), Arb.list(Arb.boolean())) { a, b ->
a.align(b).take(min(a.size, b.size)).forEach {
it.isBoth shouldBe true
}
}
checkAll(Arb.list(Arb.boolean()), Arb.list(Arb.boolean())) { a, b ->
a.align(b).drop(min(a.size, b.size)).forEach {
if (a.size < b.size) {
it.isRight shouldBe true
} else {
it.isLeft shouldBe true
}
}
}
}
"leftPadZip (with map)" {
checkAll(Arb.list(Arb.int()), Arb.list(Arb.int())) { a, b ->
val left = a.map { it } + List(max(0, b.count() - a.count())) { null }
val right = b.map { it } + List(max(0, a.count() - b.count())) { null }
val result = a.leftPadZip(b) { a, b -> a to b }
result shouldBe left.zip(right) { l, r -> l to r }.filter { it.second != null }
}
}
"leftPadZip (without map)" {
checkAll(Arb.list(Arb.int()), Arb.list(Arb.int())) { a, b ->
val left = a.map { it } + List(max(0, b.count() - a.count())) { null }
val right = b.map { it } + List(max(0, a.count() - b.count())) { null }
val result = a.leftPadZip(b)
result shouldBe left.zip(right) { l, r -> l to r }.filter { it.second != null }
}
}
"rightPadZip (without map)" {
checkAll(Arb.list(Arb.int()), Arb.list(Arb.int())) { a, b ->
val left = a.map { it } + List(max(0, b.count() - a.count())) { null }
val right = b.map { it } + List(max(0, a.count() - b.count())) { null }
val result = a.rightPadZip(b)
result shouldBe left.zip(right) { l, r -> l to r }.filter { it.first != null }
result.map { it.first } shouldBe a
}
}
"rightPadZip (with map)" {
checkAll(Arb.list(Arb.int()), Arb.list(Arb.int())) { a, b ->
val left = a.map { it } + List(max(0, b.count() - a.count())) { null }
val right = b.map { it } + List(max(0, a.count() - b.count())) { null }
val result = a.rightPadZip(b) { a, b -> a to b }
result shouldBe left.zip(right) { l, r -> l to r }.filter { it.first != null }
result.map { it.first } shouldBe a
}
}
"padZip" {
checkAll(Arb.list(Arb.int()), Arb.list(Arb.int())) { a, b ->
val left = a.map { it } + List(max(0, b.count() - a.count())) { null }
val right = b.map { it } + List(max(0, a.count() - b.count())) { null }
a.padZip(b) { l, r -> Ior.fromNullables(l, r) } shouldBe left.zip(right) { l, r -> Ior.fromNullables(l, r) }
}
}
"padZipWithNull" {
checkAll(Arb.list(Arb.int()), Arb.list(Arb.int())) { a, b ->
val left = a.map { it } + List(max(0, b.count() - a.count())) { null }
val right = b.map { it } + List(max(0, a.count() - b.count())) { null }
a.padZip(b) shouldBe left.zip(right) { l, r -> l to r }
}
}
"filterOption" {
checkAll(Arb.list(Arb.option(Arb.int()))) { listOfOption ->
listOfOption.filterOption() shouldBe listOfOption.mapNotNull { it.orNull() }
}
}
"flattenOption" {
checkAll(Arb.list(Arb.option(Arb.int()))) { listOfOption ->
listOfOption.flattenOption() shouldBe listOfOption.mapNotNull { it.orNull() }
}
}
}
}