How do I just generate data with fscheck?

1.3k views Asked by At

Is it possible to generate data, specifically a list, with fscheck for use outside of fscheck? I'm unable to debug a situation in fscheck testing where it looks like the comparison results are equal, but fscheck says they are not.

I have this generator for a list of objects. How do I generate a list I can use from this generator?

let genListObj min max  = Gen.listOf Arb.generate<obj> |> Gen.suchThat (fun l -> (l.Length >= min) && (l.Length <= max))
1

There are 1 answers

1
Kurt Schelfthout On BEST ANSWER

Edit: this function is now part of the FsCheck API (Gen.sample) so you don't need the below anymore...

Here is a sample function to generate n samples from a given generator:

let sample n gn  = 
   let rec sample i seed samples =
       if i = 0 then samples
       else sample (i-1) (Random.stdSplit seed |> snd) (Gen.eval 1000 seed gn :: samples)
   sample n (Random.newSeed()) []

Edit: the 1000 magic number in there represents the size of the generated values. 1000 is pretty big - e.g. sequences will be between 0 and 1000 elements long, and so will strings, for example. If generation takes a long time, you may want to tweak that value (or take it in as a parameter of the function).