@@ -12,6 +12,7 @@ module Clash.Testbench.Generate where
1212
1313import Hedgehog
1414import Hedgehog.Gen
15+ import Control.Monad.Extra ((<?>) , (<:>) )
1516import Control.Monad.IO.Class (MonadIO )
1617import Control.Monad.State.Lazy (liftIO , when , modify )
1718import Data.IORef (newIORef , readIORef , writeIORef )
@@ -33,24 +34,21 @@ generate gen = do
3334 TBDomain {.. } <- tbDomain @ dom
3435
3536 vRef <- liftIO $ newIORef undefined
36- checkForProgress <- progressCheck simStepRef True
37+ ifProgress <- progressCheck simStepRef True
3738 signalHistory <- newHistory
3839
3940 mind SomeSignal IOInput
4041 { signalId = NoID
41- , signalCurVal = const $ do
42- progress <- checkForProgress
43-
44- if progress
45- then do
42+ , signalCurVal = const $ ifProgress
43+ <?> do
4644 x <- sample gen
4745 writeIORef vRef x
4846 memorize signalHistory x
4947 return x
50- else
48+ <:>
5149 readIORef vRef
5250 , signalPrint = Nothing
53- ,..
51+ , ..
5452 }
5553
5654-- | Extended version of 'generate', which allows to generate a finite
@@ -65,28 +63,24 @@ generateN def gen = do
6563 TBDomain {.. } <- tbDomain @ dom
6664
6765 vRef <- liftIO $ newIORef [def]
68- checkForProgress <- progressCheck simStepRef False
66+ ifProgress <- progressCheck simStepRef False
6967 signalHistory <- newHistory
7068
7169 mind SomeSignal IOInput
7270 { signalId = NoID
73- , signalCurVal = const $ do
74- progress <- checkForProgress
75-
76- if progress
77- then
78- readIORef vRef >>= \ case
79- h : x : xr -> do
80- memorize signalHistory h
81- writeIORef vRef (x : xr)
82- return x
83- [h] -> do
84- memorize signalHistory h
85- x : xr <- sample gen
86- writeIORef vRef (x : xr)
87- return x
88- _ -> error " unreachable"
89- else readIORef vRef >>= \ case
71+ , signalCurVal = const $ ifProgress
72+ <?> readIORef vRef >>= \ case
73+ h : x : xr -> do
74+ memorize signalHistory h
75+ writeIORef vRef (x : xr)
76+ return x
77+ [h] -> do
78+ memorize signalHistory h
79+ x : xr <- sample gen
80+ writeIORef vRef (x : xr)
81+ return x
82+ _ -> error " unreachable"
83+ <:> readIORef vRef >>= \ case
9084 x : _ -> return x
9185 [] -> do
9286 x : xr <- sample gen
@@ -107,22 +101,19 @@ matchIOGen checkedOutput gen = do
107101 TBDomain {.. } <- tbDomain @ dom
108102
109103 vRef <- liftIO $ newIORef undefined
110- checkForProgress <- progressCheck simStepRef False
104+ ifProgress <- progressCheck simStepRef False
111105 signalHistory <- newHistory
112106
113107 mind SomeSignal $ IOInput
114108 { signalId = NoID
115- , signalCurVal = const $ do
116- progress <- checkForProgress
117-
118- if progress
119- then do
109+ , signalCurVal = const $ ifProgress
110+ <?> do
120111 (input, expectedOutput) <- sample gen
121112 curStep <- readIORef simStepRef
122113 signalExpect checkedOutput $ Expectation (curStep, verifier expectedOutput)
123114 writeIORef vRef input
124115 return input
125- else
116+ <:>
126117 readIORef vRef
127118 , signalPrint = Nothing
128119 , ..
@@ -157,42 +148,37 @@ matchIOGenN checkedOutput gen = mdo
157148
158149 xs <- liftIO $ sample gen
159150 modify $ \ st@ ST {.. } -> st { simSteps = max simSteps $ length xs }
160- liftIO $ Prelude. print xs
161151
162152 vRef <- liftIO $ newIORef xs
163- checkForProgress <- progressCheck simStepRef False
153+ ifProgress <- progressCheck simStepRef False
164154 signalHistory <- newHistory
165155
166156 s <- mind SomeSignal $ IOInput
167157 { signalId = NoID
168- , signalCurVal = const $ do
169- progress <- checkForProgress
170-
171- readIORef vRef >>=
172- if progress
173- then \ case
174- (h, _) : (i, o) : xr -> do
175- memorize signalHistory h
176- writeIORef vRef ((i, o) : xr)
177- curStep <- readIORef simStepRef
178- signalExpect checkedOutput $ Expectation (curStep, verifier s i o)
179- return i
180- [(h, _)] -> do
181- memorize signalHistory h
182- (i, o) : xr <- sample gen
183-
184- writeIORef vRef ((i, o) : xr)
185- curStep <- readIORef simStepRef
186- signalExpect checkedOutput $ Expectation (curStep, verifier s i o)
187- return i
188- _ -> error " unreachable"
189- else \ case
190- (i, _) : _ -> return i
191- [] -> do
192- (i, o) : xr <- sample gen
193- writeIORef vRef ((i, o) : xr)
194- Prelude. print $ (i, o) : xr
195- return i
158+ , signalCurVal = const $ ifProgress
159+ <?> readIORef vRef >>= \ case
160+ (h, _) : (i, o) : xr -> do
161+ memorize signalHistory h
162+ writeIORef vRef ((i, o) : xr)
163+ curStep <- readIORef simStepRef
164+ signalExpect checkedOutput $ Expectation (curStep, verifier s i o)
165+ return i
166+ [(h, _)] -> do
167+ memorize signalHistory h
168+ (i, o) : xr <- sample gen
169+
170+ writeIORef vRef ((i, o) : xr)
171+ curStep <- readIORef simStepRef
172+ signalExpect checkedOutput $ Expectation (curStep, verifier s i o)
173+ return i
174+ _ -> error " unreachable"
175+ <:> readIORef vRef >>= \ case
176+ (i, _) : _ -> return i
177+ [] -> do
178+ (i, o) : xr <- sample gen
179+ writeIORef vRef ((i, o) : xr)
180+ Prelude. print $ (i, o) : xr
181+ return i
196182 , signalPrint = Nothing
197183 , ..
198184 }
0 commit comments