|
| 1 | +{-# LANGUAGE FlexibleInstances #-} |
| 2 | +{-# LANGUAGE MultiParamTypeClasses #-} |
| 3 | +{-# LANGUAGE PartialTypeSignatures #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | + |
| 6 | +module Main where |
| 7 | + |
| 8 | +import Common |
| 9 | +import Control.Comonad.Cofree (Cofree ((:<))) |
| 10 | +import Control.Monad (unless) |
| 11 | +import Data.Bifunctor (Bifunctor (first)) |
| 12 | +import Data.List (isInfixOf) |
| 13 | +import Data.Ratio |
| 14 | +import Debug.Trace |
| 15 | +import PrettyPrint |
| 16 | +import qualified System.IO.Strict as Strict |
| 17 | +import Telomare |
| 18 | +import Telomare.Eval (EvalError (CompileConversionError), compileUnitTest, |
| 19 | + compileUnitTestNoAbort) |
| 20 | +import Telomare.Parser (AnnotatedUPT, TelomareParser, parseLongExpr, |
| 21 | + parsePrelude) |
| 22 | +import Telomare.Resolver (process) |
| 23 | +import Telomare.RunTime (simpleEval) |
| 24 | +import Test.Tasty |
| 25 | +import Test.Tasty.HUnit |
| 26 | +import Test.Tasty.QuickCheck as QC |
| 27 | +import Text.Megaparsec (eof, errorBundlePretty, runParser) |
| 28 | + |
| 29 | + |
| 30 | +main :: IO () |
| 31 | +main = defaultMain tests |
| 32 | + |
| 33 | +tests :: TestTree |
| 34 | +tests = testGroup "Arithmetic Tests" [ unitTestsNatArithmetic |
| 35 | + , unitTestsRatArithmetic |
| 36 | + , qcPropsNatArithmetic |
| 37 | + , qcPropsRatArithmetic |
| 38 | + ] |
| 39 | + |
| 40 | +maybeToRight :: Maybe a -> Either EvalError a |
| 41 | +maybeToRight (Just x) = Right x |
| 42 | +maybeToRight Nothing = Left CompileConversionError |
| 43 | + |
| 44 | +rightToMaybe :: Either String a -> Maybe a |
| 45 | +rightToMaybe (Right x) = Just x |
| 46 | +rightToMaybe _ = Nothing |
| 47 | + |
| 48 | +loadPreludeBindings :: IO [(String, AnnotatedUPT)] |
| 49 | +loadPreludeBindings = do |
| 50 | + preludeResult <- Strict.readFile "Prelude.tel" |
| 51 | + case parsePrelude preludeResult of |
| 52 | + Left _ -> pure [] |
| 53 | + Right bs -> pure bs |
| 54 | + |
| 55 | +evalExprString :: String -> IO (Either String String) |
| 56 | +evalExprString input = do |
| 57 | + preludeBindings <- loadPreludeBindings |
| 58 | + let parseResult = runParser (parseLongExpr <* eof) "" input |
| 59 | + case parseResult of |
| 60 | + Left err -> pure $ Left (errorBundlePretty err) |
| 61 | + Right aupt -> do |
| 62 | + let term = DummyLoc :< LetUPF preludeBindings aupt |
| 63 | + compile' :: Term3 -> Either String IExpr |
| 64 | + compile' x = case compileUnitTestNoAbort x of |
| 65 | + Left err -> Left . show $ err |
| 66 | + Right r -> case toTelomare r of |
| 67 | + Just te -> pure $ fromTelomare te |
| 68 | + Nothing -> Left $ "conversion error from compiled expr:\n" <> prettyPrint r |
| 69 | + case process term >>= compile' of |
| 70 | + Left err -> pure $ Left (show err) |
| 71 | + Right iexpr -> case eval iexpr of |
| 72 | + Left e -> pure . Left . show $ e |
| 73 | + Right result -> pure . Right . show . PrettyIExpr $ result |
| 74 | + |
| 75 | +assertExpr :: String -> String -> Assertion |
| 76 | +assertExpr input expected = do |
| 77 | + res <- evalExprString input |
| 78 | + case res of |
| 79 | + Left err -> unless (expected `isInfixOf` err) . assertFailure $ "Evaluation failed: " <> err |
| 80 | + Right val -> val @?= expected |
| 81 | + |
| 82 | +rationalToString :: Ratio Integer -> String |
| 83 | +rationalToString r = "(" <> show (numerator r) <> "," <> show (denominator r) <> ")" |
| 84 | + |
| 85 | +genInt :: Gen Int |
| 86 | +genInt = choose (0, 100) |
| 87 | + |
| 88 | +genInt2 :: Gen Int |
| 89 | +genInt2 = choose (0, 85) |
| 90 | + |
| 91 | +genInt3 :: Gen Int |
| 92 | +genInt3 = choose (0, 16) |
| 93 | + |
| 94 | +genInt4 :: Gen Int |
| 95 | +genInt4 = choose (0, 6) |
| 96 | + |
| 97 | +----------------- |
| 98 | +------ Unit Tests |
| 99 | +----------------- |
| 100 | + |
| 101 | +unitTestsNatArithmetic :: TestTree |
| 102 | +unitTestsNatArithmetic = testGroup "Unit tests on natural arithmetic expresions" |
| 103 | + [ testCase "test addition" $ assertExpr "dPlus 1 2" (show (1 + 2)) |
| 104 | + , testCase "test subtraction" $ assertExpr "dMinus 5 3" (show (5 - 3)) |
| 105 | + , testCase "test substraction lower limit" $ assertExpr "dMinus 0 1" "0" |
| 106 | + , testCase "test multiplication" $ assertExpr "dTimes 3 4" (show (3 * 4)) |
| 107 | + , testCase "test division" $ assertExpr "dDiv 8 2" (show (8 `div` 2)) |
| 108 | + , testCase "test division by zero" $ assertExpr "dDiv 1 0" "abort:" |
| 109 | + , testCase "test equality" $ assertExpr "dEqual 3 3" "1" |
| 110 | + , testCase "test inequality" $ assertExpr "dEqual 3 4" "0" |
| 111 | + , testCase "test modulo" $ assertExpr "dMod 10 3" (show (10 `mod` 3)) |
| 112 | + , testCase "test gcd" $ assertExpr "gcd 48 18" (show (gcd 48 18)) |
| 113 | + , testCase "test lcm" $ assertExpr "lcm 12 15" (show (lcm 12 15)) |
| 114 | + , testCase "test exponentiation" $ assertExpr "dPow 2 3" (show (2 ^ 3)) |
| 115 | + , testCase "test exponentiation with zero exponent" $ assertExpr "dPow 5 0" "1" |
| 116 | + , testCase "test raise zero to the zero power" $ assertExpr "dPow 0 0" "1" |
| 117 | + , testCase "test factorial" $ assertExpr "dFactorial 5" (show (product [1..5])) |
| 118 | + ] |
| 119 | + |
| 120 | +unitTestsRatArithmetic :: TestTree |
| 121 | +unitTestsRatArithmetic = testGroup "Unit tests on rational arithmetic expresions" |
| 122 | + [ testCase "test addition" $ do |
| 123 | + let exp = rationalToString (1 % 2 + 1 % 2) |
| 124 | + assertExpr "right (rPlus (fromRational 1 2) (fromRational 1 2))" exp |
| 125 | + , testCase "test subtraction" $ do |
| 126 | + let exp = rationalToString (1 % 2 - 1 % 2) |
| 127 | + assertExpr "right (rMinus (fromRational 1 2) (fromRational 1 2))" exp |
| 128 | + , testCase "test multiplication" $ do |
| 129 | + let exp = rationalToString ((1 % 2) * (1 % 2)) |
| 130 | + assertExpr "right (rTimes (fromRational 1 2) (fromRational 1 2))" exp |
| 131 | + , testCase "test division" $ do |
| 132 | + let exp = rationalToString ((1 % 2) / (1 % 2)) |
| 133 | + assertExpr "right (rDiv (fromRational 1 2) (fromRational 1 2))" exp |
| 134 | + , testCase "test division by zero" $ assertExpr "rDiv (fromRational 1 2) (fromRational 0 1)" "abort:" |
| 135 | + ] |
| 136 | + |
| 137 | +--------------------- |
| 138 | +------ Property Tests |
| 139 | +--------------------- |
| 140 | + |
| 141 | +qcPropsNatArithmetic = testGroup "Property tests on natural arithmetic expressions (QuickCheck)" |
| 142 | + [ QC.testProperty "Commutative Additive" $ |
| 143 | + \() -> withMaxSuccess 16 . QC.idempotentIOProperty $ do |
| 144 | + x <- generate genInt |
| 145 | + y <- generate genInt |
| 146 | + res <- evalExprString |
| 147 | + ( "dEqual (dPlus " <> show x <> " " <> show y <> |
| 148 | + ") (dPlus " <> show y <> " " <> show x <> ")" ) |
| 149 | + pure $ case res of |
| 150 | + Left err -> err === "1" |
| 151 | + Right val -> val === "1" |
| 152 | + , QC.testProperty "Associative Additive" $ |
| 153 | + \() -> withMaxSuccess 16 . QC.idempotentIOProperty $ do |
| 154 | + x <- generate genInt2 |
| 155 | + y <- generate genInt2 |
| 156 | + z <- generate genInt2 |
| 157 | + res <- evalExprString |
| 158 | + ( "dEqual (dPlus (dPlus " <> show x <> " " <> show y |
| 159 | + <> ")" <> show z <> ") (dPlus " <> show x <> "(dPlus " |
| 160 | + <> show y <> " " <> show z <>"))" ) |
| 161 | + pure $ case res of |
| 162 | + Left err -> err === "1" |
| 163 | + Right val -> val === "1" |
| 164 | + , QC.testProperty "Neutral Additive" $ |
| 165 | + \() -> withMaxSuccess 16 . QC.idempotentIOProperty $ do |
| 166 | + x <- generate genInt |
| 167 | + res <- evalExprString ("dPlus " <> show x <> " 0") |
| 168 | + pure $ case res of |
| 169 | + Left err -> err === show x |
| 170 | + Right val -> val === show x |
| 171 | + , QC.testProperty "Commutative Multiplicative" $ |
| 172 | + \() -> withMaxSuccess 16 . QC.idempotentIOProperty $ do |
| 173 | + x <- generate genInt3 |
| 174 | + y <- generate genInt3 |
| 175 | + res <- evalExprString |
| 176 | + ( "dEqual (dTimes " <> show x <> " " <> show y <> |
| 177 | + ") (dTimes " <> show y <> " " <> show x <> ")" ) |
| 178 | + pure $ case res of |
| 179 | + Left err -> err === "1" |
| 180 | + Right val -> val === "1" |
| 181 | + , QC.testProperty "Associative Multiplicative" $ |
| 182 | + \() -> withMaxSuccess 16 . QC.idempotentIOProperty $ do |
| 183 | + x <- generate genInt4 |
| 184 | + y <- generate genInt4 |
| 185 | + z <- generate genInt4 |
| 186 | + res <- evalExprString |
| 187 | + ( "dEqual (dTimes (dTimes " <> show x <> " " <> show y |
| 188 | + <> ")" <> show z <> ") (dTimes " <> show x <> "(dTimes " |
| 189 | + <> show y <> " " <> show z <>"))" ) |
| 190 | + pure $ case res of |
| 191 | + Left err -> err === "1" |
| 192 | + Right val -> val === "1" |
| 193 | + , QC.testProperty "Neutral Multiplicative" $ |
| 194 | + \() -> withMaxSuccess 16 . QC.idempotentIOProperty $ do |
| 195 | + x <- generate genInt |
| 196 | + res <- evalExprString ("dTimes " <> show x <> " 1") |
| 197 | + pure $ case res of |
| 198 | + Left err -> err === show x |
| 199 | + Right val -> val === show x |
| 200 | + ] |
| 201 | + |
| 202 | +qcPropsRatArithmetic = testGroup "Property tests on rational arithmetic expressions (QuickCheck)" [] |
0 commit comments