Search code examples
haskellquickcheck

Testing a random generator using QuickCheck


I've written a random generator to create a minesweeper board. It returns a Gen Board, where Board is my own defined datatype. I want to test that it does what it's supposed to. As it is a generator, I can't use the functions I'm used to which returns a Bool or Property. Is there a good way to test generators using quickCheck?


Solution

  • Assuming that you have

    genBoard :: Gen Board
    isValidBoard :: Board -> Bool
    

    You can test that the generator is valid with the forAll combinator,

    prop_genBoardMakesValidBoards :: Property
    prop_genBoardMakesValidBoards = forAll genBoard isValidBoard
    

    It is harder to test that the generator is random enough, for example that it doesn't always return a trivial board.