From 8376ed30f83c9193025da5962de5f5d78edfa185 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Tue, 24 Nov 2015 12:57:27 +1100 Subject: Add an implicit generator for Sets --- .../zancanaro/javacheck/SetFunctorRulesTest.java | 106 +++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/test/java/au/id/zancanaro/javacheck/SetFunctorRulesTest.java (limited to 'src/test/java/au/id/zancanaro/javacheck/SetFunctorRulesTest.java') diff --git a/src/test/java/au/id/zancanaro/javacheck/SetFunctorRulesTest.java b/src/test/java/au/id/zancanaro/javacheck/SetFunctorRulesTest.java new file mode 100644 index 0000000..04fa674 --- /dev/null +++ b/src/test/java/au/id/zancanaro/javacheck/SetFunctorRulesTest.java @@ -0,0 +1,106 @@ +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; + } + }; + } +} -- cgit v1.2.3