5

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!

vidi
  • 2,056
  • 16
  • 34
  • 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 Oct 08 '14 at 11:44
  • 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 Oct 08 '14 at 11:47
  • 1
    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: https://fsharp.github.io/FsCheck/Properties.html - but as I said declaring a generator using the monad as you did is more performant – Random Dev Oct 08 '14 at 11:59
  • 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 Oct 08 '14 at 12:21
  • that's almost normal in this case as most tried values will lie outside your range - this is your last approach is so much better – Random Dev Oct 08 '14 at 12:42
  • 1
    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 Oct 10 '14 at 09:34
  • I was in vacation and now that I'm back we still haven't fixed the generators but we have a different problem. I'll be back if my original problem appears again. Thanks! – vidi Oct 25 '14 at 23:30
  • I made the registration of default generators a bit more eager in 1.0.2. Can you check if that fixes the exceptions you're getting? – Kurt Schelfthout Oct 27 '14 at 21:58

1 Answers1

3

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
DharmaTurtle
  • 6,858
  • 6
  • 38
  • 52