Skip to content

Commit 3fd4fb1

Browse files
johanjanssenKevinGilmore
authored andcommitted
Kotlin split list in pairs examples. (eugenp#6368)
* Kotlin split list in pairs examples. * Renamed some items.
1 parent ae6c29d commit 3fd4fb1

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.baeldung.lambda
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.test.assertEquals
5+
6+
class SplittingTest {
7+
private val evenList = listOf(0, "a", 1, "b", 2, "c");
8+
9+
private val unevenList = listOf(0, "a", 1, "b", 2, "c", 3);
10+
11+
private fun verifyList(resultList: List<List<Any>>) {
12+
assertEquals("[[0, a], [1, b], [2, c]]", resultList.toString())
13+
}
14+
15+
private fun verifyPartialList(resultList: List<List<Any>>) {
16+
assertEquals("[[0, a], [1, b], [2, c], [3]]", resultList.toString())
17+
}
18+
19+
@Test
20+
fun whenChunked_thenListIsSplit() {
21+
val resultList = evenList.chunked(2)
22+
verifyList(resultList)
23+
}
24+
25+
@Test
26+
fun whenUnevenChunked_thenListIsSplit() {
27+
val resultList = unevenList.chunked(2)
28+
verifyPartialList(resultList)
29+
}
30+
31+
@Test
32+
fun whenWindowed_thenListIsSplit() {
33+
val resultList = evenList.windowed(2, 2)
34+
verifyList(resultList)
35+
}
36+
37+
@Test
38+
fun whenUnevenPartialWindowed_thenListIsSplit() {
39+
val resultList = unevenList.windowed(2, 2, partialWindows = true)
40+
verifyPartialList(resultList)
41+
}
42+
43+
@Test
44+
fun whenUnevenWindowed_thenListIsSplit() {
45+
val resultList = unevenList.windowed(2, 2, partialWindows = false)
46+
verifyList(resultList)
47+
}
48+
49+
@Test
50+
fun whenGroupByWithAscendingNumbers_thenListIsSplit() {
51+
val numberList = listOf(1, 2, 3, 4, 5, 6);
52+
val resultList = numberList.groupBy { (it + 1) / 2 }
53+
assertEquals("[[1, 2], [3, 4], [5, 6]]", resultList.values.toString())
54+
assertEquals("[1, 2, 3]", resultList.keys.toString())
55+
}
56+
57+
@Test
58+
fun whenGroupByWithAscendingNumbersUneven_thenListIsSplit() {
59+
val numberList = listOf(1, 2, 3, 4, 5, 6, 7);
60+
val resultList = numberList.groupBy { (it + 1) / 2 }.values
61+
assertEquals("[[1, 2], [3, 4], [5, 6], [7]]", resultList.toString())
62+
}
63+
64+
@Test
65+
fun whenGroupByWithRandomNumbers_thenListIsSplitInWrongWay() {
66+
val numberList = listOf(1, 3, 8, 20, 23, 30);
67+
val resultList = numberList.groupBy { (it + 1) / 2 }
68+
assertEquals("[[1], [3], [8], [20], [23], [30]]", resultList.values.toString())
69+
assertEquals("[1, 2, 4, 10, 12, 15]", resultList.keys.toString())
70+
}
71+
72+
@Test
73+
fun whenWithIndexGroupBy_thenListIsSplit() {
74+
val resultList = evenList.withIndex()
75+
.groupBy { it.index / 2 }
76+
.map { it.value.map { it.value } }
77+
verifyList(resultList)
78+
}
79+
80+
@Test
81+
fun whenWithIndexGroupByUneven_thenListIsSplit() {
82+
val resultList = unevenList.withIndex()
83+
.groupBy { it.index / 2 }
84+
.map { it.value.map { it.value } }
85+
verifyPartialList(resultList)
86+
}
87+
88+
@Test
89+
fun whenFoldIndexed_thenListIsSplit() {
90+
val resultList = evenList.foldIndexed(ArrayList<ArrayList<Any>>(evenList.size / 2)) { index, acc, item ->
91+
if (index % 2 == 0) {
92+
acc.add(ArrayList(2))
93+
}
94+
acc.last().add(item)
95+
acc
96+
}
97+
verifyList(resultList)
98+
}
99+
}

0 commit comments

Comments
 (0)