File: GetTests.hs

package info (click to toggle)
haskell-cereal 0.5.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 168 kB
  • sloc: haskell: 1,643; makefile: 2
file content (278 lines) | stat: -rw-r--r-- 7,009 bytes parent folder | download | duplicates (3)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE FlexibleInstances #-}

module GetTests (tests) where

import           Control.Applicative
import           Control.Monad
import           Data.Word
import           Data.Function
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LB
import           Data.Serialize.Get
import           Test.Framework (Test(),testGroup)
import           Test.Framework.Providers.QuickCheck2 (testProperty)
import           Test.QuickCheck as QC


-- Data to express Get parser to generate
data GetD
  = Get8
  | Eof
  | Get16be
  | Get32be
  | Get64be
  | Get16le
  | Get32le
  | Get64le
  | GetD  :*>   GetD
  | GetD  :<|>  GetD
  | LookAhead GetD
  | Skip Int
  deriving Show

-- Get parser generator
buildGet :: GetD -> Get ()
buildGet = d  where
  d Get8           =  getWord8    *> pure ()
  d Eof            =  guard =<< isEmpty
  d Get16be        =  getWord16be *> pure ()
  d Get32be        =  getWord32be *> pure ()
  d Get64be        =  getWord64be *> pure ()
  d Get16le        =  getWord16le *> pure ()
  d Get32le        =  getWord32le *> pure ()
  d Get64le        =  getWord64le *> pure ()
  d (x :*>  y)     =  d x *>  d y
  d (x :<|> y)     =  d x <|> d y
  d (LookAhead x)  =  lookAhead $ d x
  d (Skip i)       =  skip i

-- Randomly generate parser
genGetD :: Gen GetD
genGetD =
    oneof $
    [ pure g
    | g <- [ Get8, Eof
           , Get16be, Get32be, Get64be
           , Get16le, Get32le, Get64le
           ]
    ] ++
    [ (:*>)     <$> genGetD <*> genGetD
    , (:<|>)    <$> genGetD <*> genGetD
    , LookAhead <$> genGetD
    , Skip      <$> choose (0, 10)
    ]

instance Arbitrary GetD where
  arbitrary = genGetD

instance Arbitrary (Get ()) where
  arbitrary = buildGet <$> genGetD

newtype R a =
  R { unR :: Either String a }
  deriving Show


-- Ignore equality of error message string
instance Eq a => Eq (R a) where
  (==)  =  (==) `on` either (const Nothing) Just . unR

data Chunks = Chunks [[Word8]] deriving (Eq, Show)

mkChunks :: Word -> Chunks
mkChunks n = Chunks . take (fromIntegral n) $ cycle [ [x] | x <- [0 .. 255] ]

instance Arbitrary Chunks where
  arbitrary = mkChunks <$> choose (0, 512)


testLength :: Word
testLength = 255

-- Equality between strict and lazy parsing
eqStrictLazy :: GetD -> Property
eqStrictLazy getD =
  conjoin
  [ counterexample (show in0) $ R (runGet parser sb) == R (runGetLazy parser lb)
  | n <- [0 .. testLength]
  , let Chunks in0 = mkChunks n
        lb = LB.fromChunks [ BS.pack c | c <- in0 ]
        sb = BS.pack $ concat in0
  ]
  where
    parser = buildGet getD

-- Remaining length equality between strict and lazy parsing
remainingStrictLazy :: GetD -> Property
remainingStrictLazy getD =
  conjoin
  [ counterexample (show in0) $ R (runGet parser sb) == R (runGetLazy parser lb)
  | n <- [0 .. testLength]
  , let Chunks in0 = mkChunks n
        lb = LB.fromChunks [ BS.pack c | c <- in0 ]
        sb = BS.pack $ concat in0
  ]
  where
    parser = buildGet getD *> remaining

isEmpty2 :: Get Bool
isEmpty2 = do
  lookAhead getWord8 *> pure False
  <|>
  pure True

-- Compare with chunks
(==~) :: Eq a => Get a -> Get a -> Property
p1 ==~ p2 =
  conjoin
  [ counterexample (show in0) $ R (runGetLazy p1 s) == R (runGetLazy p2 s)
  | n <- [0 .. testLength]
  , let Chunks in0 = mkChunks n
        s = LB.fromChunks [ BS.pack c | c <- in0 ]
  ]

(==!) :: Eq a => Get a -> Get a -> Property
p1 ==! p2 =
  conjoin
  [ counterexample (show s) $ R (runGet p1 s) == R (runGet p2 s)
  | n <- [0 .. testLength]
  , let Chunks in0 = mkChunks n
        s = BS.pack $ concat in0
  ]

infix 2 ==~, ==!

-- Equality between two eof definition - lazy
eqEof :: GetD -> Property
eqEof getD =
    x *> isEmpty ==~ x *> isEmpty2
  where
    x = buildGet getD

-- Equality between two eof definition - strict
eqEof' :: GetD -> Property
eqEof' getD =
    x *> isEmpty ==! x *> isEmpty2
  where
    x = buildGet getD


monadIdL :: GetD -> Property
monadIdL getD =
    (return () >>= const x) ==~ x
  where
    x = buildGet getD

monadIdL' :: GetD -> Property
monadIdL' getD =
    (return () >>= const x) ==! x
  where
    x = buildGet getD

monadIdR :: GetD -> Property
monadIdR getD =
    (x >>= return) ==~ x
  where
    x = buildGet getD

monadIdR' :: GetD -> Property
monadIdR' getD =
    (x >>= return) ==! x
  where
    x = buildGet getD

monadAssoc :: GetD -> GetD -> GetD -> Property
monadAssoc p1 p2 p3 =
    (x >> (y >> z)) ==~ (x >> y >> z)
  where
    x = buildGet p1
    y = buildGet p2
    z = buildGet p3

monadAssoc' :: GetD -> GetD -> GetD -> Property
monadAssoc' p1 p2 p3 =
    (x >> (y >> z)) ==! (x >> y >> z)
  where
    x = buildGet p1
    y = buildGet p2
    z = buildGet p3

alterIdL :: GetD -> Property
alterIdL getD =
    empty <|> x ==~ x
  where
    x = buildGet getD

alterIdL' :: GetD -> Property
alterIdL' getD =
    empty <|> x ==! x
  where
    x = buildGet getD

alterIdR :: GetD -> Property
alterIdR getD =
    x <|> empty ==~ x
  where
    x = buildGet getD

alterIdR' :: GetD -> Property
alterIdR' getD =
    x <|> empty ==! x
  where
    x = buildGet getD

alterAssoc :: GetD -> GetD -> GetD -> Property
alterAssoc p1 p2 p3 =
    x <|> y <|> z ==~ x <|> (y <|> z)
  where
    x = buildGet p1
    y = buildGet p2
    z = buildGet p3

alterAssoc' :: GetD -> GetD -> GetD -> Property
alterAssoc' p1 p2 p3 =
    x <|> y <|> z ==! x <|> (y <|> z)
  where
    x = buildGet p1
    y = buildGet p2
    z = buildGet p3

alterDistr :: GetD -> GetD -> GetD -> Property
alterDistr p1 p2 p3 =
    x *> (y <|> z) ==~ x *> y <|> x *> z
  where
    x = buildGet p1
    y = buildGet p2
    z = buildGet p3

alterDistr' :: GetD -> GetD -> GetD -> Property
alterDistr' p1 p2 p3 =
    x *> (y <|> z) ==! x *> y <|> x *> z
  where
    x = buildGet p1
    y = buildGet p2
    z = buildGet p3


tests :: Test
tests  = testGroup "GetTests"
  [ testProperty "lazy   - monad left id"          monadIdL
  , testProperty "strict - monad left id"          monadIdL'
  , testProperty "lazy   - monad right id"         monadIdR
  , testProperty "strict - monad right id"         monadIdR'
  , testProperty "lazy   - monad assoc"            monadAssoc
  , testProperty "strict - monad assoc"            monadAssoc'
  , testProperty "strict lazy - equality"          eqStrictLazy
  , testProperty "strict lazy - remaining equality"remainingStrictLazy
  , testProperty "lazy   - two eof"                eqEof
  , testProperty "strict - two eof"                eqEof'
  , testProperty "lazy   - alternative left Id"    alterIdL
  , testProperty "strict - alternative left Id"    alterIdL'
  , testProperty "lazy   - alternative right Id"   alterIdR
  , testProperty "strict - alternative right Id"   alterIdR'
  , testProperty "lazy   - alternative assoc"      alterAssoc
  , testProperty "strict - alternative assoc"      alterAssoc'
  , testProperty "lazy   - alternative distr"      alterDistr
  , testProperty "strict - alternative distr"      alterDistr'
  ]