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 | { "-a;", Node::Ptr(new UnaryOperator(new Local("a"), OperatorToken::MINUS)) }, | ||
23 | { "+a;", Node::Ptr(new UnaryOperator(new Local("a"), OperatorToken::PLUS)) }, | ||
24 | { "!a;", Node::Ptr(new UnaryOperator(new Local("a"), OperatorToken::NOT)) }, | ||
25 | { "~a;", Node::Ptr(new UnaryOperator(new Local("a"), OperatorToken::BITNOT)) }, | ||
26 | { "~~a;", Node::Ptr(new UnaryOperator(new UnaryOperator(new Local("a"), OperatorToken::BITNOT), OperatorToken::BITNOT)) }, | ||
27 | { "!~a;", Node::Ptr(new UnaryOperator(new UnaryOperator(new Local("a"), OperatorToken::BITNOT), OperatorToken::NOT)) }, | ||
28 | { "+-a;", Node::Ptr(new UnaryOperator(new UnaryOperator(new Local("a"), OperatorToken::MINUS), OperatorToken::PLUS)) }, | ||
29 | { "-+a;", Node::Ptr(new UnaryOperator(new UnaryOperator(new Local("a"), OperatorToken::PLUS), OperatorToken::MINUS)) }, | ||
30 | { "!!!a;", Node::Ptr(new UnaryOperator( | ||
31 | new UnaryOperator( | ||
32 | new UnaryOperator(new Local("a"), OperatorToken::NOT), | ||
33 | OperatorToken::NOT | ||
34 | ), | ||
35 | OperatorToken::NOT | ||
36 | )) | ||
37 | }, | ||
38 | { "~~~a;", Node::Ptr(new UnaryOperator( | ||
39 | new UnaryOperator( | ||
40 | new UnaryOperator(new Local("a"), OperatorToken::BITNOT), | ||
41 | OperatorToken::BITNOT | ||
42 | ), | ||
43 | OperatorToken::BITNOT | ||
44 | )) | ||
45 | }, | ||
46 | { "-(a+b);", Node::Ptr(new UnaryOperator( | ||
47 | new BinaryOperator( | ||
48 | new Local("a"), new Local("b"), OperatorToken::PLUS | ||
49 | ), | ||
50 | OperatorToken::MINUS | ||
51 | )) | ||
52 | }, | ||
53 | { "!func();", Node::Ptr(new UnaryOperator(new FunctionCall("func"), OperatorToken::NOT)) }, | ||
54 | { "-@a;", Node::Ptr(new UnaryOperator(new Attribute("a", CoreType::FLOAT, true), OperatorToken::MINUS)) }, | ||
55 | { "!v@a;", Node::Ptr(new UnaryOperator(new Attribute("a", CoreType::VEC3F), OperatorToken::NOT)) }, | ||
56 | { "~v@a;", Node::Ptr(new UnaryOperator(new Attribute("a", CoreType::VEC3F), OperatorToken::BITNOT)) }, | ||
57 | { "+int(a);", Node::Ptr(new UnaryOperator(new Cast(new Local("a"), CoreType::INT32), OperatorToken::PLUS)) }, | ||
58 | { "-(float(a));", Node::Ptr(new UnaryOperator(new Cast(new Local("a"), CoreType::FLOAT), OperatorToken::MINUS)) }, | ||
59 | { "!a.x;", Node::Ptr(new UnaryOperator(new ArrayUnpack(new Local("a"), new Value<int32_t>(0)), OperatorToken::NOT)) }, | ||
60 | { "-a[0];", Node::Ptr(new UnaryOperator(new ArrayUnpack(new Local("a"), new Value<int32_t>(0)), OperatorToken::MINUS)) }, | ||
61 | { "-++a;", Node::Ptr(new UnaryOperator(new Crement(new Local("a"), Crement::Operation::Increment, false), OperatorToken::MINUS)) }, | ||
62 | { "!{a,b,c};", Node::Ptr(new UnaryOperator( | ||
63 | new ArrayPack({ | ||
64 | new Local("a"), | ||
65 | new Local("b"), | ||
66 | new Local("c") | ||
67 | }), | ||
68 | OperatorToken::NOT | ||
69 | )) | ||
70 | }, | ||
71 | { "!(a,b,c);", Node::Ptr(new UnaryOperator( | ||
72 | new CommaOperator({ | ||
73 | new Local("a"), | ||
74 | new Local("b"), | ||
75 | new Local("c") | ||
76 | }), | ||
77 | OperatorToken::NOT | ||
78 | )) | ||
79 | }, | ||
80 | // This is a bit of a weird one - should perhaps look to making this a syntax error | ||
81 | // (it will fail at compilation with an lvalue error) | ||
82 | { "-a=a;", Node::Ptr(new UnaryOperator( | ||
83 | new AssignExpression(new Local("a"), new Local("a")), | ||
84 | OperatorToken::MINUS | ||
85 | )) | ||
86 | } | ||
87 | }; | ||
88 | |||
89 | } | ||
90 | |||
91 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
2 | class TestUnaryOperatorNode : public CppUnit::TestCase |
92 | { | ||
93 | public: | ||
94 | |||
95 |
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(TestUnaryOperatorNode); |
96 |
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); |
97 |
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); |
98 |
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(); |
99 | |||
100 |
20/40✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 23 times.
✓ Branch 8 taken 1 times.
✓ Branch 10 taken 23 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 23 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 23 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 23 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 23 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 23 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 23 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 23 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 23 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 23 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 23 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 23 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 23 times.
✗ Branch 47 not taken.
✓ Branch 48 taken 23 times.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✓ Branch 51 taken 23 times.
✓ Branch 53 taken 23 times.
✗ Branch 54 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
|
72 | void testSyntax() { TEST_SYNTAX_PASSES(tests); } |
101 | void testASTNode(); | ||
102 | }; | ||
103 | |||
104 | CPPUNIT_TEST_SUITE_REGISTRATION(TestUnaryOperatorNode); | ||
105 | |||
106 | 1 | void TestUnaryOperatorNode::testASTNode() | |
107 | { | ||
108 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 1 times.
|
24 | for (const auto& test : tests) { |
109 |
1/2✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
|
23 | const std::string& code = test.first; |
110 | const Node* expected = test.second.get(); | ||
111 |
1/2✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
|
23 | const Tree::ConstPtr tree = parse(code.c_str()); |
112 |
11/22✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 23 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 23 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 23 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 23 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 23 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 23 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 23 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 23 times.
✗ Branch 29 not taken.
✓ Branch 41 taken 23 times.
✗ Branch 42 not taken.
|
69 | CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("No AST returned", code), static_cast<bool>(tree)); |
113 | |||
114 | // get the first statement | ||
115 | const Node* result = tree->child(0)->child(0); | ||
116 |
6/12✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 23 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 23 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 23 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 23 times.
✗ Branch 17 not taken.
|
23 | CPPUNIT_ASSERT(result); |
117 |
12/24✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 23 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 23 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 23 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 23 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 23 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 23 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 23 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 23 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 23 times.
✗ Branch 32 not taken.
✓ Branch 45 taken 23 times.
✗ Branch 46 not taken.
|
69 | CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("Invalid AST node", code), |
118 | Node::UnaryOperatorNode == result->nodetype()); | ||
119 | |||
120 | std::vector<const Node*> resultList, expectedList; | ||
121 |
1/2✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
|
23 | linearize(*result, resultList); |
122 |
1/2✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
|
23 | linearize(*expected, expectedList); |
123 | |||
124 |
2/4✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
|
23 | if (!unittest_util::compareLinearTrees(expectedList, resultList)) { |
125 | ✗ | std::ostringstream os; | |
126 | ✗ | os << "\nExpected:\n"; | |
127 | ✗ | openvdb::ax::ast::print(*expected, true, os); | |
128 | ✗ | os << "Result:\n"; | |
129 | ✗ | openvdb::ax::ast::print(*result, true, os); | |
130 | ✗ | CPPUNIT_FAIL(ERROR_MSG("Mismatching Trees for Unary Operator code", code) + os.str()); | |
131 | } | ||
132 | } | ||
133 | 1 | } | |
134 | |||
135 |