diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-01 16:41:07 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2015-06-01 16:41:07 +1000 |
commit | 7db38e2e4f2c24c2a21d1c19c12bfdad4727ebad (patch) | |
tree | b5717a219be11cfadb9a056eadc4b4a90524c0a1 | |
parent | 110eebaf15768d31d26be3fa52abc8fc25f8488f (diff) |
Make listOf able to take a min/max length of list
-rw-r--r-- | src/main/java/au/id/zancanaro/javacheck/Generators.java | 13 |
1 files changed, 13 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 bf1b1b3..eab3eed 100644 --- a/src/main/java/au/id/zancanaro/javacheck/Generators.java +++ b/src/main/java/au/id/zancanaro/javacheck/Generators.java @@ -80,6 +80,19 @@ public final class Generators { }; } + public static <T> Generator<List<T>> listOf(Generator<T> gen, int minElements, int maxElements) { + 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) + .filter(list -> + minElements <= list.size() + && list.size() < maxElements); + }; + } + + ; + public static <T> Generator<List<T>> listOf(Generator<T> gen) { return (random, size) -> { Generator<Integer> countGen = sized(s -> integer(0, s)); |