File tree 3 files changed +21
-18
lines changed
eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Generator
cardano-ledger-core/testlib/Test/Cardano/Ledger/Core
cardano-ledger-test/src/Test/Cardano/Ledger/Constrained
3 files changed +21
-18
lines changed Original file line number Diff line number Diff line change @@ -74,7 +74,9 @@ import qualified Data.Set as Set
74
74
import qualified Data.Vector as V
75
75
import Lens.Micro
76
76
import NoThunks.Class ()
77
+ import Test.Cardano.Ledger.Binary.Random (QC (.. ))
77
78
import Test.Cardano.Ledger.Common (tracedDiscard )
79
+ import Test.Cardano.Ledger.Core.Arbitrary (uniformSubMapElems )
78
80
import Test.Cardano.Ledger.Core.KeyPair (
79
81
KeyPair ,
80
82
KeyPairs ,
@@ -634,14 +636,7 @@ genIndices k (l', u')
634
636
-- | Select @n@ random key value pairs from the supplied map. Order of keys with
635
637
-- respect to each other will also be random, i.e. not sorted.
636
638
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
645
640
646
641
mkScriptWits ::
647
642
forall era .
Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ module Test.Cardano.Ledger.Core.Arbitrary (
41
41
-- | Will need to find a better home in the future
42
42
uniformSubSet ,
43
43
uniformSubMap ,
44
+ uniformSubMapElems ,
44
45
)
45
46
where
46
47
@@ -644,18 +645,29 @@ uniformSubMap ::
644
645
Map k v ->
645
646
g ->
646
647
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
648
660
subMapSize <- case mSubMapSize of
649
661
Nothing -> uniformRM (0 , Map. size inputMap) gen
650
662
Just n -> pure $ max 0 $ min (Map. size inputMap) n
651
- go inputMap Map. empty subMapSize
663
+ go inputMap mempty subMapSize
652
664
where
653
665
go ! s ! acc ! i
654
666
| i <= 0 = pure acc
655
667
| otherwise = do
656
668
ix <- uniformRM (0 , Map. size s - 1 ) gen
657
669
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 )
659
671
660
672
genValidUMapWithCreds :: Gen (UMap StandardCrypto , Set (Credential 'Staking StandardCrypto ))
661
673
genValidUMapWithCreds = do
Original file line number Diff line number Diff line change 8
8
module Test.Cardano.Ledger.Constrained.Combinators where
9
9
10
10
import Cardano.Ledger.Coin (Coin (.. ))
11
- import Data.Foldable as F (foldl' )
12
11
import Data.Map.Strict (Map )
13
12
import qualified Data.Map.Strict as Map
14
13
import Data.Set (Set )
15
14
import qualified Data.Set as Set
16
15
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 )
18
18
import Test.QuickCheck hiding (Fixed , total )
19
19
20
20
-- ==========================================================================
@@ -234,8 +234,4 @@ genFromMap msgs m
234
234
n = Map. size m
235
235
236
236
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
You can’t perform that action at this time.
0 commit comments