-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added test for validFlat along with Ref and Imp Decoders
- Loading branch information
1 parent
c1cc3d6
commit 761ef0c
Showing
5 changed files
with
136 additions
and
7 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
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,119 @@ | ||
{-# Language FlexibleInstances #-} | ||
-- {-# Language OverlappingInstances #-} | ||
|
||
module Tests.FlatTerm | ||
( testTree -- :: TestTree | ||
) where | ||
|
||
import Control.Applicative | ||
import Codec.CBOR.FlatTerm ( TermToken(..), fromFlatTerm, validFlatTerm, toFlatTerm ) | ||
import Codec.CBOR.Write ( toLazyByteString) | ||
import Codec.CBOR.Term ( decodeTerm, encodeTerm ) | ||
import Codec.CBOR.Encoding ( Encoding(..) | ||
, encodeInt | ||
, encodeInteger | ||
, encodeBytes | ||
, encodeBytesIndef | ||
, encodeString | ||
, encodeStringIndef | ||
, encodeListLen | ||
, encodeListLenIndef | ||
, encodeMapLen | ||
, encodeMapLenIndef | ||
, encodeBreak | ||
, encodeTag64 | ||
, encodeBool | ||
, encodeNull | ||
, encodeSimple | ||
, encodeFloat16 | ||
, encodeFloat | ||
, encodeDouble ) | ||
import qualified Tests.Reference.Implementation as Imp | ||
import Tests.Term ( toRefTerm, fromRefTerm ) | ||
import qualified Data.ByteString as BS | ||
import qualified Data.ByteString.Lazy as LBS | ||
import qualified Data.Text as T | ||
import Data.Word | ||
import Prelude hiding (encodeFloat) | ||
|
||
import Test.QuickCheck | ||
import Test.Tasty | ||
import Test.Tasty.QuickCheck | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
instance {-# OVERLAPS #-} Arbitrary [TermToken] where | ||
arbitrary = frequency | ||
[ (60, termToTermTokens <$> arbitrary) | ||
, (40, pure <$> genArbitraryFlatTerm) | ||
] | ||
|
||
genArbitraryFlatTerm :: Gen TermToken | ||
genArbitraryFlatTerm = oneof | ||
[ TkInt <$> arbitrary | ||
, TkInteger <$> arbitrary | ||
, TkBytes . BS.pack <$> arbitrary | ||
, pure TkBytesBegin | ||
, TkString . T.pack <$> arbitrary | ||
, pure TkStringBegin | ||
, TkListLen <$> arbitrary | ||
, pure TkListBegin | ||
, TkMapLen <$> arbitrary | ||
, pure TkMapBegin | ||
, pure TkBreak | ||
, TkTag <$> arbitrary | ||
, TkBool <$> arbitrary | ||
, pure TkNull | ||
, TkSimple <$> arbitrary | ||
, TkFloat16 <$> arbitrary | ||
, TkFloat32 <$> arbitrary | ||
, TkFloat64 <$> arbitrary ] | ||
|
||
-- | Converts a FlatTerm to Encoding | ||
tokenToEncoding :: TermToken -> Encoding | ||
tokenToEncoding (TkInt n) = encodeInt n | ||
tokenToEncoding (TkInteger i) = encodeInteger i | ||
tokenToEncoding (TkBytes bs) = encodeBytes bs | ||
tokenToEncoding TkBytesBegin = encodeBytesIndef | ||
tokenToEncoding (TkString txt) = encodeString txt | ||
tokenToEncoding TkStringBegin = encodeStringIndef | ||
tokenToEncoding (TkListLen w) = encodeListLen w | ||
tokenToEncoding TkListBegin = encodeListLenIndef | ||
tokenToEncoding (TkMapLen w) = encodeMapLen w | ||
tokenToEncoding TkMapBegin = encodeMapLenIndef | ||
tokenToEncoding TkBreak = encodeBreak | ||
tokenToEncoding (TkTag w64) = encodeTag64 w64 | ||
tokenToEncoding (TkBool bool) = encodeBool bool | ||
tokenToEncoding TkNull = encodeNull | ||
tokenToEncoding (TkSimple w8) = encodeSimple w8 | ||
tokenToEncoding (TkFloat16 f) = encodeFloat16 f | ||
tokenToEncoding (TkFloat32 f) = encodeFloat f | ||
tokenToEncoding (TkFloat64 d) = encodeDouble d | ||
|
||
-- | converts a @Term@ to @FlatTerm@ | ||
termToTermTokens :: Imp.Term -> [TermToken] | ||
termToTermTokens = toFlatTerm . encodeTerm . fromRefTerm | ||
|
||
|
||
-- | Changes a FlatTerm to List of Word8. | ||
termTokensToWord :: [TermToken] -> [Word8] | ||
termTokensToWord ts = LBS.unpack bytes | ||
where | ||
bytes = toLazyByteString enc | ||
enc = mconcat $ tokenToEncoding <$> ts | ||
|
||
-- | Given a FlatTerm checks whether the results of Refernce and Implementation | ||
-- decoders tally or not, also matches the result with @validFlatTerm@ function. | ||
prop_validFlatTerm :: [TermToken] -> Property | ||
prop_validFlatTerm ts = property $ case (refDecoded, impDecoded) of | ||
(Right x, Just (term, [])) -> ((Imp.canonicaliseTerm . toRefTerm $ x) == Imp.canonicaliseTerm term) | ||
&& validFlatTerm ts | ||
(Left _ , _) -> not $ validFlatTerm ts | ||
_ -> False | ||
where | ||
impDecoded = Imp.runDecoder Imp.decodeTerm (termTokensToWord ts) | ||
refDecoded = fromFlatTerm decodeTerm ts | ||
|
||
testTree :: TestTree | ||
testTree = testGroup "tests the function validFlatTerm and different decoders" | ||
[ testProperty "all should agree: RefDecoder, ImpDecoder, validFlatTerm " prop_validFlatTerm ] |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
resolver: lts-13.8 | ||
resolver: lts-13.14 | ||
|
||
packages: | ||
- 'cborg' | ||
- 'serialise' | ||
|