diff --git a/tests/src/internal/lang/parser/statementLoading.cpp b/tests/src/internal/lang/parser/statementLoading.cpp index 0261b6ff2..e78dff690 100644 --- a/tests/src/internal/lang/parser/statementLoading.cpp +++ b/tests/src/internal/lang/parser/statementLoading.cpp @@ -30,8 +30,8 @@ int main(const int argc, const char **argv) { testNamespaceLoading(); testStructLoading(); // testClassLoading(); - // testUnionLoading(); testEnumLoading(); + testUnionLoading(); testFunctionLoading(); testIfLoading(); testForLoading(); @@ -283,7 +283,82 @@ void testClassLoading() { } void testUnionLoading() { - // TODO: Add union tests + statement_t *statement = NULL; + union_t *unionType = NULL; + typedef_t *typedefType = NULL; + +#define declSmnt statement->to() +#define getDeclType declSmnt.declarations[0].variable().vartype.type +#define setUnionType() unionType = (union_t*) getDeclType +#define setTypedefType() typedefType = (typedef_t*) getDeclType + + // Test default union + setStatement( + "union idx3 {\n" + " int i, *j, &k;\n" + "};", + statementType::declaration + ); + + setUnionType(); + + ASSERT_EQ("idx3", + unionType->name()); + + ASSERT_EQ(3, + (int) unionType->fields.size()); + + ASSERT_EQ("i", + unionType->fields[0].name()); + ASSERT_EQ(&int_, + unionType->fields[0].vartype.type); + + ASSERT_EQ("j", + unionType->fields[1].name()); + ASSERT_EQ(&int_, + unionType->fields[1].vartype.type); + + ASSERT_EQ("k", + unionType->fields[2].name()); + ASSERT_EQ(&int_, + unionType->fields[2].vartype.type); + + // Test default typedef union + setStatement( + "typedef union idx3_t {\n" + " int i, *j, &k;\n" + "} idx3;", + statementType::declaration + ); + + setTypedefType(); + + ASSERT_EQ("idx3", + typedefType->name()); + + ASSERT_EQ("idx3_t", + typedefType->baseType.name()); + + // Test typedef anonymous union + setStatement( + "typedef union {\n" + " int i, *j, &k;\n" + "} idx3;", + statementType::declaration + ); + + setTypedefType(); + + ASSERT_EQ("idx3", + typedefType->name()); + + ASSERT_EQ(0, + (int) typedefType->baseType.name().size()); + +#undef declSmnt +#undef getDeclType +#undef getUnionType +#undef getTypedefType } void testEnumLoading() { diff --git a/tests/src/internal/lang/parser/typeLoading.cpp b/tests/src/internal/lang/parser/typeLoading.cpp index 51b886503..9a421d398 100644 --- a/tests/src/internal/lang/parser/typeLoading.cpp +++ b/tests/src/internal/lang/parser/typeLoading.cpp @@ -9,6 +9,7 @@ void testArgumentLoading(); void testFunctionPointerLoading(); void testStructLoading(); void testEnumLoading(); +void testUnionLoading(); void testBaseTypeErrors(); void testPointerTypeErrors(); @@ -28,6 +29,7 @@ int main(const int argc, const char **argv) { testStructLoading(); testEnumLoading(); + testUnionLoading(); std::cerr << "\n---[ Testing type errors ]----------------------\n\n"; testBaseTypeErrors(); @@ -361,6 +363,30 @@ void testEnumLoading() { ASSERT_TRUE(foo4.has(enum_)); } +void testUnionLoading() { + vartype_t type; + + type = loadType("union foo1 {}"); + ASSERT_EQ("foo1", type.name()); + ASSERT_TRUE(type.has(union_)); + + type = loadType("union foo2 {} bar2"); + ASSERT_EQ("foo2", type.name()); + ASSERT_TRUE(type.has(union_)); + + type = loadType("union {} bar3"); + ASSERT_EQ(0, (int) type.name().size()); + ASSERT_TRUE(type.has(union_)); + + type = loadType("typedef union foo4 {} bar4"); + ASSERT_EQ("bar4", type.name()); + ASSERT_TRUE(type.has(typedef_)); + + vartype_t foo4 = ((typedef_t*) type.type)->baseType; + ASSERT_EQ("foo4", foo4.name()); + ASSERT_TRUE(foo4.has(union_)); +} + void testBaseTypeErrors() { vartype_t type; type = loadType("const");