summaryrefslogtreecommitdiff
path: root/src/main/java/au/id/zancanaro/javacheck
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@zancanaro.id.au>2015-06-03 12:39:14 +1000
committerCarlo Zancanaro <carlo@zancanaro.id.au>2015-06-03 12:39:14 +1000
commit7eabc41ad41e0da58fff2a6bb04212ea0cd3baa2 (patch)
tree567a3ebfc571a3728e2caf2ef565cba24321843d /src/main/java/au/id/zancanaro/javacheck
parent27fcf37206591c774d79ec60de8d404ed83378ac (diff)
Add some more interesting tests
Diffstat (limited to 'src/main/java/au/id/zancanaro/javacheck')
-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);
};
}