Skip to content

Commit

Permalink
Extract out new module AssetId.Gen.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Dec 5, 2023
1 parent 4fc00f9 commit e3f035f
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 113 deletions.
8 changes: 4 additions & 4 deletions lib/coin-selection/lib/Cardano/CoinSelection/Balance/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (..)
)
Expand All @@ -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 (..)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (..)
)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ 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
)
import Cardano.Wallet.Primitive.Types.TokenBundle.Gen
( genTokenBundleSmallRangePositive
, shrinkTokenBundleSmallRangePositive
)
import Cardano.Wallet.Primitive.Types.TokenMap.Gen
( genAssetId
, shrinkAssetId
)
import Control.Monad
( void
)
Expand Down
6 changes: 4 additions & 2 deletions lib/coin-selection/test/spec/Cardano/CoinSelectionSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (..)
)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/primitive/cardano-wallet-primitive.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
84 changes: 84 additions & 0 deletions lib/primitive/lib/Cardano/Wallet/Primitive/Types/AssetId/Gen.hs
Original file line number Diff line number Diff line change
@@ -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
81 changes: 7 additions & 74 deletions lib/primitive/lib/Cardano/Wallet/Primitive/Types/TokenMap/Gen.hs
Original file line number Diff line number Diff line change
@@ -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 (..)
)
Expand All @@ -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
Expand Down Expand Up @@ -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
--------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (..)
)
Expand All @@ -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 (..)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (..)
)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (..)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (..)
)
Expand Down
Loading

0 comments on commit e3f035f

Please sign in to comment.