-
Notifications
You must be signed in to change notification settings - Fork 437
/
MapKTest.kt
315 lines (272 loc) · 9.39 KB
/
MapKTest.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
package arrow.core
import arrow.core.test.UnitSpec
import arrow.core.test.generators.intSmall
import arrow.core.test.generators.longSmall
import arrow.core.test.laws.MonoidLaws
import arrow.typeclasses.Monoid
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 io.kotest.property.arbitrary.long
import io.kotest.property.arbitrary.string
class MapKTest : UnitSpec() {
init {
testLaws(
MonoidLaws.laws(
Monoid.map(Semigroup.int()),
Arb.map(Arb.longSmall(), Arb.intSmall(), maxSize = 10)
)
)
"traverseEither is stacksafe" {
val acc = mutableListOf<Int>()
val res = (0..20_000).map { it to it }.toMap().traverse { v ->
acc.add(v)
Either.Right(v)
}
res shouldBe acc.map { it to it }.toMap().right()
res shouldBe (0..20_000).map { it to it }.toMap().right()
}
"traverseEither short-circuit" {
checkAll(Arb.map(Arb.int(), 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.values.takeWhile { it % 2 == 0 }
when (evens) {
is Either.Right -> evens.value shouldBe ints
is Either.Left -> evens.value shouldBe ints.values.first { it % 2 != 0 }
}
}
}
"traverseOption is stack-safe" {
// also verifies result order and execution order (l to r)
val acc = mutableListOf<Int>()
val res = (0..20_000).map { it to it }.toMap().traverse { a ->
acc.add(a)
Some(a)
}
res shouldBe Some(acc.map { it to it }.toMap())
res shouldBe Some((0..20_000).map { it to it }.toMap())
}
"traverseOption 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 }
}
}
"sequenceOption 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 }
}
}
"traverseValidated is stacksafe" {
val acc = mutableListOf<Int>()
val res = (0..20_000).map { it to it }.toMap().traverse(Semigroup.string()) { v ->
acc.add(v)
Validated.Valid(v)
}
res shouldBe acc.map { it to it }.toMap().valid()
res shouldBe (0..20_000).map { it to it }.toMap().valid()
}
"traverseValidated acummulates" {
checkAll(Arb.map(Arb.int(), Arb.int())) { ints ->
val res: ValidatedNel<Int, Map<Int, Int>> =
ints.traverse(Semigroup.nonEmptyList()) { i -> if (i % 2 == 0) i.validNel() else i.invalidNel() }
val expected: ValidatedNel<Int, Map<Int, Int>> =
Option.fromNullable(ints.values.filterNot { it % 2 == 0 }.toNonEmptyListOrNull())
.fold(
{ ints.entries.filter { (_, v) -> v % 2 == 0 }.map { (k, v) -> k to v }.toMap().validNel() },
{ it.invalid() })
res shouldBe expected
}
}
"can align maps" {
// aligned keySet is union of a's and b's keys
checkAll(Arb.map(Arb.long(), Arb.boolean()), Arb.map(Arb.long(), Arb.boolean())) { a, b ->
val aligned = a.align(b)
aligned.size shouldBe (a.keys + b.keys).size
}
// aligned map contains Both for all entries existing in a and b
checkAll(Arb.map(Arb.long(), Arb.boolean()), Arb.map(Arb.long(), Arb.boolean())) { a, b ->
val aligned = a.align(b)
a.keys.intersect(b.keys).forEach {
aligned[it]?.isBoth shouldBe true
}
}
// aligned map contains Left for all entries existing only in a
checkAll(Arb.map(Arb.long(), Arb.boolean()), Arb.map(Arb.long(), Arb.boolean())) { a, b ->
val aligned = a.align(b)
(a.keys - b.keys).forEach { key ->
aligned[key]?.isLeft shouldBe true
}
}
// aligned map contains Right for all entries existing only in b
checkAll(Arb.map(Arb.long(), Arb.boolean()), Arb.map(Arb.long(), Arb.boolean())) { a, b ->
val aligned = a.align(b)
(b.keys - a.keys).forEach { key ->
aligned[key]?.isRight shouldBe true
}
}
}
"zip2" {
checkAll(
Arb.map(Arb.intSmall(), Arb.intSmall()),
Arb.map(Arb.intSmall(), Arb.intSmall())
) { a, b ->
val result = a.zip(b) { _, aa, bb -> Pair(aa, bb) }
val expected = a.filter { (k, _) -> b.containsKey(k) }
.map { (k, v) -> Pair(k, Pair(v, b[k]!!)) }
.toMap()
result shouldBe expected
}
}
"zip3" {
checkAll(
Arb.map(Arb.intSmall(), Arb.intSmall()),
Arb.map(Arb.intSmall(), Arb.intSmall())
) { a, b ->
val result = a.zip(b, b) { _, aa, bb, cc -> Triple(aa, bb, cc) }
val expected = a.filter { (k, _) -> b.containsKey(k) }
.map { (k, v) -> Pair(k, Triple(v, b[k]!!, b[k]!!)) }
.toMap()
result shouldBe expected
}
}
"zip4" {
checkAll(
Arb.map(Arb.intSmall(), Arb.intSmall()),
Arb.map(Arb.intSmall(), Arb.intSmall()),
) { a, b ->
val result = a.zip(b, b, b) { _, aa, bb, cc, dd -> Tuple4(aa, bb, cc, dd) }
val expected = a.filter { (k, _) -> b.containsKey(k) }
.map { (k, v) -> Pair(k, Tuple4(v, b[k]!!, b[k]!!, b[k]!!)) }
.toMap()
result shouldBe expected
}
}
"zip5" {
checkAll(
Arb.map(Arb.intSmall(), Arb.intSmall()),
Arb.map(Arb.intSmall(), Arb.intSmall()),
) { a, b ->
val result = a.zip(b, b, b, b) { _, aa, bb, cc, dd, ee -> Tuple5(aa, bb, cc, dd, ee) }
val expected = a.filter { (k, _) -> b.containsKey(k) }
.map { (k, v) -> Pair(k, Tuple5(v, b[k]!!, b[k]!!, b[k]!!, b[k]!!)) }
.toMap()
result shouldBe expected
}
}
"zip6" {
checkAll(
Arb.map(Arb.intSmall(), Arb.intSmall()),
Arb.map(Arb.intSmall(), Arb.intSmall()),
) { a, b ->
val result = a.zip(b, b, b, b, b) { _, aa, bb, cc, dd, ee, ff -> Tuple6(aa, bb, cc, dd, ee, ff) }
val expected = a.filter { (k, _) -> b.containsKey(k) }
.map { (k, v) -> Pair(k, Tuple6(v, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!)) }
.toMap()
result shouldBe expected
}
}
"zip7" {
checkAll(
Arb.map(Arb.intSmall(), Arb.intSmall()),
Arb.map(Arb.intSmall(), Arb.intSmall())
) { a, b ->
val result = a.zip(b, b, b, b, b, b) { _, aa, bb, cc, dd, ee, ff, gg -> Tuple7(aa, bb, cc, dd, ee, ff, gg) }
val expected = a.filter { (k, _) -> b.containsKey(k) }
.map { (k, v) -> Pair(k, Tuple7(v, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!)) }
.toMap()
result shouldBe expected
}
}
"zip8" {
checkAll(
Arb.map(Arb.intSmall(), Arb.intSmall()),
Arb.map(Arb.intSmall(), Arb.intSmall())
) { a, b ->
val result =
a.zip(b, b, b, b, b, b, b) { _, aa, bb, cc, dd, ee, ff, gg, hh -> Tuple8(aa, bb, cc, dd, ee, ff, gg, hh) }
val expected = a.filter { (k, _) -> b.containsKey(k) }
.map { (k, v) -> Pair(k, Tuple8(v, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!)) }
.toMap()
result shouldBe expected
}
}
"zip9" {
checkAll(
Arb.map(Arb.intSmall(), Arb.intSmall()),
Arb.map(Arb.intSmall(), Arb.intSmall())
) { a, b ->
val result = a.zip(b, b, b, b, b, b, b, b) { _, aa, bb, cc, dd, ee, ff, gg, hh, ii ->
Tuple9(
aa,
bb,
cc,
dd,
ee,
ff,
gg,
hh,
ii
)
}
val expected = a.filter { (k, _) -> b.containsKey(k) }
.map { (k, v) -> Pair(k, Tuple9(v, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!)) }
.toMap()
result shouldBe expected
}
}
"zip10" {
checkAll(
Arb.map(Arb.intSmall(), Arb.intSmall()),
Arb.map(Arb.intSmall(), Arb.intSmall())
) { a, b ->
val result = a.zip(b, b, b, b, b, b, b, b, b) { _, aa, bb, cc, dd, ee, ff, gg, hh, ii, jj ->
Tuple10(
aa,
bb,
cc,
dd,
ee,
ff,
gg,
hh,
ii,
jj
)
}
val expected = a.filter { (k, _) -> b.containsKey(k) }
.map { (k, v) -> Pair(k, Tuple10(v, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!, b[k]!!)) }
.toMap()
result shouldBe expected
}
}
"flatMap" {
checkAll(
Arb.map(Arb.string(), Arb.intSmall()),
Arb.map(Arb.string(), Arb.string())
) { a, b ->
val result: Map<String, String> = a.flatMap { b }
val expected: Map<String, String> = a.filter { (k, _) -> b.containsKey(k) }
.map { (k, _) -> Pair(k, b[k]!!) }
.toMap()
result shouldBe expected
}
}
}
}