GCC Code Coverage Report


Directory: ./
File: openvdb_ax/openvdb_ax/test/frontend/TestArrayPack.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 { "{1, 2, {1,2,3}};", Node::Ptr(new ArrayPack({
23 new Value<int32_t>(1),
24 new Value<int32_t>(2),
25 new ArrayPack({
26 new Value<int32_t>(1),
27 new Value<int32_t>(2),
28 new Value<int32_t>(3)
29 })
30 }))
31 },
32 { "{1.f,2.0,3l};" , Node::Ptr(new ArrayPack({
33 new Value<float>(1.0f),
34 new Value<double>(2.0),
35 new Value<int64_t>(3)
36 }))
37 },
38 { "({1.f,2.0,3l});" , Node::Ptr(new ArrayPack({
39 new Value<float>(1.0f),
40 new Value<double>(2.0),
41 new Value<int64_t>(3)
42 }))
43 },
44 { "{{a,b,c}, {d,e,f}, {g,h,i}};", Node::Ptr(new ArrayPack({
45 new ArrayPack({
46 new Local("a"),
47 new Local("b"),
48 new Local("c")
49 }),
50 new ArrayPack({
51 new Local("d"),
52 new Local("e"),
53 new Local("f")
54 }),
55 new ArrayPack({
56 new Local("g"),
57 new Local("h"),
58 new Local("i")
59 })
60 }))
61 },
62 { "{(a),b+1,-c};", Node::Ptr(new ArrayPack({
63 new Local("a"),
64 new BinaryOperator(new Local("b"), new Value<int32_t>(1), OperatorToken::PLUS),
65 new UnaryOperator(new Local("c"), OperatorToken::MINUS)
66 }))
67 },
68 { "{@x,++z,true};", Node::Ptr(new ArrayPack({
69 new Attribute("x", CoreType::FLOAT, true),
70 new Crement(new Local("z"), Crement::Operation::Increment, false),
71 new Value<bool>(true)
72 }))
73 },
74 { "{@x,z++,\"bar\"};", Node::Ptr(new ArrayPack({
75 new Attribute("x", CoreType::FLOAT, true),
76 new Crement(new Local("z"), Crement::Operation::Increment, true),
77 new Value<std::string>("bar")
78 }))
79 },
80 { "{float(x),b=c,c.z};", Node::Ptr(new ArrayPack({
81 new Cast(new Local("x"), CoreType::FLOAT),
82 new AssignExpression(new Local("b"), new Local("c")),
83 new ArrayUnpack(new Local("c"), new Value<int32_t>(2))
84 }))
85 },
86 { "{test(),a[0],b[1,2]};", Node::Ptr(new ArrayPack({
87 new FunctionCall("test"),
88 new ArrayUnpack(new Local("a"), new Value<int32_t>(0)),
89 new ArrayUnpack(new Local("b"), new Value<int32_t>(1), new Value<int32_t>(2))
90 }))
91 },
92 { "{(1), (1,2), (1,2,(3))};", Node::Ptr(new ArrayPack({
93 new Value<int32_t>(1),
94 new CommaOperator({ new Value<int32_t>(1), new Value<int32_t>(2) }),
95 new CommaOperator({ new Value<int32_t>(1), new Value<int32_t>(2), new Value<int32_t>(3)
96 })
97 }))
98 },
99 { "{1,2,3,4,5,6,7,8,9};", Node::Ptr(new ArrayPack({
100 new Value<int32_t>(1), new Value<int32_t>(2), new Value<int32_t>(3),
101 new Value<int32_t>(4), new Value<int32_t>(5), new Value<int32_t>(6),
102 new Value<int32_t>(7), new Value<int32_t>(8), new Value<int32_t>(9)
103 }))
104 },
105 { "{ 1, 2, 3, 4, \
106 5, 6, 7, 8, \
107 9,10,11,12, \
108 13,14,15,16 };", Node::Ptr(new ArrayPack({
109 new Value<int32_t>(1), new Value<int32_t>(2), new Value<int32_t>(3), new Value<int32_t>(4),
110 new Value<int32_t>(5), new Value<int32_t>(6), new Value<int32_t>(7), new Value<int32_t>(8),
111 new Value<int32_t>(9), new Value<int32_t>(10), new Value<int32_t>(11), new Value<int32_t>(12),
112 new Value<int32_t>(13), new Value<int32_t>(14), new Value<int32_t>(15), new Value<int32_t>(16)
113 }))
114 },
115 };
116
117 }
118
119
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
2 class TestArrayPack : public CppUnit::TestCase
120 {
121 public:
122
123
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(TestArrayPack);
124
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);
125
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);
126
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();
127
128
20/40
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 12 times.
✓ Branch 8 taken 1 times.
✓ Branch 10 taken 12 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 12 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 12 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 12 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 12 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 12 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 12 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 12 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 12 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 12 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 12 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 12 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 12 times.
✗ Branch 47 not taken.
✓ Branch 48 taken 12 times.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✓ Branch 51 taken 12 times.
✓ Branch 53 taken 12 times.
✗ Branch 54 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
39 void testSyntax() { TEST_SYNTAX_PASSES(tests); }
129 void testASTNode();
130 };
131
132 CPPUNIT_TEST_SUITE_REGISTRATION(TestArrayPack);
133
134 1 void TestArrayPack::testASTNode()
135 {
136
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
13 for (const auto& test : tests) {
137
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 const std::string& code = test.first;
138 const Node* expected = test.second.get();
139
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 const Tree::ConstPtr tree = parse(code.c_str());
140
11/22
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 12 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 12 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 12 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 12 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 12 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 12 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 12 times.
✗ Branch 29 not taken.
✓ Branch 41 taken 12 times.
✗ Branch 42 not taken.
36 CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("No AST returned", code), static_cast<bool>(tree));
141
142 // get the first statement
143 const Node* result = tree->child(0)->child(0);
144
6/12
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 12 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 12 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 12 times.
✗ Branch 17 not taken.
12 CPPUNIT_ASSERT(result);
145
12/24
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 12 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 12 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 12 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 12 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 12 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 12 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 12 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 12 times.
✗ Branch 32 not taken.
✓ Branch 45 taken 12 times.
✗ Branch 46 not taken.
36 CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("Invalid AST node", code),
146 Node::ArrayPackNode == result->nodetype());
147
148 std::vector<const Node*> resultList, expectedList;
149
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 linearize(*result, resultList);
150
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 linearize(*expected, expectedList);
151
152
2/4
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
12 if (!unittest_util::compareLinearTrees(expectedList, resultList)) {
153 std::ostringstream os;
154 os << "\nExpected:\n";
155 openvdb::ax::ast::print(*expected, true, os);
156 os << "Result:\n";
157 openvdb::ax::ast::print(*result, true, os);
158 CPPUNIT_FAIL(ERROR_MSG("Mismatching Trees for Array Pack code", code) + os.str());
159 }
160 }
161 1 }
162
163