Skip to content

Commit 901b733

Browse files
Vizsoroecheyne
authored andcommitted
bael-2022 initialize array in kotlin (eugenp#5529)
* bael-2022 initialize array in kotlin * bael-2022 formatting with eclipse * bael-2022 formatting fix
1 parent c3daf88 commit 901b733

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.kotlin
2+
3+
import org.junit.Test
4+
import kotlin.test.assertEquals
5+
6+
class ArrayInitializationTest {
7+
8+
@Test
9+
fun givenArrayOfStrings_thenValuesPopulated() {
10+
val strings = arrayOf("January", "February", "March")
11+
12+
assertEquals(3, strings.size)
13+
assertEquals("March", strings[2])
14+
}
15+
16+
@Test
17+
fun givenArrayOfIntegers_thenValuesPopulated() {
18+
val integers = intArrayOf(1, 2, 3, 4)
19+
20+
assertEquals(4, integers.size)
21+
assertEquals(1, integers[0])
22+
}
23+
24+
@Test
25+
fun givenArrayOfNulls_whenPopulated_thenValuesPresent() {
26+
val array = arrayOfNulls<Number>(5)
27+
28+
for (i in array.indices) {
29+
array[i] = i * i
30+
}
31+
32+
assertEquals(16, array[4])
33+
}
34+
35+
@Test
36+
fun whenGeneratorUsed_thenValuesPresent() {
37+
val generatedArray = IntArray(10) { i -> i * i }
38+
39+
assertEquals(81, generatedArray[9])
40+
}
41+
42+
@Test
43+
fun whenStringGenerated_thenValuesPresent() {
44+
val generatedStringArray = Array(10) { i -> "Number of index: $i" }
45+
46+
assertEquals("Number of index: 0", generatedStringArray[0])
47+
}
48+
49+
}

0 commit comments

Comments
 (0)