GCC Code Coverage Report


Directory: ./
File: openvdb_ax/openvdb_ax/test/frontend/TestAssignExpressionNode.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 unittest_util::CodeTests tests =
21 {
22 // test an attribute type passes for all expression types
23 { "@a = (true);", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::FLOAT, true), new Value<bool>(true))) },
24 { "@a = (1,2,3);", Node::Ptr(new AssignExpression(
25 new Attribute("a", CoreType::FLOAT, true),
26 new CommaOperator({
27 new Value<int32_t>(1),
28 new Value<int32_t>(2),
29 new Value<int32_t>(3),
30 })
31 ))
32 },
33 { "@a = test();", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::FLOAT, true), new FunctionCall("test"))) },
34 { "@a = 1 + i@b;", Node::Ptr(new AssignExpression(
35 new Attribute("a", CoreType::FLOAT, true),
36 new BinaryOperator(new Value<int32_t>(1), new Attribute("b", CoreType::INT32), OperatorToken::PLUS)
37 ))
38 },
39 { "@a = -int@b;", Node::Ptr(new AssignExpression(
40 new Attribute("a", CoreType::FLOAT, true),
41 new UnaryOperator(new Attribute("b", CoreType::INT32), OperatorToken::MINUS)
42 ))
43 },
44 { "@a = ++float@b;", Node::Ptr(new AssignExpression(
45 new Attribute("a", CoreType::FLOAT, true),
46 new Crement(new Attribute("b", CoreType::FLOAT), Crement::Operation::Increment, false)
47 ))
48 },
49 { "@a = bool(2);", Node::Ptr(new AssignExpression(
50 new Attribute("a", CoreType::FLOAT, true),
51 new Cast(new Value<int32_t>(2), CoreType::BOOL)
52 ))
53 },
54 { "@a = {1, 2, 3};", Node::Ptr(new AssignExpression(
55 new Attribute("a", CoreType::FLOAT, true),
56 new ArrayPack({
57 new Value<int32_t>(1),
58 new Value<int32_t>(2),
59 new Value<int32_t>(3)
60 })
61 ))
62 },
63 { "@a = v@b.x;", Node::Ptr(new AssignExpression(
64 new Attribute("a", CoreType::FLOAT, true),
65 new ArrayUnpack(new Attribute("b", CoreType::VEC3F), new Value<int32_t>(0))
66 ))
67 },
68 { "@a = \"b\";", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::FLOAT, true), new Value<std::string>("b"))) },
69 { "@a = b;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::FLOAT, true), new Local("b"))) },
70
71 // test all attribute
72 { "bool@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::BOOL), new Value<bool>(true))) },
73 { "int16@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::INT16), new Value<bool>(true))) },
74 { "i@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::INT32), new Value<bool>(true))) },
75 { "int@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::INT32), new Value<bool>(true))) },
76 { "int32@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::INT32), new Value<bool>(true))) },
77 { "int64@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::INT64), new Value<bool>(true))) },
78 { "f@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::FLOAT), new Value<bool>(true))) },
79 { "float@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::FLOAT), new Value<bool>(true))) },
80 { "double@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::DOUBLE), new Value<bool>(true))) },
81 { "vec3i@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::VEC3I), new Value<bool>(true))) },
82 { "v@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::VEC3F), new Value<bool>(true))) },
83 { "vec3f@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::VEC3F), new Value<bool>(true))) },
84 { "vec3d@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::VEC3D), new Value<bool>(true))) },
85 { "s@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::STRING), new Value<bool>(true))) },
86 { "string@a = true;", Node::Ptr(new AssignExpression(new Attribute("a", CoreType::STRING), new Value<bool>(true))) },
87
88 // compound assignments (operation is stored implicitly)
89 { "@a += true;", Node::Ptr(new AssignExpression(
90 new Attribute("a", CoreType::FLOAT, true),
91 new Value<bool>(true),
92 OperatorToken::PLUS
93 ))
94 },
95 { "@a -= true;", Node::Ptr(new AssignExpression(
96 new Attribute("a", CoreType::FLOAT, true),
97 new Value<bool>(true),
98 OperatorToken::MINUS
99 ))
100 },
101 { "@a *= true;", Node::Ptr(new AssignExpression(
102 new Attribute("a", CoreType::FLOAT, true),
103 new Value<bool>(true),
104 OperatorToken::MULTIPLY
105 ))
106 },
107 { "@a /= true;", Node::Ptr(new AssignExpression(
108 new Attribute("a", CoreType::FLOAT, true),
109 new Value<bool>(true),
110 OperatorToken::DIVIDE
111 ))
112 },
113 { "@a &= true;", Node::Ptr(new AssignExpression(
114 new Attribute("a", CoreType::FLOAT, true),
115 new Value<bool>(true),
116 OperatorToken::BITAND
117 ))
118 },
119 { "@a |= true;", Node::Ptr(new AssignExpression(
120 new Attribute("a", CoreType::FLOAT, true),
121 new Value<bool>(true),
122 OperatorToken::BITOR
123 ))
124 },
125 { "@a ^= true;", Node::Ptr(new AssignExpression(
126 new Attribute("a", CoreType::FLOAT, true),
127 new Value<bool>(true),
128 OperatorToken::BITXOR
129 ))
130 },
131 { "@a %= true;", Node::Ptr(new AssignExpression(
132 new Attribute("a", CoreType::FLOAT, true),
133 new Value<bool>(true),
134 OperatorToken::MODULO
135 ))
136 },
137 { "@a <<= true;", Node::Ptr(new AssignExpression(
138 new Attribute("a", CoreType::FLOAT, true),
139 new Value<bool>(true),
140 OperatorToken::SHIFTLEFT
141 ))
142 },
143 { "@a >>= true;", Node::Ptr(new AssignExpression(
144 new Attribute("a", CoreType::FLOAT, true),
145 new Value<bool>(true),
146 OperatorToken::SHIFTRIGHT
147 ))
148 },
149
150 // test component assignment
151 { "vec3i@a.x = true;", Node::Ptr(new AssignExpression(
152 new ArrayUnpack(
153 new Attribute("a", CoreType::VEC3I), new Value<int32_t>(0)
154 ),
155 new Value<bool>(true)
156 ))
157 },
158 { "vec3i@a[1] = true;", Node::Ptr(new AssignExpression(
159 new ArrayUnpack(
160 new Attribute("a", CoreType::VEC3I), new Value<int32_t>(1)
161 ),
162 new Value<bool>(true)
163 ))
164 },
165 { "vec3i@a.b = true;", Node::Ptr(new AssignExpression(
166 new ArrayUnpack(
167 new Attribute("a", CoreType::VEC3I), new Value<int32_t>(2)
168 ),
169 new Value<bool>(true)
170 ))
171 },
172 { "vec3i@a.x += true;", Node::Ptr(new AssignExpression(
173 new ArrayUnpack(
174 new Attribute("a", CoreType::VEC3I), new Value<int32_t>(0)
175 ),
176 new Value<bool>(true),
177 OperatorToken::PLUS
178 ))
179 },
180
181 // test other lhs
182 { "a = true;", Node::Ptr(new AssignExpression(new Local("a"), new Value<bool>(true))) },
183 { "++a = true;", Node::Ptr(new AssignExpression(
184 new Crement(new Local("a"), Crement::Operation::Increment, false),
185 new Value<bool>(true)
186 ))
187 },
188
189 { "++@a = true;", Node::Ptr(new AssignExpression(
190 new Crement(new Attribute("a", CoreType::FLOAT, true), Crement::Operation::Increment, false),
191 new Value<bool>(true)
192 ))
193 },
194
195 // chains
196 { "@a = @b += 1;", Node::Ptr(new AssignExpression(
197 new Attribute("a", CoreType::FLOAT, true),
198 new AssignExpression(
199 new Attribute("b", CoreType::FLOAT, true),
200 new Value<int32_t>(1),
201 OperatorToken::PLUS)
202 ))
203 },
204 { "@a = v@b.x = 1;", Node::Ptr(new AssignExpression(
205 new Attribute("a", CoreType::FLOAT, true),
206 new AssignExpression(
207 new ArrayUnpack(new Attribute("b", CoreType::VEC3F), new Value<int32_t>(0)),
208 new Value<int32_t>(1)
209 )
210 ))
211 },
212 { "@a += v@b.x = x %= 1;", Node::Ptr(new AssignExpression(
213 new Attribute("a", CoreType::FLOAT, true),
214 new AssignExpression(
215 new ArrayUnpack(new Attribute("b", CoreType::VEC3F), new Value<int32_t>(0)),
216 new AssignExpression(
217 new Local("x"),
218 new Value<int32_t>(1),
219 OperatorToken::MODULO
220 )
221 ),
222 OperatorToken::PLUS
223 ))
224 }
225 };
226
227 }
228
229
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
2 class TestAssignExpressionNode : public CppUnit::TestCase
230 {
231 public:
232
233
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(TestAssignExpressionNode);
234
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);
235
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);
236
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();
237
238
20/40
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 46 times.
✓ Branch 8 taken 1 times.
✓ Branch 10 taken 46 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 46 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 46 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 46 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 46 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 46 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 46 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 46 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 46 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 46 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 46 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 46 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 46 times.
✗ Branch 47 not taken.
✓ Branch 48 taken 46 times.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✓ Branch 51 taken 46 times.
✓ Branch 53 taken 46 times.
✗ Branch 54 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
141 void testSyntax() { TEST_SYNTAX_PASSES(tests); }
239 void testASTNode();
240 };
241
242 CPPUNIT_TEST_SUITE_REGISTRATION(TestAssignExpressionNode);
243
244 1 void TestAssignExpressionNode::testASTNode()
245 {
246
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1 times.
47 for (const auto& test : tests) {
247
1/2
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
46 const std::string& code = test.first;
248 const Node* expected = test.second.get();
249
1/2
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
46 const Tree::ConstPtr tree = parse(code.c_str());
250
11/22
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 46 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 46 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 46 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 46 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 46 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 46 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 46 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 46 times.
✗ Branch 29 not taken.
✓ Branch 41 taken 46 times.
✗ Branch 42 not taken.
138 CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("No AST returned", code), static_cast<bool>(tree));
251
252 // get the first statement
253 const Node* result = tree->child(0)->child(0);
254
6/12
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 46 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 46 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 46 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 46 times.
✗ Branch 17 not taken.
46 CPPUNIT_ASSERT(result);
255
12/24
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 46 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 46 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 46 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 46 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 46 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 46 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 46 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 46 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 46 times.
✗ Branch 32 not taken.
✓ Branch 45 taken 46 times.
✗ Branch 46 not taken.
138 CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("Invalid AST node", code),
256 Node::AssignExpressionNode == result->nodetype());
257
258 std::vector<const Node*> resultList, expectedList;
259
1/2
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
46 linearize(*result, resultList);
260
1/2
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
46 linearize(*expected, expectedList);
261
262
2/4
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 46 times.
46 if (!unittest_util::compareLinearTrees(expectedList, resultList)) {
263 std::ostringstream os;
264 os << "\nExpected:\n";
265 openvdb::ax::ast::print(*expected, true, os);
266 os << "Result:\n";
267 openvdb::ax::ast::print(*result, true, os);
268 CPPUNIT_FAIL(ERROR_MSG("Mismatching Trees for Assign Expression code", code) + os.str());
269 }
270 }
271 1 }
272
273