假设,我想测试 Sum
的以下关联性属性在hedgehog的帮助下Haskell 中的库:
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 - 在 Hedgehog 中通过 'Gen' 或通过 'forAll' 生成随机输入的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49711248/