简体   繁体   English

如何在 QuickCheck 中使用自定义 listOf

[英]How to use custom listOf in QuickCheck

In my code, I need to generate a list of Point在我的代码中,我需要生成一个Point列表

I've instanced the custom type Point in the Arbitrary Type Class.我在Arbitrary类型类中实例化了自定义类型Point So far so good.到现在为止还挺好。

Now I need to generate a list: [Points]现在我需要生成一个列表: [Points]

But the default random list generated by QuickCheck(using listOf I believe?) is not working for me.但是listOf生成的默认随机列表(使用listOf我相信?)对我不起作用。 Because I need some special relations between the points .因为我需要points之间的一些特殊关系。 I know of the Generator combinators, ex.我知道 Generator 组合器,例如。 suchAs But they are too slow(because of very rare relation) suchAs但是他们太慢了(因为非常罕见的关系)

I've defined a customed Gen [Point] to suit my needs.我已经定义了一个定制的Gen [Point]来满足我的需求。

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.但我不知道如何让我的测试在属性测试中使用这个自定义Gen ,因为它默认为listOf生成的列表。

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) . IIUC,问题是您的测试使用Arbitrary实例,通过Testable 实例 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.获取自定义生成器的常见模式是使用所需的Arbitrary实例抛出一些newtype 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 .但我建议完全绕过Arbitrary并使用forAll 及其变体编写您的属性。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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