Skip to content

Commit b78582c

Browse files
committed
Reduce duplication
1 parent d8939c7 commit b78582c

File tree

3 files changed

+21
-18
lines changed
  • eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Generator
  • libs
    • cardano-ledger-core/testlib/Test/Cardano/Ledger/Core
    • cardano-ledger-test/src/Test/Cardano/Ledger/Constrained

3 files changed

+21
-18
lines changed

eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Generator/Utxo.hs

+3-8
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ import qualified Data.Set as Set
7474
import qualified Data.Vector as V
7575
import Lens.Micro
7676
import NoThunks.Class ()
77+
import Test.Cardano.Ledger.Binary.Random (QC (..))
7778
import Test.Cardano.Ledger.Common (tracedDiscard)
79+
import Test.Cardano.Ledger.Core.Arbitrary (uniformSubMapElems)
7880
import Test.Cardano.Ledger.Core.KeyPair (
7981
KeyPair,
8082
KeyPairs,
@@ -634,14 +636,7 @@ genIndices k (l', u')
634636
-- | Select @n@ random key value pairs from the supplied map. Order of keys with
635637
-- respect to each other will also be random, i.e. not sorted.
636638
pickRandomFromMap :: Int -> Map.Map k t -> Gen [(k, t)]
637-
pickRandomFromMap n' initMap = go (min (max 0 n') (Map.size initMap)) [] initMap
638-
where
639-
go n !acc !m
640-
| n <= 0 = pure acc
641-
| otherwise = do
642-
i <- QC.choose (0, Map.size m - 1)
643-
let (k, y) = Map.elemAt i m
644-
go (n - 1) ((k, y) : acc) (Map.deleteAt i m)
639+
pickRandomFromMap n initMap = uniformSubMapElems (\k v -> ((k, v) :)) (Just n) initMap QC
645640

646641
mkScriptWits ::
647642
forall era.

libs/cardano-ledger-core/testlib/Test/Cardano/Ledger/Core/Arbitrary.hs

+15-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module Test.Cardano.Ledger.Core.Arbitrary (
4141
-- | Will need to find a better home in the future
4242
uniformSubSet,
4343
uniformSubMap,
44+
uniformSubMapElems,
4445
)
4546
where
4647

@@ -644,18 +645,29 @@ uniformSubMap ::
644645
Map k v ->
645646
g ->
646647
m (Map k v)
647-
uniformSubMap mSubMapSize inputMap gen = do
648+
uniformSubMap = uniformSubMapElems Map.insert
649+
650+
uniformSubMapElems ::
651+
(StatefulGen g m, Monoid f) =>
652+
(k -> v -> f -> f) ->
653+
-- | Size of the subMap. If supplied will be clamped to @[0, Map.size s]@ interval,
654+
-- otherwise will be generated randomly.
655+
Maybe Int ->
656+
Map k v ->
657+
g ->
658+
m f
659+
uniformSubMapElems insert mSubMapSize inputMap gen = do
648660
subMapSize <- case mSubMapSize of
649661
Nothing -> uniformRM (0, Map.size inputMap) gen
650662
Just n -> pure $ max 0 $ min (Map.size inputMap) n
651-
go inputMap Map.empty subMapSize
663+
go inputMap mempty subMapSize
652664
where
653665
go !s !acc !i
654666
| i <= 0 = pure acc
655667
| otherwise = do
656668
ix <- uniformRM (0, Map.size s - 1) gen
657669
let (k, v) = Map.elemAt ix s
658-
go (Map.deleteAt ix s) (Map.insert k v acc) (i - 1)
670+
go (Map.deleteAt ix s) (insert k v acc) (i - 1)
659671

660672
genValidUMapWithCreds :: Gen (UMap StandardCrypto, Set (Credential 'Staking StandardCrypto))
661673
genValidUMapWithCreds = do

libs/cardano-ledger-test/src/Test/Cardano/Ledger/Constrained/Combinators.hs

+3-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
module Test.Cardano.Ledger.Constrained.Combinators where
99

1010
import Cardano.Ledger.Coin (Coin (..))
11-
import Data.Foldable as F (foldl')
1211
import Data.Map.Strict (Map)
1312
import qualified Data.Map.Strict as Map
1413
import Data.Set (Set)
1514
import qualified Data.Set as Set
1615
import GHC.Stack (HasCallStack)
17-
import Test.Cardano.Ledger.Core.Arbitrary ()
16+
import Test.Cardano.Ledger.Binary.Random (QC (..))
17+
import Test.Cardano.Ledger.Core.Arbitrary (uniformSubMap)
1818
import Test.QuickCheck hiding (Fixed, total)
1919

2020
-- ==========================================================================
@@ -234,8 +234,4 @@ genFromMap msgs m
234234
n = Map.size m
235235

236236
subMapFromMapWithSize :: Ord k => Int -> Map k a -> Gen (Map k a)
237-
subMapFromMapWithSize n m = do
238-
let indexes = [0 .. Map.size m - 1]
239-
accum ans i = let (k, v) = Map.elemAt i m in Map.insert k v ans
240-
shuffled <- shuffle indexes
241-
pure (F.foldl' accum Map.empty (take n shuffled))
237+
subMapFromMapWithSize n m = uniformSubMap (Just n) m QC

0 commit comments

Comments
 (0)