-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,798 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
-- | | ||
|
||
module BasicTest.Correct where | ||
|
||
import BasicTest.Template | ||
import Control.Concurrent.Class.MonadSTM | ||
import Test.QuickCheck | ||
import Test.StateMachine.IOSim | ||
|
||
{------------------------------------------------------------------------------- | ||
SUT | ||
-------------------------------------------------------------------------------} | ||
|
||
newtype AtomicCounter m = AtomicCounter (TVar m Int) | ||
|
||
newSUT :: MonadLabelledSTM m => m (AtomicCounter m) | ||
newSUT = do | ||
ref <- atomically $ do | ||
tv <- newTVar 0 | ||
labelTVar tv "TheCounter" | ||
pure tv | ||
return (AtomicCounter ref) | ||
|
||
incr :: MonadSTM m => AtomicCounter m -> m () | ||
incr (AtomicCounter ref) = | ||
atomically $ do | ||
i <- readTVar ref | ||
writeTVar ref (i + 1) | ||
|
||
get :: MonadSTM m => AtomicCounter m -> m Int | ||
get (AtomicCounter ref) = readTVarIO ref | ||
|
||
semantics :: MonadSTM m => AtomicCounter m -> Cmd Concrete -> m (Resp Concrete) | ||
semantics sut Incr = do | ||
incr sut *> pure Void | ||
semantics sut Get = do | ||
GetR <$> get sut | ||
|
||
transition :: Model r -> Cmd r -> Resp r -> Model r | ||
transition (Model m) Incr _ = Model (m + 1) | ||
transition m Get _ = m | ||
|
||
sm :: StateMachine Model Cmd AtomicCounter Resp | ||
sm = StateMachine initModel transition precondition postcondition Nothing | ||
generator shrinker newSUT semantics mock | ||
|
||
prop_sequential :: Property | ||
prop_sequential = forAllCommands sm Nothing $ runSequential sm | ||
|
||
prop_sequential' :: Property | ||
prop_sequential' = forAllCommands sm Nothing $ runSequentialPOR sm | ||
|
||
prop_parallel :: Property | ||
prop_parallel = forAllParallelCommands sm Nothing $ runParallel sm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
-- | | ||
|
||
module BasicTest.InternalFork where | ||
|
||
import BasicTest.Template | ||
import Control.Concurrent.Class.MonadSTM | ||
import Control.Monad.Class.MonadAsync | ||
import Test.QuickCheck | ||
import Test.StateMachine.IOSim | ||
|
||
{------------------------------------------------------------------------------- | ||
SUT | ||
-------------------------------------------------------------------------------} | ||
|
||
newtype AtomicCounter m = AtomicCounter (TVar m Int) | ||
|
||
newSUT :: MonadLabelledSTM m => m (AtomicCounter m) | ||
newSUT = do | ||
ref <- atomically $ do | ||
tv <- newTVar 0 | ||
labelTVar tv "TheCounter" | ||
pure tv | ||
return (AtomicCounter ref) | ||
|
||
incr :: MonadAsync m => AtomicCounter m -> m () | ||
incr (AtomicCounter ref) = do | ||
t1 <- async f | ||
t2 <- async f | ||
wait t1 >> wait t2 | ||
where f = do | ||
i <- readTVarIO ref | ||
atomically $ writeTVar ref (i + 1) | ||
|
||
get :: (MonadSTM m) => AtomicCounter m -> m Int | ||
get (AtomicCounter ref) = readTVarIO ref | ||
|
||
semantics :: MonadAsync m => AtomicCounter m -> Cmd Concrete -> m (Resp Concrete) | ||
semantics sut Incr = do | ||
incr sut *> pure Void | ||
semantics sut Get = do | ||
GetR <$> get sut | ||
|
||
transition :: Model r -> Cmd r -> Resp r -> Model r | ||
-- each increment will increment by 2 | ||
transition (Model m) Incr _ = Model (m + 2) | ||
transition m Get _ = m | ||
|
||
sm :: StateMachine Model Cmd AtomicCounter Resp | ||
sm = StateMachine initModel transition precondition postcondition Nothing | ||
generator shrinker newSUT semantics mock | ||
|
||
prop_sequential :: Property | ||
prop_sequential = forAllCommands sm Nothing $ runSequential sm | ||
|
||
prop_sequential' :: Property | ||
prop_sequential' = forAllCommands sm Nothing $ runSequentialPOR sm | ||
|
||
prop_parallel :: Property | ||
prop_parallel = forAllParallelCommands sm Nothing $ runParallel sm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
-- | | ||
|
||
module BasicTest.InternalForkAtomic where | ||
|
||
import BasicTest.Template | ||
import Control.Concurrent.Class.MonadSTM | ||
import Control.Monad.Class.MonadAsync | ||
import Test.QuickCheck | ||
import Test.StateMachine.IOSim | ||
|
||
{------------------------------------------------------------------------------- | ||
SUT | ||
-------------------------------------------------------------------------------} | ||
|
||
newtype AtomicCounter m = AtomicCounter (TVar m Int) | ||
|
||
newSUT :: MonadLabelledSTM m => m (AtomicCounter m) | ||
newSUT = do | ||
ref <- atomically $ do | ||
tv <- newTVar 0 | ||
labelTVar tv "TheCounter" | ||
pure tv | ||
return (AtomicCounter ref) | ||
|
||
incr :: MonadAsync m => AtomicCounter m -> m () | ||
incr (AtomicCounter ref) = do | ||
t1 <- async f | ||
t2 <- async f | ||
wait t1 >> wait t2 | ||
where f = atomically $ do | ||
i <- readTVar ref | ||
writeTVar ref (i + 1) | ||
|
||
get :: (MonadSTM m) => AtomicCounter m -> m Int | ||
get (AtomicCounter ref) = readTVarIO ref | ||
|
||
semantics :: MonadAsync m => AtomicCounter m -> Cmd Concrete -> m (Resp Concrete) | ||
semantics sut Incr = do | ||
incr sut *> pure Void | ||
semantics sut Get = do | ||
GetR <$> get sut | ||
|
||
transition :: Model r -> Cmd r -> Resp r -> Model r | ||
-- each increment will increment by 2 | ||
transition (Model m) Incr _ = Model (m + 2) | ||
transition m Get _ = m | ||
|
||
sm :: StateMachine Model Cmd AtomicCounter Resp | ||
sm = StateMachine initModel transition precondition postcondition Nothing | ||
generator shrinker newSUT semantics mock | ||
|
||
prop_sequential :: Property | ||
prop_sequential = forAllCommands sm Nothing $ runSequential sm | ||
|
||
prop_sequential' :: Property | ||
prop_sequential' = forAllCommands sm Nothing $ runSequentialPOR sm | ||
|
||
prop_parallel :: Property | ||
prop_parallel = forAllParallelCommands sm Nothing $ runParallel sm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
-- | | ||
|
||
module BasicTest.NoIncr where | ||
|
||
import BasicTest.Template | ||
import Control.Concurrent.Class.MonadSTM | ||
import Test.QuickCheck | ||
import Test.StateMachine.IOSim | ||
|
||
{------------------------------------------------------------------------------- | ||
SUT | ||
-------------------------------------------------------------------------------} | ||
|
||
newtype AtomicCounter m = AtomicCounter (TVar m Int) | ||
|
||
newSUT :: MonadLabelledSTM m => m (AtomicCounter m) | ||
newSUT = do | ||
ref <- atomically $ do | ||
tv <- newTVar 0 | ||
labelTVar tv "TheCounter" | ||
pure tv | ||
return (AtomicCounter ref) | ||
|
||
incr :: MonadSTM m => AtomicCounter m -> m () | ||
incr (AtomicCounter ref) = | ||
atomically $ do | ||
i <- readTVar ref | ||
-- Deliberate bug, no increment | ||
writeTVar ref i | ||
|
||
get :: MonadSTM m => AtomicCounter m -> m Int | ||
get (AtomicCounter ref) = readTVarIO ref | ||
|
||
semantics :: MonadSTM m => AtomicCounter m -> Cmd Concrete -> m (Resp Concrete) | ||
semantics sut Incr = do | ||
incr sut *> pure Void | ||
semantics sut Get = do | ||
GetR <$> get sut | ||
|
||
transition :: Model r -> Cmd r -> Resp r -> Model r | ||
transition (Model m) Incr _ = Model (m + 1) | ||
transition m Get _ = m | ||
|
||
sm :: StateMachine Model Cmd AtomicCounter Resp | ||
sm = StateMachine initModel transition precondition postcondition Nothing | ||
generator shrinker newSUT semantics mock | ||
|
||
prop_sequential :: Property | ||
prop_sequential = forAllCommands sm Nothing $ runSequential sm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
-- | | ||
|
||
module BasicTest.Racy where | ||
|
||
import BasicTest.Template | ||
import Control.Concurrent.Class.MonadSTM | ||
import Test.QuickCheck | ||
import Test.StateMachine.IOSim | ||
|
||
{------------------------------------------------------------------------------- | ||
SUT | ||
-------------------------------------------------------------------------------} | ||
|
||
newtype AtomicCounter m = AtomicCounter (TVar m Int) | ||
|
||
newSUT :: MonadLabelledSTM m => m (AtomicCounter m) | ||
newSUT = do | ||
ref <- atomically $ do | ||
tv <- newTVar 0 | ||
labelTVar tv "TheCounter" | ||
pure tv | ||
return (AtomicCounter ref) | ||
|
||
incr :: MonadSTM m => AtomicCounter m -> m () | ||
incr (AtomicCounter ref) = do | ||
i <- readTVarIO ref | ||
atomically $ writeTVar ref (i + 1) | ||
|
||
get :: MonadSTM m => AtomicCounter m -> m Int | ||
get (AtomicCounter ref) = readTVarIO ref | ||
|
||
semantics :: MonadSTM m => AtomicCounter m -> Cmd Concrete -> m (Resp Concrete) | ||
semantics sut Incr = do | ||
incr sut *> pure Void | ||
semantics sut Get = do | ||
GetR <$> get sut | ||
|
||
transition :: Model r -> Cmd r -> Resp r -> Model r | ||
transition (Model m) Incr _ = Model (m + 1) | ||
transition m Get _ = m | ||
|
||
sm :: StateMachine Model Cmd AtomicCounter Resp | ||
sm = StateMachine initModel transition precondition postcondition Nothing | ||
generator shrinker newSUT semantics mock | ||
|
||
prop_sequential :: Property | ||
prop_sequential = forAllCommands sm Nothing $ runSequential sm | ||
|
||
prop_sequential' :: Property | ||
prop_sequential' = forAllCommands sm Nothing $ runSequentialPOR sm | ||
|
||
prop_parallel :: Property | ||
prop_parallel = forAllParallelCommands sm Nothing $ runParallel sm |
Oops, something went wrong.