-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2885: Add generators and shrinkers for `SelectionLimit` and `SelectionSkeleton`. r=jonathanknowles a=jonathanknowles ### Comments This PR adds generators and shrinkers for `SelectionLimit` and `SelectionSkeleton`. ### Issue Number ADP-1118 Co-authored-by: Jonathan Knowles <jonathan.knowles@iohk.io>
- Loading branch information
Showing
8 changed files
with
154 additions
and
7 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
85 changes: 85 additions & 0 deletions
85
lib/core/src/Cardano/Wallet/Primitive/CoinSelection/Gen.hs
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,85 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module Cardano.Wallet.Primitive.CoinSelection.Gen | ||
( genSelectionLimit | ||
, genSelectionSkeleton | ||
, shrinkSelectionLimit | ||
, shrinkSelectionSkeleton | ||
) | ||
where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.Primitive.CoinSelection.Balance | ||
( SelectionLimit, SelectionLimitOf (..), SelectionSkeleton (..) ) | ||
import Cardano.Wallet.Primitive.Types.TokenMap.Gen | ||
( genAssetId, shrinkAssetId ) | ||
import Cardano.Wallet.Primitive.Types.Tx.Gen | ||
( genTxOut, shrinkTxOut ) | ||
import Test.QuickCheck | ||
( Gen | ||
, NonNegative (..) | ||
, arbitrary | ||
, listOf | ||
, oneof | ||
, shrink | ||
, shrinkList | ||
, shrinkMapBy | ||
) | ||
import Test.QuickCheck.Extra | ||
( liftShrink3 ) | ||
|
||
import qualified Data.Set as Set | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Selection limits | ||
-------------------------------------------------------------------------------- | ||
|
||
genSelectionLimit :: Gen SelectionLimit | ||
genSelectionLimit = oneof | ||
[ MaximumInputLimit . getNonNegative <$> arbitrary | ||
, pure NoLimit | ||
] | ||
|
||
shrinkSelectionLimit :: SelectionLimit -> [SelectionLimit] | ||
shrinkSelectionLimit = \case | ||
MaximumInputLimit n -> | ||
MaximumInputLimit . getNonNegative <$> shrink (NonNegative n) | ||
NoLimit -> | ||
[] | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Selection skeletons | ||
-------------------------------------------------------------------------------- | ||
|
||
genSelectionSkeleton :: Gen SelectionSkeleton | ||
genSelectionSkeleton = SelectionSkeleton | ||
<$> genSkeletonInputCount | ||
<*> genSkeletonOutputs | ||
<*> genSkeletonChange | ||
where | ||
genSkeletonInputCount = | ||
getNonNegative <$> arbitrary @(NonNegative Int) | ||
genSkeletonOutputs = | ||
listOf genTxOut | ||
genSkeletonChange = | ||
listOf (Set.fromList <$> listOf genAssetId) | ||
|
||
shrinkSelectionSkeleton :: SelectionSkeleton -> [SelectionSkeleton] | ||
shrinkSelectionSkeleton = | ||
shrinkMapBy tupleToSkeleton skeletonToTuple $ liftShrink3 | ||
shrinkSkeletonInputCount | ||
shrinkSkeletonOutputs | ||
shrinkSkeletonChange | ||
where | ||
shrinkSkeletonInputCount = | ||
shrink @Int | ||
shrinkSkeletonOutputs = | ||
shrinkList shrinkTxOut | ||
shrinkSkeletonChange = | ||
shrinkList $ | ||
shrinkMapBy Set.fromList Set.toList (shrinkList shrinkAssetId) | ||
|
||
skeletonToTuple (SelectionSkeleton a b c) = (a, b, c) | ||
tupleToSkeleton (a, b, c) = (SelectionSkeleton a b c) |
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