diff options
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/Generator.java (renamed from src/main/java/au/id/zancanaro/Generator.java) | 5 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/Generators.java (renamed from src/main/java/au/id/zancanaro/Generators.java) | 69 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/Iterators.java (renamed from src/main/java/au/id/zancanaro/Iterators.java) | 6 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/RoseTree.java (renamed from src/main/java/au/id/zancanaro/RoseTree.java) | 10 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/ShrinkResult.java (renamed from src/main/java/au/id/zancanaro/ShrinkResult.java) | 4 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/annotations/DataSource.java (renamed from src/main/java/au/id/zancanaro/annotations/DataSource.java) | 2 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/annotations/Property.java (renamed from src/main/java/au/id/zancanaro/annotations/Property.java) | 2 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/annotations/Seed.java (renamed from src/main/java/au/id/zancanaro/annotations/Seed.java) | 2 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/junit/Properties.java (renamed from src/main/java/au/id/zancanaro/Properties.java) | 13 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java (renamed from src/main/java/au/id/zancanaro/PropertyError.java) | 4 | ||||
-rw-r--r-- | src/test/java/au/id/zancanaro/javacheck/junit/SimpleListOperationsTest.java (renamed from src/test/java/au/id/zancanaro/SimpleListOperationsTest.java) | 22 |
11 files changed, 104 insertions, 35 deletions
diff --git a/src/main/java/au/id/zancanaro/Generator.java b/src/main/java/au/id/zancanaro/javacheck/Generator.java index 1c948af..66ebf1b 100644 --- a/src/main/java/au/id/zancanaro/Generator.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generator.java @@ -1,5 +1,6 @@ -package au.id.zancanaro; +package au.id.zancanaro.javacheck; +import java.util.List; import java.util.Random; import java.util.function.Function; @@ -11,7 +12,7 @@ public interface Generator<T> { } @SafeVarargs - static <T> Generator<T[]> tuple(Generator<T>... generators) { + static <T> Generator<List<T>> tuple(Generator<T>... generators) { return (random, size) -> { @SuppressWarnings("unchecked") RoseTree<T>[] result = (RoseTree<T>[]) new RoseTree[generators.length]; diff --git a/src/main/java/au/id/zancanaro/Generators.java b/src/main/java/au/id/zancanaro/javacheck/Generators.java index 01ab64a..ec6a329 100644 --- a/src/main/java/au/id/zancanaro/Generators.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generators.java @@ -1,4 +1,4 @@ -package au.id.zancanaro; +package au.id.zancanaro.javacheck; import java.util.Arrays; import java.util.Collections; @@ -7,7 +7,10 @@ import java.util.List; import java.util.function.Function; import java.util.function.Predicate; -public class Generators { +public final class Generators { + private Generators() { + } + public static <T> Generator<T> sized(Function<Integer, Generator<T>> makeGenerator) { return (random, size) -> makeGenerator.apply(size).generate(random, size); } @@ -28,6 +31,14 @@ public class Generators { return integer(0, gens.length).flatMap(index -> gens[index]); } + public static <T> Generator<T> elements(T[] elements) { + return elements(Arrays.asList(elements)); + } + + public static <T> Generator<T> elements(List<T> elements) { + return integer(0, elements.size()).fmap(elements::get); + } + public static Generator<Boolean> bool() { return (random, size) -> random.nextBoolean() ? @@ -70,11 +81,55 @@ public class Generators { } public static <T> Generator<List<T>> listOf(Generator<T> gen) { - return (Generator<List<T>>) sized(size -> { - @SuppressWarnings("unchecked") - Generator<T>[] gens = (Generator<T>[]) new Generator[size]; - Arrays.fill(gens, gen); - return Generator.tuple(gens).fmap(Arrays::asList).fmap(Collections::unmodifiableList); + return sized(size -> + integer(0, size).flatMap(count -> { + @SuppressWarnings("unchecked") + Generator<T>[] gens = (Generator<T>[]) new Generator[count]; + Arrays.fill(gens, gen); + return Generator.tuple(gens); + })).fmap(Collections::unmodifiableList); + } + + public static Generator<Character> character() { + return integer(0, 256).fmap(i -> (char) i.intValue()); + } + + public static Generator<Character> asciiCharacter() { + return integer(32, 127).fmap(i -> (char) i.intValue()); + } + + public static Generator<Character> alphaNumericCharacter() { + return oneOf( + integer(48, 58), + integer(65, 91), + integer(97, 123)).fmap(i -> (char) i.intValue()); + } + + public static Generator<Character> alphaCharacter() { + return oneOf( + integer(65, 91), + integer(97, 123)).fmap(i -> (char) i.intValue()); + } + + private static String makeString(Character[] arr) { + StringBuilder builder = new StringBuilder(arr.length); + for (Character c : arr) { + builder.append(c); + } + return builder.toString(); + } + + public static Generator<String> string() { + return stringOf(character()); + } + + public static Generator<String> stringOf(Generator<Character> charGen) { + return Generators.listOf(charGen).fmap(list -> { + StringBuilder builder = new StringBuilder(list.size()); + for (Object c : list) { + builder.append(c); + } + return builder.toString(); }); } } diff --git a/src/main/java/au/id/zancanaro/Iterators.java b/src/main/java/au/id/zancanaro/javacheck/Iterators.java index 08cc37e..347a927 100644 --- a/src/main/java/au/id/zancanaro/Iterators.java +++ b/src/main/java/au/id/zancanaro/javacheck/Iterators.java @@ -1,13 +1,15 @@ -package au.id.zancanaro; +package au.id.zancanaro.javacheck; import java.util.Collections; import java.util.Iterator; import java.util.List; -import java.util.Optional; import java.util.function.Function; import java.util.function.Predicate; public final class Iterators { + private Iterators() { + } + public static <T> RangeIterator<T> rangeIterator(int countTo, Function<Integer,T> fn) { return new RangeIterator<T>(countTo, fn); } diff --git a/src/main/java/au/id/zancanaro/RoseTree.java b/src/main/java/au/id/zancanaro/javacheck/RoseTree.java index 458d441..d9fb508 100644 --- a/src/main/java/au/id/zancanaro/RoseTree.java +++ b/src/main/java/au/id/zancanaro/javacheck/RoseTree.java @@ -1,9 +1,11 @@ -package au.id.zancanaro; +package au.id.zancanaro.javacheck; import java.io.IOException; import java.io.Writer; +import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; +import java.util.List; import java.util.function.Function; import java.util.function.Predicate; @@ -49,11 +51,11 @@ public class RoseTree<T> { )); } - public static <T, R> RoseTree<R> zip(Function<T[], R> fn, RoseTree<T>[] trees) { + public static <T, R> RoseTree<R> zip(Function<List<T>, R> fn, RoseTree<T>[] trees) { @SuppressWarnings("unchecked") - T[] heads = (T[]) new Object[trees.length]; + List<T> heads = new ArrayList(trees.length); for (int i = 0; i < trees.length; ++i) { - heads[i] = trees[i].getValue(); + heads.add(trees[i].getValue()); } return new RoseTree<>( fn.apply(heads), diff --git a/src/main/java/au/id/zancanaro/ShrinkResult.java b/src/main/java/au/id/zancanaro/javacheck/ShrinkResult.java index fb2f2d4..e1463f0 100644 --- a/src/main/java/au/id/zancanaro/ShrinkResult.java +++ b/src/main/java/au/id/zancanaro/javacheck/ShrinkResult.java @@ -1,6 +1,6 @@ -package au.id.zancanaro; +package au.id.zancanaro.javacheck; -class ShrinkResult { +public class ShrinkResult { public final Object[] args; public final Throwable thrown; diff --git a/src/main/java/au/id/zancanaro/annotations/DataSource.java b/src/main/java/au/id/zancanaro/javacheck/annotations/DataSource.java index 9fe255b..a954c1b 100644 --- a/src/main/java/au/id/zancanaro/annotations/DataSource.java +++ b/src/main/java/au/id/zancanaro/javacheck/annotations/DataSource.java @@ -1,4 +1,4 @@ -package au.id.zancanaro.annotations; +package au.id.zancanaro.javacheck.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/src/main/java/au/id/zancanaro/annotations/Property.java b/src/main/java/au/id/zancanaro/javacheck/annotations/Property.java index f750596..aae4ccd 100644 --- a/src/main/java/au/id/zancanaro/annotations/Property.java +++ b/src/main/java/au/id/zancanaro/javacheck/annotations/Property.java @@ -1,4 +1,4 @@ -package au.id.zancanaro.annotations; +package au.id.zancanaro.javacheck.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/src/main/java/au/id/zancanaro/annotations/Seed.java b/src/main/java/au/id/zancanaro/javacheck/annotations/Seed.java index 99f00ca..ee8c739 100644 --- a/src/main/java/au/id/zancanaro/annotations/Seed.java +++ b/src/main/java/au/id/zancanaro/javacheck/annotations/Seed.java @@ -1,4 +1,4 @@ -package au.id.zancanaro.annotations; +package au.id.zancanaro.javacheck.annotations; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/src/main/java/au/id/zancanaro/Properties.java b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java index b5a0649..271e763 100644 --- a/src/main/java/au/id/zancanaro/Properties.java +++ b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java @@ -1,8 +1,11 @@ -package au.id.zancanaro; +package au.id.zancanaro.javacheck.junit; -import au.id.zancanaro.annotations.DataSource; -import au.id.zancanaro.annotations.Property; -import au.id.zancanaro.annotations.Seed; +import au.id.zancanaro.javacheck.Generator; +import au.id.zancanaro.javacheck.RoseTree; +import au.id.zancanaro.javacheck.ShrinkResult; +import au.id.zancanaro.javacheck.annotations.DataSource; +import au.id.zancanaro.javacheck.annotations.Property; +import au.id.zancanaro.javacheck.annotations.Seed; import org.junit.AssumptionViolatedException; import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.model.FrameworkMethod; @@ -191,7 +194,7 @@ public class Properties extends BlockJUnit4ClassRunner { generators[index++] = this.generators.get(type); } @SuppressWarnings("unchecked") - Generator<Object[]> generator = Generator.tuple((Generator<Object>[]) generators); + Generator<Object[]> generator = Generator.tuple((Generator<Object>[]) generators).fmap(List::toArray); long seed = getSeed(method); Random random = new Random(seed); diff --git a/src/main/java/au/id/zancanaro/PropertyError.java b/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java index a0ee6df..138c47f 100644 --- a/src/main/java/au/id/zancanaro/PropertyError.java +++ b/src/main/java/au/id/zancanaro/javacheck/junit/PropertyError.java @@ -1,4 +1,6 @@ -package au.id.zancanaro; +package au.id.zancanaro.javacheck.junit; + +import au.id.zancanaro.javacheck.ShrinkResult; import java.util.Arrays; import java.util.Collection; diff --git a/src/test/java/au/id/zancanaro/SimpleListOperationsTest.java b/src/test/java/au/id/zancanaro/javacheck/junit/SimpleListOperationsTest.java index 20f419a..8a298be 100644 --- a/src/test/java/au/id/zancanaro/SimpleListOperationsTest.java +++ b/src/test/java/au/id/zancanaro/javacheck/junit/SimpleListOperationsTest.java @@ -1,15 +1,16 @@ -package au.id.zancanaro; +package au.id.zancanaro.javacheck.junit; -import au.id.zancanaro.annotations.DataSource; -import au.id.zancanaro.annotations.Property; +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; import java.util.Collections; import java.util.List; -import static au.id.zancanaro.Generators.integer; -import static au.id.zancanaro.Generators.listOf; +import static au.id.zancanaro.javacheck.Generators.*; import static org.junit.Assert.assertEquals; @RunWith(Properties.class) @@ -18,7 +19,10 @@ public class SimpleListOperationsTest { @DataSource public static Generator<List<Integer>> listOfIntegers = listOf(integer()); - @Property + @DataSource + public static Generator<List<String>> listOfStrings = listOf(stringOf(alphaNumericCharacter())); + + @Property(maxSize = 100, runs = 1000) public void sortingIsIdempotent(List<Integer> list) { List<Integer> left = new ArrayList<>(list); Collections.sort(left); @@ -30,9 +34,9 @@ public class SimpleListOperationsTest { assertEquals(left, right); } - @Property(maxSize = 10000, runs = 10000) - public void reverseIsItsOwnInverse(List<Integer> list) { - List<Integer> reversed = new ArrayList<>(list); + @Property(maxSize = 100, runs = 1000) + public void reverseIsItsOwnInverse(List<String> list) { + List<String> reversed = new ArrayList<>(list); Collections.reverse(reversed); Collections.reverse(reversed); |