Skip to content

Commit 6f43997

Browse files
committed
Move geometric into Test.Util.QuickCheck
1 parent c0d8e62 commit 6f43997

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,14 @@ genBlk chunkInfo Model{..} =
16071607
)
16081608
]
16091609

1610+
genSecurityParam :: Gen SecurityParam
1611+
genSecurityParam =
1612+
SecurityParam
1613+
. unsafeNonZero
1614+
. fromIntegral
1615+
. (+ 2) -- shift to the right to avoid degenerate cases
1616+
<$> geometric 0.5 -- range in [0, +inf); mean = 1/p = 2
1617+
16101618
{-------------------------------------------------------------------------------
16111619
Top-level tests
16121620
-------------------------------------------------------------------------------}

0 commit comments

Comments
 (0)