From abe7916b2a66306f07c498784222933ade60cad7 Mon Sep 17 00:00:00 2001 From: Johannes Lund Date: Mon, 10 Aug 2020 15:41:44 +0200 Subject: [PATCH] Use fewer type conversions --- .../Primitive/CoinSelection/LargestFirstSpec.hs | 11 ++++------- .../Wallet/Primitive/CoinSelection/RandomSpec.hs | 6 +++--- .../Cardano/Wallet/Primitive/CoinSelectionSpec.hs | 8 +++++--- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelection/LargestFirstSpec.hs b/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelection/LargestFirstSpec.hs index ebb689ccf70..63b972dde08 100644 --- a/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelection/LargestFirstSpec.hs +++ b/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelection/LargestFirstSpec.hs @@ -33,8 +33,6 @@ import Data.Functor.Identity ( Identity (runIdentity) ) import Data.List.NonEmpty ( NonEmpty (..) ) -import Data.Quantity - ( Quantity (..) ) import Test.Hspec ( Spec, describe, it, shouldSatisfy ) import Test.QuickCheck @@ -255,9 +253,8 @@ propDeterministic -> Property propDeterministic (CoinSelProp utxo wdrl txOuts) = do let opts = CoinSelectionOptions (const 100) noValidation - let wdrl' = Quantity wdrl - let resultOne = runIdentity $ runExceptT $ largestFirst opts txOuts wdrl' utxo - let resultTwo = runIdentity $ runExceptT $ largestFirst opts txOuts wdrl' utxo + let resultOne = runIdentity $ runExceptT $ largestFirst opts txOuts wdrl utxo + let resultTwo = runIdentity $ runExceptT $ largestFirst opts txOuts wdrl utxo resultOne === resultTwo propAtLeast @@ -270,7 +267,7 @@ propAtLeast (CoinSelProp utxo wdrl txOuts) = L.length (inputs cs) `shouldSatisfy` (>= NE.length txOuts) selection = runIdentity $ runExceptT $ do let opts = CoinSelectionOptions (const 100) noValidation - largestFirst opts txOuts (Quantity wdrl) utxo + largestFirst opts txOuts wdrl utxo propInputDecreasingOrder :: CoinSelProp @@ -289,4 +286,4 @@ propInputDecreasingOrder (CoinSelProp utxo wdrl txOuts) = getExtremumValue f = f . map (getCoin . coin . snd) selection = runIdentity $ runExceptT $ do let opts = CoinSelectionOptions (const 100) noValidation - largestFirst opts txOuts (Quantity wdrl) utxo + largestFirst opts txOuts wdrl utxo diff --git a/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelection/RandomSpec.hs b/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelection/RandomSpec.hs index 790abc95e18..0ef1843be71 100644 --- a/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelection/RandomSpec.hs +++ b/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelection/RandomSpec.hs @@ -357,7 +357,7 @@ propFragmentation drg (CoinSelProp utxo wdrl txOuts) = do (selection1,_) = withDRG drg (runExceptT $ random opt txOuts (Quantity 0) utxo) selection2 = runIdentity $ runExceptT $ - largestFirst opt txOuts (Quantity wdrl) utxo + largestFirst opt txOuts wdrl utxo opt = CoinSelectionOptions (const 100) noValidation propErrors @@ -372,7 +372,7 @@ propErrors drg (CoinSelProp utxo wdrl txOuts) = do prop (err1, err2) = err1 === err2 (selection1,_) = withDRG drg - (runExceptT $ random opt txOuts (Quantity wdrl) utxo) + (runExceptT $ random opt txOuts wdrl utxo) selection2 = runIdentity $ runExceptT $ - largestFirst opt txOuts (Quantity wdrl) utxo + largestFirst opt txOuts wdrl utxo opt = (CoinSelectionOptions (const 1) noValidation) diff --git a/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelectionSpec.hs b/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelectionSpec.hs index 02dc5548add..01f5755b41b 100644 --- a/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelectionSpec.hs +++ b/lib/core/test/unit/Cardano/Wallet/Primitive/CoinSelectionSpec.hs @@ -230,7 +230,7 @@ data CoinSelectionsSetup = CoinSelectionsSetup data CoinSelProp = CoinSelProp { csUtxO :: UTxO -- ^ Available UTxO for the selection - , csWithdrawal :: Word64 + , csWithdrawal :: Quantity "lovelace" Word64 -- ^ Availible Withdrawal , csOuts :: NonEmpty TxOut -- ^ Requested outputs for the payment @@ -347,11 +347,13 @@ instance Arbitrary a => Arbitrary (NonEmpty a) where instance Arbitrary CoinSelProp where shrink (CoinSelProp utxo wdrl outs) = [ CoinSelProp utxo' wdrl outs | utxo' <- shrink utxo ] - ++ [ CoinSelProp utxo wdrl' outs | wdrl' <- shrink wdrl ] + ++ [ CoinSelProp utxo wdrl' outs | wdrl' <- shrinkWdrl wdrl ] ++ [ CoinSelProp utxo wdrl outs' | outs' <- shrink outs ] + where + shrinkWdrl = map Quantity . shrink . getQuantity arbitrary = do utxo <- arbitrary - wdrl <- frequency [(65, return 0), (35, arbitrary)] + wdrl <- Quantity <$> frequency [(65, return 0), (35, arbitrary)] outs <- arbitrary return $ CoinSelProp utxo wdrl outs