Skip to content

Commit

Permalink
jacoco support
Browse files Browse the repository at this point in the history
  • Loading branch information
belyaev-mikhail committed Jul 23, 2021
1 parent bf4dac9 commit f405c26
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/commonMain/kotlin/ru/spbstu/wheels/Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ inline fun <A, B, R, C: MutableCollection<R>> Iterable<A>.productTo(that: Iterab
fun <A, B, C: MutableCollection<Pair<A, B>>> Iterable<A>.productTo(that: Iterable<B>, to: C): C =
productTo(that, to, ::Pair)

fun <T> Iterable<Iterable<T>>.product(): List<List<T>> =
fold(mutableListOf(listOf())) { acc, set ->
acc.flatMapTo(mutableListOf()) { list: List<T> ->
set.map { element -> list + element }
}
}


fun <T> List<T>.firstOption(): Option<T> = when {
isEmpty() -> Option.empty()
else -> Option.just(get(0))
Expand Down
48 changes: 48 additions & 0 deletions src/commonTest/kotlin/ru/spbstu/wheels/CollectionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ru.spbstu.wheels

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertSame

class CollectionsTest {
@Test
Expand Down Expand Up @@ -59,6 +60,53 @@ class CollectionsTest {
(lst1 product lst2).toSet(),
(lst2 product lst1).map { (a, b) -> b to a }.toSet()
)

run {
val l1 = listOf(1, 2, 3)
val l2 = listOf(4, 5, 6)
val l3 = mutableListOf<Pair<Int, Int>>()

val l3copy = l1.productTo(l2, l3)

assertSame(l3, l3copy)
assertEquals(
listOf(
1 to 4, 1 to 5, 1 to 6,
2 to 4, 2 to 5, 2 to 6,
3 to 4, 3 to 5, 3 to 6
),
l3
)
}

run {
val base = listOf(listOf(1, 2, 3), listOf(4, 5, 6))
val p = base.product()

assertEquals(
listOf(
listOf(1, 4), listOf(1, 5), listOf(1, 6),
listOf(2, 4), listOf(2, 5), listOf(2, 6),
listOf(3, 4), listOf(3, 5), listOf(3, 6),
),
p
)
}

run {
val base = listOf(listOf(1, 2), listOf(4, 5), listOf(7, 8))
val p = base.product()

assertEquals(
listOf(
listOf(1, 4, 7), listOf(1, 4, 8),
listOf(1, 5, 7), listOf(1, 5, 8),
listOf(2, 4, 7), listOf(2, 4, 8),
listOf(2, 5, 7), listOf(2, 5, 8),
),
p
)
}
}

@Test
Expand Down

0 comments on commit f405c26

Please sign in to comment.