Added tests for genDeclaration, and tweaked the backends to make the tests pass

This commit is contained in:
Neil Brown 2007-10-03 17:58:59 +00:00
parent f6af19c67e
commit 65fb758dbd
3 changed files with 56 additions and 11 deletions

View File

@ -91,6 +91,7 @@ data GenOps = GenOps {
genConversionSymbol :: GenOps -> A.Type -> A.Type -> A.ConversionMode -> CGen (),
genDecl :: GenOps -> A.AbbrevMode -> A.Type -> A.Name -> CGen (),
genDeclType :: GenOps -> A.AbbrevMode -> A.Type -> CGen (),
-- | Generates a declaration of a variable of the specified type and name
genDeclaration :: GenOps -> A.Type -> A.Name -> CGen (),
genDirectedVariable :: GenOps -> CGen () -> A.Direction -> CGen (),
genDyadic :: GenOps -> Meta -> A.DyadicOp -> A.Expression -> A.Expression -> CGen (),
@ -1168,29 +1169,30 @@ cdeclareType ops t = call genType ops t
-- | Generate a declaration of a new variable.
cgenDeclaration :: GenOps -> A.Type -> A.Name -> CGen ()
cgenDeclaration ops (A.Chan {}) n
-- Channels are of type "Channel", but channel-ends are of type "Channel*"
cgenDeclaration ops (A.Chan A.DirUnknown _ _) n
= do tell ["Channel "]
genName n
tell [";\n"]
tell [";"]
cgenDeclaration ops (A.Array ds t) n
= do call declareType ops t
tell [" "]
genName n
call genFlatArraySize ops ds
tell [";\n"]
tell [";"]
call declareArraySizes ops ds n
cgenDeclaration ops t n
= do call declareType ops t
tell [" "]
genName n
tell [";\n"]
tell [";"]
-- | Generate the size of the C array that an occam array of the given
-- dimensions maps to.
cgenFlatArraySize :: GenOps -> [A.Dimension] -> CGen ()
cgenFlatArraySize ops ds
= do tell ["["]
sequence $ intersperse (tell [" * "])
sequence $ intersperse (tell ["*"])
[case d of A.Dimension n -> tell [show n] | d <- ds]
tell ["]"]

View File

@ -649,11 +649,11 @@ cppgenProcCall ops n as
-- | Changed from CIF's untyped channels to C++CSP's typed (templated) channels, and changed the declaration type of an array to be a vector.
cppdeclareType :: GenOps -> A.Type -> CGen ()
cppdeclareType ops (A.Array ds t)
= do tell [" std::vector< "]
= do tell ["std::vector<"]
call genType ops t
tell ["/**/>/**/"]
cppdeclareType ops (A.Counted countType valueType)
= do tell [" std::vector< "]
= do tell ["std::vector<"]
case valueType of
--Don't nest when it's a counted array of arrays:
(A.Array _ t) -> call genType ops t
@ -685,9 +685,9 @@ cppgenDeclaration ops arrType@(A.Array ds t) n
= do call declareType ops arrType
tell [" "]
genName n
tell ["_actual ("]
tell ["_actual("]
call genFlatArraySize ops ds
tell ["); "]
tell [");"]
call genType ops arrType
tell [" "]
genName n;
@ -695,12 +695,12 @@ cppgenDeclaration ops arrType@(A.Array ds t) n
genName n
tell ["_actual,tockDims("]
genDims ds
tell ["));\n"]
tell ["));"]
cppgenDeclaration ops t n
= do call declareType ops t
tell [" "]
genName n
tell [";\n"]
tell [";"]
-- | Changed because of channel arrays.
cppdeclareInit :: GenOps -> Meta -> A.Type -> A.Variable -> Maybe (CGen ())

View File

@ -243,6 +243,48 @@ testArraySubscript = TestList
stateTrans = defineName (simpleName "foo") $ simpleDefDecl "foo" (A.Array [A.Dimension 7,A.Dimension 8,A.Dimension 8] A.Int)
m = "\"" ++ show emptyMeta ++ "\""
testDeclaration :: Test
testDeclaration = TestList
[
--Simple:
testBothSame "genDeclaration 0" "int foo;" (tcall2 genDeclaration A.Int foo)
--Channels and channel-ends:
,testBoth "genDeclaration 1" "Channel foo;" "csp::One2OneChannel<int> foo;" (tcall2 genDeclaration (A.Chan A.DirUnknown (A.ChanAttributes False False) A.Int) foo)
,testBoth "genDeclaration 2" "Channel foo;" "csp::Any2OneChannel<int> foo;" (tcall2 genDeclaration (A.Chan A.DirUnknown (A.ChanAttributes True False) A.Int) foo)
,testBoth "genDeclaration 3" "Channel foo;" "csp::One2AnyChannel<int> foo;" (tcall2 genDeclaration (A.Chan A.DirUnknown (A.ChanAttributes False True) A.Int) foo)
,testBoth "genDeclaration 4" "Channel foo;" "csp::Any2AnyChannel<int> foo;" (tcall2 genDeclaration (A.Chan A.DirUnknown (A.ChanAttributes True True) A.Int) foo)
,testBoth "genDeclaration 5" "Channel* foo;" "csp::Chanin<int> foo;" (tcall2 genDeclaration (A.Chan A.DirInput (A.ChanAttributes False False) A.Int) foo)
,testBoth "genDeclaration 6" "Channel* foo;" "csp::Chanin<int> foo;" (tcall2 genDeclaration (A.Chan A.DirInput (A.ChanAttributes False True) A.Int) foo)
,testBoth "genDeclaration 7" "Channel* foo;" "csp::Chanout<int> foo;" (tcall2 genDeclaration (A.Chan A.DirOutput (A.ChanAttributes False False) A.Int) foo)
,testBoth "genDeclaration 8" "Channel* foo;" "csp::Chanout<int> foo;" (tcall2 genDeclaration (A.Chan A.DirOutput (A.ChanAttributes True False) A.Int) foo)
--Arrays (of simple):
,testBoth "genDeclaration 100" "int foo[8];const int foo_sizes[]={8};" "std::vector<int> foo_actual(8);tockArrayView<int,1> foo(foo_actual,tockDims(8));"
(tcall2 genDeclaration (A.Array [A.Dimension 8] A.Int) foo)
,testBoth "genDeclaration 101" "int foo[8*9];const int foo_sizes[]={8,9};" "std::vector<int> foo_actual(8*9);tockArrayView<int,2> foo(foo_actual,tockDims(8,9));"
(tcall2 genDeclaration (A.Array [A.Dimension 8,A.Dimension 9] A.Int) foo)
,testBoth "genDeclaration 102" "int foo[8*9*10];const int foo_sizes[]={8,9,10};" "std::vector<int> foo_actual(8*9*10);tockArrayView<int,3> foo(foo_actual,tockDims(8,9,10));"
(tcall2 genDeclaration (A.Array [A.Dimension 8,A.Dimension 9,A.Dimension 10] A.Int) foo)
--Arrays of channels and channel-ends:
,testBoth "genDeclaration 200" "Channel* foo[8];const int foo_sizes[]={8};"
"std::vector<csp::One2OneChannel<int>*> foo_actual(8);tockArrayView<csp::One2OneChannel<int>*,1> foo(foo_actual,tockDims(8));"
(tcall2 genDeclaration (A.Array [A.Dimension 8] $ A.Chan A.DirUnknown (A.ChanAttributes False False) A.Int) foo)
,testBoth "genDeclaration 201" "Channel* foo[8*9];const int foo_sizes[]={8,9};"
"std::vector<csp::One2OneChannel<int>*> foo_actual(8*9);tockArrayView<csp::One2OneChannel<int>*,2> foo(foo_actual,tockDims(8,9));"
(tcall2 genDeclaration (A.Array [A.Dimension 8, A.Dimension 9] $ A.Chan A.DirUnknown (A.ChanAttributes False False) A.Int) foo)
,testBoth "genDeclaration 202" "Channel* foo[8];const int foo_sizes[]={8};"
"std::vector<csp::Chanin<int>> foo_actual(8);tockArrayView<csp::Chanin<int>,1> foo(foo_actual,tockDims(8));"
(tcall2 genDeclaration (A.Array [A.Dimension 8] $ A.Chan A.DirInput (A.ChanAttributes False False) A.Int) foo)
,testBoth "genDeclaration 203" "Channel* foo[8*9];const int foo_sizes[]={8,9};"
"std::vector<csp::Chanout<int>> foo_actual(8*9);tockArrayView<csp::Chanout<int>,2> foo(foo_actual,tockDims(8,9));"
(tcall2 genDeclaration (A.Array [A.Dimension 8, A.Dimension 9] $ A.Chan A.DirOutput (A.ChanAttributes False False) A.Int) foo)
]
---Returns the list of tests:
tests :: Test
tests = TestList
@ -250,6 +292,7 @@ tests = TestList
testActuals
,testArraySizes
,testArraySubscript
,testDeclaration
,testGenType
,testStop
]