package au.id.zancanaro.javacheck; import au.id.zancanaro.javacheck.annotations.DataSource; import au.id.zancanaro.javacheck.annotations.Property; import au.id.zancanaro.javacheck.junit.Properties; import org.junit.runner.RunWith; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; import static au.id.zancanaro.javacheck.Generators.longInteger; import static au.id.zancanaro.javacheck.Generators.oneOf; import static org.junit.Assert.assertEquals; @RunWith(Properties.class) public class ListFunctorRulesTest { @DataSource public static Generator> integerFunction = oneOf( longInteger().map(ListFunctorRulesTest::plusI), longInteger().map(ListFunctorRulesTest::timesI), longInteger().map(ListFunctorRulesTest::constantlyI)); @Property public void mappingCompositionsWithStreams( List list, Function f, Function g) { List left = list.stream() .map(g) .map(f) .collect(Collectors.toList()); List right = list.stream() .map(f.compose(g)) .collect(Collectors.toList()); assertEquals(left, right); } @Property public void mappingCompositionsWithIntermediateList( List list, Function f, Function g) { List intermediate = list.stream().map(g).collect(Collectors.toList()); List left = intermediate.stream().map(f).collect(Collectors.toList()); List right = list.stream() .map(f.compose(g)) .collect(Collectors.toList()); assertEquals(left, right); } @Property public void mapIdentityIsIdentity(List list) { List mapped = list.stream().map(x -> x).collect(Collectors.toList()); assertEquals(list, mapped); } private static Function plusI(final long i) { return new Function() { @Override public Long apply(Long integer) { return i + integer; } @Override public String toString() { return "x -> x + " + i; } }; } private static Function timesI(final long i) { return new Function() { @Override public Long apply(Long integer) { return i * integer; } @Override public String toString() { return "x -> x * " + i; } }; } private static Function constantlyI(final long i) { return new Function() { @Override public Long apply(Long integer) { return i; } @Override public String toString() { return "x -> " + i; } }; } }