-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2829: Rewrite generators to use the QC size parameter r=jonathanknowles a=jonathanknowles ### Issue Number Follow-up from #2768 Supports work for #2819 ### Comments This PR adjusts generators for the following types to use the QC size parameter: - `Address` - `Hash "Tx"` - `TxIndex` (`Word32`) - `TxIn` - `TxOut` - `UTxO` - `UTxOIndex` Some minor adjustments to coverage conditions were necessary (but surprisingly few). This PR also: - Adds the module `UTxO.Gen`, so that we can generate values of `UTxO` without having to generate indices (which add extra overhead), and redefines the `UTxOIndex` generators in terms of these generators. - Adds the functions `genSized2` and `genSized2With`, which restore size linearity to generators of compound values (those defined in terms of other generators). Co-authored-by: Jonathan Knowles <[email protected]>
- Loading branch information
Showing
19 changed files
with
222 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module Cardano.Wallet.Primitive.Types.UTxO.Gen | ||
( genUTxO | ||
, genUTxOLarge | ||
, genUTxOLargeN | ||
, shrinkUTxO | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.Primitive.Types.Tx | ||
( TxIn, TxOut ) | ||
import Cardano.Wallet.Primitive.Types.Tx.Gen | ||
( genTxIn, genTxInLargeRange, genTxOut, shrinkTxIn, shrinkTxOut ) | ||
import Cardano.Wallet.Primitive.Types.UTxO | ||
( UTxO (..) ) | ||
import Control.Monad | ||
( replicateM ) | ||
import Test.QuickCheck | ||
( Gen, choose, shrinkList, sized ) | ||
import Test.QuickCheck.Extra | ||
( shrinkInterleaved ) | ||
|
||
import qualified Data.Map.Strict as Map | ||
|
||
-------------------------------------------------------------------------------- | ||
-- UTxO sets generated according to the size parameter | ||
-------------------------------------------------------------------------------- | ||
|
||
genUTxO :: Gen UTxO | ||
genUTxO = sized $ \size -> do | ||
entryCount <- choose (0, size) | ||
UTxO . Map.fromList <$> replicateM entryCount genEntry | ||
|
||
shrinkUTxO :: UTxO -> [UTxO] | ||
shrinkUTxO | ||
= take 16 | ||
. fmap (UTxO . Map.fromList) | ||
. shrinkList shrinkEntry | ||
. Map.toList | ||
. unUTxO | ||
|
||
genEntry :: Gen (TxIn, TxOut) | ||
genEntry = (,) <$> genTxIn <*> genTxOut | ||
|
||
shrinkEntry :: (TxIn, TxOut) -> [(TxIn, TxOut)] | ||
shrinkEntry (i, o) = uncurry (,) <$> shrinkInterleaved | ||
(i, shrinkTxIn) | ||
(o, shrinkTxOut) | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Large UTxO sets | ||
-------------------------------------------------------------------------------- | ||
|
||
genUTxOLarge :: Gen UTxO | ||
genUTxOLarge = do | ||
entryCount <- choose (1024, 4096) | ||
genUTxOLargeN entryCount | ||
|
||
genUTxOLargeN :: Int -> Gen UTxO | ||
genUTxOLargeN entryCount = do | ||
UTxO . Map.fromList <$> replicateM entryCount genEntryLargeRange | ||
|
||
genEntryLargeRange :: Gen (TxIn, TxOut) | ||
genEntryLargeRange = (,) | ||
<$> genTxInLargeRange | ||
-- Note that we don't need to choose outputs from a large range, as inputs | ||
-- are already chosen from a large range: | ||
<*> genTxOut |
Oops, something went wrong.