| 1 |
{-# LANGUAGE OverloadedStrings #-}
|
| 2 |
|
| 3 |
module Data.SCargot.ReprQC (reprQC) where
|
| 4 |
|
| 5 |
import Data.SCargot ( SExprParser
|
| 6 |
, SExprPrinter
|
| 7 |
, mkParser
|
| 8 |
, flatPrint
|
| 9 |
, encodeOne
|
| 10 |
, decodeOne
|
| 11 |
, asRich
|
| 12 |
, asWellFormed
|
| 13 |
)
|
| 14 |
import Data.SCargot.Repr ( SExpr(..)
|
| 15 |
, RichSExpr
|
| 16 |
, fromRich
|
| 17 |
, toRich
|
| 18 |
, WellFormedSExpr(..)
|
| 19 |
, fromWellFormed
|
| 20 |
, toWellFormed
|
| 21 |
)
|
| 22 |
import Test.QuickCheck
|
| 23 |
import Test.QuickCheck.Arbitrary
|
| 24 |
import Text.Parsec (char)
|
| 25 |
import Text.Parsec.Text (Parser)
|
| 26 |
|
| 27 |
instance Arbitrary a => Arbitrary (SExpr a) where
|
| 28 |
arbitrary = sized $ \n ->
|
| 29 |
if n <= 0
|
| 30 |
then pure SNil
|
| 31 |
else oneof [ SAtom <$> arbitrary
|
| 32 |
, do
|
| 33 |
k <- choose (0, n)
|
| 34 |
elems <- sequence [ resize (n-k) arbitrary
|
| 35 |
| _ <- [0..k]
|
| 36 |
]
|
| 37 |
tail <- oneof [ SAtom <$> arbitrary
|
| 38 |
, pure SNil
|
| 39 |
]
|
| 40 |
pure (foldr SCons tail elems)
|
| 41 |
]
|
| 42 |
|
| 43 |
instance Arbitrary a => Arbitrary (RichSExpr a) where
|
| 44 |
arbitrary = toRich `fmap` arbitrary
|
| 45 |
|
| 46 |
instance Arbitrary a => Arbitrary (WellFormedSExpr a) where
|
| 47 |
arbitrary = sized $ \n ->
|
| 48 |
oneof [ WFSAtom <$> arbitrary
|
| 49 |
, do
|
| 50 |
k <- choose (0, n)
|
| 51 |
WFSList <$> sequence
|
| 52 |
[ resize (n-k) arbitrary
|
| 53 |
| _ <- [0..k]
|
| 54 |
]
|
| 55 |
]
|
| 56 |
|
| 57 |
parser :: SExprParser () (SExpr ())
|
| 58 |
parser = mkParser (() <$ char 'X')
|
| 59 |
|
| 60 |
printer :: SExprPrinter () (SExpr ())
|
| 61 |
printer = flatPrint (const "X")
|
| 62 |
|
| 63 |
richIso :: SExpr () -> Bool
|
| 64 |
richIso s = fromRich (toRich s) == s
|
| 65 |
|
| 66 |
richIsoBk :: RichSExpr () -> Bool
|
| 67 |
richIsoBk s = toRich (fromRich s) == s
|
| 68 |
|
| 69 |
|
| 70 |
wfIso :: SExpr () -> Bool
|
| 71 |
wfIso s = case toWellFormed s of
|
| 72 |
Left _ -> True
|
| 73 |
Right y -> s == fromWellFormed y
|
| 74 |
|
| 75 |
wfIsoBk :: WellFormedSExpr () -> Bool
|
| 76 |
wfIsoBk s = toWellFormed (fromWellFormed s) == Right s
|
| 77 |
|
| 78 |
|
| 79 |
encDec :: SExpr () -> Bool
|
| 80 |
encDec s = decodeOne parser (encodeOne printer s) == Right s
|
| 81 |
|
| 82 |
encDecRich :: RichSExpr () -> Bool
|
| 83 |
encDecRich s = decodeOne (asRich parser) (encodeOne printer (fromRich s))
|
| 84 |
== Right s
|
| 85 |
|
| 86 |
encDecWF :: WellFormedSExpr () -> Bool
|
| 87 |
encDecWF s = decodeOne (asWellFormed parser) (encodeOne printer (fromWellFormed s))
|
| 88 |
== Right s
|
| 89 |
|
| 90 |
reprQC :: IO ()
|
| 91 |
reprQC = do
|
| 92 |
putStrLn "The SExpr <--> Rich translation should be isomorphic"
|
| 93 |
quickCheck richIso
|
| 94 |
quickCheck richIsoBk
|
| 95 |
putStrLn "The SExpr <--> WF translation should be near-isomorphic"
|
| 96 |
quickCheck wfIso
|
| 97 |
quickCheck wfIsoBk
|
| 98 |
putStrLn "This should be true when parsing, as well"
|
| 99 |
quickCheck encDec
|
| 100 |
quickCheck encDecRich
|
| 101 |
quickCheck encDecWF
|