GCC Code Coverage Report


Directory: ./
File: openvdb_ax/openvdb_ax/test/frontend/TestDeclareLocalNode.cc
Date: 2022-07-25 17:40:05
Exec Total Coverage
Lines: 17 23 73.9%
Functions: 4 4 100.0%
Branches: 76 178 42.7%

Line Branch Exec Source
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
4 #include <openvdb_ax/ast/AST.h>
5 #include <openvdb_ax/ast/Scanners.h>
6 #include <openvdb_ax/ast/PrintTree.h>
7 #include <openvdb_ax/Exceptions.h>
8
9 #include "../util.h"
10
11 #include <cppunit/extensions/HelperMacros.h>
12
13 #include <string>
14
15 using namespace openvdb::ax::ast;
16 using namespace openvdb::ax::ast::tokens;
17
18 namespace {
19
20 static const unittest_util::CodeTests tests =
21 {
22 { "bool a_;", Node::Ptr(new DeclareLocal(CoreType::BOOL, new Local("a_"))) },
23 { "int32 _;", Node::Ptr(new DeclareLocal(CoreType::INT32, new Local("_"))) },
24 { "int64 aa;", Node::Ptr(new DeclareLocal(CoreType::INT64, new Local("aa"))) },
25 { "float A;", Node::Ptr(new DeclareLocal(CoreType::FLOAT, new Local("A"))) },
26 { "double _A;", Node::Ptr(new DeclareLocal(CoreType::DOUBLE, new Local("_A"))) },
27 { "vec2i a1;", Node::Ptr(new DeclareLocal(CoreType::VEC2I, new Local("a1"))) },
28 { "vec2f _1;", Node::Ptr(new DeclareLocal(CoreType::VEC2F, new Local("_1"))) },
29 { "vec2d abc;", Node::Ptr(new DeclareLocal(CoreType::VEC2D, new Local("abc"))) },
30 { "vec3i a1;", Node::Ptr(new DeclareLocal(CoreType::VEC3I, new Local("a1"))) },
31 { "vec3f _1;", Node::Ptr(new DeclareLocal(CoreType::VEC3F, new Local("_1"))) },
32 { "vec3d abc;", Node::Ptr(new DeclareLocal(CoreType::VEC3D, new Local("abc"))) },
33 { "vec4i a1;", Node::Ptr(new DeclareLocal(CoreType::VEC4I, new Local("a1"))) },
34 { "vec4f _1;", Node::Ptr(new DeclareLocal(CoreType::VEC4F, new Local("_1"))) },
35 { "vec4d abc;", Node::Ptr(new DeclareLocal(CoreType::VEC4D, new Local("abc"))) },
36 { "mat3f _1;", Node::Ptr(new DeclareLocal(CoreType::MAT3F, new Local("_1"))) },
37 { "mat3d abc;", Node::Ptr(new DeclareLocal(CoreType::MAT3D, new Local("abc"))) },
38 { "mat4f _1;", Node::Ptr(new DeclareLocal(CoreType::MAT4F, new Local("_1"))) },
39 { "mat4d abc;", Node::Ptr(new DeclareLocal(CoreType::MAT4D, new Local("abc"))) },
40 { "string D1f;", Node::Ptr(new DeclareLocal(CoreType::STRING, new Local("D1f"))) },
41 { "float a = 1.0f;", Node::Ptr(new DeclareLocal(CoreType::FLOAT, new Local("a"), new Value<float>(1.0f))) },
42 { "float a = 1;", Node::Ptr(new DeclareLocal(CoreType::FLOAT, new Local("a"), new Value<int32_t>(1))) },
43 { "float a = a + 1;", Node::Ptr(new DeclareLocal(CoreType::FLOAT, new Local("a"),
44 new BinaryOperator(new Local("a"), new Value<int32_t>(1), OperatorToken::PLUS)))
45 },
46 { "float a = v.x;", Node::Ptr(new DeclareLocal(CoreType::FLOAT, new Local("a"),
47 new ArrayUnpack(new Local("v"), new Value<int32_t>(0))))
48 },
49 { "vec3f v = {1, 2, 3};", Node::Ptr(new DeclareLocal(CoreType::VEC3F, new Local("v"),
50 new ArrayPack({
51 new Value<int32_t>(1),
52 new Value<int32_t>(2),
53 new Value<int32_t>(3),
54 })))
55 },
56 { "mat3f m = 1;", Node::Ptr(new DeclareLocal(CoreType::MAT3F,
57 new Local("m"),
58 new Value<int32_t>(1)))
59 },
60 { "string s = \"foo\";", Node::Ptr(new DeclareLocal(CoreType::STRING,
61 new Local("s"),
62 new Value<std::string>("foo")))
63 },
64 { "float a = b = c;", Node::Ptr(new DeclareLocal(CoreType::FLOAT,
65 new Local("a"),
66 new AssignExpression(new Local("b"), new Local("c"))))
67 },
68 };
69
70 }
71
72
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
2 class TestDeclareLocalNode : public CppUnit::TestCase
73 {
74 public:
75
76
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
4 CPPUNIT_TEST_SUITE(TestDeclareLocalNode);
77
5/10
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
6 CPPUNIT_TEST(testSyntax);
78
5/10
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
6 CPPUNIT_TEST(testASTNode);
79
4/8
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
4 CPPUNIT_TEST_SUITE_END();
80
81
20/40
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 27 times.
✓ Branch 8 taken 1 times.
✓ Branch 10 taken 27 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 27 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 27 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 27 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 27 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 27 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 27 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 27 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 27 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 27 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 27 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 27 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 27 times.
✗ Branch 47 not taken.
✓ Branch 48 taken 27 times.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✓ Branch 51 taken 27 times.
✓ Branch 53 taken 27 times.
✗ Branch 54 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
84 void testSyntax() { TEST_SYNTAX_PASSES(tests); }
82 void testASTNode();
83 };
84
85 CPPUNIT_TEST_SUITE_REGISTRATION(TestDeclareLocalNode);
86
87 1 void TestDeclareLocalNode::testASTNode()
88 {
89
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1 times.
28 for (const auto& test : tests) {
90
1/2
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
27 const std::string& code = test.first;
91 const Node* expected = test.second.get();
92
1/2
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
27 const Tree::ConstPtr tree = parse(code.c_str());
93
11/22
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 27 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 27 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 27 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 27 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 27 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 27 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 27 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 27 times.
✗ Branch 29 not taken.
✓ Branch 41 taken 27 times.
✗ Branch 42 not taken.
81 CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("No AST returned", code), static_cast<bool>(tree));
94
95 // get the first statement
96 const Node* result = tree->child(0)->child(0);
97
6/12
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 27 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 27 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 27 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 27 times.
✗ Branch 17 not taken.
27 CPPUNIT_ASSERT(result);
98
12/24
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 27 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 27 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 27 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 27 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 27 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 27 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 27 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 27 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 27 times.
✗ Branch 32 not taken.
✓ Branch 45 taken 27 times.
✗ Branch 46 not taken.
81 CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("Invalid AST node", code),
99 Node::DeclareLocalNode == result->nodetype());
100
101 std::vector<const Node*> resultList, expectedList;
102
1/2
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
27 linearize(*result, resultList);
103
1/2
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
27 linearize(*expected, expectedList);
104
105
2/4
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
27 if (!unittest_util::compareLinearTrees(expectedList, resultList)) {
106 std::ostringstream os;
107 os << "\nExpected:\n";
108 openvdb::ax::ast::print(*expected, true, os);
109 os << "Result:\n";
110 openvdb::ax::ast::print(*result, true, os);
111 CPPUNIT_FAIL(ERROR_MSG("Mismatching Trees for Declaration code", code) + os.str());
112 }
113 }
114 1 }
115
116
117