summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/au/id/zancanaro/javacheck/Generator.java5
-rw-r--r--src/main/java/au/id/zancanaro/javacheck/GeneratorSampleIterator.java30
-rw-r--r--src/main/java/au/id/zancanaro/javacheck/Generators.java10
3 files changed, 42 insertions, 3 deletions
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<T> {
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)));
}
+
+ default Iterator<T> 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<T> implements Iterator<T> {
+ private final Generator<T> generator;
+ private final Random random;
+ private final int maxSize;
+ private int size;
+
+ public GeneratorSampleIterator(Generator<T> 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<Integer> 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<Integer> 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);
};
}