From 623ecd9658fa5b15f71b0d86bac6b714b4b86dc4 Mon Sep 17 00:00:00 2001 From: Martijn Bastiaan Date: Mon, 29 Jul 2024 13:08:57 +0200 Subject: [PATCH] Add `eoTrace` (#95) Can be useful to find out what Hedgehog is doing --- src/Protocols/Hedgehog.hs | 16 ++++++++++++++++ src/Protocols/Hedgehog/Internal.hs | 3 +++ 2 files changed, 19 insertions(+) diff --git a/src/Protocols/Hedgehog.hs b/src/Protocols/Hedgehog.hs index a40ebe8e..b6188ba2 100644 --- a/src/Protocols/Hedgehog.hs +++ b/src/Protocols/Hedgehog.hs @@ -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) @@ -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 @@ -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} @@ -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 diff --git a/src/Protocols/Hedgehog/Internal.hs b/src/Protocols/Hedgehog/Internal.hs index b164fc4e..176888f1 100644 --- a/src/Protocols/Hedgehog/Internal.hs +++ b/src/Protocols/Hedgehog/Internal.hs @@ -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 @@ -67,6 +69,7 @@ defExpectOptions = , eoResetCycles = 30 , eoDriveEarly = True , eoTimeoutMs = Nothing + , eoTrace = False } -- | Superclass class to reduce syntactical noise.