fscheck generating string with size between min & max

南楼画角 提交于 2019-12-23 10:18:38

问题


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!

来源:https://stackoverflow.com/questions/26255550/fscheck-generating-string-with-size-between-min-max

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!