Skip to content

Commit 3688bc3

Browse files
committed
Move geometric into Test.Util.QuickCheck
1 parent a754eae commit 3688bc3

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
@@ -1740,14 +1740,6 @@ genSecurityParam =
17401740
. fromIntegral
17411741
. (+ 2) -- shift to the right to avoid degenerate cases
17421742
<$> geometric 0.5 -- range in [0, +inf); mean = 1/p = 2
1743-
where
1744-
geometric :: Double -> Gen Int
1745-
geometric p
1746-
| p <= 0 || p > 1 = error "p must be in (0,1]"
1747-
| otherwise = do
1748-
u <- choose (0.0, 1.0)
1749-
let k = floor (log u / log (1 - p))
1750-
return k
17511743

17521744
{-------------------------------------------------------------------------------
17531745
Top-level tests

0 commit comments

Comments
 (0)