Skip to content

Commit

Permalink
[Test] [Bug] Make 'tryHard' lazy again (IntersectMBO#5596)
Browse files Browse the repository at this point in the history
  • Loading branch information
effectfully authored and v0d1ch committed Dec 6, 2024
1 parent b1c27f1 commit 15566d3
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions plutus-tx-plugin/test/StdLib/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ tests =
, goldenPir "errorTrace" errorTrace
]

-- | Evaluate (deeply, to get through tuples) a value, throwing away any exception and just representing it as 'Nothing'.
-- We really should use something like "Control.Exception.Enclosed" here and in other similar
-- places.
-- | Evaluate (deeply, to get through tuples) a value, throwing away any exception and just
-- representing it as 'Nothing'.
tryHard :: (MonadIO m, NFData a) => a -> m (Maybe a)
tryHard a = reoption <$> (liftIO $ try @SomeException $ evaluate $ force a)
-- We have @Strict@ enabled, hence without the tilda this function evaluates @a@ before evaluating
-- the body, i.e. outside of the call to 'try', defeating the whole purpose.
tryHard ~a = reoption <$> (liftIO $ try @SomeException $ evaluate $ force a)

testRatioProperty :: (Show a, Eq a) => TestName -> (Ratio.Rational -> a) -> (Rational -> a) -> TestNested
testRatioProperty nm plutusFunc ghcFunc = pure $ testPropertyNamed nm (fromString nm) $ Hedgehog.property $ do
Expand All @@ -74,7 +79,8 @@ testRatioProperty nm plutusFunc ghcFunc = pure $ testPropertyNamed nm (fromStrin

testDivMod :: Property
testDivMod = Hedgehog.property $ do
let gen = Gen.integral (Range.linear (-10000) 100000)
-- Generating zeroes often enough to trigger any potential bugs related to handling of zeroes.
let gen = Gen.frequency [(1, pure 0), (10, Gen.integral (Range.linear (-10000) 100000))]
(n1, n2) <- Hedgehog.forAll $ (,) <$> gen <*> gen
ghcResult <- tryHard $ divMod n1 n2
plutusResult <- tryHard $ PlutusTx.divMod n1 n2
Expand All @@ -84,7 +90,8 @@ testDivMod = Hedgehog.property $ do

testQuotRem :: Property
testQuotRem = Hedgehog.property $ do
let gen = Gen.integral (Range.linear (-10000) 100000)
-- Generating zeroes often enough to trigger any potential bugs related to handling of zeroes.
let gen = Gen.frequency [(1, pure 0), (10, Gen.integral (Range.linear (-10000) 100000))]
(n1, n2) <- Hedgehog.forAll $ (,) <$> gen <*> gen
ghcResult <- tryHard $ quotRem n1 n2
plutusResult <- tryHard $ PlutusTx.quotRem n1 n2
Expand Down

0 comments on commit 15566d3

Please sign in to comment.