[英]How do I write a QuickCheck property which expects a non-empty list of non-zero numbers?
This is a hack ( cogsRpm
is a user supplied function which is supposed to match the output of go): 这是一个hack(
cogsRpm
是一个用户提供的函数,应该匹配go的输出):
propCheck ns = cogsRpm (ns ++ [42]) == go (ns ++ [42])
I added the 42 to stop quickcheck from generating zero length lists. 我添加了42来停止生成零长度列表的快速检查。 It also could fail still though because it isn't supposed to have zeroes in there either.
它也可能会失败,因为它也不应该在那里有零。
I read the docs, but there are not enough examples for me to parse out how to actually use them. 我阅读了文档,但是没有足够的例子来解析如何实际使用它们。 I did manage to get this on another case:
我确实设法在另一个案例中得到这个:
prop_check (Positive x) (Positive y) = updateHealth x y == if y > x then 0 else x-y
Which forces positives, and I tried combining a bunch of things to get listOf1
and NonZero
, but I could not figure out the syntax, hence the hack to add an element to the list. 这是强制积极的,我尝试结合一堆东西来获得
listOf1
和NonZero
,但我无法弄清楚语法,因此黑客将一个元素添加到列表中。 How can I avoid this hack? 我该如何避免这种黑客攻击?
QuickCheck comes bundled with a NonEmptyList
newtype, whose Arbitrary
instance only generates non-empty lists. QuickCheck捆绑了一个
NonEmptyList
newtype,其Arbitrary
实例只生成非空列表。 You can combine this with NonZero
to get a non-empty list of non-zero integers: 您可以将其与
NonZero
结合使用以获取非零整数的非空列表:
propCheck :: NonEmptyList (NonZero Int) -> Bool
propCheck (coerce -> ns) = cogsRpm ns == go ns
I'm using coerce
in a view pattern to tersely and efficiently turn the NonEmptyList (NonZero Int)
back into a regular list of Int
s. 我使用
coerce
的视图模式 ,以简洁,高效地打开NonEmptyList (NonZero Int)
回的常规列表Int
秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.