1+ package com.baeldung.kotlin
2+
3+ import org.junit.Test
4+ import kotlin.test.assertTrue
5+ import kotlin.test.assertFalse
6+ import kotlin.test.assertEquals
7+
8+ class CollectionsTest {
9+
10+ @Test
11+ fun whenUseDifferentCollections_thenSuccess () {
12+ val theList = listOf (" one" , " two" , " three" )
13+ assertTrue(theList.contains(" two" ))
14+
15+ val theMutableList = mutableListOf (" one" , " two" , " three" )
16+ theMutableList.add(" four" )
17+ assertTrue(theMutableList.contains(" four" ))
18+
19+ val theSet = setOf (" one" , " two" , " three" )
20+ assertTrue(theSet.contains(" three" ))
21+
22+ val theMutableSet = mutableSetOf (" one" , " two" , " three" )
23+ theMutableSet.add(" four" )
24+ assertTrue(theMutableSet.contains(" four" ))
25+
26+ val theMap = mapOf (1 to " one" , 2 to " two" , 3 to " three" )
27+ assertEquals(theMap[2 ]," two" )
28+
29+ val theMutableMap = mutableMapOf (1 to " one" , 2 to " two" , 3 to " three" )
30+ theMutableMap[4 ] = " four"
31+ assertEquals(theMutableMap[4 ]," four" )
32+ }
33+
34+ @Test
35+ fun whenSliceCollection_thenSuccess () {
36+ val theList = listOf (" one" , " two" , " three" )
37+ val resultList = theList.slice(1 .. 2 )
38+
39+ assertEquals(2 , resultList.size)
40+ assertTrue(resultList.contains(" two" ))
41+ }
42+
43+ @Test
44+ fun whenJoinTwoCollections_thenSuccess () {
45+ val firstList = listOf (" one" , " two" , " three" )
46+ val secondList = listOf (" four" , " five" , " six" )
47+ val resultList = firstList + secondList
48+
49+ assertEquals(6 , resultList.size)
50+ assertTrue(resultList.contains(" two" ))
51+ assertTrue(resultList.contains(" five" ))
52+ }
53+
54+ @Test
55+ fun whenFilterNullValues_thenSuccess () {
56+ val theList = listOf (" one" , null , " two" , null , " three" )
57+ val resultList = theList.filterNotNull()
58+
59+ assertEquals(3 , resultList.size)
60+ }
61+
62+ @Test
63+ fun whenFilterNonPositiveValues_thenSuccess () {
64+ val theList = listOf (1 , 2 , - 3 , - 4 , 5 , - 6 )
65+ val resultList = theList.filter{ it > 0 }
66+ // val resultList = theList.filter{ x -> x > 0}
67+
68+ assertEquals(3 , resultList.size)
69+ assertTrue(resultList.contains(1 ))
70+ assertFalse(resultList.contains(- 4 ))
71+ }
72+
73+ @Test
74+ fun whenDropFirstItems_thenRemoved () {
75+ val theList = listOf (" one" , " two" , " three" , " four" )
76+ val resultList = theList.drop(2 )
77+
78+ assertEquals(2 , resultList.size)
79+ assertFalse(resultList.contains(" one" ))
80+ assertFalse(resultList.contains(" two" ))
81+ }
82+
83+ @Test
84+ fun whenDropFirstItemsBasedOnCondition_thenRemoved () {
85+ val theList = listOf (" one" , " two" , " three" , " four" )
86+ val resultList = theList.dropWhile{ it.length < 4 }
87+
88+ assertEquals(2 , resultList.size)
89+ assertFalse(resultList.contains(" one" ))
90+ assertFalse(resultList.contains(" two" ))
91+ }
92+
93+ @Test
94+ fun whenExcludeItems_thenRemoved () {
95+ val firstList = listOf (" one" , " two" , " three" )
96+ val secondList = listOf (" one" , " three" )
97+ val resultList = firstList - secondList
98+
99+ assertEquals(1 , resultList.size)
100+ assertTrue(resultList.contains(" two" ))
101+ }
102+
103+ @Test
104+ fun whenSearchForExistingItem_thenFound () {
105+ val theList = listOf (" one" , " two" , " three" )
106+
107+ assertTrue(" two" in theList)
108+ }
109+
110+ @Test
111+ fun whenGroupItems_thenSuccess () {
112+ val theList = listOf (1 , 2 , 3 , 4 , 5 , 6 )
113+ val resultMap = theList.groupBy{ it % 3 }
114+
115+ assertEquals(3 , resultMap.size)
116+ print (resultMap[1 ])
117+ assertTrue(resultMap[1 ]!! .contains(1 ))
118+ assertTrue(resultMap[2 ]!! .contains(5 ))
119+ }
120+
121+ }
0 commit comments