From 29ddb08808161d6e56e4850f4ad61350ec790d41 Mon Sep 17 00:00:00 2001 From: Mona Mohamadinia Date: Fri, 19 Jun 2020 19:30:11 +0430 Subject: [PATCH] Introducing windowed() and chunked() (#9490) --- .../baeldung/collections/CollectionsTest.kt | 180 ++++++++++++------ 1 file changed, 123 insertions(+), 57 deletions(-) diff --git a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/CollectionsTest.kt b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/CollectionsTest.kt index 64b1f72eab..67fbb29026 100644 --- a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/CollectionsTest.kt +++ b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/CollectionsTest.kt @@ -1,5 +1,6 @@ package com.baeldung.collections +import org.assertj.core.api.Assertions.assertThat import org.junit.Test import kotlin.test.assertEquals import kotlin.test.assertFalse @@ -8,139 +9,204 @@ import kotlin.test.assertTrue class CollectionsTest { @Test - fun whenUseDifferentCollections_thenSuccess () { + fun whenUseDifferentCollections_thenSuccess() { val theList = listOf("one", "two", "three") - assertTrue(theList.contains("two")) + assertTrue(theList.contains("two")) val theMutableList = mutableListOf("one", "two", "three") theMutableList.add("four") - assertTrue(theMutableList.contains("four")) + assertTrue(theMutableList.contains("four")) val theSet = setOf("one", "two", "three") - assertTrue(theSet.contains("three")) + assertTrue(theSet.contains("three")) val theMutableSet = mutableSetOf("one", "two", "three") theMutableSet.add("four") - assertTrue(theMutableSet.contains("four")) + assertTrue(theMutableSet.contains("four")) val theMap = mapOf(1 to "one", 2 to "two", 3 to "three") - assertEquals(theMap[2],"two") + assertEquals(theMap[2], "two") val theMutableMap = mutableMapOf(1 to "one", 2 to "two", 3 to "three") theMutableMap[4] = "four" - assertEquals(theMutableMap[4],"four") + assertEquals(theMutableMap[4], "four") } @Test - fun whenSliceCollection_thenSuccess () { + fun whenSliceCollection_thenSuccess() { val theList = listOf("one", "two", "three") val resultList = theList.slice(1..2) - assertEquals(2, resultList.size) - assertTrue(resultList.contains("two")) + assertEquals(2, resultList.size) + assertTrue(resultList.contains("two")) } - + @Test - fun whenJoinTwoCollections_thenSuccess () { + fun whenJoinTwoCollections_thenSuccess() { val firstList = listOf("one", "two", "three") val secondList = listOf("four", "five", "six") val resultList = firstList + secondList - assertEquals(6, resultList.size) - assertTrue(resultList.contains("two")) - assertTrue(resultList.contains("five")) - } - + assertEquals(6, resultList.size) + assertTrue(resultList.contains("two")) + assertTrue(resultList.contains("five")) + } + @Test - fun whenFilterNullValues_thenSuccess () { + fun whenFilterNullValues_thenSuccess() { val theList = listOf("one", null, "two", null, "three") val resultList = theList.filterNotNull() - assertEquals(3, resultList.size) - } + assertEquals(3, resultList.size) + } @Test - fun whenFilterNonPositiveValues_thenSuccess () { + fun whenFilterNonPositiveValues_thenSuccess() { val theList = listOf(1, 2, -3, -4, 5, -6) - val resultList = theList.filter{ it > 0} + val resultList = theList.filter { it > 0 } //val resultList = theList.filter{ x -> x > 0} - assertEquals(3, resultList.size) + assertEquals(3, resultList.size) assertTrue(resultList.contains(1)) - assertFalse(resultList.contains(-4)) - } + assertFalse(resultList.contains(-4)) + } @Test - fun whenDropFirstItems_thenRemoved () { + fun whenDropFirstItems_thenRemoved() { val theList = listOf("one", "two", "three", "four") val resultList = theList.drop(2) - assertEquals(2, resultList.size) - assertFalse(resultList.contains("one")) - assertFalse(resultList.contains("two")) - } + assertEquals(2, resultList.size) + assertFalse(resultList.contains("one")) + assertFalse(resultList.contains("two")) + } @Test - fun whenDropFirstItemsBasedOnCondition_thenRemoved () { + fun whenDropFirstItemsBasedOnCondition_thenRemoved() { val theList = listOf("one", "two", "three", "four") - val resultList = theList.dropWhile{ it.length < 4 } + val resultList = theList.dropWhile { it.length < 4 } - assertEquals(2, resultList.size) - assertFalse(resultList.contains("one")) - assertFalse(resultList.contains("two")) - } + assertEquals(2, resultList.size) + assertFalse(resultList.contains("one")) + assertFalse(resultList.contains("two")) + } @Test - fun whenExcludeItems_thenRemoved () { + fun whenExcludeItems_thenRemoved() { val firstList = listOf("one", "two", "three") val secondList = listOf("one", "three") val resultList = firstList - secondList - assertEquals(1, resultList.size) - assertTrue(resultList.contains("two")) - } + assertEquals(1, resultList.size) + assertTrue(resultList.contains("two")) + } @Test - fun whenSearchForExistingItem_thenFound () { + fun whenSearchForExistingItem_thenFound() { val theList = listOf("one", "two", "three") - assertTrue("two" in theList) - } + assertTrue("two" in theList) + } @Test - fun whenGroupItems_thenSuccess () { + fun whenGroupItems_thenSuccess() { val theList = listOf(1, 2, 3, 4, 5, 6) - val resultMap = theList.groupBy{ it % 3} + val resultMap = theList.groupBy { it % 3 } - assertEquals(3, resultMap.size) + assertEquals(3, resultMap.size) print(resultMap[1]) assertTrue(resultMap[1]!!.contains(1)) - assertTrue(resultMap[2]!!.contains(5)) - } + assertTrue(resultMap[2]!!.contains(5)) + } @Test - fun whenApplyFunctionToAllItems_thenSuccess () { + fun whenApplyFunctionToAllItems_thenSuccess() { val theList = listOf(1, 2, 3, 4, 5, 6) - val resultList = theList.map{ it * it } + val resultList = theList.map { it * it } print(resultList) assertEquals(4, resultList[1]) assertEquals(9, resultList[2]) - } + } @Test - fun whenApplyMultiOutputFunctionToAllItems_thenSuccess () { + fun whenApplyMultiOutputFunctionToAllItems_thenSuccess() { val theList = listOf("John", "Tom") - val resultList = theList.flatMap{ it.toLowerCase().toList()} + val resultList = theList.flatMap { it.toLowerCase().toList() } print(resultList) assertEquals(7, resultList.size) assertTrue(resultList.contains('j')) - } + } @Test - fun whenApplyFunctionToAllItemsWithStartingValue_thenSuccess () { + fun whenApplyFunctionToAllItemsWithStartingValue_thenSuccess() { val theList = listOf(1, 2, 3, 4, 5, 6) - val finalResult = theList.fold( 1000, { oldResult, currentItem -> oldResult + (currentItem *currentItem)}) + val finalResult = theList.fold(1000, { oldResult, currentItem -> oldResult + (currentItem * currentItem) }) print(finalResult) assertEquals(1091, finalResult) } -} \ No newline at end of file + + @Test + fun whenApplyingChunked_thenShouldBreakTheCollection() { + val theList = listOf(1, 2, 3, 4, 5) + val chunked = theList.chunked(2) + + assertThat(chunked.size).isEqualTo(3) + assertThat(chunked.first()).contains(1, 2) + assertThat(chunked[1]).contains(3, 4) + assertThat(chunked.last()).contains(5) + } + + @Test + fun whenApplyingChunkedWithTransformation_thenShouldBreakTheCollection() { + val theList = listOf(1, 2, 3, 4, 5) + val chunked = theList.chunked(3) { it.joinToString(", ") } + + assertThat(chunked.size).isEqualTo(2) + assertThat(chunked.first()).isEqualTo("1, 2, 3") + assertThat(chunked.last()).isEqualTo("4, 5") + } + + @Test + fun whenApplyingWindowed_thenShouldCreateSlidingWindowsOfElements() { + val theList = (1..6).toList() + val windowed = theList.windowed(3) + + assertThat(windowed.size).isEqualTo(4) + assertThat(windowed.first()).contains(1, 2, 3) + assertThat(windowed[1]).contains(2, 3, 4) + assertThat(windowed[2]).contains(3, 4, 5) + assertThat(windowed.last()).contains(4, 5, 6) + } + + @Test + fun whenApplyingWindowedWithTwoSteps_thenShouldCreateSlidingWindowsOfElements() { + val theList = (1..6).toList() + val windowed = theList.windowed(size = 3, step = 2) + + assertThat(windowed.size).isEqualTo(2) + assertThat(windowed.first()).contains(1, 2, 3) + assertThat(windowed.last()).contains(3, 4, 5) + } + + @Test + fun whenApplyingPartialWindowedWithTwoSteps_thenShouldCreateSlidingWindowsOfElements() { + val theList = (1..6).toList() + val windowed = theList.windowed(size = 3, step = 2, partialWindows = true) + + assertThat(windowed.size).isEqualTo(3) + assertThat(windowed.first()).contains(1, 2, 3) + assertThat(windowed[1]).contains(3, 4, 5) + assertThat(windowed.last()).contains(5, 6) + } + + @Test + fun whenApplyingTransformingWindows_thenShouldCreateSlidingWindowsOfElements() { + val theList = (1..6).toList() + val windowed = theList.windowed(size = 3, step = 2, partialWindows = true) { it.joinToString(", ") } + + assertThat(windowed.size).isEqualTo(3) + assertThat(windowed.first()).isEqualTo("1, 2, 3") + assertThat(windowed[1]).isEqualTo("3, 4, 5") + assertThat(windowed.last()).isEqualTo("5, 6") + } +}