diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-03 14:32:07 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-03 14:32:07 +1000 |
commit | 7b1a783b749ab04ab8219ef28f9b1abb0ded6ca4 (patch) | |
tree | a0d25f117dbf3af5adab6468e513c0b0c6ec4b34 /src/main/java/au/id/zancanaro/javacheck | |
parent | 7eabc41ad41e0da58fff2a6bb04212ea0cd3baa2 (diff) |
Rename RoseTree to ShrinkTree (less confusing and more specific)
Diffstat (limited to 'src/main/java/au/id/zancanaro/javacheck')
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/Generator.java | 24 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/Generators.java | 18 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java (renamed from src/main/java/au/id/zancanaro/javacheck/RoseTree.java) | 60 | ||||
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/junit/Properties.java | 12 |
4 files changed, 57 insertions, 57 deletions
diff --git a/src/main/java/au/id/zancanaro/javacheck/Generator.java b/src/main/java/au/id/zancanaro/javacheck/Generator.java index 557cf97..9d88020 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generator.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generator.java @@ -10,7 +10,7 @@ import java.util.function.Function; * trees in a controlled and deterministic way. * * A generator must implement one method: {@link #generate(Random, int)}. The - * {@link RoseTree} it produces defines both the value to be returned, as well + * {@link ShrinkTree} it produces defines both the value to be returned, as well * as the shrink tree for that value. * * Generators form a "monad", and hence have the {@link #map(Function)} and @@ -23,7 +23,7 @@ import java.util.function.Function; @SuppressWarnings("unused") public interface Generator<T> { /** - * Return a {@link RoseTree} containing a new random value of the required + * Return a {@link ShrinkTree} containing a new random value of the required * type, as well as its associated shrink tree. * * Generators also have an abstract notion of "size". Represented by an @@ -33,10 +33,10 @@ public interface Generator<T> { * * @param random The random source for generation purposes * @param size An integer specifying how "big" a thing to produce - * @return The {@link RoseTree} specifying the generated thing and its + * @return The {@link ShrinkTree} specifying the generated thing and its * shrink tree */ - RoseTree<T> generate(Random random, int size); + ShrinkTree<T> generate(Random random, int size); /** * A generator which simply generates the provided value. Does not shrink. @@ -46,7 +46,7 @@ public interface Generator<T> { * @return A {@link Generator} which generates the provided value */ static <T> Generator<T> pure(T value) { - return (random, size) -> RoseTree.pure(value); + return (random, size) -> ShrinkTree.pure(value); } /** @@ -66,12 +66,12 @@ public interface Generator<T> { static <T> Generator<List<T>> tuple(Generator<T>... generators) { return (random, size) -> { @SuppressWarnings("unchecked") - RoseTree<T>[] result = (RoseTree<T>[]) new RoseTree[generators.length]; + ShrinkTree<T>[] result = (ShrinkTree<T>[]) new ShrinkTree[generators.length]; int index = 0; for (Generator<T> generator : generators) { result[index++] = generator.generate(random, size); } - return RoseTree.zip(Function.identity(), result); + return ShrinkTree.zip(Function.identity(), result); }; } @@ -81,8 +81,8 @@ public interface Generator<T> { * * Shrinking for this type involves attempting to remove terms and shrink * each subtree in turn, recursively. (If the length must remain fixed then - * the {@link RoseTree} produced by this generator should be filtered with - * {@link RoseTree#filter(java.util.function.Predicate)}. + * the {@link ShrinkTree} produced by this generator should be filtered with + * {@link ShrinkTree#filter(java.util.function.Predicate)}. * * @param count The length of the list to generate * @param generator The generator to use for each term in the generated @@ -93,11 +93,11 @@ public interface Generator<T> { static <T> Generator<List<T>> list(int count, Generator<T> generator) { return (random, size) -> { @SuppressWarnings("unchecked") - RoseTree<T>[] result = (RoseTree<T>[]) new RoseTree[count]; + ShrinkTree<T>[] result = (ShrinkTree<T>[]) new ShrinkTree[count]; for (int i = 0; i < count; ++i) { result[i] = generator.generate(random, size); } - return RoseTree.shrink(Function.identity(), result); + return ShrinkTree.shrink(Function.identity(), result); }; } @@ -127,7 +127,7 @@ public interface Generator<T> { * result of this */ default <R> Generator<R> flatMap(Function<T, Generator<R>> action) { - return (random, size) -> RoseTree.join(this.generate(random, size).map(action).map(g -> g.generate(random, size))); + return (random, size) -> ShrinkTree.join(this.generate(random, size).map(action).map(g -> g.generate(random, size))); } default Iterator<T> sample(Random random, int maxSize) { diff --git a/src/main/java/au/id/zancanaro/javacheck/Generators.java b/src/main/java/au/id/zancanaro/javacheck/Generators.java index 4049149..5dee924 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generators.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generators.java @@ -18,7 +18,7 @@ public final class Generators { public static <T> Generator<T> suchThat(Generator<T> gen, Predicate<T> predicate) { return (random, size) -> { - RoseTree<T> result = gen.generate(random, size); + ShrinkTree<T> result = gen.generate(random, size); if (predicate.test(result.getValue())) { return result.filter(predicate); } else { @@ -28,7 +28,7 @@ public final class Generators { } public static <T> Generator<T> noShrink(Generator<T> gen) { - return (random, size) -> new RoseTree<>( + return (random, size) -> new ShrinkTree<>( gen.generate(random, size).getValue(), Collections.emptyList()); } @@ -49,15 +49,15 @@ public final class Generators { public static Generator<Boolean> bool() { return (random, size) -> random.nextBoolean() ? - new RoseTree<>(true, Collections.singletonList(new RoseTree<>(false, Collections.emptyList()))) : - new RoseTree<>(false, Collections.emptyList()); + new ShrinkTree<>(true, Collections.singletonList(new ShrinkTree<>(false, Collections.emptyList()))) : + new ShrinkTree<>(false, Collections.emptyList()); } public static Generator<Integer> integer(int lower, int upper) { return (random, size) -> { int value = lower + random.nextInt(upper - lower); int bound = lower > 0 ? lower : (upper < 0 ? upper : 0); - return new RoseTree<>(value, intShrinkingIterable(value, bound)); + return new ShrinkTree<>(value, intShrinkingIterable(value, bound)); }; } @@ -69,8 +69,8 @@ public final class Generators { return sized(size -> integer(0, size)); } - private static Iterable<RoseTree<Integer>> intShrinkingIterable(final int value, final int bound) { - return () -> new Iterator<RoseTree<Integer>>() { + private static Iterable<ShrinkTree<Integer>> intShrinkingIterable(final int value, final int bound) { + return () -> new Iterator<ShrinkTree<Integer>>() { int curr = value - bound; @Override @@ -79,10 +79,10 @@ public final class Generators { } @Override - public RoseTree<Integer> next() { + public ShrinkTree<Integer> next() { int prevCurr = curr; curr = curr / 2; - return new RoseTree<>(value - prevCurr, intShrinkingIterable(value - prevCurr, bound)); + return new ShrinkTree<>(value - prevCurr, intShrinkingIterable(value - prevCurr, bound)); } }; } diff --git a/src/main/java/au/id/zancanaro/javacheck/RoseTree.java b/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java index c735b46..a424806 100644 --- a/src/main/java/au/id/zancanaro/javacheck/RoseTree.java +++ b/src/main/java/au/id/zancanaro/javacheck/ShrinkTree.java @@ -7,11 +7,11 @@ import java.util.function.Function; import java.util.function.Predicate; @SuppressWarnings("unused") -public class RoseTree<T> { +public class ShrinkTree<T> { private final T value; - private final Iterable<RoseTree<T>> children; + private final Iterable<ShrinkTree<T>> children; - public RoseTree(T value, Iterable<RoseTree<T>> children) { + public ShrinkTree(T value, Iterable<ShrinkTree<T>> children) { this.value = value; this.children = children; } @@ -20,28 +20,28 @@ public class RoseTree<T> { return value; } - public Iterator<RoseTree<T>> getChildren() { + public Iterator<ShrinkTree<T>> getChildren() { return children.iterator(); } - public static <T> RoseTree<T> pure(T value) { - return new RoseTree<>(value, Collections.emptyList()); + public static <T> ShrinkTree<T> pure(T value) { + return new ShrinkTree<>(value, Collections.emptyList()); } - public static <T> RoseTree<T> join(RoseTree<RoseTree<T>> tree) { - return new RoseTree<>( + public static <T> ShrinkTree<T> join(ShrinkTree<ShrinkTree<T>> tree) { + return new ShrinkTree<>( tree.getValue().getValue(), () -> Iterators.concat( - Iterators.mappingIterator(RoseTree::join, tree.children.iterator()), + Iterators.mappingIterator(ShrinkTree::join, tree.children.iterator()), tree.getValue().children.iterator())); } - private static <T> Iterator<RoseTree<T>[]> permutations(RoseTree<T>[] trees) { + private static <T> Iterator<ShrinkTree<T>[]> permutations(ShrinkTree<T>[] trees) { return Iterators.flatten( Iterators.rangeIterator(trees.length, index -> Iterators.mappingIterator(child -> { @SuppressWarnings("unchecked") - RoseTree<T>[] result = (RoseTree<T>[]) new RoseTree[trees.length]; + ShrinkTree<T>[] result = (ShrinkTree<T>[]) new ShrinkTree[trees.length]; for (int i = 0; i < trees.length; ++i) { result[i] = (i == index ? child : trees[i]); } @@ -50,27 +50,27 @@ public class RoseTree<T> { )); } - private static <T> List<T> makeHeadList(RoseTree<T>[] trees) { + private static <T> List<T> makeHeadList(ShrinkTree<T>[] trees) { List<T> heads = new ArrayList<>(trees.length); - for (RoseTree<T> tree : trees) { + for (ShrinkTree<T> tree : trees) { heads.add(tree.getValue()); } return heads; } - public static <T, R> RoseTree<R> zip(Function<List<T>, R> fn, RoseTree<T>[] trees) { - return new RoseTree<>( + public static <T, R> ShrinkTree<R> zip(Function<List<T>, R> fn, ShrinkTree<T>[] trees) { + return new ShrinkTree<>( fn.apply(makeHeadList(trees)), () -> Iterators.mappingIterator( - roses -> RoseTree.zip(fn, roses), - RoseTree.permutations(trees))); + shrinks -> ShrinkTree.zip(fn, shrinks), + ShrinkTree.permutations(trees))); } - private static <T> Iterator<RoseTree<T>[]> removeEach(RoseTree<T>[] trees) { + private static <T> Iterator<ShrinkTree<T>[]> removeEach(ShrinkTree<T>[] trees) { return Iterators.concat( Iterators.rangeIterator(trees.length, index -> { @SuppressWarnings("unchecked") - RoseTree<T>[] result = (RoseTree<T>[]) new RoseTree[trees.length - 1]; + ShrinkTree<T>[] result = (ShrinkTree<T>[]) new ShrinkTree[trees.length - 1]; for (int i = 0; i < trees.length - 1; ++i) { result[i] = trees[(i >= index ? i + 1 : i)]; } @@ -79,27 +79,27 @@ public class RoseTree<T> { permutations(trees)); } - public static <T, R> RoseTree<R> shrink(Function<List<T>, R> fn, RoseTree<T>[] trees) { - return new RoseTree<>( + public static <T, R> ShrinkTree<R> shrink(Function<List<T>, R> fn, ShrinkTree<T>[] trees) { + return new ShrinkTree<>( fn.apply(makeHeadList(trees)), () -> Iterators.mappingIterator( - roses -> RoseTree.shrink(fn, roses), - RoseTree.removeEach(trees))); + shrinks -> ShrinkTree.shrink(fn, shrinks), + ShrinkTree.removeEach(trees))); } - public <R> RoseTree<R> map(Function<T, R> f) { - return new RoseTree<>( + public <R> ShrinkTree<R> map(Function<T, R> f) { + return new ShrinkTree<>( f.apply(this.value), () -> Iterators.mappingIterator(tree -> tree.map(f), this.children.iterator())); } - public <R> RoseTree<R> flatMap(Function<T, RoseTree<R>> f) { - return RoseTree.join(this.map(f)); + public <R> ShrinkTree<R> flatMap(Function<T, ShrinkTree<R>> f) { + return ShrinkTree.join(this.map(f)); } - public RoseTree<T> filter(Predicate<T> predicate) { + public ShrinkTree<T> filter(Predicate<T> predicate) { if (predicate.test(this.getValue())) { - return new RoseTree<>( + return new ShrinkTree<>( this.getValue(), () -> Iterators.mappingIterator(tree -> tree.filter(predicate), Iterators.filteringIterator( @@ -119,7 +119,7 @@ public class RoseTree<T> { public void print(Writer output, Function<T, String> toString) throws IOException { output.write(toString.apply(this.getValue())); output.write('['); - for (RoseTree<T> child : children) { + for (ShrinkTree<T> child : children) { child.print(output, toString); } output.write(']'); diff --git a/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java index 1e3f502..b1cb375 100644 --- a/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java +++ b/src/main/java/au/id/zancanaro/javacheck/junit/Properties.java @@ -1,7 +1,7 @@ package au.id.zancanaro.javacheck.junit; import au.id.zancanaro.javacheck.Generator; -import au.id.zancanaro.javacheck.RoseTree; +import au.id.zancanaro.javacheck.ShrinkTree; import au.id.zancanaro.javacheck.ShrinkResult; import au.id.zancanaro.javacheck.annotations.DataSource; import au.id.zancanaro.javacheck.annotations.Property; @@ -208,7 +208,7 @@ public class Properties extends BlockJUnit4ClassRunner { int numTests = property.runs(); for (int i = 0; i < numTests; ++i) { int size = Math.min(i + 1, maxSize); - RoseTree<Object[]> tree = generator.generate(random, size); + ShrinkTree<Object[]> tree = generator.generate(random, size); try { runTest(tree.getValue()); assumptionsViolated = 0; @@ -225,7 +225,7 @@ public class Properties extends BlockJUnit4ClassRunner { } } - private ShrinkResult shrink(RoseTree<Object[]> failed, Throwable originalEx) { + private ShrinkResult shrink(ShrinkTree<Object[]> failed, Throwable originalEx) { // this array is a mutable container so the shutdown handler can see the new version ShrinkResult[] smallest = new ShrinkResult[]{ new ShrinkResult(failed.getValue(), originalEx)}; @@ -233,10 +233,10 @@ public class Properties extends BlockJUnit4ClassRunner { Thread shutdownHandler = makeShutdownHandler(smallest, originalEx); Runtime.getRuntime().addShutdownHook(shutdownHandler); - Iterator<RoseTree<Object[]>> trees = failed.getChildren(); + Iterator<ShrinkTree<Object[]>> trees = failed.getChildren(); Set<List<Object>> seenArgs = new HashSet<>(); while (trees.hasNext()) { - RoseTree<Object[]> tree = trees.next(); + ShrinkTree<Object[]> tree = trees.next(); if (seenArgs.add(Arrays.asList(tree.getValue()))) { try { runTest(tree.getValue()); @@ -244,7 +244,7 @@ public class Properties extends BlockJUnit4ClassRunner { // ignore, because it's not useful } catch (Throwable ex) { smallest[0] = new ShrinkResult(tree.getValue(), ex); - Iterator<RoseTree<Object[]>> children = tree.getChildren(); + Iterator<ShrinkTree<Object[]>> children = tree.getChildren(); if (children.hasNext()) { trees = children; } else { |