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