More Cases covered

This commit is contained in:
Niket Agrawal 2023-10-06 18:53:27 +05:30
parent 602dba71be
commit 31b23630ec
2 changed files with 71 additions and 41 deletions

View File

@ -48,7 +48,16 @@
<artifactId>protonpack</artifactId>
<version>1.16</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>one.util</groupId>
<artifactId>streamex</artifactId>
<version>0.8.1</version>
</dependency>
</dependencies>
<build>

View File

@ -5,42 +5,35 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.IntStream;
import org.apache.commons.collections4.IterableUtils;
import org.assertj.core.util.Lists;
import org.junit.Test;
import com.codepoetics.protonpack.StreamUtils;
import com.google.common.collect.Iterables;
import io.vavr.collection.Stream;
import one.util.streamex.EntryStream;
public class FirstMatchingElementUnitTest {
@Test
public void testWithForLoop() {
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE);
int index = -1;
for (int i = 0; i < dataList.size(); i++) {
if (dataList.get(i) instanceof Boolean) {
index = i;
}
}
assertEquals(2, index);
}
private final List<Object> dataList = Lists.newArrayList("String", Boolean.TRUE, Integer.valueOf(10), Boolean.FALSE, Double.valueOf(20.0));
@Test
public void testWithIndexOf() {
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE);
Optional<Object> booleanData = dataList.stream()
public void whenCalled_thenFindIndexUsingStream() {
int index = dataList.stream()
.filter(data -> data instanceof Boolean)
.findFirst();
int index = booleanData.isPresent() ? dataList.indexOf(booleanData.get()) : -1;
assertEquals(2, index);
.mapToInt(data -> dataList.indexOf(data))
.findFirst()
.orElse(-1);
assertEquals(1, index);
}
@Test
public void testWithIterator() {
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE);
public void whenCalled_thenFindIndexUsingStreamIterator() {
int index = -1;
Iterator<Object> iterator = dataList.iterator();
while (iterator.hasNext()) {
@ -50,12 +43,11 @@ public class FirstMatchingElementUnitTest {
break;
}
}
assertEquals(2, index);
assertEquals(1, index);
}
@Test
public void testWithListIterator() {
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE);
public void whenCalled_thenFindIndexUsingStreamListIterator() {
int index = -1;
ListIterator<Object> listIterator = dataList.listIterator();
while (listIterator.hasNext()) {
@ -64,41 +56,70 @@ public class FirstMatchingElementUnitTest {
break;
}
}
assertEquals(2, index);
assertEquals(1, index);
}
@Test
public void testWithTakeAWhile() {
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE, 2, 3);
public void whenCalled_thenFindIndexUsingIntStream() {
int index = IntStream.range(0, dataList.size() - 1)
.filter(streamIndex -> dataList.get(streamIndex) instanceof Boolean)
.findFirst()
.orElse(-1);
assertEquals(1, index);
}
@Test
public void whenCalled_thenFindIndexUsingStreamTakeWhile() {
OptionalInt booleanIndex = dataList.stream()
.takeWhile(data -> !(data instanceof Boolean))
.mapToInt(dataList::indexOf)
.max();
if (booleanIndex.isPresent() && dataList.get(booleanIndex.getAsInt()) instanceof Boolean) {
assertEquals(2, booleanIndex.getAsInt());
assertEquals(1, booleanIndex.getAsInt());
}
}
@Test
public void testWithIntStream() {
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE);
int index = IntStream.of(0, dataList.size() - 1)
.filter(streamIndex -> dataList.get(streamIndex) instanceof Boolean)
.findFirst()
.orElse(-1);
assertEquals(2, index);
}
@Test
public void testWithZipWithIndex() {
List<Object> dataList = Lists.newArrayList("String", 1, Boolean.TRUE, 2, 3);
public void whenCalled_thenFindIndexUsingZipWithIndex() {
int index = StreamUtils.zipWithIndex(dataList.stream())
.filter(i -> i.getValue() instanceof Boolean)
.mapToInt(i -> Long.valueOf(i.getIndex())
.intValue())
.findFirst()
.orElse(-1);
assertEquals(2, index);
assertEquals(1, index);
}
@Test
public void whenCalled_thenFindIndexUsingGoogleGuava() {
int index = Iterables.indexOf(dataList, data -> data instanceof Boolean);
assertEquals(1, index);
}
@Test
public void whenCalled_thenFindIndexUsingApacheCommons() {
int index = IterableUtils.indexOf(dataList, data -> data instanceof Boolean);
assertEquals(1, index);
}
@Test
public void whenCalled_thenFindIndexUsingStreamEx() {
Integer foundIndex = EntryStream.of(dataList)
.filterKeyValue((index, data) -> data instanceof Boolean)
.keys()
.findFirst()
.orElse(-1);
assertEquals(1, foundIndex);
}
@Test
public void whenCalled_thenFindIndexUsingVavrStream() {
Integer foundIndex = Stream.of(dataList.toArray())
.zipWithIndex()
.find(tuple -> tuple._1() instanceof Boolean)
.map(tuple -> tuple._2())
.getOrElse(-1);
assertEquals(1, foundIndex);
}
}