-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4fc00f9
commit e3f035f
Showing
14 changed files
with
139 additions
and
113 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
84 changes: 84 additions & 0 deletions
84
lib/primitive/lib/Cardano/Wallet/Primitive/Types/AssetId/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,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 |
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
Oops, something went wrong.