glance/test/UnitTests.hs

408 lines
12 KiB
Haskell
Raw Normal View History

2016-12-13 05:47:12 +03:00
module UnitTests(
allUnitTests
) where
import Test.HUnit
import qualified Data.Graph.Inductive.Graph as ING
import qualified Data.Graph.Inductive.PatriciaTree as FGR
2016-12-14 10:13:43 +03:00
import Data.List(foldl', sort, sortOn)
import Translate(translateStringToSyntaxGraph)
import TranslateCore(syntaxGraphToFglGraph, SyntaxGraph(..), SgBind(..))
import Types(Labeled(..), SgNamedNode(..), Edge(..), SyntaxNode(..),
IngSyntaxGraph, NodeName(..), LikeApplyFlavor(..), NameAndPort(..))
2016-12-13 05:47:12 +03:00
import qualified GraphAlgorithms
2016-12-14 10:21:34 +03:00
import Util(fromMaybeError)
2016-12-13 05:47:12 +03:00
-- Unit Test Helpers --
assertAllEqual :: (Eq a, Show a) => [a] -> Test
assertAllEqual items = case items of
[] -> TestCase $ assertFailure "assertAllEqual: argument is empty list"
(first : rest) -> TestList $ fmap (first ~=?) rest
2018-11-11 14:17:06 +03:00
-- TODO Remove the Lambda node's node list.
assertEqualSyntaxGraphs :: [String] -> Test
assertEqualSyntaxGraphs ls = assertAllEqual $ fmap (renameGraph . translateStringToSyntaxGraph) ls
-- BEGIN renameGraph --
type NameMap = [(NodeName, NodeName)]
renameNode
:: NameMap -> Int -> SgNamedNode -> (SgNamedNode, NameMap, Int)
2016-12-27 12:32:51 +03:00
renameNode nameMap counter (SgNamedNode nodeName syntaxNode) = (newNamedNode, nameMap3, newCounter) where
newNodeName = NodeName counter
nameMap2 = (nodeName, newNodeName) : nameMap
(newSyntaxNode, nameMap3, newCounter) = renameSyntaxNode nameMap2 syntaxNode (counter + 1)
2016-12-27 12:32:51 +03:00
newNamedNode = SgNamedNode newNodeName newSyntaxNode
maybeRenameNodeFolder ::
([Labeled (Maybe SgNamedNode)], NameMap, Int)
-> Maybe SgNamedNode
-> ([Labeled (Maybe SgNamedNode)], NameMap, Int)
maybeRenameNodeFolder (renamedNodes, nameMap, counter) mNode = case mNode of
2018-11-11 14:17:06 +03:00
Nothing -> (pure Nothing : renamedNodes, nameMap, counter)
Just node -> (pure (Just newNamedNode) : renamedNodes, newNameMap, newCounter)
where
(newNamedNode, newNameMap, newCounter) = renameNode nameMap counter node
renameSyntaxNode :: NameMap -> SyntaxNode -> Int -> (SyntaxNode, NameMap, Int)
renameSyntaxNode nameMap node counter = case node of
-- TODO Keep the Nothing subNodes
NestedPatternApplyNode s subNodes
-> (NestedPatternApplyNode s (reverse renamedSubNodes)
, newNameMap
, counter2)
where
(renamedSubNodes, newNameMap, counter2)
= foldl'
maybeRenameNodeFolder
([], nameMap, counter)
(fmap laValue subNodes)
_ -> (node, nameMap, counter)
renameNodeFolder :: ([SgNamedNode], NameMap, Int) -> SgNamedNode -> ([SgNamedNode], NameMap, Int)
2016-12-27 12:32:51 +03:00
renameNodeFolder state@(renamedNodes, nameMap, counter) node@(SgNamedNode nodeName _) = case lookup nodeName nameMap of
Nothing -> (newNamedNode:renamedNodes, newNameMap, newCounter) where
(newNamedNode, newNameMap, newCounter) = renameNode nameMap counter node
Just _ -> error $ "renameNode: node already in name map. State = " ++ show state ++ " Node = " ++ show node
renameNamePort :: NameMap -> NameAndPort -> NameAndPort
renameNamePort nameMap nameAndPort@(NameAndPort name port) = NameAndPort newName port where
newName = fromMaybeError errorStr $ lookup name nameMap
errorStr = "renameNamePort: name not found. name = " ++ show name ++ "\nNameAndPort = " ++ show nameAndPort ++ "\nNameMap = " ++ show nameMap
renameEdge :: NameMap -> Edge -> Edge
renameEdge nameMap (Edge options ends (np1, np2)) =
Edge options ends (renameNamePort nameMap np1, renameNamePort nameMap np2)
renameSource :: NameMap -> SgBind -> SgBind
renameSource nameMap (SgBind str ref) = SgBind str newRef where
newRef = case ref of
Left _ -> ref
Right namePort@(NameAndPort _ _) -> Right $ renameNamePort nameMap namePort
renameEmbed :: NameMap -> (NodeName, NodeName) -> (NodeName, NodeName)
renameEmbed nameMap (leftName, rightName) = (newLeftName, newRightName) where
newLeftName = fromMaybeError "renameEmbed: leftName not found" (lookup leftName nameMap)
newRightName = fromMaybeError "renameEmbed: RightName not found" (lookup rightName nameMap)
2016-12-14 10:13:43 +03:00
-- TODO May want to remove names for sub-nodes
removeNames :: SgNamedNode -> SyntaxNode
2016-12-27 12:32:51 +03:00
removeNames (SgNamedNode _ syntaxNode) = syntaxNode
2016-12-14 10:13:43 +03:00
-- TODO Rename sinks
-- TODO Add unit tests for renameGraph
renameGraph :: SyntaxGraph -> SyntaxGraph
renameGraph (SyntaxGraph nodes edges sinks sources embedMap) =
SyntaxGraph renamedNodes renamedEdges sinks renamedSources renamedEmbedMap
where
(renamedNodes, nameMap, _) = foldl' renameNodeFolder ([], [], 0) $ sortOn removeNames nodes
renamedEdges = sort $ fmap (renameEdge nameMap) edges
renamedSources = sort $ fmap (renameSource nameMap) sources
renamedEmbedMap = sort $ fmap (renameEmbed nameMap) embedMap
2018-11-05 09:54:17 +03:00
-- END renameGraph
-- END Unit Test Helpers --
2016-12-13 05:47:12 +03:00
-- 0:(toName "app02",ApplyNode 1)->[]
-- 1:(toName "f0",LiteralNode "f")->[(Edge {edgeOptions = [], edgeEnds = (EndNone,EndNone), edgeConnection = (NameAndPort (toName "f0") Nothing,NameAndPort (toName "app02") (Just 0))},0)]
-- 2:(toName "x1",LiteralNode "x")->[(Edge {edgeOptions = [], edgeEnds = (EndNone,EndNone), edgeConnection = (NameAndPort (toName "x1") Nothing,NameAndPort (toName "app02") (Just 2))},0)]
-- 3:(toName "y3",NameNode "y")->[(Edge {edgeOptions = [], edgeEnds = (EndNone,EndNone), edgeConnection = (NameAndPort (toName "y3") Nothing,NameAndPort (toName "app02") (Just 1))},0)]
singleApplyGraph :: FGR.Gr SgNamedNode Edge
singleApplyGraph = syntaxGraphToFglGraph $ translateStringToSyntaxGraph "y = f x"
2016-12-13 05:47:12 +03:00
makeTreeRootTest :: (String, [Maybe SgNamedNode], String) -> Test
makeTreeRootTest (testName, expected, haskellString) = TestCase $ assertEqual testName expected actual where
actual = fmap (ING.lab graph) treeRoots
graph = syntaxGraphToFglGraph $ translateStringToSyntaxGraph haskellString
2016-12-13 05:47:12 +03:00
treeRoots = GraphAlgorithms.findTreeRoots graph
treeRootTests :: Test
treeRootTests = TestList $ fmap makeTreeRootTest treeRootTestList where
treeRootTestList = [
2016-12-27 12:32:51 +03:00
("single apply", [Just $ SgNamedNode (NodeName 2) (LikeApplyNode ApplyNodeFlavor 1)], "y = f x"),
2016-12-13 05:47:12 +03:00
-- TODO Fix test below
2016-12-27 12:32:51 +03:00
("double apply", [Just $ SgNamedNode (NodeName 3) (LikeApplyNode ComposeNodeFlavor 2)], "y = f (g x)"),
("recursive apply", [Just $ SgNamedNode (NodeName 3) (LikeApplyNode ComposeNodeFlavor 2)], "y = f (g y)")
2016-12-13 05:47:12 +03:00
]
makeChildCanBeEmbeddedTest ::
ING.Graph gr =>
(String, IngSyntaxGraph gr, ING.Node, Bool) -> Test
makeChildCanBeEmbeddedTest (testName, graph, node, expected) =TestCase $ assertEqual testName expected canBeEmbedded where
canBeEmbedded = GraphAlgorithms.nodeWillBeEmbedded graph node
-- TODO Add more cases for childCanBeEmbeddedTests
-- TODO Fix these tests
childCanBeEmbeddedTests :: Test
2018-11-05 09:54:17 +03:00
childCanBeEmbeddedTests
= TestList $ fmap makeChildCanBeEmbeddedTest childCanBeEmbeddedList
where
childCanBeEmbeddedList = [
-- ("single apply, ap", singleApplyGraph, 0, False),
("single apply, f", singleApplyGraph, 1, True),
-- ("single apply, x", singleApplyGraph, 2, True),
("single apply, y", singleApplyGraph, 3, False)
]
2016-12-13 05:47:12 +03:00
collapseUnitTests :: Test
collapseUnitTests = TestList[
TestLabel "findTreeRoots" treeRootTests
2018-11-05 09:54:17 +03:00
, TestLabel "childCanBeEmbedded" childCanBeEmbeddedTests
2016-12-13 05:47:12 +03:00
]
-- Translate unit tests
applyTests :: Test
applyTests = TestList [
TestLabel "dollarTests1" $ assertEqualSyntaxGraphs [
"y = f x",
"y = f $ x"
]
,
TestLabel "dollarTests2" $ assertEqualSyntaxGraphs [
"y = f (g x)",
"y = f $ (g x)",
"y = f $ g $ x",
"y = f (g $ x)"
]
,
TestLabel "dollarTests3" $ assertEqualSyntaxGraphs [
"y = f 1 (g 2)",
"y = f 1 $ g 2"
]
,
assertEqualSyntaxGraphs [
"y = f 3 4",
"y = (f 3) 4"
]
]
2016-12-14 10:21:34 +03:00
composeApplyTests :: Test
composeApplyTests = TestList [
TestLabel "composeApplyTests1" $ assertEqualSyntaxGraphs [
"y = f (g x)",
"y = (f . g) x",
"y = f . g $ x"
]
,
TestLabel "composeApplyTests2" $ assertEqualSyntaxGraphs [
"y = f3 (f2 (f1 x))",
"y = f3 . f2 . f1 $ x",
"y = (f3 . f2 . f1) x"
]
]
2016-12-14 10:21:34 +03:00
infixTests :: Test
infixTests = TestList [
TestLabel "infixTests1" $ assertEqualSyntaxGraphs [
"y = (+) 1 2",
"y = ((+) 1) 2",
"y = 1 + 2",
"y = (1 +) 2"
]
,
TestLabel "infixTests2" $ assertEqualSyntaxGraphs [
"y = f (1 +) 2",
"y = f ((+) 1) 2"
]
2016-12-13 05:47:12 +03:00
]
2016-12-14 10:21:34 +03:00
letTests :: Test
letTests = TestList [
TestLabel "letTests1" $ assertEqualSyntaxGraphs [
"y = f 1",
2016-12-14 10:13:43 +03:00
"y = let x = 1 in f x",
"y = let {b = a; a = 1} in f b"
]
,
assertEqualSyntaxGraphs [
"y = 2",
"y = let z = 2 in z",
"y = let {z = 2; z2 = z} in z2"
]
,
assertEqualSyntaxGraphs [
"y = f y",
"y = let x = f x in x"
]
,
assertEqualSyntaxGraphs [
"y = f 7 5",
"y = let {a = 7; b = f a 5} in b"
]
,
assertEqualSyntaxGraphs [
"y x = x",
"y x = let z = x in z"
]
,
assertEqualSyntaxGraphs [
"fibs = let {y = cons 0 (cons 1 (zipWith (+) y (tail y)))} in y",
"fibs = cons 0 (cons 1 (zipWith (+) fibs (tail fibs)))"
]
2016-12-14 10:13:43 +03:00
,
assertEqualSyntaxGraphs [
"y x = y x",
"y = let {z = (\\x -> y x)} in z",
"y = let {z x = y x} in z "
]
,
assertEqualSyntaxGraphs [
"y = f 3 y",
"y = x where x = f 3 y",
"y = let x = f 3 y in x"
]
,
2018-11-11 14:17:06 +03:00
-- TODO Fix this test. It fails due to the names in the lambda region (which
-- are not renamed
-- assertEqualSyntaxGraphs [
-- "y x1 = f x1",
-- "y x1 = let {x2 = x1; x3 = x2; x4 = f x3} in x4"
-- ]
-- ,
2016-12-14 10:13:43 +03:00
-- TODO Fix this test. The second line has two apply icons instead of one.
-- See VisualTranslateTests/letTests
-- assertEqualSyntaxGraphs [
-- "y x1 = (f x1) x1",
-- "y x1 = let x2 = f x1 in x2 x1"
-- ]
assertEqualSyntaxGraphs [
"y x = 3",
"y x = let x = 3 in x"
]
-- TODO Fix test. Second line should use compose apply.
-- See VisualTranslateTests/letTests
-- assertEqualSyntaxGraphs [
-- "y = g $ f y",
-- "y = let {a = f b; b = g a} in b"
-- ]
]
negateTests :: Test
negateTests = TestList [
assertEqualSyntaxGraphs [
"y = negate 1",
"y = -1"
]
,
assertEqualSyntaxGraphs [
"y = negate ((/) 1 2)",
"y = -1/2"
]
,
assertEqualSyntaxGraphs [
"y = negate x",
"y = -x"
]
]
enumTests :: Test
enumTests = TestList [
assertEqualSyntaxGraphs [
"y = enumFrom 1",
"y = [1..]"
]
,
assertEqualSyntaxGraphs [
"y = enumFromThen 1 2",
"y = [1,2..]"
]
,
assertEqualSyntaxGraphs [
"y = enumFromTo 0 10",
"y = [0..10]"
]
,
assertEqualSyntaxGraphs [
"y = enumFromThenTo 0 1 10",
"y = [0,1..10]"
]
]
2016-12-14 12:10:18 +03:00
patternTests :: Test
patternTests = TestList [
2016-12-14 12:10:18 +03:00
assertEqualSyntaxGraphs [
"y (F x) = x",
"y = (\\(F x) -> x)"
]
,
assertEqualSyntaxGraphs [
"y = let {F x y = 3} in x y",
"y = let {g = 3; F x y = g} in x y"
]
]
lambdaTests :: Test
lambdaTests = TestList [
assertEqualSyntaxGraphs [
"y x = (\\z -> x)",
"y = (\\x -> (\\z -> x))"
]
,
2017-01-02 04:43:00 +03:00
-- TODO These tests fail since the lambda node has a " tempvar" param name.
-- assertEqualSyntaxGraphs [
-- "y x = case x of {0 -> 1; 3 -> 5}",
-- "{y 0 = 1; y 3 = 5}"
-- ]
-- ,
-- assertEqualSyntaxGraphs [
-- "y p = case p of {F x -> x; G x -> x}",
-- "{y (F x) = x; y (G x) = x}"
-- ]
-- ,
-- assertEqualSyntaxGraphs [
-- -- TODO Since there are no patterns for z, this should just be "case p of"
-- "y p z = case (p, z) of {((F x), z') -> x z'; ((G x), z') -> z' x}",
-- "{y (F x) z = x z; y (G x) z = z x}"
-- ]
-- ,
assertEqualSyntaxGraphs [
"y x = f x y",
"y x = z where z = f x y"
]
]
fmapTests :: Test
fmapTests = TestList [
assertEqualSyntaxGraphs [
"y = fmap f x",
"y = f <$> x"
]
,
assertEqualSyntaxGraphs [
"y = f1 (fmap f2 (f3 x))",
"y = f1 $ f2 <$> f3 x"
]
]
-- Yes, the commas get their own line
translateUnitTests :: Test
translateUnitTests = TestList [
TestLabel "fmapTest" fmapTests
,
TestLabel "applyTests" applyTests
,
TestLabel "composeApplyTests" composeApplyTests
,
TestLabel "infixTests" infixTests
, TestLabel "letTests" letTests
, TestLabel "negateTests" negateTests
, TestLabel "enumTests" enumTests
2016-12-14 12:10:18 +03:00
, TestLabel "patternTests" patternTests
, TestLabel "lambdaTests" lambdaTests
]
2016-12-13 05:47:12 +03:00
allUnitTests :: Test
allUnitTests = TestList[
TestLabel "collapseUnitTests" collapseUnitTests,
TestLabel "translateTests" translateUnitTests
]