Add FilterIteratorTest.testConstructorPredicateFilterInts()

This commit is contained in:
Gary Gregory 2024-10-29 07:58:29 -04:00
parent 7d24215db1
commit 1e8b3cbadc
1 changed files with 23 additions and 2 deletions

View File

@ -45,10 +45,12 @@ import org.junit.jupiter.api.Test;
*/ */
public class FilterIteratorTest<E> extends AbstractIteratorTest<E> { public class FilterIteratorTest<E> extends AbstractIteratorTest<E> {
private String[] array; private static final List<Integer> collectionInts = Arrays.asList(1, 2, 3, 4, 5, 6);
private String[] array;
private List<E> list; private List<E> list;
private FilterIterator<E> iterator; private FilterIterator<E> iterator;
/** Creates new TestFilterIterator */ /** Creates new TestFilterIterator */
public FilterIteratorTest() { public FilterIteratorTest() {
super(FilterIteratorTest.class.getSimpleName()); super(FilterIteratorTest.class.getSimpleName());
@ -123,6 +125,22 @@ public class FilterIteratorTest<E> extends AbstractIteratorTest<E> {
iterator = null; iterator = null;
} }
/**
* Tests a predicate that accepts some but not all elements.
*/
@Test
public void testConstructorPredicateFilterInts() {
final List<Integer> expected = Arrays.asList(2, 4, 6);
final Predicate<Integer> predicate = i -> i % 2 == 0;
final FilterIterator<Integer> filter = new FilterIterator<>(collectionInts.iterator(), predicate);
final List<Integer> actual = new ArrayList<>();
filter.forEachRemaining(actual::add);
assertEquals(expected, actual);
}
/**
* Tests a predicate that accepts everything.
*/
@Test @Test
public void testForEachRemainingAcceptAllCtor() { public void testForEachRemainingAcceptAllCtor() {
final List<E> expected = IteratorUtils.toList(makeObject()); final List<E> expected = IteratorUtils.toList(makeObject());
@ -142,6 +160,9 @@ public class FilterIteratorTest<E> extends AbstractIteratorTest<E> {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
/**
* Tests a predicate that rejects everything.
*/
@Test @Test
public void testForEachRemainingRejectAllCtor() { public void testForEachRemainingRejectAllCtor() {
final List<E> expected = IteratorUtils.toList(makeObject()); final List<E> expected = IteratorUtils.toList(makeObject());
@ -167,6 +188,7 @@ public class FilterIteratorTest<E> extends AbstractIteratorTest<E> {
verifyNoMoreElements(); verifyNoMoreElements();
} }
@Test @Test
public void testReturnValues() { public void testReturnValues() {
verifyElementsInPredicate(ArrayUtils.EMPTY_STRING_ARRAY); verifyElementsInPredicate(ArrayUtils.EMPTY_STRING_ARRAY);
@ -179,7 +201,6 @@ public class FilterIteratorTest<E> extends AbstractIteratorTest<E> {
verifyElementsInPredicate(new String[] { "a", "b", "c" }); verifyElementsInPredicate(new String[] { "a", "b", "c" });
} }
/** /**
* Test that when the iterator is changed, the hasNext method returns the * Test that when the iterator is changed, the hasNext method returns the
* correct response for the new iterator. * correct response for the new iterator.