在Hedgehog中通过'Gen'或'forAll'生成随机输入之间的区别

She*_*rsh 7 testing automated-tests haskell property-based-testing haskell-hedgehog

假设,我想在Haskell Sumhedgehog库的帮助下测试以下的associativity属性:

a <> (b <> c) ? (a <> b) <> c
Run Code Online (Sandbox Code Playgroud)

我实际上有两种方法来生成随机输入.

1.生成全部Gen(使用Gen'Applicative和Monad实例)

genTriple :: Get (Int, Int, Int)
genTriple = liftA3 (,,) Gen.enumBounded Gen.enumBounded Gen.enumBounded

prop_assoc :: Property
prop_assoc = property $ do
  (a, b, c) <- forAll genTriple
  (Sum a <> Sum b) <> Sum c === Sum a <> (Sum b <> Sum c)
Run Code Online (Sandbox Code Playgroud)

2.生成每个字段 forAll

prop_assoc :: Property
prop_assoc = property $ do
  a <- forAll Gen.enumBounded
  b <- forAll Gen.enumBounded
  c <- forAll Gen.enumBounded
  (Sum a <> Sum b) <> Sum c === Sum a <> (Sum b <> Sum c)
Run Code Online (Sandbox Code Playgroud)

我想知道,两种方法有什么区别?它是否以某种方式影响性能或并行化或随机性?

She*_*rsh 0

包维护者在相应的 GitHub 问题下回答了这个问题: