-
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.
- Loading branch information
Showing
18 changed files
with
304 additions
and
379 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,60 @@ | ||
{-# LANGUAGE NumericUnderscores #-} | ||
|
||
module Cardano.Wallet.Primitive.Types.Coin.Gen | ||
( genCoinAny | ||
, genCoinSmall | ||
, genCoinSmallPositive | ||
, genCoinLargePositive | ||
, shrinkCoinAny | ||
, shrinkCoinSmall | ||
, shrinkCoinSmallPositive | ||
, shrinkCoinLargePositive | ||
( genCoin | ||
, genCoinPositive | ||
, genCoinFullRange | ||
, shrinkCoin | ||
, shrinkCoinPositive | ||
, shrinkCoinFullRange | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.Primitive.Types.Coin | ||
( Coin (..) ) | ||
import Test.QuickCheck | ||
( Gen, choose, shrink ) | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Coins chosen from the full range available | ||
-------------------------------------------------------------------------------- | ||
|
||
genCoinAny :: Gen Coin | ||
genCoinAny = Coin <$> choose (unCoin minBound, unCoin maxBound) | ||
|
||
shrinkCoinAny :: Coin -> [Coin] | ||
shrinkCoinAny (Coin c) = Coin <$> shrink c | ||
( Gen, choose, frequency, shrink, sized ) | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Coins chosen to be small and possibly zero | ||
-- Coins chosen according to the size parameter. | ||
-------------------------------------------------------------------------------- | ||
|
||
genCoinSmall :: Gen Coin | ||
genCoinSmall = Coin <$> choose (0, 10) | ||
genCoin :: Gen Coin | ||
genCoin = sized $ \n -> Coin . fromIntegral <$> choose (0, n) | ||
|
||
shrinkCoinSmall :: Coin -> [Coin] | ||
shrinkCoinSmall (Coin c) = Coin <$> shrink c | ||
shrinkCoin :: Coin -> [Coin] | ||
shrinkCoin (Coin c) = Coin <$> shrink c | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Coins chosen to be small and strictly positive | ||
-- Coins chosen according to the size parameter, but strictly positive. | ||
-------------------------------------------------------------------------------- | ||
|
||
genCoinSmallPositive :: Gen Coin | ||
genCoinSmallPositive = Coin <$> choose (1, 10) | ||
genCoinPositive :: Gen Coin | ||
genCoinPositive = sized $ \n -> Coin . fromIntegral <$> choose (1, max 1 n) | ||
|
||
shrinkCoinSmallPositive :: Coin -> [Coin] | ||
shrinkCoinSmallPositive (Coin c) = Coin <$> filter (> 0) (shrink c) | ||
shrinkCoinPositive :: Coin -> [Coin] | ||
shrinkCoinPositive (Coin c) = Coin <$> filter (> 0) (shrink c) | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Coins chosen from a large range and strictly positive | ||
-- Coins chosen from the full range available. | ||
-------------------------------------------------------------------------------- | ||
|
||
genCoinLargePositive :: Gen Coin | ||
genCoinLargePositive = Coin <$> choose (1, 1_000_000_000_000) | ||
-- | Generates coins across the full range available. | ||
-- | ||
-- This generator has a slight bias towards the limits of the range, but | ||
-- otherwise generates values uniformly across the whole range. | ||
-- | ||
-- This can be useful when testing roundtrip conversions between different | ||
-- types. | ||
-- | ||
genCoinFullRange :: Gen Coin | ||
genCoinFullRange = frequency | ||
[ (1, pure (Coin 0)) | ||
, (1, pure (maxBound :: Coin)) | ||
, (8, Coin <$> choose (1, unCoin (maxBound :: Coin) - 1)) | ||
] | ||
|
||
shrinkCoinLargePositive :: Coin -> [Coin] | ||
shrinkCoinLargePositive (Coin c) = Coin <$> filter (> 0) (shrink c) | ||
shrinkCoinFullRange :: Coin -> [Coin] | ||
shrinkCoinFullRange = | ||
-- Given that we may have a large value, we limit the number of results | ||
-- returned in order to avoid processing long lists of shrunken values. | ||
take 8 . shrinkCoin |
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.