kotlin collection (#2241)
* minor logging fix * spring security sso * use basic auth * use form login * cleanup * cleanup * final cleanup * second client app for sso * spring boot bootstrap * add logic * cleanup * add simple controller * add thymeleaf and security * minor fix * minor fix * add more boot properties * fix live test * fix live test * minor fix * semaphores * fix configuration * kotlin collection
This commit is contained in:
parent
e422edadd0
commit
fc33d62cc3
|
@ -0,0 +1,121 @@
|
|||
package com.baeldung.kotlin
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class CollectionsTest {
|
||||
|
||||
@Test
|
||||
fun whenUseDifferentCollections_thenSuccess () {
|
||||
val theList = listOf("one", "two", "three")
|
||||
assertTrue(theList.contains("two"))
|
||||
|
||||
val theMutableList = mutableListOf("one", "two", "three")
|
||||
theMutableList.add("four")
|
||||
assertTrue(theMutableList.contains("four"))
|
||||
|
||||
val theSet = setOf("one", "two", "three")
|
||||
assertTrue(theSet.contains("three"))
|
||||
|
||||
val theMutableSet = mutableSetOf("one", "two", "three")
|
||||
theMutableSet.add("four")
|
||||
assertTrue(theMutableSet.contains("four"))
|
||||
|
||||
val theMap = mapOf(1 to "one", 2 to "two", 3 to "three")
|
||||
assertEquals(theMap[2],"two")
|
||||
|
||||
val theMutableMap = mutableMapOf(1 to "one", 2 to "two", 3 to "three")
|
||||
theMutableMap[4] = "four"
|
||||
assertEquals(theMutableMap[4],"four")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenSliceCollection_thenSuccess () {
|
||||
val theList = listOf("one", "two", "three")
|
||||
val resultList = theList.slice(1..2)
|
||||
|
||||
assertEquals(2, resultList.size)
|
||||
assertTrue(resultList.contains("two"))
|
||||
}
|
||||
|
||||
@Test
|
||||
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"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenFilterNullValues_thenSuccess () {
|
||||
val theList = listOf("one", null, "two", null, "three")
|
||||
val resultList = theList.filterNotNull()
|
||||
|
||||
assertEquals(3, resultList.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenFilterNonPositiveValues_thenSuccess () {
|
||||
val theList = listOf(1, 2, -3, -4, 5, -6)
|
||||
val resultList = theList.filter{ it > 0}
|
||||
//val resultList = theList.filter{ x -> x > 0}
|
||||
|
||||
assertEquals(3, resultList.size)
|
||||
assertTrue(resultList.contains(1))
|
||||
assertFalse(resultList.contains(-4))
|
||||
}
|
||||
|
||||
@Test
|
||||
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"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenDropFirstItemsBasedOnCondition_thenRemoved () {
|
||||
val theList = listOf("one", "two", "three", "four")
|
||||
val resultList = theList.dropWhile{ it.length < 4 }
|
||||
|
||||
assertEquals(2, resultList.size)
|
||||
assertFalse(resultList.contains("one"))
|
||||
assertFalse(resultList.contains("two"))
|
||||
}
|
||||
|
||||
@Test
|
||||
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"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenSearchForExistingItem_thenFound () {
|
||||
val theList = listOf("one", "two", "three")
|
||||
|
||||
assertTrue("two" in theList)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenGroupItems_thenSuccess () {
|
||||
val theList = listOf(1, 2, 3, 4, 5, 6)
|
||||
val resultMap = theList.groupBy{ it % 3}
|
||||
|
||||
assertEquals(3, resultMap.size)
|
||||
print(resultMap[1])
|
||||
assertTrue(resultMap[1]!!.contains(1))
|
||||
assertTrue(resultMap[2]!!.contains(5))
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue