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