在 QuickCheck 中使用自定义生成器与任意实例

jto*_*bin 6 haskell quickcheck

这是一个简单的函数。它接受一个输入Int并返回一个(可能为空)对列表(Int, Int),其中输入Int是任何对的立方元素的总和。

cubeDecomposition :: Int -> [(Int, Int)]
cubeDecomposition n = [(x, y) | x <- [1..m], y <- [x..m], x^3 + y^3 == n] 
  where m = truncate $ fromIntegral n ** (1/3)

-- cubeDecomposition 1729
-- [(1,12),(9,10)]
Run Code Online (Sandbox Code Playgroud)

我想测试上述属性是否正确;如果我对每个元素进行立方并对任何返回元组求和,那么我会得到我的输入:

import Control.Arrow 

cubedElementsSumToN :: Int -> Bool
cubedElementsSumToN n = all (== n) d
    where d = map (uncurry (+) . ((^3) *** (^3))) (cubeDecomposition n)
Run Code Online (Sandbox Code Playgroud)

出于运行时考虑,我想Int在使用 QuickCheck 进行测试时将输入限制为一定大小。我可以定义适当的类型和Arbitrary实例:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

import Test.QuickCheck

newtype SmallInt = SmallInt Int
    deriving (Show, Eq, Enum, Ord, Num, Real, Integral)

instance Arbitrary SmallInt where
    arbitrary = fmap SmallInt (choose (-10000000, 10000000))
Run Code Online (Sandbox Code Playgroud)

然后我想我必须定义使用SmallInt而不是的函数和属性的版本Int

cubeDecompositionQC :: SmallInt -> [(SmallInt, SmallInt)]
cubeDecompositionQC n = [(x, y) | x <- [1..m], y <- [x..m], x^3 + y^3 == n] 
  where m = truncate $ fromIntegral n ** (1/3)

cubedElementsSumToN' :: SmallInt -> Bool
cubedElementsSumToN' n = all (== n) d
    where d = map (uncurry (+) . ((^3) *** (^3))) (cubeDecompositionQC n)

-- cubeDecompositionQC 1729
-- [(SmallInt 1,SmallInt 12),(SmallInt 9,SmallInt 10)]
Run Code Online (Sandbox Code Playgroud)

这工作正常,标准 100 次测试按预期通过。但当我真正需要的是一个自定义生成器时,似乎没有必要定义新的类型、实例和函数。所以我尝试了这个:

smallInts :: Gen Int
smallInts = choose (-10000000, 10000000)

cubedElementsSumToN'' :: Int -> Property
cubedElementsSumToN'' n = forAll smallInts $ \m -> all (== n) (d m)
    where d =   map (uncurry (+) . ((^3) *** (^3)))
              . cubeDecomposition
Run Code Online (Sandbox Code Playgroud)

现在,我运行这个的前几次,一切正常,并且所有测试都通过了。但在随后的运行中我观察到失败。提高测试规模可靠地发现:

*** Failed! Falsifiable (after 674 tests and 1 shrink):  
0
8205379
Run Code Online (Sandbox Code Playgroud)

我在这里有点困惑,因为存在两个缩小的输入 - 0 和 8205379 - 从 QuickCheck 返回,我直觉地期望有一个。此外,这些输入按预期工作(至少在我的可展示属性上):

*Main> cubedElementsSumToN 0
True
*Main> cubedElementsSumToN 8205379
True
Run Code Online (Sandbox Code Playgroud)

因此,使用我定义的自定义的属性显然存在问题Gen

我做错了什么?

jto*_*bin 5

我很快意识到我所写的属性显然是不正确的。这是使用原始cubedElementsSumToN属性的正确方法:

quickCheck (forAll smallInts cubedElementsSumToN)
Run Code Online (Sandbox Code Playgroud)

读起来很自然。