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 | { "if ((a));", Node::Ptr(new ConditionalStatement(new Local("a"), new Block())) }, | ||
23 | { "if ((a,b));", Node::Ptr(new ConditionalStatement( | ||
24 | new CommaOperator({ | ||
25 | new Local("a"), | ||
26 | new Local("b") | ||
27 | }), | ||
28 | new Block() | ||
29 | )) | ||
30 | }, | ||
31 | { "if (a);", Node::Ptr(new ConditionalStatement(new Local("a"), new Block())) }, | ||
32 | { "if(a);", Node::Ptr(new ConditionalStatement(new Local("a"), new Block())) }, | ||
33 | { "if (@a);", Node::Ptr(new ConditionalStatement(new Attribute("a", CoreType::FLOAT, true), new Block())) }, | ||
34 | { "if (1.0f);", Node::Ptr(new ConditionalStatement(new Value<float>(1.0f), new Block())) }, | ||
35 | { "if (func());", Node::Ptr(new ConditionalStatement(new FunctionCall("func"), new Block())) }, | ||
36 | { "if (a+b);", Node::Ptr(new ConditionalStatement( | ||
37 | new BinaryOperator( | ||
38 | new Local("a"), | ||
39 | new Local("b"), | ||
40 | OperatorToken::PLUS | ||
41 | ), | ||
42 | new Block()) | ||
43 | ) | ||
44 | }, | ||
45 | { "if (-a);", Node::Ptr(new ConditionalStatement( | ||
46 | new UnaryOperator( | ||
47 | new Local("a"), | ||
48 | OperatorToken::MINUS | ||
49 | ), | ||
50 | new Block()) | ||
51 | ) | ||
52 | }, | ||
53 | { "if (a = 1);", Node::Ptr(new ConditionalStatement( | ||
54 | new AssignExpression( | ||
55 | new Local("a"), | ||
56 | new Value<int32_t>(1) | ||
57 | ), | ||
58 | new Block()) | ||
59 | ) | ||
60 | }, | ||
61 | { "if (a.x);", Node::Ptr(new ConditionalStatement( | ||
62 | new ArrayUnpack( | ||
63 | new Local("a"), | ||
64 | new Value<int32_t>(0) | ||
65 | ), | ||
66 | new Block()) | ||
67 | ) | ||
68 | }, | ||
69 | { "if (a++);", Node::Ptr(new ConditionalStatement( | ||
70 | new Crement( | ||
71 | new Local("a"), | ||
72 | Crement::Operation::Increment, | ||
73 | true | ||
74 | ), | ||
75 | new Block()) | ||
76 | ) | ||
77 | }, | ||
78 | { "if (float(a));", Node::Ptr(new ConditionalStatement( | ||
79 | new Cast( | ||
80 | new Local("a"), | ||
81 | CoreType::FLOAT | ||
82 | ), | ||
83 | new Block()) | ||
84 | ) | ||
85 | }, | ||
86 | { "if ({1.0, 2.0, 3.0});", Node::Ptr(new ConditionalStatement( | ||
87 | new ArrayPack({ | ||
88 | new Value<double>(1.0), | ||
89 | new Value<double>(2.0), | ||
90 | new Value<double>(3.0) | ||
91 | }), | ||
92 | new Block()) | ||
93 | ) | ||
94 | }, | ||
95 | { "if (a, b);", Node::Ptr(new ConditionalStatement( | ||
96 | new CommaOperator({ | ||
97 | new Local("a"), | ||
98 | new Local("b") | ||
99 | }), | ||
100 | new Block())) }, | ||
101 | { "if (a, b, true, c = 1);", Node::Ptr(new ConditionalStatement( | ||
102 | new CommaOperator({ | ||
103 | new Local("a"), | ||
104 | new Local("b"), | ||
105 | new Value<bool>(true), | ||
106 | new AssignExpression( | ||
107 | new Local("c"), | ||
108 | new Value<int32_t>(1) | ||
109 | ), | ||
110 | }), | ||
111 | new Block())) }, | ||
112 | { "if (a) {b;}", Node::Ptr(new ConditionalStatement( | ||
113 | new Local("a"), | ||
114 | new Block(new Local("b")))) | ||
115 | }, | ||
116 | { "if (a); else ;", Node::Ptr(new ConditionalStatement(new Local("a"), new Block(), new Block())) }, | ||
117 | { "if (a) {} else ;", Node::Ptr(new ConditionalStatement(new Local("a"), new Block(), new Block())) }, | ||
118 | { "if (a); else {}", Node::Ptr(new ConditionalStatement(new Local("a"), new Block(), new Block())) }, | ||
119 | { "if (a); else (b);", Node::Ptr(new ConditionalStatement( | ||
120 | new Local("a"), | ||
121 | new Block(), | ||
122 | new Block(new Local("b")))) | ||
123 | }, | ||
124 | { "if (a); else {};", Node::Ptr(new ConditionalStatement(new Local("a"), new Block(), new Block())) }, | ||
125 | { "if (a) {} else {}", Node::Ptr(new ConditionalStatement(new Local("a"), new Block(), new Block())) }, | ||
126 | { "if (a) b = 1; else {}", Node::Ptr(new ConditionalStatement(new Local("a"), | ||
127 | new Block( | ||
128 | new AssignExpression( | ||
129 | new Local("b"), | ||
130 | new Value<int32_t>(1) | ||
131 | ) | ||
132 | ), | ||
133 | new Block())) | ||
134 | }, | ||
135 | |||
136 | { "if (a) {b = 1;} else {}", Node::Ptr(new ConditionalStatement(new Local("a"), | ||
137 | new Block( | ||
138 | new AssignExpression( | ||
139 | new Local("b"), | ||
140 | new Value<int32_t>(1) | ||
141 | ) | ||
142 | ), | ||
143 | new Block())) | ||
144 | }, | ||
145 | { "if (a); else if(b) ;", Node::Ptr(new ConditionalStatement( | ||
146 | new Local("a"), | ||
147 | new Block(), | ||
148 | new Block( | ||
149 | new ConditionalStatement( | ||
150 | new Local("b"), | ||
151 | new Block() | ||
152 | ) | ||
153 | ) | ||
154 | )) | ||
155 | }, | ||
156 | { "if (a); else if((a,b)) ;", Node::Ptr(new ConditionalStatement( | ||
157 | new Local("a"), | ||
158 | new Block(), | ||
159 | new Block( | ||
160 | new ConditionalStatement( | ||
161 | new CommaOperator({ | ||
162 | new Local("a"), | ||
163 | new Local("b") | ||
164 | }), | ||
165 | new Block() | ||
166 | ) | ||
167 | ) | ||
168 | )) | ||
169 | }, | ||
170 | { "if (a) if(b) ; else ;", Node::Ptr(new ConditionalStatement( | ||
171 | new Local("a"), | ||
172 | new Block( | ||
173 | new ConditionalStatement( | ||
174 | new Local("b"), | ||
175 | new Block(), | ||
176 | new Block() | ||
177 | ) | ||
178 | ) | ||
179 | )) | ||
180 | }, | ||
181 | { "if (a) if(b) {} else {} else ;", Node::Ptr(new ConditionalStatement( | ||
182 | new Local("a"), | ||
183 | new Block( | ||
184 | new ConditionalStatement( | ||
185 | new Local("b"), | ||
186 | new Block(), | ||
187 | new Block() | ||
188 | ) | ||
189 | ), | ||
190 | new Block() | ||
191 | )) | ||
192 | }, | ||
193 | { "if (a) if(b) {if (c) ; else ;} else {} else ;", Node::Ptr(new ConditionalStatement( | ||
194 | new Local("a"), | ||
195 | new Block( | ||
196 | new ConditionalStatement( | ||
197 | new Local("b"), | ||
198 | new Block( | ||
199 | new ConditionalStatement( | ||
200 | new Local("c"), | ||
201 | new Block(), | ||
202 | new Block() | ||
203 | ) | ||
204 | ), | ||
205 | new Block() | ||
206 | ) | ||
207 | ), | ||
208 | new Block() | ||
209 | )) | ||
210 | }, | ||
211 | { "if (a) if(b) {if (c) ;} else {}", Node::Ptr(new ConditionalStatement( | ||
212 | new Local("a"), | ||
213 | new Block( | ||
214 | new ConditionalStatement( | ||
215 | new Local("b"), | ||
216 | new Block( | ||
217 | new ConditionalStatement( | ||
218 | new Local("c"), | ||
219 | new Block() | ||
220 | ) | ||
221 | ), | ||
222 | new Block() | ||
223 | ) | ||
224 | ) | ||
225 | )) | ||
226 | }, | ||
227 | { "if (a) {} else if(b) {if (c) ;} else {}", Node::Ptr(new ConditionalStatement( | ||
228 | new Local("a"), | ||
229 | new Block(), | ||
230 | new Block( | ||
231 | new ConditionalStatement( | ||
232 | new Local("b"), | ||
233 | new Block( | ||
234 | new ConditionalStatement( | ||
235 | new Local("c"), | ||
236 | new Block() | ||
237 | ) | ||
238 | ), | ||
239 | new Block() | ||
240 | ) | ||
241 | ) | ||
242 | )) | ||
243 | }, | ||
244 | { "if (a) { a,a; }", Node::Ptr(new ConditionalStatement(new Local("a"), | ||
245 | new Block( | ||
246 | new CommaOperator({ new Local("a"), new Local("a") }) | ||
247 | ))) | ||
248 | }, | ||
249 | { "if (a) { a,a };", Node::Ptr(new ConditionalStatement(new Local("a"), | ||
250 | new Block( | ||
251 | new ArrayPack({ new Local("a"), new Local("a") }) | ||
252 | ))) | ||
253 | }, | ||
254 | { "if (a) { a,a }; else { a,a; }", Node::Ptr(new ConditionalStatement(new Local("a"), | ||
255 | new Block( | ||
256 | new ArrayPack({ new Local("a"), new Local("a") }) | ||
257 | ), | ||
258 | new Block( | ||
259 | new CommaOperator({ new Local("a"), new Local("a") }) | ||
260 | ))) | ||
261 | }, | ||
262 | { "if (a) { a,a }; else { a,a };", Node::Ptr(new ConditionalStatement(new Local("a"), | ||
263 | new Block( | ||
264 | new ArrayPack({ new Local("a"), new Local("a") }) | ||
265 | ), | ||
266 | new Block( | ||
267 | new ArrayPack({ new Local("a"), new Local("a") }) | ||
268 | ) | ||
269 | )) | ||
270 | }, | ||
271 | { "if (a) { { a,a; } }", Node::Ptr(new ConditionalStatement(new Local("a"), | ||
272 | new Block( | ||
273 | new Block( | ||
274 | new CommaOperator({ new Local("a"), new Local("a") }) | ||
275 | ) | ||
276 | ) | ||
277 | )) | ||
278 | }, | ||
279 | { "if (a) { { a,a }; };", Node::Ptr(new ConditionalStatement(new Local("a"), | ||
280 | new Block( | ||
281 | new ArrayPack({ new Local("a"), new Local("a") }) | ||
282 | ) | ||
283 | )) | ||
284 | } | ||
285 | }; | ||
286 | |||
287 | } | ||
288 | |||
289 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
2 | class TestConditionalStatementNode : public CppUnit::TestCase |
290 | { | ||
291 | public: | ||
292 | |||
293 |
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(TestConditionalStatementNode); |
294 |
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); |
295 |
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); |
296 |
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(); |
297 | |||
298 |
20/40✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 38 times.
✓ Branch 8 taken 1 times.
✓ Branch 10 taken 38 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 38 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 38 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 38 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 38 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 38 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 38 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 38 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 38 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 38 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 38 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 38 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 38 times.
✗ Branch 47 not taken.
✓ Branch 48 taken 38 times.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✓ Branch 51 taken 38 times.
✓ Branch 53 taken 38 times.
✗ Branch 54 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
|
117 | void testSyntax() { TEST_SYNTAX_PASSES(tests); } |
299 | void testASTNode(); | ||
300 | }; | ||
301 | |||
302 | CPPUNIT_TEST_SUITE_REGISTRATION(TestConditionalStatementNode); | ||
303 | |||
304 | 1 | void TestConditionalStatementNode::testASTNode() | |
305 | { | ||
306 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 1 times.
|
39 | for (const auto& test : tests) { |
307 |
1/2✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
|
38 | const std::string& code = test.first; |
308 | const Node* expected = test.second.get(); | ||
309 |
1/2✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
|
38 | const Tree::ConstPtr tree = parse(code.c_str()); |
310 |
11/22✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 38 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 38 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 38 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 38 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 38 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 38 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 38 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 38 times.
✗ Branch 29 not taken.
✓ Branch 41 taken 38 times.
✗ Branch 42 not taken.
|
114 | CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("No AST returned", code), static_cast<bool>(tree)); |
311 | |||
312 | // get the first statement | ||
313 | const Node* result = tree->child(0)->child(0); | ||
314 |
6/12✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 38 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 38 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 38 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 38 times.
✗ Branch 17 not taken.
|
38 | CPPUNIT_ASSERT(result); |
315 |
12/24✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 38 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 38 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 38 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 38 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 38 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 38 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 38 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 38 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 38 times.
✗ Branch 32 not taken.
✓ Branch 45 taken 38 times.
✗ Branch 46 not taken.
|
114 | CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("Invalid AST node", code), |
316 | Node::ConditionalStatementNode == result->nodetype()); | ||
317 | |||
318 | std::vector<const Node*> resultList, expectedList; | ||
319 |
1/2✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
|
38 | linearize(*result, resultList); |
320 |
1/2✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
|
38 | linearize(*expected, expectedList); |
321 | |||
322 |
2/4✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
|
38 | if (!unittest_util::compareLinearTrees(expectedList, resultList)) { |
323 | ✗ | std::ostringstream os; | |
324 | ✗ | os << "\nExpected:\n"; | |
325 | ✗ | openvdb::ax::ast::print(*expected, true, os); | |
326 | ✗ | os << "Result:\n"; | |
327 | ✗ | openvdb::ax::ast::print(*result, true, os); | |
328 | ✗ | CPPUNIT_FAIL(ERROR_MSG("Mismatching Trees for Conditional Statement code", code) + os.str()); | |
329 | } | ||
330 | } | ||
331 | 1 | } | |
332 | |||
333 |