diff options
3 files changed, 236 insertions, 0 deletions
| -rw-r--r-- | homework/81248/clex/test/Clex/LexSpec.hs | 58 | ||||
| -rw-r--r-- | homework/81248/clex/test/Clex/TypesSpec.hs | 177 | ||||
| -rw-r--r-- | homework/81248/clex/test/Spec.hs | 1 |
diff --git a/homework/81248/clex/test/Clex/LexSpec.hs b/homework/81248/clex/test/Clex/LexSpec.hs new file mode 100644 index 0000000..c5fec81 --- /dev/null +++ b/homework/81248/clex/test/Clex/LexSpec.hs @@ -0,0 +1,58 @@ +module Clex.LexSpec + ( spec + ) where + +import qualified Data.ByteString as BS +import Data.ByteString (ByteString) +import Data.ByteString.Char8 (pack) + +import Clex.Types +import Clex.Lex (lexC) + +import Test.QuickCheck +import Test.Hspec +import Test.Hspec.QuickCheck +import Clex.TypesSpec () + +spec :: Spec +spec = describe "lexing" $ do + prop "tokensToBS after lexC is id" $ + \x -> let x' = unValidC x in + case lexC x' of + (Left _) -> False + (Right x'') -> x' == tokensToBS x'' + prop "lexing after tokensToBS is id" $ + \x -> let x' = unTokenC x in + case lexC $ tokensToBS x' of + (Left _) -> False + (Right x'') -> x' == x'' + +newtype TokenC = TokenC {unTokenC :: [Token]} + deriving (Eq, Show) + +newtype ValidC = ValidC {unValidC :: ByteString} + deriving (Eq, Show) + +instance Arbitrary TokenC where + arbitrary = TokenC . packWhitespaceTokens . concat <$> + listOf1 (do + x <- arbitrary + whitespace <- whitespaceGen + pure [x, whitespace]) + where + whitespaceGen = Whitespace . pack <$> do listOf1 $ elements ['\t', ' ', '\n'] + +instance Arbitrary ValidC where + arbitrary = ValidC . BS.concat . map tokenToBS <$> listOf1 (frequency + [ (30, arbitrary) + , (60, whitespaceGen) + ]) + where + whitespaceGen = Whitespace . pack <$> do listOf1 $ elements ['\t', ' ', '\n'] + +packWhitespaceTokens :: [Token] -> [Token] +packWhitespaceTokens [] = [] +packWhitespaceTokens [x] = [x] +packWhitespaceTokens (Whitespace x : Whitespace y : xs) + = packWhitespaceTokens $ Whitespace (x <> y) : xs +packWhitespaceTokens (x:xs) = x : packWhitespaceTokens xs diff --git a/homework/81248/clex/test/Clex/TypesSpec.hs b/homework/81248/clex/test/Clex/TypesSpec.hs new file mode 100644 index 0000000..805affc --- /dev/null +++ b/homework/81248/clex/test/Clex/TypesSpec.hs @@ -0,0 +1,177 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# OPTIONS -fno-warn-orphans #-} + +module Clex.TypesSpec + ( spec + ) where + +import Data.ByteString.Char8 (pack) + +import Clex.Types + +import Test.QuickCheck +import Test.Hspec +import Test.Hspec.QuickCheck +import Data.ByteString.Arbitrary + +spec :: Spec +spec = describe "types" + conversionSpec + +conversionSpec :: Spec +conversionSpec = describe "ByteString conversions" $ do + prop "recognize is inverse to tokenToBS" $ + \x -> (not . isLiteral) x ==> x == recognize (tokenToBS x) + prop "tokenToBS is inverse to recognize" $ + \x -> let x' = fromABS x in x' == tokenToBS (recognize x') + +isLiteral :: Token -> Bool +isLiteral (HexNumber _) = True +isLiteral (OctNumber _) = True +isLiteral (Number _) = True +isLiteral (CharLit _) = True +isLiteral (Scientific _) = True +isLiteral (DotFloat _) = True +isLiteral (LeadingDot _) = True +isLiteral (StringLit _) = True +isLiteral _ = False + +instance Arbitrary Token where + arbitrary = frequency + [ (30, nullaryGen) + , (30, identifierGen) + , (10, literalGen) + ] + where + nullaryGen = elements + [ Auto + , Break + , Case + , Char + , Const + , Continue + , Default + , Do + , Double + , Else + , Enum + , Extern + , Float + , For + , Goto + , If + , Int + , Long + , Register + , Return + , Short + , Signed + , Sizeof + , Static + , Struct + , Switch + , Typedef + , Union + , Unsigned + , Void + , Volatile + , While + , Ellipsis + , RightShiftAssign + , LeftShiftAssign + , AddAssign + , SubAssign + , MulAssign + , DivAssign + , ModAssign + , AndAssign + , XorAssign + , OrAssign + , Incr + , Decr + , Ptr + , Dot + , And + , Or + , Not + , LtEq + , GtEq + , Eq + , NEq + , Lt + , Gt + , Semicolon + , LeftCurly + , RightCurly + , Comma + , Colon + , Assign + , LeftBracket + , RightBracket + , LeftSquare + , RightSquare + , RightShift + , LeftShift + , BitAnd + , BitOr + , BitNot + , Sub + , Add + , Mul + , Div + , Mod + , Xor + , Ternary + ] + identifierGen = Identifier <$> (pack <$> do listOf1 $ elements $ '_' : ['a'..'z'] ++ ['A'..'Z']) + `suchThat` (not . reserved) + reserved = (`elem` allKeyWords) + -- don't feel like overdoing this right now + literalGen = oneof + [ elements + [ HexNumber "0xb23abcf4" + , HexNumber "0x123ab5f4ul" + , HexNumber "0X123a4cf4" + , HexNumber "0x1e3ebcf4lull" + + , OctNumber "0234" + , OctNumber "012354ul" + , OctNumber "012744" + , OctNumber "0134lull" + + , Number "234" + , Number "12354ul" + , Number "12744" + , Number "134lull" + ] + , elements + [ CharLit "'c'" + , CharLit "L'c'" + , CharLit "'\n'" + , CharLit "'\t'" + , CharLit "'a'" + ] + , elements + [ DotFloat ".213E+10f" + , DotFloat ".213e-10" + , DotFloat ".213e-10l" + , DotFloat ".213e10l" + , DotFloat ".213l" + , DotFloat ".213" + , DotFloat ".213e9L" + + , LeadingDot "213.2E+10f" + , LeadingDot "213.3e-10" + , LeadingDot "213.e-10l" + , LeadingDot "213.e10l" + , LeadingDot "213.e9L" + , LeadingDot "213.9L" + , LeadingDot "213.L" + ] + , elements + [ StringLit "\"\"" + , StringLit "L\"asdfwqe\"" + , StringLit "\"asd\nfw\tqe\"" + , StringLit "L\"\"" + ] + ] diff --git a/homework/81248/clex/test/Spec.hs b/homework/81248/clex/test/Spec.hs new file mode 100644 index 0000000..a824f8c --- /dev/null +++ b/homework/81248/clex/test/Spec.hs @@ -0,0 +1 @@ +{-# OPTIONS_GHC -F -pgmF hspec-discover #-} |
