quickCheckAll总是返回“真” [英] quickCheckAll always return "True"
问题描述
我尝试使用另一个回答使用QuickCheck。
我这样测试:
{ - #LANGUAGE TemplateHaskell# - }
import Test.QuickCheck
import Test.QuickCheck.All
last':: [a] - > a
last'[x] = x
last'(_:xs)= last'xs
prop_test x = last'x == last x
check = do
putStrLnquickCheck
quickCheck(prop_test :: [Char] - > Bool)
check2 = do
putStrLnquickCheckAll
$ quickCheckAll
然后我将它载入winGHCI并调用 check
和 check2
。我得到
quickCheck
***失败! (1测试后):
例外:
list.hs:(7,1) - (8,23):函数last'
中的非穷举模式
我认为这是合理的。但是,我从 check2
$ p $
quickCheckAll
True
我很困惑,因为无论我如何更改 last'
函数,甚至是错误的, quickCheckAll
总是返回True。
我的代码出了什么问题?我该如何解决这个问题?
从 Test.QuickCheck.All
文档:
要使用
quickCheckAll
,请在模块中添加一个定义到return []
runTests = $ quickCheckAll
然后执行
runTests
。
注意:GHC 7.8需要上述示例中的奇怪
return []
;没有它,quickCheckAll
将无法找到任何属性。
$在您的
检查前添加
return []
/ code>使它适用于我。I'm trying to use QuickCheck following another answer. I test like this:
{-# LANGUAGE TemplateHaskell #-} import Test.QuickCheck import Test.QuickCheck.All last' :: [a] -> a last' [x] = x last' (_:xs) = last' xs prop_test x = last' x == last x check = do putStrLn "quickCheck" quickCheck (prop_test :: [Char]-> Bool) check2 = do putStrLn "quickCheckAll" $quickCheckAll
Then I load it in winGHCI and call
check
andcheck2
. I getquickCheck *** Failed! (after 1 test): Exception: list.hs:(7,1)-(8,23): Non-exhaustive patterns in function last' ""
which I think it's reasonable. However, I get this from
check2
quickCheckAll True
I'm confused because no matter how I change the
last'
function, even wrong,quickCheckAll
always return True.What's wrong with my code? How can I fix this?
解决方案From the
Test.QuickCheck.All
docs:To use
quickCheckAll
, add a definition to your module along the lines ofreturn [] runTests = $quickCheckAll
and then execute
runTests
.Note: the bizarre
return []
in the example above is needed on GHC 7.8; without it,quickCheckAll
will not be able to find any of the properties.Adding
return []
before yourcheck
makes it work for me.这篇关于quickCheckAll总是返回“真”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!