Skip to content
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

Adding "convergence" specification to integration-tests #48

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions spectacle.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ test-suite integration-tests

other-modules:
Specifications.BitClock
Specifications.Convergence
Specifications.Diehard
Specifications.RateLimit
Specifications.SimpleClock
Expand Down
96 changes: 96 additions & 0 deletions test/integration/Specifications/Convergence.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{-# LANGUAGE OverloadedLabels #-}

-- |
--
-- @since 1.0.0
module Specifications.Convergence
( interactSingleConvergenceSpec,
interactSplitConvergenceSpec,
)
where

import Control.Monad (guard)
import Language.Spectacle

------------------------------------------------------------------------------------------------------------------------

type SingleActs =
'[ "takeStep" # 'WeakFair
]

interactSingleConvergenceSpec :: IO ()
interactSingleConvergenceSpec = interaction singleConvergenceSpec

singleConvergenceSpec :: ConvergenceSpec SingleActs
singleConvergenceSpec =
Specification
{ specInit =
ConF #stepsLeft (pure 6)
. ConF #position (pure 0)
$ NilF
, specNext =
ConF #takeStep (ActionWF (oneOf [succ, pred] >>= takeStep)) NilF
, specProp =
ConF #positionZero (PropFG positionZero) NilF
}

------------------------------------------------------------------------------------------------------------------------

type SplitActs =
'[ "stepNegative" # 'WeakFair
, "stepPositive" # 'WeakFair
]

interactSplitConvergenceSpec :: IO ()
interactSplitConvergenceSpec = interaction splitConvergenceSpec

splitConvergenceSpec :: ConvergenceSpec SplitActs
splitConvergenceSpec =
Specification
{ specInit =
ConF #stepsLeft (pure 6)
. ConF #position (pure 0)
$ NilF
, specNext =
ConF #stepNegative (ActionWF (takeStep pred))
. ConF #stepPositive (ActionWF (takeStep succ))
$ NilF
, specProp =
ConF #positionZero (PropFG positionZero) NilF
}

------------------------------------------------------------------------------------------------------------------------

type ConvergenceSpec acts =
Specification
ConvergenceVar
acts
ConvergenceProp

type ConvergenceVar =
'[ "stepsLeft" # Int
, "position" # Int
]

type ConvergenceProp =
'[ "positionZero" # 'Stays
]

takeStep :: (Int -> Int) -> Action ConvergenceVar Bool
takeStep f = do
stepsLeft <- plain #stepsLeft
position <- plain #position
guard (0 < stepsLeft)

let position' = f position
guard (abs position' < stepsLeft)

#position .= pure position'
#stepsLeft .= pure (pred stepsLeft)

pure True

positionZero :: Temporal ConvergenceVar Bool
positionZero = do
position <- plain #position
pure (position == 0)