Skip to content

Commit 9addabb

Browse files
committed
Property tests for SatInt
1 parent eb17481 commit 9addabb

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

plutus-core/satint/test/TestSatInt.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE RankNTypes #-}
33
{-# LANGUAGE ScopedTypeVariables #-}
44
{-# LANGUAGE TypeApplications #-}
5+
{-# OPTIONS_GHC -Wno-orphans #-}
56

67
-- These tests are deliberately not in the same style as our other tests, but rather mirror the tests
78
-- in safeint, since I want to upstream this in due course.
@@ -77,6 +78,9 @@ tests =
7778
, testProperty "+" (propBinOp (+))
7879
, testProperty "-" (propBinOp (-))
7980
, testProperty "/0" propDividedBy0
81+
, testProperty "plusSI" (withMaxSuccess 10000 propPlusSI)
82+
, testProperty "minusSI" (withMaxSuccess 10000 propMinusSI)
83+
, testProperty "timesSI" (withMaxSuccess 10000 propTimesSI)
8084
-- lcm and gcd do *not* pass `behavesOk` since they *internally* use `abs` (which will give the wrong/saturated
8185
-- answer for minBound), and hence go astray after that. But we can't easily detect that this is the "correct"
8286
-- saturated thing to do as we do for other operations (where we can just see if the saturating version is
@@ -98,3 +102,29 @@ propDividedBy0 :: Property
98102
propDividedBy0 = withMaxSuccess 1000 $
99103
forAll intWithSpecialCases $
100104
\n -> saturatesPos ((fromIntegral n) `dividedBy` 0)
105+
106+
propPlusSI :: SatInt -> SatInt -> Property
107+
propPlusSI x y = x + y === fromInteger (fromSatInt x + fromSatInt y)
108+
109+
propMinusSI :: SatInt -> SatInt -> Property
110+
propMinusSI x y = x - y === fromInteger (fromSatInt x - fromSatInt y)
111+
112+
propTimesSI :: SatInt -> SatInt -> Property
113+
propTimesSI x y = x * y === fromInteger (fromSatInt x * fromSatInt y)
114+
115+
instance Arbitrary SatInt where
116+
arbitrary =
117+
unsafeToSatInt
118+
<$> frequency
119+
[ (1, pure (-1))
120+
, (1, pure (-2))
121+
, (1, pure 0)
122+
, (1, pure 1)
123+
, (1, pure 2)
124+
, (1, pure minBound)
125+
, (1, pure maxBound)
126+
, (4, choose (2 ^ (30 :: Int), 2 ^ (62 :: Int)))
127+
, (4, choose (negate (2 ^ (62 :: Int)), negate (2 ^ (30 :: Int))))
128+
, (50, arbitrary)
129+
]
130+
shrink = fmap unsafeToSatInt . shrink . unSatInt

0 commit comments

Comments
 (0)