### Question:
Algorithm { static <I, O, Col extends Collection<I>> ArrayList<O> transform(Col input, Function<I, O> function) { ArrayList<O> output = new ArrayList<O>(input.size()); for (I element : input) try { output.add(function.execute(element)); } catch (Throwable throwable) { throw new RuntimeException(throwable); } return output; }  static ArrayList<E> findAll(Ite input, Predicate<E> predicate);  }### Answer:
@Test public void testTransformWithEmptyCollection() { ArrayList<Boolean> collection = new ArrayList<Boolean>(); ArrayList<Boolean> result = transform(collection, new Function<Boolean, Boolean>() { @Override public Boolean execute(Boolean value) { fail("The function should not be called with empty collection"); return value; } }); assertThat("The result should remain empty", result.size(), is(0)); }
@Test public void testTransformWithEmptyIterable() { Iterable<Boolean> iterable = new ArrayList<Boolean>(); ArrayList<Boolean> result = transform(iterable, new Function<Boolean, Boolean>() { @Override public Boolean execute(Boolean value) { fail("The function should not be called with empty iterable"); return value; } }); assertThat("The result should remain empty", result.size(), is(0)); }
@Test public void testTransformWithNonEmptyCollection() { ArrayList<Boolean> collection = new ArrayList<Boolean>(Arrays.asList(Boolean.TRUE)); final AtomicBoolean functionCheck = new AtomicBoolean(false); ArrayList<Boolean> result = transform(collection, new Function<Boolean, Boolean>() { @Override public Boolean execute(Boolean value) { functionCheck.set(true); return ! value; } }); assertThat("The result should have the same size than input", result.size(), is(equalTo(collection.size()))); assertThat("The result should have opposite element than input", result.get(0), is(false)); assertThat("During evaluation, the function should have set check to true", functionCheck.get(), is(true)); }
@Test public void testTransformWithNonEmptyIterable() { Iterable<Boolean> iterable = new ArrayList<Boolean>(Arrays.asList(Boolean.TRUE)); final AtomicBoolean functionCheck = new AtomicBoolean(false); ArrayList<Boolean> result = transform(iterable, new Function<Boolean, Boolean>() { @Override public Boolean execute(Boolean value) { functionCheck.set(true); return ! value; } }); assertThat("The result should have the same size than input", result.size(), is(1)); assertThat("The result should have opposite element than input", result.get(0), is(false)); assertThat("During evaluation, the function should have set check to true", functionCheck.get(), is(true)); }