关于Haskell的QuickCheck,缩小是多少? [英] What is a shrink, with regard to Haskell's QuickCheck?

查看:139
本文介绍了关于Haskell的QuickCheck,缩小是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习QuickCheck> = 2.6,但我不明白收缩是多少。从查看键入签名,缩小外观更喜欢展开!请解释一下:)

I'm learning the ropes of QuickCheck >= 2.6 but I don't understand what a shrink is. From looking at the type signature shrink looks more like expand! Please illuminate me :)

推荐答案

当QuickCheck发现违反属性的输入时,它将首先尝试查找更小的输入违反财产,以便为开发人员提供有关失败性质的更好信息。

When QuickCheck finds an input that violates a property, it will first try to find smaller inputs that also violate the property, in order to give the developer a better message about the nature of the failure.

当然,小的含义取决于数据类型题;快速检查它是来自 shrink 函数的任何内容。

What it means to be „small" of course depends on the datatype in question; to QuickCheck it is anything that comes out from from the shrink function.

最好在QuickCheck会话中解释:

It is best explained in a QuickCheck session:


Prelude Test.QuickCheck> let prop l = all (/= 5) l
Prelude Test.QuickCheck> quickCheck prop
*** Failed! Falsifiable (after 10 tests and 2 shrinks):    
[5]

所以QuickCheck能够提供最小的计数器,例如,但从评论来看,它首先有一个更大的列表,然后使用 shrink 减少它。为了更仔细地看看发生了什么,我们使用 verboseCheck

So here QuickCheck was able to give the smallest counter-example, but judging from the comments, it first had a larger list in mind and then reduced it using shrink. To have a closer look at what is happening, we use verboseCheck:


Prelude Test.QuickCheck> verboseCheck prop
Passed:  
[]
Passed: 
[0]
Passed:  
[-2,1]
Passed:  
[-2,2,-2]
Passed:  
[-4]
Failed:  
[-1,-2,5,4,2]
*** Failed! Passed:                       
[]
Failed:                                       
[5,4,2]
Passed:                                    
[]
Passed:                                       
[4,2]
Failed:                                       
[5,2]
Passed:                                     
[]
Passed:                                       
[2]
Failed:                                       
[5]
Passed:                                     
[]
Passed:                                       
[0]
Passed:                                       
[3]
Passed:                                       
[4]
Falsifiable (after 6 tests and 3 shrinks):    
[5]

QuickCheck尝试一下这个命题所包含的一些列表,然后找到 [ - 1,-2,5,4,2] 。现在它通过尝试它的子列表来减少列表。您可以在GHCi中说服自己缩小[-1,-2,5,4,2] == [[],[5,4,2],[ - 1,-2,2] ,... ,第二个条目是第一个仍然无法通过测试的条目。然后QuickCheck继续并进一步收缩: shrink [5,4,2] == [[],[4,2],[5,2],... ,并进一步 shrink [5,2]
[[],[2],[5],...
。最后,它试图进一步收缩 [5] ,但没有一个缩小[5] ==
[[],[0], [3],[4]]
失败了,所以最终的计数示例是 [5]

QuickCheck tries a few lists for which the proposition holds, and then finds [-1,-2,5,4,2]. Now it reduces the list, by trying sublists of it. You can convince yourself in GHCi that shrink [-1,-2,5,4,2] == [[],[5,4,2],[-1,-2,2],... and the second entry is the first that still fails the test. QuickCheck then continues with that and shrinks further: shrink [5,4,2] == [[],[4,2],[5,2],..., and further shrink [5,2] [[],[2],[5],.... Lastly it tries to shrink [5] further, but none of shrink [5] == [[],[0],[3],[4]] fail the proposition, so the final count-example is [5].

这篇关于关于Haskell的QuickCheck,缩小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆