From a682acca0adfd805bde4b5159df551e4cd353135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Dybiec?= Date: Thu, 9 Jan 2020 18:16:17 +0100 Subject: More tests --- test/TypecheckTest.hs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'test') diff --git a/test/TypecheckTest.hs b/test/TypecheckTest.hs index b08574a..299c8c0 100644 --- a/test/TypecheckTest.hs +++ b/test/TypecheckTest.hs @@ -12,3 +12,23 @@ typecheckTest = describe "inferExpr" $ do inferExpr (Var "a") [("a", TPtr TInt)] `shouldBe` ( Just $ TPtr TInt ) it "shouldn't infer undefined variables" $ do inferExpr (Var "a") [] `shouldBe` Nothing + it "should be able to infer types of arithmetic expressions" $ do + inferExpr (IntLit 5) [] `shouldBe` (Just TInt) + inferExpr (Add (IntLit 1) (IntLit 3)) [] `shouldBe` (Just TInt) + inferExpr (Mult (IntLit 1) (IntLit 3)) [] `shouldBe` (Just TInt) + inferExpr (Neg (IntLit 3)) [] `shouldBe` (Just TInt) + it "shouldn't be able to do arithmetic operations on pointer" $ do + inferExpr (Add (IntLit 1) (Var "a")) [("a",(TPtr TInt))] `shouldBe` Nothing + inferExpr (Add (Var "a") (IntLit 1)) [("a",(TPtr TInt))] `shouldBe` Nothing + inferExpr (Mult (IntLit 1) (Var "a")) [("a",(TPtr TInt))] `shouldBe` Nothing + inferExpr (Mult (Var "a") (IntLit 1)) [("a",(TPtr TInt))] `shouldBe` Nothing + inferExpr (Neg (Var "a")) [("a",(TPtr TInt))] `shouldBe` Nothing + it "should infer compound types" $ do + inferExpr (Variant (TVariant [("Cons",TInt),("Cons2",TPtr TInt)]) "Cons" (IntLit 5)) [] `shouldBe` (Just $ TVariant [("Cons",TInt),("Cons2",TPtr TInt)]) + inferExpr (Record [("aa",Var "a"),("bb",Var "b")]) [("a",TInt), ("b",TPtr TInt)] `shouldBe` (Just $ TRecord [("aa",TInt),("bb",TPtr TInt)]) + it "should unpack Ptr for every dereference" $ do + inferExpr (Deref (Var "a")) [("a",TPtr TInt)] `shouldBe` (Just TInt) + inferExpr (Deref (Var "a")) [("a",TPtr $ TPtr TInt)] `shouldBe` (Just $ TPtr TInt) + + + -- cgit v1.2.3