6
votes

I try to write a FsCheck generator that generates strings with length in a given interval.

My attempt is the following:

let genString minLength maxLength = 
    let isValidLength (s : string) = 
        s.Length >= minLength && s.Length <= maxLength

    Arb.generate
    |> Gen.suchThat isValidLength
    |> Arb.fromGen

...and I get the error:

"System.Exception : No instances of class FsCheck.Arbitrary`1[a] for type System.String with arguments set []"

What am I doing wrong?

thanks!

UPDATE 1:

I managed to write the generator like this:

let genStrings minLength maxLength = 
    gen {
        let! length = Gen.choose (minLength, maxLength)
        let! chars = Gen.arrayOfLength length Arb.generate<char>
        return new String(chars)
    }

Is there a better way?

UPDATE 2: I wanted to add this as a separate question but it's pretty much the same issue as my original one.

So I refactored the above code to the following structure in order to reuse the sequence generator:

let seqOfLength lengthInterval generator =
    gen {
        let! length = Gen.choose lengthInterval
        let! items = Gen.arrayOfLength length generator
        return items |> Seq.ofArray
    }

let sizedString lengthInterval =
    seqOfLength lengthInterval Arb.generate<char>
    |> Gen.map Strings.ofCharSeq

Now I'm getting the runtime error:

System.Exception : No instances of class FsCheck.Arbitrary`1[a] for type System.Char with arguments set []

...which brings me back to my original issue: Why do can't it find any instance of Arbitrary for System.Char? I thought the arbitrary for the basic types are registered by default. What am I doing wrong?

Thanks!

1
Go with your last example, that's the most performant by far (the rest will try strings till it gets one with a length in your interval) - Random Dev
Thanks. I'll keep the question open for a while, maybe somebody posts a better solution. To me this looks a bit too complicated for such a common task as generating strings - vidi
It's a while since I last had a detailed look and maybe there is a helper now but it's not that common to only generate stings in a certain length-range - normaly I would use conditinal properties for this: fsharp.github.io/FsCheck/Properties.html - but as I said declaring a generator using the monad as you did is more performant - Random Dev
I tried the properties approach but I got "arguments exhausted" exception. I guess I will leave the code like this. Thanks for the comments! - vidi
the simulated type-classes are a bit special ... just wrap it back in a gen { ... } - for Arbitrary.generate to work the defaults has to be set (as far as I remember - it's more than a year that I looked into this) here you use it in the context of your module and I suspect that at this time the arbitraries where not set yet - if you wrap it inside a gen the workflow will not run just there and inside FsChecks .Check methods the arbitraries will surely be set - it's a bit like asserting exceptions - you have to make things lazy so that they happen in their right time - Random Dev

1 Answers

2
votes

Your sample code works as of FsCheck v2.14.3

let seqOfLength lengthInterval generator =
    gen {
        let! length = Gen.choose lengthInterval
        let! items = Gen.arrayOfLength length generator
        return items |> Seq.ofArray
    }
let stringOfLength lengthInterval =
    seqOfLength lengthInterval Arb.generate<char>
    |> Gen.map String.Concat