在 Hedgehog 中通过 'Gen' 或通过 'forAll' 生成随机输入的区别
Difference between generating random input through 'Gen' or through 'forAll' in Hedgehog
假设,我想为 Haskell 中的 Sum
with the help of hedgehog 库测试以下关联性 属性:
a <> (b <> c) ≡ (a <> b) <> c
实际上我有两种生成随机输入的方法。
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)
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)
我想知道,这两种方法有什么区别?它会以某种方式影响性能或并行化或随机性吗?
包维护者在相应的 GitHub 问题下回答了这个问题:
假设,我想为 Haskell 中的 Sum
with the help of hedgehog 库测试以下关联性 属性:
a <> (b <> c) ≡ (a <> b) <> c
实际上我有两种生成随机输入的方法。
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)
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)
我想知道,这两种方法有什么区别?它会以某种方式影响性能或并行化或随机性吗?
包维护者在相应的 GitHub 问题下回答了这个问题: