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 | { "int32 a = (1,2,3), b=1, c=(b=1);", Node::Ptr(new StatementList({ | ||
23 | new DeclareLocal(CoreType::INT32, new Local("a"), | ||
24 | new CommaOperator({ | ||
25 | new Value<int32_t>(1), | ||
26 | new Value<int32_t>(2), | ||
27 | new Value<int32_t>(3), | ||
28 | })), | ||
29 | new DeclareLocal(CoreType::INT32, new Local("b"), new Value<int32_t>(1)), | ||
30 | new DeclareLocal(CoreType::INT32, new Local("c"), | ||
31 | new AssignExpression( | ||
32 | new Local("b"), | ||
33 | new Value<int32_t>(1))), | ||
34 | })) | ||
35 | }, | ||
36 | { "int32 a, b;", Node::Ptr(new StatementList({ | ||
37 | new DeclareLocal(CoreType::INT32, new Local("a")), | ||
38 | new DeclareLocal(CoreType::INT32, new Local("b")) | ||
39 | })) | ||
40 | }, | ||
41 | { "int32 a, b = 1;", Node::Ptr(new StatementList({ | ||
42 | new DeclareLocal(CoreType::INT32, new Local("a")), | ||
43 | new DeclareLocal(CoreType::INT32, new Local("b"), new Value<int32_t>(1)) | ||
44 | })) | ||
45 | }, | ||
46 | { "int32 a, b = 1, c = 1;", Node::Ptr(new StatementList({ | ||
47 | new DeclareLocal(CoreType::INT32, new Local("a")), | ||
48 | new DeclareLocal(CoreType::INT32, new Local("b"), new Value<int32_t>(1)), | ||
49 | new DeclareLocal(CoreType::INT32, new Local("c"), new Value<int32_t>(1)) | ||
50 | })) | ||
51 | }, | ||
52 | { "int32 a, b = 1, c;", Node::Ptr(new StatementList({ | ||
53 | new DeclareLocal(CoreType::INT32, new Local("a")), | ||
54 | new DeclareLocal(CoreType::INT32, new Local("b"), new Value<int32_t>(1)), | ||
55 | new DeclareLocal(CoreType::INT32, new Local("c")) | ||
56 | })) | ||
57 | }, | ||
58 | { "int32 a, b = 1, c, d = 1;", Node::Ptr(new StatementList({ | ||
59 | new DeclareLocal(CoreType::INT32, new Local("a")), | ||
60 | new DeclareLocal(CoreType::INT32, new Local("b"), new Value<int32_t>(1)), | ||
61 | new DeclareLocal(CoreType::INT32, new Local("c")), | ||
62 | new DeclareLocal(CoreType::INT32, new Local("d"), new Value<int32_t>(1)) | ||
63 | })) | ||
64 | } | ||
65 | }; | ||
66 | |||
67 | } | ||
68 | |||
69 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
2 | class TestStatementList : public CppUnit::TestCase |
70 | { | ||
71 | public: | ||
72 | |||
73 |
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(TestStatementList); |
74 |
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); |
75 |
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); |
76 |
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(); |
77 | |||
78 |
20/40✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 6 times.
✓ Branch 8 taken 1 times.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 6 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 6 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 6 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 6 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 6 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 6 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 6 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 6 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 6 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 6 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 6 times.
✗ Branch 44 not taken.
✓ Branch 46 taken 6 times.
✗ Branch 47 not taken.
✓ Branch 48 taken 6 times.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✓ Branch 51 taken 6 times.
✓ Branch 53 taken 6 times.
✗ Branch 54 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
|
21 | void testSyntax() { TEST_SYNTAX_PASSES(tests); } |
79 | void testASTNode(); | ||
80 | }; | ||
81 | |||
82 | CPPUNIT_TEST_SUITE_REGISTRATION(TestStatementList); | ||
83 | |||
84 | 1 | void TestStatementList::testASTNode() | |
85 | { | ||
86 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | for (const auto& test : tests) { |
87 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | const std::string& code = test.first; |
88 | const Node* expected = test.second.get(); | ||
89 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | const Tree::ConstPtr tree = parse(code.c_str()); |
90 |
11/22✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 6 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 6 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 6 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 6 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 6 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 6 times.
✗ Branch 29 not taken.
✓ Branch 41 taken 6 times.
✗ Branch 42 not taken.
|
18 | CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("No AST returned", code), static_cast<bool>(tree)); |
91 | |||
92 | // get the first statement | ||
93 | const Node* result = tree->child(0)->child(0); | ||
94 |
6/12✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 6 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 6 times.
✗ Branch 17 not taken.
|
6 | CPPUNIT_ASSERT(result); |
95 |
12/24✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 6 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 6 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 6 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 6 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 6 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 6 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 6 times.
✗ Branch 32 not taken.
✓ Branch 45 taken 6 times.
✗ Branch 46 not taken.
|
18 | CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("Invalid AST node", code), |
96 | Node::StatementListNode == result->nodetype()); | ||
97 | |||
98 | std::vector<const Node*> resultList, expectedList; | ||
99 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | linearize(*result, resultList); |
100 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | linearize(*expected, expectedList); |
101 | |||
102 |
2/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
|
6 | if (!unittest_util::compareLinearTrees(expectedList, resultList)) { |
103 | ✗ | std::ostringstream os; | |
104 | ✗ | os << "\nExpected:\n"; | |
105 | ✗ | openvdb::ax::ast::print(*expected, true, os); | |
106 | ✗ | os << "Result:\n"; | |
107 | ✗ | openvdb::ax::ast::print(*result, true, os); | |
108 | ✗ | CPPUNIT_FAIL(ERROR_MSG("Mismatching Trees for Statement List code", code) + os.str()); | |
109 | } | ||
110 | } | ||
111 | 1 | } | |
112 | |||
113 |