From 901b733ca4b2b7b16f5af2661ef6d7f9a55386ec Mon Sep 17 00:00:00 2001 From: Vizsoro Date: Sun, 28 Oct 2018 02:34:49 +0100 Subject: [PATCH] bael-2022 initialize array in kotlin (#5529) * bael-2022 initialize array in kotlin * bael-2022 formatting with eclipse * bael-2022 formatting fix --- .../kotlin/ArrayInitializationTest.kt | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/kotlin/ArrayInitializationTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/ArrayInitializationTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/ArrayInitializationTest.kt new file mode 100644 index 0000000000..ba3694c831 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/ArrayInitializationTest.kt @@ -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(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]) + } + +} \ No newline at end of file