summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@zancanaro.id.au>2015-11-24 12:57:27 +1100
committerCarlo Zancanaro <carlo@zancanaro.id.au>2015-11-24 12:57:27 +1100
commit8376ed30f83c9193025da5962de5f5d78edfa185 (patch)
tree6b79a0a9bdedce965d2905de2bc2a6ff10fbccc9 /src/main
parent1b0565b1f6b4d5009da689ba5d486bce203e4905 (diff)
Add an implicit generator for Sets
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/au/id/zancanaro/javacheck/Generators.java11
-rw-r--r--src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java6
2 files changed, 17 insertions, 0 deletions
diff --git a/src/main/java/au/id/zancanaro/javacheck/Generators.java b/src/main/java/au/id/zancanaro/javacheck/Generators.java
index af8f8ad..631e14a 100644
--- a/src/main/java/au/id/zancanaro/javacheck/Generators.java
+++ b/src/main/java/au/id/zancanaro/javacheck/Generators.java
@@ -179,6 +179,17 @@ public final class Generators {
};
}
+ public static <T> Generator<Set<T>> setOf(Generator<T> gen) {
+ 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)
+ .map(HashSet::new)
+ .map(Collections::unmodifiableSet);
+ };
+ }
+
@SuppressWarnings("unchecked")
public static <K, V> Generator<Map<K, V>> mapOf(Generator<K> keyGen, Generator<V> valueGen) {
return (random, size) -> {
diff --git a/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java b/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java
index 827ba3f..c975933 100644
--- a/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java
+++ b/src/main/java/au/id/zancanaro/javacheck/object/GeneratorProvider.java
@@ -11,6 +11,7 @@ import java.lang.reflect.TypeVariable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
+import java.util.Set;
import static au.id.zancanaro.javacheck.Generators.*;
@@ -81,6 +82,11 @@ public interface GeneratorProvider {
List.class.getTypeParameters()[0],
new Annotation[0],
provider));
+ } else if (type == Set.class) {
+ return setOf(provider.getGenerator(
+ Set.class.getTypeParameters()[0],
+ new Annotation[0],
+ provider));
} else if (type == Map.class) {
TypeVariable<?>[] params = Map.class.getTypeParameters();
TypeVariable<?> keyType = params[0], valueType = params[1];