Skip to content

Commit 48f1d63

Browse files
kian-oharakris-rowe
authored andcommitted
[tests]Add union tests, statement & type loading
We add the necessary tests to ensure that (typedef) unions can be parsed and loaded by OCCA.
1 parent 0c7652c commit 48f1d63

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

tests/src/internal/lang/parser/statementLoading.cpp

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ int main(const int argc, const char **argv) {
3030
testNamespaceLoading();
3131
testStructLoading();
3232
// testClassLoading();
33-
// testUnionLoading();
3433
testEnumLoading();
34+
testUnionLoading();
3535
testFunctionLoading();
3636
testIfLoading();
3737
testForLoading();
@@ -283,7 +283,82 @@ void testClassLoading() {
283283
}
284284

285285
void testUnionLoading() {
286-
// TODO: Add union tests
286+
statement_t *statement = NULL;
287+
union_t *unionType = NULL;
288+
typedef_t *typedefType = NULL;
289+
290+
#define declSmnt statement->to<declarationStatement>()
291+
#define getDeclType declSmnt.declarations[0].variable().vartype.type
292+
#define setUnionType() unionType = (union_t*) getDeclType
293+
#define setTypedefType() typedefType = (typedef_t*) getDeclType
294+
295+
// Test default union
296+
setStatement(
297+
"union idx3 {\n"
298+
" int i, *j, &k;\n"
299+
"};",
300+
statementType::declaration
301+
);
302+
303+
setUnionType();
304+
305+
ASSERT_EQ("idx3",
306+
unionType->name());
307+
308+
ASSERT_EQ(3,
309+
(int) unionType->fields.size());
310+
311+
ASSERT_EQ("i",
312+
unionType->fields[0].name());
313+
ASSERT_EQ(&int_,
314+
unionType->fields[0].vartype.type);
315+
316+
ASSERT_EQ("j",
317+
unionType->fields[1].name());
318+
ASSERT_EQ(&int_,
319+
unionType->fields[1].vartype.type);
320+
321+
ASSERT_EQ("k",
322+
unionType->fields[2].name());
323+
ASSERT_EQ(&int_,
324+
unionType->fields[2].vartype.type);
325+
326+
// Test default typedef union
327+
setStatement(
328+
"typedef union idx3_t {\n"
329+
" int i, *j, &k;\n"
330+
"} idx3;",
331+
statementType::declaration
332+
);
333+
334+
setTypedefType();
335+
336+
ASSERT_EQ("idx3",
337+
typedefType->name());
338+
339+
ASSERT_EQ("idx3_t",
340+
typedefType->baseType.name());
341+
342+
// Test typedef anonymous union
343+
setStatement(
344+
"typedef union {\n"
345+
" int i, *j, &k;\n"
346+
"} idx3;",
347+
statementType::declaration
348+
);
349+
350+
setTypedefType();
351+
352+
ASSERT_EQ("idx3",
353+
typedefType->name());
354+
355+
ASSERT_EQ(0,
356+
(int) typedefType->baseType.name().size());
357+
358+
#undef declSmnt
359+
#undef getDeclType
360+
#undef getUnionType
361+
#undef getTypedefType
287362
}
288363

289364
void testEnumLoading() {

tests/src/internal/lang/parser/typeLoading.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ void testArgumentLoading();
99
void testFunctionPointerLoading();
1010
void testStructLoading();
1111
void testEnumLoading();
12+
void testUnionLoading();
1213

1314
void testBaseTypeErrors();
1415
void testPointerTypeErrors();
@@ -28,6 +29,7 @@ int main(const int argc, const char **argv) {
2829
testStructLoading();
2930
testEnumLoading();
3031

32+
testUnionLoading();
3133

3234
std::cerr << "\n---[ Testing type errors ]----------------------\n\n";
3335
testBaseTypeErrors();
@@ -361,6 +363,30 @@ void testEnumLoading() {
361363
ASSERT_TRUE(foo4.has(enum_));
362364
}
363365

366+
void testUnionLoading() {
367+
vartype_t type;
368+
369+
type = loadType("union foo1 {}");
370+
ASSERT_EQ("foo1", type.name());
371+
ASSERT_TRUE(type.has(union_));
372+
373+
type = loadType("union foo2 {} bar2");
374+
ASSERT_EQ("foo2", type.name());
375+
ASSERT_TRUE(type.has(union_));
376+
377+
type = loadType("union {} bar3");
378+
ASSERT_EQ(0, (int) type.name().size());
379+
ASSERT_TRUE(type.has(union_));
380+
381+
type = loadType("typedef union foo4 {} bar4");
382+
ASSERT_EQ("bar4", type.name());
383+
ASSERT_TRUE(type.has(typedef_));
384+
385+
vartype_t foo4 = ((typedef_t*) type.type)->baseType;
386+
ASSERT_EQ("foo4", foo4.name());
387+
ASSERT_TRUE(foo4.has(union_));
388+
}
389+
364390
void testBaseTypeErrors() {
365391
vartype_t type;
366392
type = loadType("const");

0 commit comments

Comments
 (0)