-
Notifications
You must be signed in to change notification settings - Fork 437
/
NonEmptyListTest.kt
271 lines (245 loc) · 8.73 KB
/
NonEmptyListTest.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
package arrow.core
import arrow.core.test.UnitSpec
import arrow.core.test.laws.SemigroupLaws
import arrow.typeclasses.Semigroup
import io.kotest.property.Arb
import io.kotest.matchers.shouldBe
import io.kotest.property.arbitrary.boolean
import io.kotest.property.arbitrary.int
import kotlin.math.max
import kotlin.math.min
class NonEmptyListTest : UnitSpec() {
init {
testLaws(SemigroupLaws.laws(Semigroup.nonEmptyList(), Arb.nonEmptyList(Arb.int())))
"traverse for Either stack-safe" {
// also verifies result order and execution order (l to r)
val acc = mutableListOf<Int>()
val res = (0..20_000).toNonEmptyListOrNull()?.traverse { a ->
acc.add(a)
Either.Right(a)
}
res shouldBe Either.Right(acc.toNonEmptyListOrNull())
res shouldBe Either.Right((0..20_000).toNonEmptyListOrNull())
}
"traverse for Either short-circuit" {
checkAll(Arb.nonEmptyList(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 }
}
}
}
"sequence for Either should be consistent with traverseEither" {
checkAll(Arb.nonEmptyList(Arb.int())) { ints ->
ints.map { Either.conditionally(it % 2 == 0, { it }, { it }) }.sequence() shouldBe
ints.traverse { Either.conditionally(it % 2 == 0, { it }, { it }) }
}
}
"traverse for Option is stack-safe" {
// also verifies result order and execution order (l to r)
val acc = mutableListOf<Int>()
val res = (0..20_000).toNonEmptyListOrNull()?.traverse { a ->
acc.add(a)
Some(a)
}
res shouldBe Some(acc.toNonEmptyListOrNull())
res shouldBe Some((0..20_000).toNonEmptyListOrNull())
}
"traverse for Option short-circuits" {
checkAll(Arb.nonEmptyList(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 for Option yields some when all entries in the list are some" {
checkAll(Arb.nonEmptyList(Arb.int())) { ints ->
val evens = ints.map { (it % 2 == 0).maybe { it } }.sequence()
evens.fold({ Unit }) { it shouldBe ints }
}
}
"sequence for Option should be consistent with traverseOption" {
checkAll(Arb.nonEmptyList(Arb.int())) { ints ->
ints.map { (it % 2 == 0).maybe { it } }.sequence() shouldBe
ints.traverse { (it % 2 == 0).maybe { it } }
}
}
"traverse for 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 for Validated acummulates" {
checkAll(Arb.nonEmptyList(Arb.int())) { ints ->
val res: ValidatedNel<Int, NonEmptyList<Int>> =
ints.traverse(Semigroup.nonEmptyList()) { i: Int -> if (i % 2 == 0) i.validNel() else i.invalidNel() }
val expected: ValidatedNel<Int, NonEmptyList<Int>> =
ints.filterNot { it % 2 == 0 }.toNonEmptyListOrNull()?.invalid() ?: ints.filter { it % 2 == 0 }.toNonEmptyListOrNull()!!.valid()
res shouldBe expected
}
}
"sequence for Validated should be consistent with traverseValidated" {
checkAll(Arb.nonEmptyList(Arb.int())) { ints ->
ints.map { if (it % 2 == 0) Valid(it) else Invalid(it) }.sequence(Semigroup.int()) shouldBe
ints.traverse(Semigroup.int()) { if (it % 2 == 0) Valid(it) else Invalid(it) }
}
}
"can align lists with different lengths" {
checkAll(Arb.nonEmptyList(Arb.boolean()), Arb.nonEmptyList(Arb.boolean())) { a, b ->
a.align(b).size shouldBe max(a.size, b.size)
}
checkAll(Arb.nonEmptyList(Arb.boolean()), Arb.nonEmptyList(Arb.boolean())) { a, b ->
a.align(b).all.take(min(a.size, b.size)).forEach {
it.isBoth shouldBe true
}
}
}
"zip2" {
checkAll(Arb.nonEmptyList(Arb.int()), Arb.nonEmptyList(Arb.int())) { a, b ->
val result = a.zip(b)
val expected = a.all.zip(b.all).toNonEmptyListOrNull()
result shouldBe expected
}
}
"zip3" {
checkAll(
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int())
) { a, b, c ->
val result = a.zip(b, c, ::Triple)
val expected = a.all.zip(b.all, c.all, ::Triple).toNonEmptyListOrNull()
result shouldBe expected
}
}
"zip4" {
checkAll(
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int())
) { a, b, c, d ->
val result = a.zip(b, c, d, ::Tuple4)
val expected = a.all.zip(b.all, c.all, d.all, ::Tuple4).toNonEmptyListOrNull()
result shouldBe expected
}
}
"zip5" {
checkAll(
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int())
) { a, b, c, d, e ->
val result = a.zip(b, c, d, e, ::Tuple5)
val expected = a.all.zip(b.all, c.all, d.all, e.all, ::Tuple5).toNonEmptyListOrNull()
result shouldBe expected
}
}
"zip6" {
checkAll(
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int())
) { a, b, c, d, e, f ->
val result = a.zip(b, c, d, e, f, ::Tuple6)
val expected =
a.all.zip(b.all, c.all, d.all, e.all, f.all, ::Tuple6).toNonEmptyListOrNull()
result shouldBe expected
}
}
"zip7" {
checkAll(
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int())
) { a, b, c, d, e, f, g ->
val result = a.zip(b, c, d, e, f, g, ::Tuple7)
val expected =
a.all.zip(b.all, c.all, d.all, e.all, f.all, g.all, ::Tuple7).toNonEmptyListOrNull()
result shouldBe expected
}
}
"zip8" {
checkAll(
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(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.all.zip(b.all, c.all, d.all, e.all, f.all, g.all, h.all, ::Tuple8)
.toNonEmptyListOrNull()
result shouldBe expected
}
}
"zip9" {
checkAll(
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(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.all.zip(b.all, c.all, d.all, e.all, f.all, g.all, h.all, i.all, ::Tuple9)
.toNonEmptyListOrNull()
result shouldBe expected
}
}
"zip10" {
checkAll(
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(Arb.int()),
Arb.nonEmptyList(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.all.zip(b.all, c.all, d.all, e.all, f.all, g.all, h.all, i.all, j.all, ::Tuple10)
.toNonEmptyListOrNull()
result shouldBe expected
}
}
}
}