Skip to content

Commit 4ae8416

Browse files
committed
Add a reverse round-trip test for ExUnits translation
1 parent f6eec39 commit 4ae8416

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

libs/cardano-ledger-core/testlib/Test/Cardano/Ledger/Core/Arbitrary.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,15 @@ instance Arbitrary PV1.Data where
914914
]
915915
| otherwise = oneof [PV1.I <$> arbitrary, PV1.B <$> arbitrary]
916916

917+
instance Arbitrary PV1.ExBudget where
918+
arbitrary = PV1.ExBudget <$> arbitrary <*> arbitrary
919+
920+
instance Arbitrary PV1.ExCPU where
921+
arbitrary = fromInteger . getNonNegative <$> arbitrary
922+
923+
instance Arbitrary PV1.ExMemory where
924+
arbitrary = fromInteger . getNonNegative <$> arbitrary
925+
917926
genValidCostModel :: Language -> Gen CostModel
918927
genValidCostModel lang = do
919928
newParamValues <- vectorOf (costModelInitParamCount lang) arbitrary

libs/cardano-ledger-core/testlib/Test/Cardano/Ledger/Plutus/ExUnits.hs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,28 @@ import Test.Cardano.Ledger.Core.Arbitrary ()
99

1010
spec :: Spec
1111
spec = do
12-
prop "Round-trip to ExBudget" exUnitsTranslationRoundTrip
12+
prop "Round-trip to ExBudget" exUnitsToExBudgetRoundTrip
13+
prop "Round-trip from ExBudget" exBudgetToExUnitsRoundTrip
1314

14-
-- ExUnits should remain intact when translating to and from the plutus type
15-
exUnitsTranslationRoundTrip :: Gen Property
16-
exUnitsTranslationRoundTrip = do
15+
-- ExUnits should remain intact when translating to and from the Plutus ExBudget type
16+
exUnitsToExBudgetRoundTrip :: Gen Property
17+
exUnitsToExBudgetRoundTrip = do
1718
e <- arbitrary
18-
let result = exBudgetToExUnits (transExUnits e)
19+
let result = exBudgetToExUnits $ transExUnits e
20+
pure
21+
$ counterexample
22+
( "Before: "
23+
<> show e
24+
<> "\n After: "
25+
<> show result
26+
)
27+
$ result == Just e
28+
29+
-- Plutus ExBudget should remain intact when translating to and from the ExUnits type
30+
exBudgetToExUnitsRoundTrip :: Gen Property
31+
exBudgetToExUnitsRoundTrip = do
32+
e <- arbitrary
33+
let result = transExUnits <$> exBudgetToExUnits e
1934
pure
2035
$ counterexample
2136
( "Before: "

0 commit comments

Comments
 (0)