diff --git a/lib/coin-selection/lib/Cardano/CoinSelection/Balance/Gen.hs b/lib/coin-selection/lib/Cardano/CoinSelection/Balance/Gen.hs index 9024831ae97..4cea6926ed0 100644 --- a/lib/coin-selection/lib/Cardano/CoinSelection/Balance/Gen.hs +++ b/lib/coin-selection/lib/Cardano/CoinSelection/Balance/Gen.hs @@ -18,6 +18,10 @@ import Cardano.CoinSelection.Balance import Cardano.CoinSelection.Context ( SelectionContext (..) ) +import Cardano.Wallet.Primitive.Types.AssetId.Gen + ( genAssetId + , shrinkAssetId + ) import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) @@ -28,10 +32,6 @@ import Cardano.Wallet.Primitive.Types.TokenBundle.Gen ( genTokenBundleSmallRange , shrinkTokenBundleSmallRange ) -import Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( genAssetId - , shrinkAssetId - ) import Generics.SOP ( NP (..) ) diff --git a/lib/coin-selection/test/spec/Cardano/CoinSelection/BalanceSpec.hs b/lib/coin-selection/test/spec/Cardano/CoinSelection/BalanceSpec.hs index bf099e79c17..172c5f8e65e 100644 --- a/lib/coin-selection/test/spec/Cardano/CoinSelection/BalanceSpec.hs +++ b/lib/coin-selection/test/spec/Cardano/CoinSelection/BalanceSpec.hs @@ -130,6 +130,11 @@ import Cardano.Numeric.Util import Cardano.Wallet.Primitive.Types.AssetId ( AssetId (..) ) +import Cardano.Wallet.Primitive.Types.AssetId.Gen + ( genAssetId + , genAssetIdLargeRange + , shrinkAssetId + ) import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) @@ -155,10 +160,7 @@ import Cardano.Wallet.Primitive.Types.TokenMap ( TokenMap ) import Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( genAssetId - , genAssetIdLargeRange - , genTokenMapSmallRange - , shrinkAssetId + ( genTokenMapSmallRange , shrinkTokenMap ) import Cardano.Wallet.Primitive.Types.TokenName diff --git a/lib/coin-selection/test/spec/Cardano/CoinSelection/UTxOIndexSpec.hs b/lib/coin-selection/test/spec/Cardano/CoinSelection/UTxOIndexSpec.hs index b8405129395..36e0b65d624 100644 --- a/lib/coin-selection/test/spec/Cardano/CoinSelection/UTxOIndexSpec.hs +++ b/lib/coin-selection/test/spec/Cardano/CoinSelection/UTxOIndexSpec.hs @@ -33,6 +33,10 @@ import Cardano.CoinSelection.UTxOIndex.Internal import Cardano.Wallet.Primitive.Types.AssetId ( AssetId ) +import Cardano.Wallet.Primitive.Types.AssetId.Gen + ( genAssetId + , shrinkAssetId + ) import Cardano.Wallet.Primitive.Types.TokenBundle ( TokenBundle ) @@ -40,10 +44,6 @@ import Cardano.Wallet.Primitive.Types.TokenBundle.Gen ( genTokenBundleSmallRangePositive , shrinkTokenBundleSmallRangePositive ) -import Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( genAssetId - , shrinkAssetId - ) import Control.Monad ( void ) diff --git a/lib/coin-selection/test/spec/Cardano/CoinSelectionSpec.hs b/lib/coin-selection/test/spec/Cardano/CoinSelectionSpec.hs index b64c9d95b60..edd843d6d25 100644 --- a/lib/coin-selection/test/spec/Cardano/CoinSelectionSpec.hs +++ b/lib/coin-selection/test/spec/Cardano/CoinSelectionSpec.hs @@ -67,6 +67,9 @@ import Cardano.CoinSelection.UTxOSelection.Gen ( genUTxOSelection , shrinkUTxOSelection ) +import Cardano.Wallet.Primitive.Types.AssetId.Gen + ( genAssetId + ) import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) @@ -87,8 +90,7 @@ import Cardano.Wallet.Primitive.Types.TokenMap ( TokenMap ) import Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( genAssetId - , genTokenMap + ( genTokenMap , shrinkTokenMap ) import Cardano.Wallet.Primitive.Types.TokenQuantity diff --git a/lib/primitive/cardano-wallet-primitive.cabal b/lib/primitive/cardano-wallet-primitive.cabal index 89155a6eaa4..2835a1d9855 100644 --- a/lib/primitive/cardano-wallet-primitive.cabal +++ b/lib/primitive/cardano-wallet-primitive.cabal @@ -152,6 +152,7 @@ library Cardano.Wallet.Primitive.Types.Address.Gen Cardano.Wallet.Primitive.Types.AnyExplicitScripts Cardano.Wallet.Primitive.Types.AssetId + Cardano.Wallet.Primitive.Types.AssetId.Gen Cardano.Wallet.Primitive.Types.Block Cardano.Wallet.Primitive.Types.Block.Gen Cardano.Wallet.Primitive.Types.BlockSummary diff --git a/lib/primitive/lib/Cardano/Wallet/Primitive/Types/AssetId/Gen.hs b/lib/primitive/lib/Cardano/Wallet/Primitive/Types/AssetId/Gen.hs new file mode 100644 index 00000000000..55d5b4e2e5b --- /dev/null +++ b/lib/primitive/lib/Cardano/Wallet/Primitive/Types/AssetId/Gen.hs @@ -0,0 +1,84 @@ +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE NamedFieldPuns #-} + +module Cardano.Wallet.Primitive.Types.AssetId.Gen + ( genAssetId + , genAssetIdLargeRange + , shrinkAssetId + , AssetIdF (..) + ) where + +import Prelude + +import Cardano.Wallet.Primitive.Types.AssetId + ( AssetId (..) + ) +import Cardano.Wallet.Primitive.Types.TokenName.Gen + ( genTokenName + , genTokenNameLargeRange + , shrinkTokenName + , testTokenNames + ) +import Cardano.Wallet.Primitive.Types.TokenPolicyId.Gen + ( genTokenPolicyId + , genTokenPolicyIdLargeRange + , shrinkTokenPolicyId + , testTokenPolicyIds + ) +import Data.List + ( elemIndex + ) +import Data.Maybe + ( fromMaybe + ) +import GHC.Generics + ( Generic + ) +import Test.QuickCheck + ( CoArbitrary (..) + , Function (..) + , Gen + , functionMap + , variant + ) +import Test.QuickCheck.Extra + ( genSized2With + , shrinkInterleaved + ) + +-------------------------------------------------------------------------------- +-- Asset identifiers chosen from a range that depends on the size parameter +-------------------------------------------------------------------------------- + +genAssetId :: Gen AssetId +genAssetId = genSized2With AssetId genTokenPolicyId genTokenName + +shrinkAssetId :: AssetId -> [AssetId] +shrinkAssetId (AssetId p t) = uncurry AssetId <$> shrinkInterleaved + (p, shrinkTokenPolicyId) + (t, shrinkTokenName) + +-------------------------------------------------------------------------------- +-- Asset identifiers chosen from a large range (to minimize collisions) +-------------------------------------------------------------------------------- + +genAssetIdLargeRange :: Gen AssetId +genAssetIdLargeRange = AssetId + <$> genTokenPolicyIdLargeRange + <*> genTokenNameLargeRange + +-------------------------------------------------------------------------------- +-- Filtering functions +-------------------------------------------------------------------------------- + +newtype AssetIdF = AssetIdF AssetId + deriving (Generic, Eq, Show, Read) + +instance Function AssetIdF where + function = functionMap show read + +instance CoArbitrary AssetIdF where + coarbitrary (AssetIdF AssetId {tokenName, tokenPolicyId}) genB = do + let n = fromMaybe 0 (elemIndex tokenName testTokenNames) + let m = fromMaybe 0 (elemIndex tokenPolicyId testTokenPolicyIds) + variant (n + m) genB diff --git a/lib/primitive/lib/Cardano/Wallet/Primitive/Types/TokenMap/Gen.hs b/lib/primitive/lib/Cardano/Wallet/Primitive/Types/TokenMap/Gen.hs index cb924bf8964..01797e27290 100644 --- a/lib/primitive/lib/Cardano/Wallet/Primitive/Types/TokenMap/Gen.hs +++ b/lib/primitive/lib/Cardano/Wallet/Primitive/Types/TokenMap/Gen.hs @@ -1,38 +1,20 @@ -{-# LANGUAGE DeriveGeneric #-} -{-# LANGUAGE NamedFieldPuns #-} - module Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( genAssetId - , genAssetIdLargeRange - , genTokenMap + ( genTokenMap , genTokenMapSmallRange - , shrinkAssetId , shrinkTokenMap - , AssetIdF (..) , genTokenMapPartition , genTokenMapPartitionNonNull ) where import Prelude -import Cardano.Wallet.Primitive.Types.AssetId - ( AssetId (..) +import Cardano.Wallet.Primitive.Types.AssetId.Gen + ( genAssetId + , shrinkAssetId ) import Cardano.Wallet.Primitive.Types.TokenMap ( TokenMap ) -import Cardano.Wallet.Primitive.Types.TokenName.Gen - ( genTokenName - , genTokenNameLargeRange - , shrinkTokenName - , testTokenNames - ) -import Cardano.Wallet.Primitive.Types.TokenPolicyId.Gen - ( genTokenPolicyId - , genTokenPolicyIdLargeRange - , shrinkTokenPolicyId - , testTokenPolicyIds - ) import Cardano.Wallet.Primitive.Types.TokenQuantity ( TokenQuantity (..) ) @@ -45,62 +27,29 @@ import Control.Monad ( replicateM ) import Data.List - ( elemIndex - , transpose + ( transpose ) import Data.List.NonEmpty ( NonEmpty ) -import Data.Maybe - ( fromMaybe - ) -import GHC.Generics - ( Generic - ) import Safe ( fromJustNote ) import Test.QuickCheck - ( CoArbitrary (..) - , Function (..) - , Gen + ( Gen , choose - , functionMap , oneof , shrinkList , sized - , variant ) import Test.QuickCheck.Extra - ( genSized2With - , shrinkInterleaved + ( shrinkInterleaved ) import qualified Cardano.Wallet.Primitive.Types.TokenMap as TokenMap import qualified Data.Foldable as F import qualified Data.List.NonEmpty as NE --------------------------------------------------------------------------------- --- Asset identifiers chosen from a range that depends on the size parameter --------------------------------------------------------------------------------- - -genAssetId :: Gen AssetId -genAssetId = genSized2With AssetId genTokenPolicyId genTokenName - -shrinkAssetId :: AssetId -> [AssetId] -shrinkAssetId (AssetId p t) = uncurry AssetId <$> shrinkInterleaved - (p, shrinkTokenPolicyId) - (t, shrinkTokenName) - --------------------------------------------------------------------------------- --- Asset identifiers chosen from a large range (to minimize collisions) --------------------------------------------------------------------------------- - -genAssetIdLargeRange :: Gen AssetId -genAssetIdLargeRange = AssetId - <$> genTokenPolicyIdLargeRange - <*> genTokenNameLargeRange - -------------------------------------------------------------------------------- -- Token maps with assets and quantities chosen from ranges that depend on the -- size parameter @@ -142,22 +91,6 @@ shrinkTokenMap (a, shrinkAssetId) (q, shrinkTokenQuantity) --------------------------------------------------------------------------------- --- Filtering functions --------------------------------------------------------------------------------- - -newtype AssetIdF = AssetIdF AssetId - deriving (Generic, Eq, Show, Read) - -instance Function AssetIdF where - function = functionMap show read - -instance CoArbitrary AssetIdF where - coarbitrary (AssetIdF AssetId{tokenName, tokenPolicyId}) genB = do - let n = fromMaybe 0 (elemIndex tokenName testTokenNames) - let m = fromMaybe 0 (elemIndex tokenPolicyId testTokenPolicyIds) - variant (n+m) genB - -------------------------------------------------------------------------------- -- Partitioning token maps -------------------------------------------------------------------------------- diff --git a/lib/primitive/lib/Cardano/Wallet/Primitive/Types/Tx/TxOut/Gen.hs b/lib/primitive/lib/Cardano/Wallet/Primitive/Types/Tx/TxOut/Gen.hs index d179abab972..7715f7bedd0 100644 --- a/lib/primitive/lib/Cardano/Wallet/Primitive/Types/Tx/TxOut/Gen.hs +++ b/lib/primitive/lib/Cardano/Wallet/Primitive/Types/Tx/TxOut/Gen.hs @@ -18,6 +18,9 @@ import Cardano.Wallet.Primitive.Types.Address.Gen ( genAddress , shrinkAddress ) +import Cardano.Wallet.Primitive.Types.AssetId.Gen + ( genAssetIdLargeRange + ) import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) @@ -28,9 +31,6 @@ import Cardano.Wallet.Primitive.Types.TokenBundle.Gen ( genTokenBundleSmallRange , shrinkTokenBundleSmallRange ) -import Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( genAssetIdLargeRange - ) import Cardano.Wallet.Primitive.Types.TokenQuantity ( TokenQuantity (..) ) diff --git a/lib/primitive/test/spec/Cardano/Wallet/Primitive/Types/TokenMapSpec.hs b/lib/primitive/test/spec/Cardano/Wallet/Primitive/Types/TokenMapSpec.hs index 12fcdbae645..2168c933032 100644 --- a/lib/primitive/test/spec/Cardano/Wallet/Primitive/Types/TokenMapSpec.hs +++ b/lib/primitive/test/spec/Cardano/Wallet/Primitive/Types/TokenMapSpec.hs @@ -25,6 +25,12 @@ import Cardano.Numeric.Util import Cardano.Wallet.Primitive.Types.AssetId ( AssetId (..) ) +import Cardano.Wallet.Primitive.Types.AssetId.Gen + ( AssetIdF (..) + , genAssetId + , genAssetIdLargeRange + , shrinkAssetId + ) import Cardano.Wallet.Primitive.Types.Hash ( Hash (..) ) @@ -35,12 +41,8 @@ import Cardano.Wallet.Primitive.Types.TokenMap , TokenMap ) import Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( AssetIdF (..) - , genAssetId - , genAssetIdLargeRange - , genTokenMapPartition + ( genTokenMapPartition , genTokenMapSmallRange - , shrinkAssetId , shrinkTokenMap ) import Cardano.Wallet.Primitive.Types.TokenName diff --git a/lib/primitive/test/spec/Cardano/Wallet/Primitive/Types/TxSpec.hs b/lib/primitive/test/spec/Cardano/Wallet/Primitive/Types/TxSpec.hs index b5d54cb1891..186f53d6cc6 100644 --- a/lib/primitive/test/spec/Cardano/Wallet/Primitive/Types/TxSpec.hs +++ b/lib/primitive/test/spec/Cardano/Wallet/Primitive/Types/TxSpec.hs @@ -20,13 +20,13 @@ import Prelude import Cardano.Wallet.Primitive.Types.AssetId ( AssetId (..) ) -import Cardano.Wallet.Primitive.Types.Hash - ( Hash (..) - ) -import Cardano.Wallet.Primitive.Types.TokenMap.Gen +import Cardano.Wallet.Primitive.Types.AssetId.Gen ( genAssetId , shrinkAssetId ) +import Cardano.Wallet.Primitive.Types.Hash + ( Hash (..) + ) import Cardano.Wallet.Primitive.Types.TokenName ( TokenName (..) ) diff --git a/lib/primitive/test/spec/Cardano/Wallet/Primitive/Types/UTxOSpec.hs b/lib/primitive/test/spec/Cardano/Wallet/Primitive/Types/UTxOSpec.hs index 556a9345a1c..5bf97874c51 100644 --- a/lib/primitive/test/spec/Cardano/Wallet/Primitive/Types/UTxOSpec.hs +++ b/lib/primitive/test/spec/Cardano/Wallet/Primitive/Types/UTxOSpec.hs @@ -25,14 +25,14 @@ import Cardano.Wallet.Primitive.Types.Address.Gen import Cardano.Wallet.Primitive.Types.AssetId ( AssetId (..) ) +import Cardano.Wallet.Primitive.Types.AssetId.Gen + ( genAssetId + , shrinkAssetId + ) import Cardano.Wallet.Primitive.Types.Hash ( Hash (..) , mockHash ) -import Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( genAssetId - , shrinkAssetId - ) import Cardano.Wallet.Primitive.Types.TokenName ( TokenName (..) ) diff --git a/lib/wallet/test/unit/Cardano/Wallet/Api/TypesSpec.hs b/lib/wallet/test/unit/Cardano/Wallet/Api/TypesSpec.hs index f5c6d8b36c8..065a75085b0 100644 --- a/lib/wallet/test/unit/Cardano/Wallet/Api/TypesSpec.hs +++ b/lib/wallet/test/unit/Cardano/Wallet/Api/TypesSpec.hs @@ -350,6 +350,9 @@ import Cardano.Wallet.Primitive.Types.Address import Cardano.Wallet.Primitive.Types.AssetId ( AssetId (..) ) +import Cardano.Wallet.Primitive.Types.AssetId.Gen + ( genAssetId + ) import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) @@ -377,8 +380,7 @@ import Cardano.Wallet.Primitive.Types.TokenMap ( TokenMap ) import Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( genAssetId - , genTokenMapSmallRange + ( genTokenMapSmallRange , shrinkTokenMap ) import Cardano.Wallet.Primitive.Types.TokenMetadata diff --git a/lib/wallet/test/unit/Cardano/Wallet/Primitive/Types/Tx/TxSeqSpec.hs b/lib/wallet/test/unit/Cardano/Wallet/Primitive/Types/Tx/TxSeqSpec.hs index f70f862d95e..b60edde5efa 100644 --- a/lib/wallet/test/unit/Cardano/Wallet/Primitive/Types/Tx/TxSeqSpec.hs +++ b/lib/wallet/test/unit/Cardano/Wallet/Primitive/Types/Tx/TxSeqSpec.hs @@ -20,13 +20,13 @@ import Cardano.Wallet.Primitive.Types.Address.Gen import Cardano.Wallet.Primitive.Types.AssetId ( AssetId ) -import Cardano.Wallet.Primitive.Types.Hash - ( Hash (..) - ) -import Cardano.Wallet.Primitive.Types.TokenMap.Gen +import Cardano.Wallet.Primitive.Types.AssetId.Gen ( genAssetId , shrinkAssetId ) +import Cardano.Wallet.Primitive.Types.Hash + ( Hash (..) + ) import Cardano.Wallet.Primitive.Types.TokenName ( TokenName (..) ) diff --git a/lib/wallet/test/unit/Cardano/WalletSpec.hs b/lib/wallet/test/unit/Cardano/WalletSpec.hs index a369d8960e6..dc4074d8da0 100644 --- a/lib/wallet/test/unit/Cardano/WalletSpec.hs +++ b/lib/wallet/test/unit/Cardano/WalletSpec.hs @@ -135,6 +135,9 @@ import Cardano.Wallet.Primitive.Types.Address import Cardano.Wallet.Primitive.Types.Address.Gen ( genAddress ) +import Cardano.Wallet.Primitive.Types.AssetId.Gen + ( genAssetIdLargeRange + ) import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) @@ -151,9 +154,6 @@ import Cardano.Wallet.Primitive.Types.TokenBundle ( TokenBundle (TokenBundle) , getAssets ) -import Cardano.Wallet.Primitive.Types.TokenMap.Gen - ( genAssetIdLargeRange - ) import Cardano.Wallet.Primitive.Types.TokenQuantity.Gen ( genTokenQuantityPositive )