forked from Baeldung/kotlin-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[KTLN-495] Add Sammples (Baeldung#612)
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
core-kotlin-modules/core-kotlin-arrays/src/test/kotlin/com/baeldung/indexof/IndexOfTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.baeldung.indexof | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
|
||
class IndexOfTests { | ||
|
||
@Test | ||
fun `Find index using indexOf`() { | ||
val numbers = arrayOf(1, 2, 3, 4, 5) | ||
val elementToFind = 3 | ||
val index = numbers.indexOf(elementToFind) | ||
|
||
assertEquals(2, index) | ||
} | ||
|
||
@Test | ||
fun `Find index of first`() { | ||
val numbers = arrayOf(1, 2, 3, 4, 3, 5) | ||
val elementToFind = 3 | ||
val index = numbers.indexOfFirst { it == elementToFind } | ||
|
||
assertEquals(2, index) | ||
} | ||
|
||
@Test | ||
fun `Find last index of element`() { | ||
val numbers = arrayOf(1, 2, 3, 4, 3, 5) | ||
val elementToFind = 3 | ||
val lastIndex = numbers.lastIndexOf(elementToFind) | ||
|
||
assertEquals(4, lastIndex) | ||
} | ||
|
||
@Test | ||
fun `Find index of last`() { | ||
val numbers = arrayOf(1, 2, 3, 4, 3, 5) | ||
val elementToFind = 3 | ||
val index = numbers.indexOfLast { it == elementToFind } | ||
|
||
assertEquals(4, index) | ||
} | ||
|
||
@Test | ||
fun `Find indices using loop`() { | ||
val numbers = arrayOf(1, 2, 3, 4, 3, 5) | ||
val elementToFind = 3 | ||
val indices = mutableListOf<Int>() | ||
|
||
for (i in numbers.indices) { | ||
if (numbers[i] == elementToFind) { | ||
indices.add(i) | ||
} | ||
} | ||
|
||
assertEquals(listOf(2, 4), indices) | ||
} | ||
|
||
} |