gpt4 book ai didi

testing - haskell /小检查 : How to control the `Depth` parameter?

转载 作者:行者123 更新时间:2023-11-28 19:52:42 26 4
gpt4 key购买 nike

我有一个简单的数据结构要在 smallcheck 中测试。

{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
{-# LANGUAGE DeriveGeneric #-}

import Test.Tasty
import Test.Tasty.SmallCheck
import Test.SmallCheck.Series
import GHC.Generics

data T1 = T1 { p1 :: Int,
p2 :: Char,
p3 :: [Int]
} deriving (Eq, Show, Generic)

instance Monad m => Serial m T1

main :: IO ()
main = defaultMain tests

tests :: TestTree
tests = testGroup "Tests" [scProps]

scProps = testGroup "(checked by SmallCheck)"
[ testProperty "Test1" prop_test1
]

prop_test1 x = x == x
where types = (x :: T1)

在运行测试时,是否有任何通用的解决方案来为(单独的)测试设置 Depth 参数,或者更好的是,一个细粒度的解决方案来设置 Depth单个字段的参数,例如将 p3 的深度限制为 2 以避免搜索空间的组合爆炸?

非常感谢!

朱尔斯

信息:一个有点相关的问题是 here .

编辑:
我采用了 Roman Cheplyaka 接受的答案中给出的解决方案并在一个最小的工作示例中实现它们(谢谢,Roman):

{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}

import Test.Tasty
import Test.Tasty.Options
import Test.Tasty.SmallCheck
import Test.SmallCheck.Series
import Data.Functor
-- =============================================================================
main :: IO ()
main = defaultMain tests
-- =============================================================================
-- the data structure to be tested
data T1 = T1 { p1 :: Int,
p2 :: Char,
p3 :: Int,
p5 :: [Int]
} deriving (Show, Eq)
-- =============================================================================
-- some test-properties
prop_test1 x y = x == y
where types = (x :: T1, y :: T1)
prop_test2 x = x == x
where types = (x :: T1)
-- =============================================================================
-- how to change the depth of the search spaces?
{-| I Possibility: Command Line |-}
-- foo@bar$ runhaskell Test.hs --smallcheck-depth 2

-- -----------------------------------------------------------------------------
{-| II Possibility: |-}
-- normal:
-- tests :: TestTree
-- tests = testGroup "Tests" [scProps]

-- custom:
tests :: TestTree
tests = localOption d $ testGroup "Tests" [scProps]
where d = 3 :: SmallCheckDepth

scProps = testGroup "(checked by SmallCheck)"
[ testProperty "Test1" prop_test1,
testProperty "Test2" prop_test2
]

-- -----------------------------------------------------------------------------
{-| III Possibility: Adjust Depth when making the type instance of Serial |-}
-- normal:
-- instance (Monad m) => Serial m T1 where
-- series = T1 <$> series <~> series <~> series <~> series

-- custom:
instance (Monad m) => Serial m T1 where
series = localDepth (const 4) $ T1 <$> (localDepth (const 2) series) <~> series <~> series <~> (decDepth series)

-- (a few more examples):
-- instance (Monad m) => Serial m T1 where
-- series = decDepth $ T1 <$> series <~> series <~> series <~> (decDepth series )
-- instance (Monad m) => Serial m T1 where
-- series = localDepth (const 3) $ T1 <$> series <~> series <~> series <~> series
-- instance (Monad m) => Serial m T1 where
-- series = localDepth (const 4) $ T1 <$> series <~> series <~> series <~> (decDepth series)

最佳答案

我将从列出 tasty & smallcheck 提供的开箱即用的控制方式开始:

  • 运行测试套件时,您可以通过--smallcheck-depth选项来控制«common»深度
  • 编写测试套件时,您可以使用adjustOption 调整任何子树的深度,相对于命令行上指定的深度(或父树)
  • 最后,在编写 Serial 实例时,您可以使用 localDepth 来调整每个特定字段相对于整个结构的深度所需的深度。通常它是整体深度 - 1(这就是 decDepth 在标准定义中所做的),但您可以发明自己的规则。

所以,这种控制是可能的,尽管是以间接的方式。通常这就是您想要的 — 另请参阅 this answer .

如果您真的想在运行时控制各个字段的深度...是的,这是可能的,尽管它有点复杂,因为它不是预期的用例。 (尽管如此,这还是很酷的!)Here's a working example.

如果此功能对您很重要,可以在 github 上提出一个问题并解释您为什么需要它。 (但首先,请务必阅读我对上面链接的 ocharles 的回复。您很少需要在运行时调整深度。)

关于testing - haskell /小检查 : How to control the `Depth` parameter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20582795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com