简体   繁体   English

Haskell QuickCheck的详尽属性?

[英]Exhaustive properties in Haskell QuickCheck?

Consider the following QuickCheck program in Haskell 考虑Haskell中的以下QuickCheck程序

{-# LANGUAGE TemplateHaskell #-}
import Test.QuickCheck
import Test.QuickCheck.All

prop_trivial :: Bool
prop_trivial = 42 == (6 * 7)

-- Wacky boilerplate to make all tests run.
return []
runTests = $quickCheckAll
main = do
  runTests

This works with ghc version 7.8.3 and QuickCheck 2.7.6. 这适用于ghc版本7.8.3和QuickCheck 2.7.6。 Problem is that it repeats the test 100 times. 问题是它重复测试100次。 I look around for a mitigation and find exhaustive in the QuickCheck docs here . 我到处寻找缓解措施,并在此处的QuickCheck文档中找到了exhaustive 的方法 Groovy! 时髦! I change my prop_trivial to the following: 我将prop_trivial更改为以下内容:

prop_trivial = exhaustive $ property $ 42 == (6 * 7)

which type-checks and compiles, but fails: 进行类型检查和编译,但失败:

=== prop_trivial from /blahblahblah/FooTest.hs:6 ===
*** Failed! Falsifiable (after 1 test): 
False

I'm a bit stuck on how to understand and debug this result; 我对如何理解和调试此结果有些困惑; the docs are a bit too thin for me to figure out what's going on. 我的文档太薄了,无法弄清楚到底发生了什么。

You might like smallcheck . 您可能喜欢smallcheck It is not as developed as QuickCheck, but quite nice for exhaustive testing. 它不像QuickCheck那样开发,但是对于详尽的测试来说相当不错。 Where QuickCheck generates random inputs, smallcheck generates its inputs systematically, running your properties on all inputs up to a given (by you) size. 在QuickCheck生成随机输入的地方,smallcheck会系统地生成其输入,并在所有输入(不超过您指定的大小)上运行属性。 For types with finitely many inhabitants, this makes it pretty trivial to set up exhaustive tests, and because we're not trying to generate random stuff, it also makes it easy to avoid repeating tests. 对于居住人数有限的类型,设置详尽的测试非常简单,并且由于我们不尝试生成随机的东西,因此也很容易避免重复测试。 Here's a simple ghci example: 这是一个简单的ghci示例:

Test.SmallCheck> smallCheck 10 (6*7 == 42)
Completed 1 tests without failure.

The 10 is a maximum depth used to choose when to stop generating inputs; 10是用于选择何时停止生成输入的最大深度。 since there are no inputs, it's ignored in this example. 由于没有输入,因此在此示例中将其忽略。 For an example where the size is used, consider: 有关使用大小的示例,请考虑:

Test.SmallCheck> smallCheck 10 (\x -> x <= (10 :: Integer))
Completed 21 tests without failure.
Test.SmallCheck> smallCheck 20 (\x -> x <= (10 :: Integer))
Failed test no. 22.
there exists 11 such that
  condition is false

It seems that you can use once to modify a property to run only once. 似乎您可以使用once将属性修改为仅运行一次。

Some example code: 一些示例代码:

{-# LANGUAGE TemplateHaskell #-}
import Test.QuickCheck
import Test.QuickCheck.All

prop_trivial :: Int -> Bool
prop_trivial x = x == x
prop_trivial2 = once prop_trivial
prop_true = True
prop_true2 = once True

-- Wacky boilerplate to make all tests run.
return []
runTests = $quickCheckAll
main = do
  putStrLn $ "exhaustive prop_trivial  = " ++ show (exhaustive prop_trivial)
  putStrLn $ "exhaustive prop_trivial2 = " ++ show (exhaustive prop_trivial2)
  putStrLn $ "exhaustive prop_true     = " ++ show (exhaustive prop_true)
  putStrLn $ "exhaustive prop_true2    = " ++ show (exhaustive prop_true2)
  runTests

Output: 输出:

exhaustive prop_trivial  = False
exhaustive prop_trivial2 = False
exhaustive prop_true     = True
exhaustive prop_true2    = False
=== prop_trivial from qc2.hs:5 ===
+++ OK, passed 100 tests.

=== prop_trivial2 from qc2.hs:7 ===
+++ OK, passed 1 tests.

=== prop_true from qc2.hs:8 ===
+++ OK, passed 100 tests.

=== prop_true2 from qc2.hs:9 ===
+++ OK, passed 1 tests.

exhaustive prop only returns True if testing of prop is known to be exhaustive - note the difference between exhaustive True and exhaustive $ once True . 仅当已知prop的测试是exhaustive 道具才返回True-请注意exhaustive Trueexhaustive $ once True之间的区别。

The source is really straight-forward. 消息来源确实很直接。 The default definition for exhaustive is used for the Property instance and the default is exhaustive _ = False . exhaustive的默认定义用于Property实例,默认定义为exhaustive _ = False I think you probably want exhaustive $ 42 == (6 * 7) 我想您可能想要exhaustive $ 42 == (6 * 7)

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

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