简体   繁体   中英

How to use custom listOf in QuickCheck

In my code, I need to generate a list of Point

I've instanced the custom type Point in the Arbitrary Type Class. So far so good.

Now I need to generate a list: [Points]

But the default random list generated by QuickCheck(using listOf I believe?) is not working for me. Because I need some special relations between the points . I know of the Generator combinators, ex. suchAs But they are too slow(because of very rare relation)

I've defined a customed Gen [Point] to suit my needs.

randomBoard :: Gen [Point]
randomBoard = ...

But I have no idea how to let my test to use this custom Gen in the property test because it defaults to the listOf generated list.

IIUC, the issue is that your tests are using Arbitrary instances, by way of the Testable instance for (Arbitrary a, Show a, Testable prop) => Testable (a -> prop) .

A common pattern to get custom generators is to throw around some newtype s with the desired Arbitrary instances. So you'd define something like

newtype Board = Board [Point]

instance Arbitrary Board where
  ...

That would work. But I'd recommend bypassing Arbitrary altogether and writing your properties with forAll and its variants .

genBoard :: Gen [Point]
genBoard = ...

shrinkBoard :: [Point] -> [[Point]]
shrinkBoard = ...

myProperty :: Property
myProperty = forAllShrink genBoard shrinkBoard (\board -> ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM