【问题标题】:Example that shows the limitations of integrated shrinking显示集成收缩限制的示例
【发布时间】:2019-06-22 01:05:21
【问题描述】:

我刚刚观看了a video,它提出了用于基于属性的测试的集成收缩的概念。该方法似乎比 类型定向收缩 具有一些优势,但是在 this reddit thread 中指出,集成收缩方法不适用于单子生成器:

以您的方式进行收缩并不适合生成器的一元样式。这是一个例子,考虑生成一个任意列表(暂时忽略终止):

do x <- arbitrary
   xs <- arbitrary
   return (x:xs)

现在,收缩的默认行为将首先收缩 x(保持 xs 不变),然后收缩 xs(保持 x 不变),这严重限制了收缩(局部最小值的概念现在不那么强大了)。

我将上述评论读为“集成收缩可能无法提供全局最小计数器示例”。但是,由于hedgehog 似乎能够为列表中的失败属性找到最少的反例,我想知道是否有一个示例可以显示上面引用中指出的缺点。

【问题讨论】:

  • hedgehog 如何搜索列表空间?我希望它不会采用天真的单子方法(正是因为您指出的原因)。

标签: haskell quickcheck property-based-testing haskell-hedgehog


【解决方案1】:

在微积分方面,问题在于您没有遵循负梯度(最陡下降),而是首先沿 1 个轴最小化,然后沿另一个轴最小化。基于这个类比,很容易想出至少一个人为的例子——考虑函数

-- f x y = ((x^2 - 1)^2 - 0.2*x) * ((y^2 - 1/2)^2 - 0.1*y)
f x y = (x^4 - 2.2*x^2 + 1) * (y^4 - 1.1*y^2 + 1/4)

See plot on WolframAlpha.

我们正在测试它的属性f x y &gt; 0,假设一个最小的例子有一个最接近原点(0, 0) 的点。根据您第一次开始收缩的位置,您完全有可能最终接近(±1, 0),因为您首先调整x,然后不允许y 发生太大变化。但是,在理想情况下,您可能希望在接近 (0, ±1/2) 的地方结束,以满足最小标准。

【讨论】:

    【解决方案2】:

    这里是一个涉及列表的示例,仅供参考:

    {-# LANGUAGE OverloadedStrings #-}
    
    module Main where
    
    import Hedgehog
    import qualified Hedgehog.Gen as Gen
    import qualified Hedgehog.Range as Range
    
    notLargeOrBothNotEmpty :: Property
    notLargeOrBothNotEmpty = property $ do
      xs <- forAll randomIntLists
      ys <- forAll randomIntLists
      assert $ length xs < 4 && (xs /= [] && ys /=[])
      where
        randomIntLists = Gen.frequency
          [ (1, Gen.list (Range.constant 0 1) randomInt)
          , (10, Gen.list (Range.constant 1 100) randomInt)
          ]
        randomInt = Gen.integral (Range.constat 1 10)
    
    main :: IO Bool
    main = checkParallel $ 
      Group "Test.Example" [("Produce a minimal counter-example", notLargeOrBothNotEmpty)]
    
    

    所以刺猬有时会返回列表( [ 1 , 1 , 1 , 1 ], []) 作为反例。然而([], []) 是一个较小的反例(hedgehog 有时也会报告)。 

    在这种情况下,违反属性的条件是:

    4 <= length xs || (xs == [] && ys == [])
    

    如果最初找到一个反例,其中ys /= []4 &lt;= length xs,集成收缩方法将首先尝试收缩xs,然后继续收缩ys,保持xs 不变,如我在原始问题中引用的帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      相关资源
      最近更新 更多