Skip to content

Commit

Permalink
Add eoTrace (#95)
Browse files Browse the repository at this point in the history
Can be useful to find out what Hedgehog is doing
  • Loading branch information
martijnbastiaan authored Jul 29, 2024
1 parent 688dae3 commit 623ecd9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Protocols/Hedgehog.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Protocols.Hedgehog (

-- base
import Control.Concurrent (threadDelay)
import Control.Monad (when)
import Control.Monad.IO.Class (liftIO)
import Data.Proxy (Proxy (Proxy))
import GHC.Stack (HasCallStack)
Expand Down Expand Up @@ -119,9 +120,11 @@ propWithModel ::
propWithModel eOpts genData model prot prop =
H.property $ maybe id withTimeoutMs (eoTimeoutMs eOpts) $ do
dat <- H.forAll genData
when (eoTrace eOpts) $ liftIO $ putStr "propWithModel: dat: " >> print dat

-- TODO: Different 'n's for each output
n <- H.forAll (Gen.integral (Range.linear 0 (eoSampleMax eOpts)))
when (eoTrace eOpts) $ liftIO $ putStr "propWithModel: n: " >> print n

-- TODO: Different distributions?
let genStall = genSmallInt
Expand All @@ -130,13 +133,21 @@ propWithModel eOpts genData model prot prop =
-- whether to stall or not. The second determines how many cycles to stall
-- on each _valid_ cycle.
lhsStallModes <- H.forAll (genVec genStallMode)
when (eoTrace eOpts) $
liftIO $
putStr "propWithModel: lhsStallModes: " >> print lhsStallModes
lhsStalls <- H.forAll (traverse (genStalls genStall n) lhsStallModes)
when (eoTrace eOpts) $ liftIO $ putStr "propWithModel: lhsStalls: " >> print lhsStalls

-- Generate stalls for RHS part of the protocol. The first line determines
-- whether to stall or not. The second determines how many cycles to stall
-- on each _valid_ cycle.
rhsStallModes <- H.forAll (genVec genStallMode)
when (eoTrace eOpts) $
liftIO $
putStr "propWithModel: rhsStallModes: " >> print rhsStallModes
rhsStalls <- H.forAll (traverse (genStalls genStall n) rhsStallModes)
when (eoTrace eOpts) $ liftIO $ putStr "propWithModel: rhsStalls: " >> print rhsStalls

let
simConfig = def{resetCycles = eoResetCycles eOpts}
Expand All @@ -157,9 +168,14 @@ propWithModel eOpts genData model prot prop =
-- expectN errors if circuit does not produce enough data
trimmed <- expectN (Proxy @b) eOpts sampled

when (eoTrace eOpts) $ liftIO $ putStrLn "propWithModel: before forcing trimmed.."
_ <- H.evalNF trimmed
when (eoTrace eOpts) $ liftIO $ putStrLn "propWithModel: before forcing expected.."
_ <- H.evalNF expected

when (eoTrace eOpts) $
liftIO $
putStrLn "propWithModel: executing property.."
prop expected trimmed

{- | Test a protocol against a pure model implementation. Circuit under test will
Expand Down
3 changes: 3 additions & 0 deletions src/Protocols/Hedgehog/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ data ExpectOptions = ExpectOptions
-- never acknowledge data while this is happening.
, eoTimeoutMs :: Maybe Int
-- ^ Terminate the test after /n/ milliseconds.
, eoTrace :: Bool
-- ^ Trace data generation for debugging purposes
}

{- | Resets for 30 cycles, checks for superfluous data for 50 cycles after
Expand All @@ -67,6 +69,7 @@ defExpectOptions =
, eoResetCycles = 30
, eoDriveEarly = True
, eoTimeoutMs = Nothing
, eoTrace = False
}

-- | Superclass class to reduce syntactical noise.
Expand Down

0 comments on commit 623ecd9

Please sign in to comment.