Skip to content

Commit

Permalink
Add generators and coverage for SlotNo
Browse files Browse the repository at this point in the history
  • Loading branch information
sevanspowell committed Oct 21, 2021
1 parent 0548595 commit 21bb5cf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
4 changes: 4 additions & 0 deletions lib/core/src/Cardano/Api/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Cardano.Api.Gen
, genTxIndex
, genShelleyHash
, genTxInsCollateral
, genSlotNo
) where

import Prelude
Expand Down Expand Up @@ -49,3 +50,6 @@ genTxInsCollateral era =
[ pure TxInsCollateralNone
, TxInsCollateral supported <$> listOf genTxIn
]

genSlotNo :: Gen SlotNo
genSlotNo = SlotNo <$> arbitrary
42 changes: 30 additions & 12 deletions lib/core/test/unit/Cardano/Api/GenSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Prelude

import Cardano.Api
( CardanoEra (..)
, SlotNo (..)
, TxIn (..)
, TxInsCollateral (..)
, TxIx (..)
Expand Down Expand Up @@ -61,20 +62,11 @@ spec =
property
$ forAll (genTxInsCollateral AlonzoEra)
$ genTxInCollateralCoverage AlonzoEra
it "genSlotNo" $
property genSlotNoCoverage

genTxIxCoverage :: TxIx -> Property
genTxIxCoverage txIx = checkCoverage
$ cover 1 (txIx == TxIx 0)
"txIx is zero"
$ cover 2 (txIx >= veryLargeTxIx)
"txIx is very large"
$ cover 10 (txIx > TxIx 0 && txIx < veryLargeTxIx)
"txIx is between smallest and very large"
$ label "no txIx is negative" (txIx >= TxIx 0)
& counterexample "txIx was negative"
where
veryLargeTxIx :: TxIx
veryLargeTxIx = TxIx $ fromInteger $ toInteger (maxBound :: Word32)
genTxIxCoverage (TxIx ix) = unsignedCoverage "txIx" ix

instance Arbitrary TxIx where
arbitrary = genTxIndex
Expand Down Expand Up @@ -119,3 +111,29 @@ genTxInCollateralCoverage era collateral =
collateralLength = \case
TxInsCollateralNone -> Nothing
TxInsCollateral _ cs -> Just $ length cs

genSlotNoCoverage :: SlotNo -> Property
genSlotNoCoverage = unsignedCoverage "slot number"

instance Arbitrary SlotNo where
arbitrary = genSlotNo

unsignedCoverage
:: ( Num a
, Ord a
)
=> String
-> a
-> Property
unsignedCoverage name x = checkCoverage
$ cover 1 (x == 0)
(name <> " is zero")
$ cover 30 (x > 0 && x < veryLarge)
(name <> " is between zero and very large")
$ cover 5 (x > veryLarge)
(name <> " is greater than very large")
$ label (name <> " is non-negative") (x >= 0)
& counterexample (name <> " was negative")

where
veryLarge = fromIntegral (maxBound :: Word32)

0 comments on commit 21bb5cf

Please sign in to comment.