This commit is related to BAEL-7856 (#16480)

This commit aims to add a test class named "LastOccurrenceFinderUnitTest".
This commit is contained in:
Mo Helmy 2024-04-24 18:52:21 +02:00 committed by GitHub
parent 8193ddf314
commit f94b8d96ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,43 @@
package com.baeldung.LastOccurrenceFinder;
import org.junit.Test;
import java.util.OptionalInt;
import java.util.stream.IntStream;
import static org.junit.Assert.assertEquals;
public class LastOccurrenceFinderUnitTest {
String str = "Welcome to Baeldung";
char target = 'e';
int n = 2;
int expectedIndex = 6;
@Test
public void givenStringAndCharAndN_whenFindingNthLastOccurrence_thenCorrectIndexReturned() {
int count = 0;
int index = -1;
for (int i = str.length() - 1; i >= 0; i--) {
if (str.charAt(i) == target) {
count++;
if (count == n) {
index = i;
break;
}
}
}
assertEquals(expectedIndex, index);
}
@Test
public void givenStringAndCharAndN_whenFindingNthLastOccurrenceUsingStreams_thenCorrectIndexReturned() {
OptionalInt result = IntStream.range(0, str.length())
.map(i -> str.length() - 1 - i)
.filter(i -> str.charAt(i) == target)
.skip(n - 1)
.findFirst();
int index = result.orElse(-1);
assertEquals(expectedIndex, index);
}
}