summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@zancanaro.id.au>2015-06-03 12:39:14 +1000
committerCarlo Zancanaro <carlo@zancanaro.id.au>2015-06-03 12:39:14 +1000
commit7eabc41ad41e0da58fff2a6bb04212ea0cd3baa2 (patch)
tree567a3ebfc571a3728e2caf2ef565cba24321843d /src/test
parent27fcf37206591c774d79ec60de8d404ed83378ac (diff)
Add some more interesting tests
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/au/id/zancanaro/javacheck/ListFunctorRulesTest.java110
-rw-r--r--src/test/java/au/id/zancanaro/javacheck/OptionalFunctorRulesTest.java110
-rw-r--r--src/test/java/au/id/zancanaro/javacheck/SimpleListOperationsTest.java (renamed from src/test/java/au/id/zancanaro/javacheck/junit/SimpleListOperationsTest.java)10
3 files changed, 227 insertions, 3 deletions
diff --git a/src/test/java/au/id/zancanaro/javacheck/ListFunctorRulesTest.java b/src/test/java/au/id/zancanaro/javacheck/ListFunctorRulesTest.java
new file mode 100644
index 0000000..4e516a2
--- /dev/null
+++ b/src/test/java/au/id/zancanaro/javacheck/ListFunctorRulesTest.java
@@ -0,0 +1,110 @@
+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.*;
+import static org.junit.Assert.assertEquals;
+
+@RunWith(Properties.class)
+public class ListFunctorRulesTest {
+
+ private final static int runs = 1000;
+ private final static int maxSize = 1000;
+
+ @DataSource
+ public static Generator<List<Integer>> listOfIntegers = listOf(integer());
+
+ @DataSource
+ public static Generator<Function<Integer, Integer>> integerFunction =
+ oneOf(
+ integer().map(ListFunctorRulesTest::plusI),
+ integer().map(ListFunctorRulesTest::timesI),
+ integer().map(ListFunctorRulesTest::constantlyI));
+
+ @Property(maxSize = maxSize, runs = runs)
+ public void mappingCompositionsWithStreams(
+ List<Integer> list,
+ Function<Integer, Integer> f,
+ Function<Integer, Integer> g) {
+ List<Integer> left = list.stream()
+ .map(g)
+ .map(f)
+ .collect(Collectors.toList());
+
+ List<Integer> right = list.stream()
+ .map(x -> f.apply(g.apply(x)))
+ .collect(Collectors.toList());
+
+ assertEquals(left, right);
+ }
+
+ @Property(maxSize = maxSize, runs = runs)
+ public void mappingCompositionsWithIntermediateList(
+ List<Integer> list,
+ Function<Integer, Integer> f,
+ Function<Integer, Integer> g) {
+ List<Integer> intermediate = list.stream().map(g).collect(Collectors.toList());
+ List<Integer> left = intermediate.stream().map(f).collect(Collectors.toList());
+
+ List<Integer> right = list.stream()
+ .map(x -> f.apply(g.apply(x)))
+ .collect(Collectors.toList());
+
+ assertEquals(left, right);
+ }
+
+ @Property(maxSize = maxSize, runs = runs)
+ public void mapIdentityIsIdentity(List<Integer> list) {
+ List<Integer> mapped = list.stream().map(x -> x).collect(Collectors.toList());
+
+ assertEquals(list, mapped);
+ }
+
+ private static Function<Integer,Integer> plusI(final int i) {
+ return new Function<Integer, Integer>() {
+ @Override
+ public Integer apply(Integer integer) {
+ return i + integer;
+ }
+
+ @Override
+ public String toString() {
+ return "x -> x + " + i;
+ }
+ };
+ }
+
+ private static Function<Integer,Integer> timesI(final int i) {
+ return new Function<Integer, Integer>() {
+ @Override
+ public Integer apply(Integer integer) {
+ return i * integer;
+ }
+
+ @Override
+ public String toString() {
+ return "x -> x * " + i;
+ }
+ };
+ }
+ private static Function<Integer,Integer> constantlyI(final int i) {
+ return new Function<Integer, Integer>() {
+ @Override
+ public Integer apply(Integer integer) {
+ return i;
+ }
+
+ @Override
+ public String toString() {
+ return "x -> " + i;
+ }
+ };
+ }
+}
diff --git a/src/test/java/au/id/zancanaro/javacheck/OptionalFunctorRulesTest.java b/src/test/java/au/id/zancanaro/javacheck/OptionalFunctorRulesTest.java
new file mode 100644
index 0000000..b13fb8b
--- /dev/null
+++ b/src/test/java/au/id/zancanaro/javacheck/OptionalFunctorRulesTest.java
@@ -0,0 +1,110 @@
+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.Optional;
+import java.util.function.Function;
+
+import static au.id.zancanaro.javacheck.Generator.pure;
+import static au.id.zancanaro.javacheck.Generators.integer;
+import static au.id.zancanaro.javacheck.Generators.oneOf;
+import static org.junit.Assert.assertEquals;
+
+@RunWith(Properties.class)
+public class OptionalFunctorRulesTest {
+
+ private final static int runs = 1000;
+ private final static int maxSize = 1000;
+
+ @DataSource
+ public static Generator<Optional<Integer>> listOfIntegers = oneOf(
+ pure(Optional.empty()),
+ integer().map(Optional::of));
+
+ @DataSource
+ public static Generator<Function<Integer, Integer>> integerFunction =
+ oneOf(
+ integer().map(OptionalFunctorRulesTest::plusI),
+ integer().map(OptionalFunctorRulesTest::timesI),
+ integer().map(OptionalFunctorRulesTest::constantlyI));
+
+ @Property(maxSize = maxSize, runs = runs)
+ public void mappingCompositionsWithStreams(
+ Optional<Integer> optional,
+ Function<Integer, Integer> f,
+ Function<Integer, Integer> g) {
+ Optional<Integer> left = optional
+ .map(g)
+ .map(f);
+
+ Optional<Integer> right = optional
+ .map(x -> f.apply(g.apply(x)));
+
+ assertEquals(left, right);
+ }
+
+ @Property(maxSize = maxSize, runs = runs)
+ public void mappingCompositionsWithIntermediateList(
+ Optional<Integer> optional,
+ Function<Integer, Integer> f,
+ Function<Integer, Integer> g) {
+ Optional<Integer> intermediate = optional.map(g);
+ Optional<Integer> left = intermediate.map(f);
+
+ Optional<Integer> right = optional
+ .map(x -> f.apply(g.apply(x)));
+
+ assertEquals(left, right);
+ }
+
+ @Property(maxSize = maxSize, runs = runs)
+ public void mapIdentityIsIdentity(Optional<Integer> optional) {
+ Optional<Integer> mapped = optional.map(x -> x);
+
+ assertEquals(optional, mapped);
+ }
+
+ private static Function<Integer,Integer> plusI(final int i) {
+ return new Function<Integer, Integer>() {
+ @Override
+ public Integer apply(Integer integer) {
+ return i + integer;
+ }
+
+ @Override
+ public String toString() {
+ return "x -> x + " + i;
+ }
+ };
+ }
+
+ private static Function<Integer,Integer> timesI(final int i) {
+ return new Function<Integer, Integer>() {
+ @Override
+ public Integer apply(Integer integer) {
+ return i * integer;
+ }
+
+ @Override
+ public String toString() {
+ return "x -> x * " + i;
+ }
+ };
+ }
+ private static Function<Integer,Integer> constantlyI(final int i) {
+ return new Function<Integer, Integer>() {
+ @Override
+ public Integer apply(Integer integer) {
+ return i;
+ }
+
+ @Override
+ public String toString() {
+ return "x -> " + i;
+ }
+ };
+ }
+}
diff --git a/src/test/java/au/id/zancanaro/javacheck/junit/SimpleListOperationsTest.java b/src/test/java/au/id/zancanaro/javacheck/SimpleListOperationsTest.java
index 089451e..972b5b6 100644
--- a/src/test/java/au/id/zancanaro/javacheck/junit/SimpleListOperationsTest.java
+++ b/src/test/java/au/id/zancanaro/javacheck/SimpleListOperationsTest.java
@@ -1,8 +1,9 @@
-package au.id.zancanaro.javacheck.junit;
+package au.id.zancanaro.javacheck;
import au.id.zancanaro.javacheck.Generator;
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.ArrayList;
@@ -15,13 +16,16 @@ import static org.junit.Assert.assertEquals;
@RunWith(Properties.class)
public class SimpleListOperationsTest {
+ private final static int runs = 1000;
+ private final static int maxSize = 1000;
+
@DataSource
public static Generator<List<Integer>> listOfIntegers = listOf(integer());
@DataSource
public static Generator<List<String>> listOfStrings = listOf(stringOf(alphaNumericCharacter()));
- @Property(maxSize = 100, runs = 1000)
+ @Property(maxSize = maxSize, runs = runs)
public void sortingIsIdempotent(List<Integer> list) {
List<Integer> left = new ArrayList<>(list);
Collections.sort(left);
@@ -33,7 +37,7 @@ public class SimpleListOperationsTest {
assertEquals(left, right);
}
- @Property(maxSize = 100, runs = 1000)
+ @Property(maxSize = 100, runs = runs)
public void reverseIsItsOwnInverse(List<String> list) {
List<String> reversed = new ArrayList<>(list);
Collections.reverse(reversed);