Haskell:QuickCheck属性使用含义失败测试 [英] Haskell: QuickCheck property fails tests using implications

查看:109
本文介绍了Haskell:QuickCheck属性使用含义失败测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用quickcheck测试以下属性:

  prop_zip xs ys =长度xs ==长度ys ==> 
unzip(zip xs ys)==(xs,ys)

尽管它似乎根据zip和unzip的定义,在逻辑上是正确的,该属性应该是相同长度的列表,quickcheck以下列结尾:

  ***起床!仅通过了49次测试。 

感谢您提供任何提示或建议!

解决方案

通过生成随机测试用例难以满足的先决条件在QuickCheck中通常是一个糟糕的主意。例如,在这种情况下,您可以使用 forAll 来创建符合前提条件的测试用例。 / code>生成与第一个列表长度相同的第二个列表:

  prop_zip'(xs :: [a])= 
forAll(vectorOf(length xs)任意)$ \(ys :: [a]) - >
unzip(zip xs ys)==(xs,ys)

(我是另外还可以在这里使用 ScopedTypeVariables 来确定第二个列表的类型,您可能需要对其进行更改以满足您的特定需求。)


I've got the following property I want to test using quickcheck:

prop_zip xs ys = length xs == length ys ==> 
    unzip (zip xs ys) == (xs,ys)

Eventhough it seems to be logically right according to the definition of zip and unzip, that this property should be correct fo lists of the same length, the quickcheck ends with:

*** Gave up! Passed only 49 tests.

Thanks in advance for any hint or advice!

解决方案

Preconditions that are difficult to meet by generating random test cases are often a bad idea in QuickCheck. Instead, you should use generators carefully to construct test cases that meet the precondition automatically.

For example, in this case, you can use forAll to generate the second list to be of the same length as the first list:

prop_zip' (xs :: [a]) =
  forAll (vectorOf (length xs) arbitrary) $ \ (ys :: [a]) ->
  unzip (zip xs ys) == (xs, ys)

(I'm additionally using ScopedTypeVariables here to determine the type of the second list. You might want to change this to suit your specific needs.)

这篇关于Haskell:QuickCheck属性使用含义失败测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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