-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCartesianProductTest.kt
138 lines (126 loc) · 3.92 KB
/
CartesianProductTest.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
package com.marcinmoskala.math.tests
import com.marcinmoskala.math.cartesianProduct
import io.kotest.property.Arb
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.next
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals
internal class CartesianProductTest {
@Test
fun `cartesian product with size more than MAX_INT throws`() {
assertThrows(IllegalArgumentException::class.java) {
listOf(1, 2, 3, 5, 6, 7, 8, 9, 0).cartesianProduct(10).toList()
}
}
@Test
fun `cartesian product with repeat less than 1 throws`() {
val repeat = Arb.int(-100..0).next()
assertThrows(IllegalArgumentException::class.java) {
listOf(1, 2, 3).cartesianProduct(repeat)
}
}
@Test
fun `cartesian product with repeat = 1`() {
val actual = listOf(1, 2, 3).cartesianProduct(1).toList()
val expected = listOf(
listOf(1),
listOf(2),
listOf(3)
)
assertEquals(expected, actual)
}
@Test
fun `cartesian product with repeat`() {
val actual = listOf(1, 2, 3).cartesianProduct(2).toList()
val expected = listOf(
listOf(1, 1),
listOf(1, 2),
listOf(1, 3),
listOf(2, 1),
listOf(2, 2),
listOf(2, 3),
listOf(3, 1),
listOf(3, 2),
listOf(3, 3)
)
assertEquals(expected, actual)
}
@Test
fun `cartesian product of different sizes`() {
val actual = listOf(1, 2, 3).cartesianProduct(listOf(8, 9)).toList()
val expected = listOf(
listOf(1, 8),
listOf(1, 9),
listOf(2, 8),
listOf(2, 9),
listOf(3, 8),
listOf(3, 9)
)
assertEquals(expected, actual)
}
@Test
fun `cartesian product of same sizes`() {
val actual = listOf(1, 2).cartesianProduct(listOf(8, 9)).toList()
val expected = listOf(
listOf(1, 8),
listOf(1, 9),
listOf(2, 8),
listOf(2, 9)
)
assertEquals(expected, actual)
}
@Test
fun `cartesian product of 3 lists`() {
val actual = listOf(1, 2).cartesianProduct(
listOf(3, 4),
listOf(5, 6)
).toList()
val expected = listOf(
listOf(1, 3, 5),
listOf(1, 3, 6),
listOf(1, 4, 5),
listOf(1, 4, 6),
listOf(2, 3, 5),
listOf(2, 3, 6),
listOf(2, 4, 5),
listOf(2, 4, 6)
)
assertEquals(expected, actual)
}
@Test
fun `cartesian product of 3 lists with different size`() {
val actual = listOf(0, 1, 2).cartesianProduct(
listOf(3, 4),
listOf(5, 6)
).toList()
val expected = listOf(
listOf(0, 3, 5),
listOf(0, 3, 6),
listOf(0, 4, 5),
listOf(0, 4, 6),
listOf(1, 3, 5),
listOf(1, 3, 6),
listOf(1, 4, 5),
listOf(1, 4, 6),
listOf(2, 3, 5),
listOf(2, 3, 6),
listOf(2, 4, 5),
listOf(2, 4, 6)
)
assertEquals(expected, actual)
}
@Test
fun `cartesian product of 2 lists with different types`() {
val expected = listOf(
listOf("A", 1),
listOf("A", 2),
listOf("A", 3),
listOf("B", 1),
listOf("B", 2),
listOf("B", 3)
)
val actual = listOf("A", "B").cartesianProduct(listOf(1, 2, 3)).toList()
assertEquals(expected, actual)
}
}