Damian Nadales
Damian Nadales

Reputation: 5037

QuickCheck combinators for shrinking lists of length within given bounds

Is there's a QuickCheck function to produce a list whose length is in a given range, similar to what one would achieve with hedgegog, e.g.:

Gen.list (Range.constant 5 13) someGen

I guess one would write something like:

  do
    n <- choose (minLength, maxLength)
    vectorOf n genA

making such function superfluous, but then I wonder whether there are QuickCheck combinators to shrink lists whose invariant is that they should have a minimum length. A shrink definition like:

  filter ((minLength <=) . length) [ xs' | xs' <- shrink xs ]

seems to be quite inefficient, since we're shrinking terms which we could see in advance that are not valid (since the length will be lower than the minimum threshold).

Upvotes: 2

Views: 162

Answers (1)

cxandru
cxandru

Reputation: 158

If you look at the source of the listOf1 function you see that it is implemented essentially like in your second code snippet, except it uses the sized function, which means the upper bound decreases as shrinks are performed. I think replacing 1 by your desired minimum bound there should do the trick.

Upvotes: 1

Related Questions