From 7eabc41ad41e0da58fff2a6bb04212ea0cd3baa2 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Wed, 3 Jun 2015 12:39:14 +1000 Subject: Add some more interesting tests --- .../java/au/id/zancanaro/javacheck/Generator.java | 5 ++++ .../javacheck/GeneratorSampleIterator.java | 30 ++++++++++++++++++++++ .../java/au/id/zancanaro/javacheck/Generators.java | 10 +++++--- 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 src/main/java/au/id/zancanaro/javacheck/GeneratorSampleIterator.java (limited to 'src/main/java') diff --git a/src/main/java/au/id/zancanaro/javacheck/Generator.java b/src/main/java/au/id/zancanaro/javacheck/Generator.java index c28d967..557cf97 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generator.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generator.java @@ -1,5 +1,6 @@ package au.id.zancanaro.javacheck; +import java.util.Iterator; import java.util.List; import java.util.Random; import java.util.function.Function; @@ -128,4 +129,8 @@ public interface Generator { default Generator flatMap(Function> action) { return (random, size) -> RoseTree.join(this.generate(random, size).map(action).map(g -> g.generate(random, size))); } + + default Iterator sample(Random random, int maxSize) { + return new GeneratorSampleIterator<>(this, random, maxSize); + } } diff --git a/src/main/java/au/id/zancanaro/javacheck/GeneratorSampleIterator.java b/src/main/java/au/id/zancanaro/javacheck/GeneratorSampleIterator.java new file mode 100644 index 0000000..f7d8e17 --- /dev/null +++ b/src/main/java/au/id/zancanaro/javacheck/GeneratorSampleIterator.java @@ -0,0 +1,30 @@ +package au.id.zancanaro.javacheck; + +import java.util.Iterator; +import java.util.Random; + +public class GeneratorSampleIterator implements Iterator { + private final Generator generator; + private final Random random; + private final int maxSize; + private int size; + + public GeneratorSampleIterator(Generator generator, Random random, int maxSize) { + this.generator = generator; + this.random = random; + this.maxSize = maxSize; + this.size = 0; + } + + @Override + public boolean hasNext() { + return true; + } + + @Override + public T next() { + return generator + .generate(random, Math.min(size++, maxSize)) + .getValue(); + } +} diff --git a/src/main/java/au/id/zancanaro/javacheck/Generators.java b/src/main/java/au/id/zancanaro/javacheck/Generators.java index 548bd29..4049149 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generators.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generators.java @@ -91,10 +91,12 @@ public final class Generators { return (random, size) -> { Generator countGen = sized(s -> integer(minElements, maxElements)); int count = countGen.generate(random, size).getValue(); - return Generator.list(count, gen).generate(random, size) + return Generator.list(count, gen) + .generate(random, size) .filter(list -> minElements <= list.size() - && list.size() < maxElements); + && list.size() < maxElements) + .map(Collections::unmodifiableList); }; } @@ -102,7 +104,9 @@ public final class Generators { return (random, size) -> { Generator countGen = sized(s -> integer(0, s)); int count = countGen.generate(random, size).getValue(); - return Generator.list(count, gen).generate(random, size); + return Generator.list(count, gen) + .generate(random, size) + .map(Collections::unmodifiableList); }; } -- cgit v1.2.3