Skip to content

Allow running the action sequence from a user supplied initial state #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions quickcheck-dynamic/src/Test/QuickCheck/StateModel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Test.QuickCheck.StateModel (
monitorPost,
counterexamplePost,
stateAfter,
runActionsFrom,
runActions,
lookUpVar,
lookUpVarMaybe,
Expand Down Expand Up @@ -386,7 +387,7 @@ class QCDProp state p | p -> state where
instance QCDProp state (QCDProperty state) where
qcdProperty = id

instance Testable p => QCDProp state (Actions state -> p) where
instance (StateModel state, Testable p) => QCDProp state (Actions state -> p) where
qcdProperty p = QCDProperty (property . p) defaultOptions

modifyOptions :: QCDProperty state -> (Options state -> Options state) -> QCDProperty state
Expand All @@ -396,25 +397,23 @@ modifyOptions p f =

moreActions :: QCDProp state p => Rational -> p -> QCDProperty state
moreActions r p =
modifyOptions (qcdProperty p) $ \opts -> opts{actionLengthMultiplier = actionLengthMultiplier opts * r}
modifyOptions (qcdProperty p) $ \opts -> opts{oActionLengthMultiplier = oActionLengthMultiplier opts * r}

-- NOTE: indexed on state for forwards compatibility, e.g. when we
-- want to give an explicit initial state
data Options state = Options {actionLengthMultiplier :: Rational}
data Options state = Options {oActionLengthMultiplier :: Rational, oInitialAnnotatedState :: Annotated state}

defaultOptions :: Options state
defaultOptions = Options{actionLengthMultiplier = 1}
defaultOptions :: StateModel state => Options state
defaultOptions = Options{oActionLengthMultiplier = 1, oInitialAnnotatedState = initialAnnotatedState}

-- | Generate arbitrary actions with the `GenActionsOptions`. More flexible than using the type-based
-- modifiers.
generateActionsWithOptions :: forall state. StateModel state => Options state -> Gen (Actions state)
generateActionsWithOptions Options{..} = do
(as, rejected) <- arbActions [] [] initialAnnotatedState 1
(as, rejected) <- arbActions [] [] oInitialAnnotatedState 1
return $ Actions_ rejected (Smart 0 as)
where
arbActions :: [Step state] -> [String] -> Annotated state -> Int -> Gen ([Step state], [String])
arbActions steps rejected s step = sized $ \n -> do
let w = round (actionLengthMultiplier * fromIntegral n) `div` 2 + 1
let w = round (oActionLengthMultiplier * fromIntegral n) `div` 2 + 1
continue <- frequency [(1, pure False), (w, pure True)]
if continue
then do
Expand Down Expand Up @@ -548,7 +547,19 @@ runActions
)
=> Actions state
-> PropertyM m (Annotated state, Env)
runActions (Actions_ rejected (Smart _ actions)) = do
runActions = runActionsFrom initialAnnotatedState

runActionsFrom
:: forall state m e
. ( StateModel state
, RunModel state m
, e ~ Error state m
, forall a. IsPerformResult e a
)
=> Annotated state
-> Actions state
-> PropertyM m (Annotated state, Env)
runActionsFrom annotatedState (Actions_ rejected (Smart _ actions)) = do
let bucket = \n -> let (a, b) = go n in show a ++ " - " ++ show b
where
go n
Expand All @@ -557,7 +568,7 @@ runActions (Actions_ rejected (Smart _ actions)) = do
where
d = div n 10
monitor $ tabulate "# of actions" [show $ bucket $ length actions]
(finalState, env, names, polars) <- runSteps initialAnnotatedState [] actions
(finalState, env, names, polars) <- runSteps annotatedState [] actions
monitor $ tabulate "Actions" names
monitor $ tabulate "Action polarity" $ map show polars
unless (null rejected) $
Expand Down
Loading