bael-2022 initialize array in kotlin (#5529)

* bael-2022 initialize array in kotlin

* bael-2022 formatting with eclipse

* bael-2022 formatting fix
This commit is contained in:
Vizsoro 2018-10-28 02:34:49 +01:00 committed by Emily Cheyne
parent c3daf8868a
commit 901b733ca4
1 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,49 @@
package com.baeldung.kotlin
import org.junit.Test
import kotlin.test.assertEquals
class ArrayInitializationTest {
@Test
fun givenArrayOfStrings_thenValuesPopulated() {
val strings = arrayOf("January", "February", "March")
assertEquals(3, strings.size)
assertEquals("March", strings[2])
}
@Test
fun givenArrayOfIntegers_thenValuesPopulated() {
val integers = intArrayOf(1, 2, 3, 4)
assertEquals(4, integers.size)
assertEquals(1, integers[0])
}
@Test
fun givenArrayOfNulls_whenPopulated_thenValuesPresent() {
val array = arrayOfNulls<Number>(5)
for (i in array.indices) {
array[i] = i * i
}
assertEquals(16, array[4])
}
@Test
fun whenGeneratorUsed_thenValuesPresent() {
val generatedArray = IntArray(10) { i -> i * i }
assertEquals(81, generatedArray[9])
}
@Test
fun whenStringGenerated_thenValuesPresent() {
val generatedStringArray = Array(10) { i -> "Number of index: $i" }
assertEquals("Number of index: 0", generatedStringArray[0])
}
}