Last active
February 5, 2016 13:33
-
-
Save ondrap/a0791fafcce466966699 to your computer and use it in GitHub Desktop.
Failing QuickCheck test on serialise/deserialise
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
module Main where | |
import Data.Binary.Serialise.CBOR | |
import Data.Binary.Serialise.CBOR.Decoding (decodeListLen, decodeWord) | |
import Data.Binary.Serialise.CBOR.Encoding (encodeListLen, encodeWord) | |
import qualified Data.ByteString as BS | |
import qualified Data.ByteString.Lazy as BL | |
import Data.Monoid ((<>)) | |
import qualified Data.Text as T | |
import Test.Hspec | |
import Test.Hspec.QuickCheck | |
import Test.QuickCheck | |
import Test.QuickCheck.Instances () | |
import Test.QuickCheck.Random | |
data Value | |
= VNum Integer | |
| VTerms [T.Text] | |
deriving (Show, Eq) | |
instance Serialise Value where | |
encode (VNum num) = encodeListLen 2 <> encodeWord 0 <> encode num | |
encode (VTerms tset) = encodeListLen 2 <> encodeWord 8 <> encodeList tset | |
decode = do | |
marker <- (,) <$> decodeListLen <*> decodeWord | |
case marker of | |
(2, 0) -> VNum <$> decode | |
(2, 8) -> VTerms <$> decodeList | |
_ -> fail "Incorrect CBOR value" | |
instance Arbitrary Value where | |
arbitrary = oneof [ | |
VNum <$> arbitrary | |
, VTerms <$> arbitrary | |
] | |
goodProp :: [Value] -> Bool | |
goodProp v = BL.length ser `seq` deserialise ser == v | |
where ser = serialise v | |
chunkProp :: [Value] -> Bool | |
chunkProp v = (deserialise . tokenize . serialise) v == v | |
where | |
tokenize = BL.fromChunks . map (\a -> BS.pack [a]) . BS.unpack . BS.concat . BL.toChunks | |
failProp :: [Value] -> Bool | |
failProp v = deserialise (serialise v) == v | |
spec :: Spec | |
spec = describe "CBOR testing" $ do | |
prop "deserialize . force . serialize " goodProp | |
prop "deserialize . tokenize . serialize" chunkProp | |
prop "deserialize . serialize" failProp | |
main:: IO () | |
main = hspec spec |
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
import Distribution.Simple | |
main = defaultMain |
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
name: test | |
version: 0.1.0.0 | |
license-file: LICENSE | |
author: Ondrej Palkovsky | |
maintainer: palkovsky.ondrej@gmail.com | |
build-type: Simple | |
cabal-version: >=1.10 | |
executable test | |
main-is: Main.hs | |
ghc-options: -Wall | |
build-depends: base >=4.8 && <4.9, | |
QuickCheck, binary-serialise-cbor, text, | |
containers, quickcheck-instances, | |
time, bytestring, hspec, QuickCheck | |
default-language: Haskell2010 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment