diff options
-rw-r--r-- | src/main/java/au/id/zancanaro/Generators.java | 16 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/Properties.java (renamed from src/main/java/au/id/zancanaro/PropertyTestRunner.java) | 6 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/PropertyError.java (renamed from src/main/java/au/id/zancanaro/PropertyTestError.java) | 4 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/annotations/Generator.java | 8 | ||||
-rw-r--r-- | src/test/java/au/id/zancanaro/PropertyTests.java | 58 | ||||
-rw-r--r-- | src/test/java/au/id/zancanaro/SimpleGenerator.java | 19 | ||||
-rw-r--r-- | src/test/java/au/id/zancanaro/SimpleListOperationsTest.java | 43 | ||||
-rw-r--r-- | src/test/java/au/id/zancanaro/TheoriesTest.java | 22 |
8 files changed, 56 insertions, 120 deletions
diff --git a/src/main/java/au/id/zancanaro/Generators.java b/src/main/java/au/id/zancanaro/Generators.java index 852a290..e0927ff 100644 --- a/src/main/java/au/id/zancanaro/Generators.java +++ b/src/main/java/au/id/zancanaro/Generators.java @@ -22,14 +22,6 @@ public class Generators { return integer(0, gens.length).flatMap(index -> gens[index]); } - public static Generator<Integer> integer() { - return (random, size) -> integer(-size, size).generate(random, size); - } - - public static Generator<Integer> natural() { - return (random, size) -> integer(0, size).generate(random, size); - } - public static Generator<Integer> integer(int lower, int upper) { return (random, size) -> { int value = lower + random.nextInt(upper - lower); @@ -38,6 +30,14 @@ public class Generators { }; } + public static Generator<Integer> integer() { + return (random, size) -> integer(-size, size).generate(random, size); + } + + public static Generator<Integer> natural() { + return (random, size) -> integer(0, size).generate(random, size); + } + private static Iterable<RoseTree<Integer>> intShrinkingIterable(final int value, final int bound) { return () -> new Iterator<RoseTree<Integer>>() { int curr = value - bound; diff --git a/src/main/java/au/id/zancanaro/PropertyTestRunner.java b/src/main/java/au/id/zancanaro/Properties.java index ea36341..fab48f4 100644 --- a/src/main/java/au/id/zancanaro/PropertyTestRunner.java +++ b/src/main/java/au/id/zancanaro/Properties.java @@ -13,10 +13,10 @@ import java.lang.reflect.*; import java.util.*; -public class PropertyTestRunner extends BlockJUnit4ClassRunner { +public class Properties extends BlockJUnit4ClassRunner { private final Map<Type, Generator<Object>> generators = new HashMap<>(); - public PropertyTestRunner(Class<?> klass) throws InitializationError { + public Properties(Class<?> klass) throws InitializationError { super(klass); } @@ -184,7 +184,7 @@ public class PropertyTestRunner extends BlockJUnit4ClassRunner { } ; } catch (Throwable ex) { - throw new PropertyTestError(method.getName(), seed, shrink(tree, ex)); + throw new PropertyError(method.getName(), seed, shrink(tree, ex)); } } } diff --git a/src/main/java/au/id/zancanaro/PropertyTestError.java b/src/main/java/au/id/zancanaro/PropertyError.java index afd614b..b2fb589 100644 --- a/src/main/java/au/id/zancanaro/PropertyTestError.java +++ b/src/main/java/au/id/zancanaro/PropertyError.java @@ -4,8 +4,8 @@ import java.util.Arrays; import java.util.Collection; import java.util.Iterator; -public class PropertyTestError extends AssertionError { - public PropertyTestError(String methodName, long seed, ShrinkResult shrunk) { +public class PropertyError extends AssertionError { + public PropertyError(String methodName, long seed, ShrinkResult shrunk) { super(String.format("%s(%s)\n\tSeed: %s\n%s", methodName, join(", ", shrunk.args), seed, diff --git a/src/main/java/au/id/zancanaro/annotations/Generator.java b/src/main/java/au/id/zancanaro/annotations/Generator.java deleted file mode 100644 index 98e9446..0000000 --- a/src/main/java/au/id/zancanaro/annotations/Generator.java +++ /dev/null @@ -1,8 +0,0 @@ -package au.id.zancanaro.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Target; - -@Target(ElementType.PARAMETER) -public @interface Generator { -} diff --git a/src/test/java/au/id/zancanaro/PropertyTests.java b/src/test/java/au/id/zancanaro/PropertyTests.java deleted file mode 100644 index dd595e1..0000000 --- a/src/test/java/au/id/zancanaro/PropertyTests.java +++ /dev/null @@ -1,58 +0,0 @@ -package au.id.zancanaro; - -import au.id.zancanaro.annotations.Property; -import au.id.zancanaro.annotations.Seed; -import org.hamcrest.CoreMatchers; -import org.junit.*; -import org.junit.experimental.theories.DataPoint; -import org.junit.matchers.JUnitMatchers; -import org.junit.runner.RunWith; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -@RunWith(PropertyTestRunner.class) -public class PropertyTests { - public static Generator<Integer> gen = Generators.integer(); - public static Generator<List<Integer>> list = Generators.listOf(Generators.integer()); - - @Ignore - @Property - public void aIsNotOdd(int a, int b) { - Assert.assertFalse(a % 2 == 1); - } - - @Ignore - @Property - public void aIsNotLessThanB(int a, int b) { - Assert.assertFalse(a < b); - } - - @Ignore - @Property - public void aPlusBLessThanOneHundred(int a, int b) { - Assert.assertTrue(a + b < 100); - } - - @Property - public void sortingIsIdempotent(List<Integer> list) { - List<Integer> left = new ArrayList<>(list); - Collections.sort(left); - - List<Integer> right = new ArrayList<>(list); - Collections.sort(right); - Collections.sort(right); - - Assert.assertEquals(left, right); - } - - @Property - public void reverseIsItsOwnInverse(List<Integer> list) { - List<Integer> reversed = new ArrayList<>(list); - Collections.reverse(reversed); - Collections.reverse(reversed); - - Assert.assertEquals(list, reversed); - } -} diff --git a/src/test/java/au/id/zancanaro/SimpleGenerator.java b/src/test/java/au/id/zancanaro/SimpleGenerator.java deleted file mode 100644 index 1b1d570..0000000 --- a/src/test/java/au/id/zancanaro/SimpleGenerator.java +++ /dev/null @@ -1,19 +0,0 @@ -package au.id.zancanaro; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.Random; - -public class SimpleGenerator { - @Test - public void testSimpleGeneratorFlatmap() throws Exception { - Generator<Integer> generator = Generator - .pure(10) - .flatMap((value) -> - Generator.pure(value + 10)); - Assert.assertEquals( - (Object) 20, - generator.generate(null, 0).getValue()); - } -} diff --git a/src/test/java/au/id/zancanaro/SimpleListOperationsTest.java b/src/test/java/au/id/zancanaro/SimpleListOperationsTest.java new file mode 100644 index 0000000..2ff44e7 --- /dev/null +++ b/src/test/java/au/id/zancanaro/SimpleListOperationsTest.java @@ -0,0 +1,43 @@ +package au.id.zancanaro; + +import au.id.zancanaro.annotations.Property; +import org.junit.runner.RunWith; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static au.id.zancanaro.Generators.integer; +import static au.id.zancanaro.Generators.listOf; +import static org.junit.Assert.assertEquals; + +@RunWith(Properties.class) +public class SimpleListOperationsTest { + + @SuppressWarnings("unused") + public static Generator<Integer> integers = integer(); + + @SuppressWarnings("unused") + public static Generator<List<Integer>> listOfIntegers = listOf(integer()); + + @Property(maxSize = 10000, runs = 10000) + public void sortingIsIdempotent(List<Integer> list) { + List<Integer> left = new ArrayList<>(list); + Collections.sort(left); + + List<Integer> right = new ArrayList<>(list); + Collections.sort(right); + Collections.sort(right); + + assertEquals(left, right); + } + + @Property(maxSize = 10000, runs = 10000) + public void reverseIsItsOwnInverse(List<Integer> list) { + List<Integer> reversed = new ArrayList<>(list); + Collections.reverse(reversed); + Collections.reverse(reversed); + + assertEquals(list, reversed); + } +} diff --git a/src/test/java/au/id/zancanaro/TheoriesTest.java b/src/test/java/au/id/zancanaro/TheoriesTest.java deleted file mode 100644 index 7d5071c..0000000 --- a/src/test/java/au/id/zancanaro/TheoriesTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package au.id.zancanaro; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.experimental.theories.DataPoint; -import org.junit.experimental.theories.Theories; -import org.junit.experimental.theories.Theory; -import org.junit.runner.RunWith; - -@RunWith(Theories.class) -public class TheoriesTest { - @DataPoint - public static String car = "carlo"; - - @DataPoint - public static String car2 = "carlo2"; - - @Theory - public void testName(String str) throws Exception { - Assert.assertEquals(str, car2); - } -} |