Skip to content

Commit

Permalink
[tests]Add union tests, statement & type loading
Browse files Browse the repository at this point in the history
We add the necessary tests to ensure that (typedef) unions
can be parsed and loaded by OCCA.
  • Loading branch information
kian-ohara authored and kris-rowe committed Sep 12, 2023
1 parent 0c7652c commit 48f1d63
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
79 changes: 77 additions & 2 deletions tests/src/internal/lang/parser/statementLoading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ int main(const int argc, const char **argv) {
testNamespaceLoading();
testStructLoading();
// testClassLoading();
// testUnionLoading();
testEnumLoading();
testUnionLoading();
testFunctionLoading();
testIfLoading();
testForLoading();
Expand Down Expand Up @@ -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<declarationStatement>()
#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() {
Expand Down
26 changes: 26 additions & 0 deletions tests/src/internal/lang/parser/typeLoading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void testArgumentLoading();
void testFunctionPointerLoading();
void testStructLoading();
void testEnumLoading();
void testUnionLoading();

void testBaseTypeErrors();
void testPointerTypeErrors();
Expand All @@ -28,6 +29,7 @@ int main(const int argc, const char **argv) {
testStructLoading();
testEnumLoading();

testUnionLoading();

std::cerr << "\n---[ Testing type errors ]----------------------\n\n";
testBaseTypeErrors();
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 48f1d63

Please sign in to comment.