-
Notifications
You must be signed in to change notification settings - Fork 17
/
CombinationTest.kt
59 lines (52 loc) · 2.2 KB
/
CombinationTest.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
package com.marcinmoskala.math.tests
import com.marcinmoskala.math.combinations
import com.marcinmoskala.math.combinationsNumber
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
internal class CombinationTest {
@Test
fun `combinations function is throwing error when asked for subsets of size smaller then 0`() {
val set = setOf(1, 2, 3, 4)
for (subsetSize in -10..-1) {
assertIsThrowingError { set.combinations(subsetSize) }
}
}
@Test fun `combinationsNumber function is throwing error when asked for subsets of size smaller then 0`() {
val set = setOf(1, 2, 3, 4)
for (subsetSize in -10..-1) {
assertIsThrowingError { set.combinationsNumber(subsetSize) }
}
}
@Test fun `Test combinationsNumber for Sets with different sizes`() {
val set = setOf(1, 2, 3, 4)
val subsetSizeToCombinationNumber = mapOf(
0 to 1L,
1 to 4L,
2 to 6L,
3 to 4L,
4 to 1L,
5 to 1L
)
for ((subsetSize, expectedCombinationNumber) in subsetSizeToCombinationNumber) {
assertEquals(expectedCombinationNumber, set.combinationsNumber(subsetSize))
}
}
@Test fun `Test combinations for Sets with different sizes`() {
val set = setOf(1, 2, 3, 4)
val sizeToCombinations = mapOf(
0 to setOf(setOf()),
1 to setOf(setOf(1), setOf(2), setOf(3), setOf(4)),
2 to setOf(setOf(1, 2), setOf(2, 3), setOf(3, 4), setOf(1, 3), setOf(2, 4), setOf(1, 4)),
3 to setOf(setOf(1, 2, 3), setOf(1, 2, 4), setOf(1, 3, 4), setOf(2, 3, 4)),
4 to setOf(setOf(1, 2, 3, 4)),
5 to setOf(setOf(1, 2, 3, 4))
)
for ((subsetSize, expectedCombinations) in sizeToCombinations) {
assertEquals(expectedCombinations, set.combinations(subsetSize))
}
}
@Test fun `Test combinations size and combinationsNumber correctness`() {
val set = (1..6).toSet()
(1..7).forEach { i -> assertEquals(set.combinationsNumber(i), set.combinations(i).size.toLong()) }
}
}