### Question:
Algorithm { public static <E, Ite extends Iterable<E>> ArrayList<E> findAll(Ite input, Predicate<E> predicate) { ArrayList<E> result = new ArrayList<E>(); for (E element : input) if(predicate.test(element)) result.add(element); return result; }  static ArrayList<E> findAll(Ite input, Predicate<E> predicate);  }### Answer:
@Test public void testFindAllWithEmptyIterable() { Iterable<Boolean> iterable = new ArrayList<Boolean>(); ArrayList<Boolean> result = findAll(iterable, new Predicate<Boolean>() { @Override public boolean test(Boolean aBoolean) { fail("The predicate should not be called with empty iterable"); return true; } }); assertThat("The result list should be empty as the input", result.size(), is(0)); }
@Test public void testFindAllWithNonEmptyIterableThatFind() { Iterable<Boolean> iterable = new ArrayList<Boolean>(Arrays.asList(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE)); final AtomicBoolean predicateCheck = new AtomicBoolean(false); ArrayList<Boolean> result = findAll(iterable, new Predicate<Boolean>() { @Override public boolean test(Boolean aBoolean) { predicateCheck.set(true); return aBoolean; } }); assertThat("The result list should have 2 elements", result.size(), is(2)); assertThat("The list should only have true items", result, everyItem(is(true))); assertThat("During evaluation, the predicate should have set check to true", predicateCheck.get(), is(true)); }
@Test public void testFindAllWithNonEmptyIterableThatNotFind() { Iterable<Boolean> iterable = new ArrayList<Boolean>(Arrays.asList(Boolean.FALSE, Boolean.FALSE)); final AtomicBoolean predicateCheck = new AtomicBoolean(false); ArrayList<Boolean> result = findAll(iterable, new Predicate<Boolean>() { @Override public boolean test(Boolean aBoolean) { predicateCheck.set(true); return aBoolean; } }); assertThat("The result list should have 2 elements", result.size(), is(0)); assertThat("During evaluation, the predicate should have set check to true", predicateCheck.get(), is(true)); }