quickCheckAll总是返回“真” [英] quickCheckAll always return "True"

查看:132
本文介绍了quickCheckAll总是返回“真”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用另一个回答使用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 and check2. I get

quickCheck
*** 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 of

return []
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 your check makes it work for me.

这篇关于quickCheckAll总是返回“真”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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