2

Probably a stupid question, but how do I generate a list of a specific size for FSCheck?

I can restrict using:

let fn_of_2_check xs = (xs.Length=2) ==> fn_of_2 xs

but, obviously, this will throw away loads of lists.

Here fn_of_2 does some test on lists of length 2 only and returns true or false.

Abel
  • 56,041
  • 24
  • 146
  • 247
b1g3ar5
  • 335
  • 2
  • 9

2 Answers2

6

How about:

let listOfLength n = Gen.listOfLength n Arb.generate |> Arb.fromGen
Check.Quick (Prop.forAll (listOfLength 2) fn_of_2)
Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
2

A trivial solution would be to write a test that takes two inputs and builds a two-element list from them:

let fn_of_2_check x y = fn_of_2 [x; y]

However, if you're testing the function for two-elements list only, then perhaps the function should take a two-element tuple as the input. Or, you could write a test that verifies some behaviour for two-element lists and some other behaviour for lists of other length.

(But if you want to check the behaviour for two-element lists specifically, then the above should work.)

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • The functions will have different lengths of lists (numbers of parameters), but it will be fixed for each function. I suppose it's like map, map2, map3 - I could write fn_of_2_check fn_of_3_check etc. – b1g3ar5 Mar 19 '12 at 14:22