Skip to content

Commit f5e1366

Browse files
committed
Move geometric into Test.Util.QuickCheck
1 parent e8c8282 commit f5e1366

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/QuickCheck.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ module Test.Util.QuickCheck
2323
, frequency'
2424
, oneof'
2525

26+
-- * Sampling from distributions
27+
, geometric
28+
2629
-- * Comparing maps
2730
, isSubmapOfBy
2831

@@ -308,3 +311,19 @@ frequency' xs0 = lift (choose (1, tot)) >>= (`pick` xs0)
308311
oneof' :: (MonadTrans t, Monad (t Gen)) => [t Gen a] -> t Gen a
309312
oneof' [] = error "QuickCheck.oneof used with empty list"
310313
oneof' gs = lift (chooseInt (0, length gs - 1)) >>= (gs !!)
314+
315+
{-------------------------------------------------------------------------------
316+
Sampling from distributions
317+
-------------------------------------------------------------------------------}
318+
319+
-- NOTE: if more advanced sampling is required, consider using 'mwc-random':
320+
-- https://hackage.haskell.org/package/mwc-random
321+
322+
-- | Sample from a geometric distribution
323+
geometric :: Double -> Gen Int
324+
geometric p
325+
| p <= 0 || p > 1 = error "p must be in (0,1]"
326+
| otherwise = do
327+
u <- choose (0.0, 1.0)
328+
let k = floor (log u / log (1 - p))
329+
return k

ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/ChainDB/StateMachine.hs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,14 +1739,6 @@ genSecurityParam =
17391739
. fromIntegral
17401740
. (+ 2) -- shift to the right to avoid degenerate cases
17411741
<$> geometric 0.5 -- range in [0, +inf); mean = 1/p = 2
1742-
where
1743-
geometric :: Double -> Gen Int
1744-
geometric p
1745-
| p <= 0 || p > 1 = error "p must be in (0,1]"
1746-
| otherwise = do
1747-
u <- choose (0.0, 1.0)
1748-
let k = floor (log u / log (1 - p))
1749-
return k
17501742

17511743
{-------------------------------------------------------------------------------
17521744
Top-level tests

0 commit comments

Comments
 (0)