-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.hs
234 lines (198 loc) · 6.11 KB
/
test.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
--
-- Based on https://github.com/owickstrom/hedgehog-inline-java-testing/blob/d0e51bd6712ce548b175262718d25649c5280933/src/test/haskell/Main.hs
--
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-deprecations #-}
import Clr.Inline (Clr)
import qualified Clr.Inline as Clr
import qualified CircularBuffer
import Control.Monad (guard, unless)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Maybe (isJust, listToMaybe)
import Hedgehog hiding (Size)
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
------------------------------------------------------------------------
-- State
newtype State (v :: * -> *) =
State {
models :: [(Var (Opaque (Clr "CircularBuffer<int>")) v, Model)]
}
initialState :: State v
initialState =
State []
data Model =
Model {
contents :: [Int]
, capacity :: Int
}
addOrReplace :: Eq k => k -> v -> [(k, v)] -> [(k, v)]
addOrReplace key value assoc =
(key, value) : filter ((key /=) . fst) assoc
------------------------------------------------------------------------
-- New
newtype New (v :: * -> *) =
New Int
deriving (Show)
instance HTraversable New where
htraverse _ (New c) =
pure (New c)
new :: (Monad n, MonadGen n, MonadIO m) => Command n m State
new =
let
gen _ =
Just $
New <$> Gen.int (Range.linear 0 100)
execute (New c) =
fmap Opaque . liftIO $ CircularBuffer.new c
in
Command gen execute [
Update $ \s (New capacity) buf ->
s {
models =
addOrReplace buf (Model [] capacity) (models s)
}
]
------------------------------------------------------------------------
-- Get
newtype Get (v :: * -> *) =
Get (Var (Opaque (Clr "CircularBuffer<int>")) v)
deriving (Show)
instance HTraversable Get where
htraverse f (Get buf) =
Get <$> htraverse f buf
get :: (MonadGen n, Monad n, MonadIO m) => Command n m State
get =
let
gen State {..}
| null models =
Nothing
| otherwise =
pure (Get <$> Gen.element (map fst models))
execute (Get ref) =
liftIO (CircularBuffer.get (opaque ref))
in
Command gen execute [
Require $ \State {..} (Get ref) ->
isJust $ do
Model {..} <- lookup ref models
guard (not (null contents))
, Update $ \s@State {..} (Get ref) _ ->
case lookup ref models of
Just model ->
s {
models =
addOrReplace
ref
model {
contents =
tail (contents model)
}
models
}
Nothing -> s
, Ensure $ \State {..} _ (Get ref) output ->
output === (listToJust . contents =<< lookup ref models)
]
where
listToJust xs =
let
m =
listToMaybe xs
in
if isJust m then
m
else
-- T in CircularBuffer<T> is a Value Type (CLR's System.Int32)
-- and Value Types can't be null so simulate it via 'Just 0'.
Just 0
------------------------------------------------------------------------
-- Put
data Put (v :: * -> *) =
Put (Var (Opaque (Clr "CircularBuffer<int>")) v) Int
deriving (Show)
instance HTraversable Put where
htraverse f (Put buf n) =
Put <$> htraverse f buf <*> pure n
put :: (MonadGen n, Monad n, MonadIO m) => Command n m State
put =
let
gen State {..}
| null models =
Nothing
| otherwise =
pure
(Put <$> Gen.element (map fst models) <*> Gen.int (Range.linear 0 100))
execute (Put ref n) =
liftIO (CircularBuffer.put (opaque ref) n)
in
Command gen execute [
Require $ \State {..} (Put ref _) ->
isJust $ do
Model {..} <- lookup ref models
guard (length contents < capacity)
, Update $ \s@State {..} (Put ref x) _ ->
case lookup ref models of
Just model ->
s {
models =
addOrReplace
ref
model {
contents =
contents model ++ [x]
}
models
}
Nothing ->
s
]
------------------------------------------------------------------------
-- Size
newtype Size (v :: * -> *) =
Size (Var (Opaque (Clr "CircularBuffer<int>")) v)
deriving (Show)
instance HTraversable Size where
htraverse f (Size buf) =
Size <$> htraverse f buf
size :: (MonadGen n, Monad n, MonadIO m) => Command n m State
size =
let
gen State{..}
| null models =
Nothing
| otherwise =
pure (Size <$> Gen.element (map fst models))
execute (Size ref) =
liftIO (CircularBuffer.size (opaque ref))
in
Command gen execute [
Require $ \State {..} (Size ref) ->
isJust (lookup ref models)
, Ensure $ \State {..} _ (Size ref) s -> do
let
Just model =
lookup ref models
assert $ s <= capacity model
s === length (contents model)
]
------------------------------------------------------------------------
prop_circular_buffer_sequential :: Property
prop_circular_buffer_sequential =
withTests 25 . property $ do
actions <- forAll $
Gen.sequential
(Range.linear 1 100)
initialState
[new, get, put, size]
executeSequential initialState actions
------------------------------------------------------------------------
main :: IO ()
main = do
Clr.startClr
succeeded <- checkSequential $$discover
unless succeeded (error "There were test failures.")