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.Set; 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 SetFunctorRulesTest { @DataSource public static Generator> integerFunction = oneOf( longInteger().map(SetFunctorRulesTest::plusI), longInteger().map(SetFunctorRulesTest::timesI), longInteger().map(SetFunctorRulesTest::constantlyI)); @Property public void mappingCompositionsWithStreams( Set list, Function f, Function g) { Set left = list.stream() .map(g) .map(f) .collect(Collectors.toSet()); Set right = list.stream() .map(f.compose(g)) .collect(Collectors.toSet()); assertEquals(left, right); } @Property public void mappingCompositionsWithIntermediateList( Set list, Function f, Function g) { Set intermediate = list.stream().map(g).collect(Collectors.toSet()); Set left = intermediate.stream().map(f).collect(Collectors.toSet()); Set right = list.stream() .map(f.compose(g)) .collect(Collectors.toSet()); assertEquals(left, right); } @Property public void mapIdentityIsIdentity(Set list) { Set mapped = list.stream().map(x -> x).collect(Collectors.toSet()); 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; } }; } }