【问题标题】:QuickCheck values equalQuickCheck 值相等
【发布时间】:2014-02-25 22:46:39
【问题描述】:

我有一个如下所示的 QuickCheck 属性:

prop42 :: Foo -> Bool
prop42 foo = fn1 foo == fn2 foo

如果此属性失败,它将打印出 foo 是什么。但我真的很想知道fn1fn2 返回了什么。如果foo 很大,手动生成这些信息就不是一件容易的事。 (即,坐在那里,手动输入打印到 Windows 控制台窗口的大量文本。)

测试框架通常具有比较相等性的东西,如果相等性不成立,则打印出两个值。但是我似乎无法为 QuickCheck 找到这样的功能...

【问题讨论】:

    标签: haskell quickcheck


    【解决方案1】:

    看看here 的组合子。例如,printTestCase 允许在失败案例的输出中添加任意字符串。一个简单的例子:

    prop x = let f = sin x
        in printTestCase ("Should be at least " ++ show f) $ x >= sin x
    
    $> quickCheck 道具 *** 失败的!可证伪(经过 2 次测试和 1 次缩小): -1.0 至少应为 -0.8414709848078965

    【讨论】:

    【解决方案2】:

    根据勇利的回答,我是这样做的:

    (?==?) :: (Eq x, Show x) => x -> x -> Property
    x ?==? y =
      printTestCase ("Left:  " ++ show x) $
      printTestCase ("Right: " ++ show y) $
      x == y
    

    现在我可以写类似的东西了

    prop42 :: Foo -> Prop
    prop42 foo = fn1 foo ?==? fn2 foo
    

    【讨论】:

      【解决方案3】:

      假设fn1gn2 返回Bar,我认为可以执行以下操作:

      newtype Bar1 = B1 Bar deriving Show
      newtype Bar2 = B2 Bar deriving Show
      instance Arbitrary Bar1 where
         arbitrary = arbitrary >>= return . B1 . fn1
      instance Arbitrary Bar2 where
         arbitrary = arbitrary >>= return . B2 . fn2
      
      prop_xx = forAll arbitrary $ \(B1 b1) ->
                   forAll arbitrary $ \(B2 b2) ->
                      b1 == b2
      

      【讨论】:

      • 哇。巧妙。幸运的是,这不是必需的。
      • @kosmikus 感谢您的编辑。我想我们也可以写prop_xx (B1 b1) (B2 b2) = b1 == b2,但当时不确定。
      猜你喜欢
      • 2019-08-14
      • 1970-01-01
      • 2022-01-14
      • 2017-06-09
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      相关资源
      最近更新 更多