GCC Code Coverage Report


Directory: ./
File: openvdb_ax/openvdb_ax/test/util.h
Date: 2022-07-25 17:40:05
Exec Total Coverage
Lines: 72 74 97.3%
Functions: 3 3 100.0%
Branches: 90 144 62.5%

Line Branch Exec Source
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
4 /// @file test/util.h
5 ///
6 /// @author Nick Avramoussis
7 ///
8 /// @brief Test utilities
9
10 #ifndef OPENVDB_AX_UNITTEST_UTIL_HAS_BEEN_INCLUDED
11 #define OPENVDB_AX_UNITTEST_UTIL_HAS_BEEN_INCLUDED
12
13 #include <openvdb_ax/ast/AST.h>
14 #include <openvdb_ax/ast/PrintTree.h>
15 #include <openvdb_ax/ast/Parse.h>
16 #include <openvdb_ax/ast/Tokens.h>
17 #include <openvdb_ax/compiler/Logger.h>
18
19 #include <openvdb/Types.h>
20
21 #include <memory>
22 #include <vector>
23 #include <utility>
24 #include <string>
25 #include <type_traits>
26 #include <map>
27
28 #define ERROR_MSG(Msg, Code) Msg + std::string(": \"") + Code + std::string("\"")
29
30 #define TEST_SYNTAX_PASSES(Tests) \
31 { \
32 openvdb::ax::Logger logger;\
33 for (const auto& test : Tests) { \
34 logger.clear();\
35 const std::string& code = test.first; \
36 openvdb::ax::ast::Tree::ConstPtr tree = openvdb::ax::ast::parse(code.c_str(), logger);\
37 std::stringstream str; \
38 CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("Unexpected parsing error(s)\n", str.str()), tree && !logger.hasError()); \
39 } \
40 } \
41
42 #define TEST_SYNTAX_FAILS(Tests) \
43 { \
44 openvdb::ax::Logger logger([](const std::string&) {});\
45 for (const auto& test : Tests) { \
46 logger.clear();\
47 const std::string& code = test.first; \
48 openvdb::ax::ast::Tree::ConstPtr tree = openvdb::ax::ast::parse(code.c_str(), logger);\
49 CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("Expected parsing error", code), !tree && logger.hasError()); \
50 } \
51 } \
52
53 namespace unittest_util
54 {
55 // Use shared pointers rather than unique pointers so initializer lists can easily
56 // be used. Could easily introduce some move semantics to work around this if
57 // necessary.
58 using CodeTests = std::vector<std::pair<std::string, openvdb::ax::ast::Node::Ptr>>;
59 // Ordered map for consistency across platforms
60 using ConfigMap = std::map<std::string, std::map<std::string, std::string>>;
61
62 //
63
64 // Find + Replace all string helper
65 1314 inline void replace(std::string& str, const std::string& oldStr, const std::string& newStr)
66 {
67 std::string::size_type pos = 0u;
68
2/2
✓ Branch 0 taken 2639 times.
✓ Branch 1 taken 1314 times.
3953 while ((pos = str.find(oldStr, pos)) != std::string::npos) {
69 str.replace(pos, oldStr.length(), newStr);
70 2639 pos += newStr.length();
71 }
72 1314 }
73
74 //
75
76 465 inline bool compareLinearTrees(const std::vector<const openvdb::ax::ast::Node*>& a,
77 const std::vector<const openvdb::ax::ast::Node*>& b, const bool allowEmpty = false)
78 {
79
3/6
✓ Branch 0 taken 465 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 465 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 465 times.
✗ Branch 5 not taken.
465 if (!allowEmpty && (a.empty() || b.empty())) return false;
80
1/2
✓ Branch 0 taken 465 times.
✗ Branch 1 not taken.
465 if (a.size() != b.size()) return false;
81 const size_t size = a.size();
82
2/2
✓ Branch 0 taken 1794 times.
✓ Branch 1 taken 465 times.
2259 for (size_t i = 0; i < size; ++i) {
83
1/2
✓ Branch 0 taken 1794 times.
✗ Branch 1 not taken.
1794 if ((a[i] == nullptr) ^ (b[i] == nullptr)) return false;
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1794 times.
1794 if (a[i] == nullptr) continue;
85
1/2
✓ Branch 2 taken 1794 times.
✗ Branch 3 not taken.
1794 if (a[i]->nodetype() != b[i]->nodetype()) return false;
86
87 // Specific handling of various node types to compare child data
88 // @todo generalize this
89 // @note Value methods does not compare child text data
90
91
2/2
✓ Branch 1 taken 69 times.
✓ Branch 2 taken 1725 times.
1794 if (a[i]->nodetype() == openvdb::ax::ast::Node::AssignExpressionNode) {
92
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 if (static_cast<const openvdb::ax::ast::AssignExpression*>(a[i])->operation() !=
93
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 static_cast<const openvdb::ax::ast::AssignExpression*>(b[i])->operation()) {
94 return false;
95 }
96 }
97
2/2
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 1653 times.
1725 else if (a[i]->nodetype() == openvdb::ax::ast::Node::BinaryOperatorNode) {
98
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 if (static_cast<const openvdb::ax::ast::BinaryOperator*>(a[i])->operation() !=
99
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 static_cast<const openvdb::ax::ast::BinaryOperator*>(b[i])->operation()) {
100 return false;
101 }
102 }
103
2/2
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 1596 times.
1653 else if (a[i]->nodetype() == openvdb::ax::ast::Node::CrementNode) {
104
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57 if (static_cast<const openvdb::ax::ast::Crement*>(a[i])->operation() !=
105
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57 static_cast<const openvdb::ax::ast::Crement*>(b[i])->operation()) {
106 return false;
107 }
108
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57 if (static_cast<const openvdb::ax::ast::Crement*>(a[i])->post() !=
109 static_cast<const openvdb::ax::ast::Crement*>(b[i])->post()) {
110 return false;
111 }
112 }
113
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 1560 times.
1596 else if (a[i]->nodetype() == openvdb::ax::ast::Node::CastNode) {
114
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (static_cast<const openvdb::ax::ast::Cast*>(a[i])->type() !=
115
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 static_cast<const openvdb::ax::ast::Cast*>(b[i])->type()) {
116 return false;
117 }
118 }
119
2/2
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 1519 times.
1560 else if (a[i]->nodetype() == openvdb::ax::ast::Node::FunctionCallNode) {
120
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 if (static_cast<const openvdb::ax::ast::FunctionCall*>(a[i])->name() !=
121
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 static_cast<const openvdb::ax::ast::FunctionCall*>(b[i])->name()) {
122 return false;
123 }
124 }
125
2/2
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 1477 times.
1519 else if (a[i]->nodetype() == openvdb::ax::ast::Node::LoopNode) {
126
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (static_cast<const openvdb::ax::ast::Loop*>(a[i])->loopType() !=
127
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 static_cast<const openvdb::ax::ast::Loop*>(b[i])->loopType()) {
128 return false;
129 }
130 }
131
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1474 times.
1477 else if (a[i]->nodetype() == openvdb::ax::ast::Node::KeywordNode) {
132
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (static_cast<const openvdb::ax::ast::Keyword*>(a[i])->keyword() !=
133
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 static_cast<const openvdb::ax::ast::Keyword*>(b[i])->keyword()) {
134 return false;
135 }
136 }
137
2/2
✓ Branch 1 taken 121 times.
✓ Branch 2 taken 1353 times.
1474 else if (a[i]->nodetype() == openvdb::ax::ast::Node::AttributeNode) {
138
1/2
✓ Branch 0 taken 121 times.
✗ Branch 1 not taken.
121 if (static_cast<const openvdb::ax::ast::Attribute*>(a[i])->type() !=
139
1/2
✓ Branch 0 taken 121 times.
✗ Branch 1 not taken.
121 static_cast<const openvdb::ax::ast::Attribute*>(b[i])->type()) {
140 return false;
141 }
142
1/2
✓ Branch 0 taken 121 times.
✗ Branch 1 not taken.
121 if (static_cast<const openvdb::ax::ast::Attribute*>(a[i])->name() !=
143 static_cast<const openvdb::ax::ast::Attribute*>(b[i])->name()) {
144 return false;
145 }
146
1/2
✓ Branch 0 taken 121 times.
✗ Branch 1 not taken.
121 if (static_cast<const openvdb::ax::ast::Attribute*>(a[i])->inferred() !=
147 static_cast<const openvdb::ax::ast::Attribute*>(b[i])->inferred()) {
148 return false;
149 }
150 }
151
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 1336 times.
1353 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ExternalVariableNode) {
152
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (static_cast<const openvdb::ax::ast::ExternalVariable*>(a[i])->type() !=
153
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 static_cast<const openvdb::ax::ast::ExternalVariable*>(b[i])->type()) {
154 return false;
155 }
156
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (static_cast<const openvdb::ax::ast::ExternalVariable*>(a[i])->name() !=
157 static_cast<const openvdb::ax::ast::ExternalVariable*>(b[i])->name()) {
158 return false;
159 }
160 }
161
2/2
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 1278 times.
1336 else if (a[i]->nodetype() == openvdb::ax::ast::Node::DeclareLocalNode) {
162
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 if (static_cast<const openvdb::ax::ast::DeclareLocal*>(a[i])->type() !=
163
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 static_cast<const openvdb::ax::ast::DeclareLocal*>(b[i])->type()) {
164 return false;
165 }
166 }
167
2/2
✓ Branch 1 taken 456 times.
✓ Branch 2 taken 822 times.
1278 else if (a[i]->nodetype() == openvdb::ax::ast::Node::LocalNode) {
168
1/2
✓ Branch 0 taken 456 times.
✗ Branch 1 not taken.
456 if (static_cast<const openvdb::ax::ast::Local*>(a[i])->name() !=
169
1/2
✓ Branch 0 taken 456 times.
✗ Branch 1 not taken.
456 static_cast<const openvdb::ax::ast::Local*>(b[i])->name()) {
170 return false;
171 }
172 }
173 // @note Value methods does not compare child text data
174
2/2
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 758 times.
822 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueBoolNode) {
175
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 if (static_cast<const openvdb::ax::ast::Value<bool>*>(a[i])->value() !=
176
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 static_cast<const openvdb::ax::ast::Value<bool>*>(b[i])->value()) {
177 return false;
178 }
179 }
180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 758 times.
758 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueInt16Node) {
181 if (static_cast<const openvdb::ax::ast::Value<int16_t>*>(a[i])->value() !=
182 static_cast<const openvdb::ax::ast::Value<int16_t>*>(b[i])->value()) {
183 return false;
184 }
185 }
186
2/2
✓ Branch 1 taken 292 times.
✓ Branch 2 taken 466 times.
758 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueInt32Node) {
187
1/2
✓ Branch 0 taken 292 times.
✗ Branch 1 not taken.
292 if (static_cast<const openvdb::ax::ast::Value<int32_t>*>(a[i])->value() !=
188
1/2
✓ Branch 0 taken 292 times.
✗ Branch 1 not taken.
292 static_cast<const openvdb::ax::ast::Value<int32_t>*>(b[i])->value()) {
189 return false;
190 }
191 }
192
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 457 times.
466 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueInt64Node) {
193
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (static_cast<const openvdb::ax::ast::Value<int64_t>*>(a[i])->value() !=
194
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 static_cast<const openvdb::ax::ast::Value<int64_t>*>(b[i])->value()) {
195 return false;
196 }
197 }
198
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 433 times.
457 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueFloatNode) {
199
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (static_cast<const openvdb::ax::ast::Value<float>*>(a[i])->value() !=
200
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 static_cast<const openvdb::ax::ast::Value<float>*>(b[i])->value()) {
201 return false;
202 }
203 }
204
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 406 times.
433 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueDoubleNode) {
205
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (static_cast<const openvdb::ax::ast::Value<double>*>(a[i])->value() !=
206
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 static_cast<const openvdb::ax::ast::Value<double>*>(b[i])->value()) {
207 return false;
208 }
209 }
210
2/2
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 384 times.
406 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueStrNode) {
211
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (static_cast<const openvdb::ax::ast::Value<std::string>*>(a[i])->value() !=
212
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 static_cast<const openvdb::ax::ast::Value<std::string>*>(b[i])->value()) {
213 return false;
214 }
215 }
216 }
217 return true;
218 }
219
220 inline std::vector<std::string>
221
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 nameSequence(const std::string& base, const size_t number)
222 {
223 std::vector<std::string> names;
224
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
61 if (number <= 0) return names;
225
1/2
✓ Branch 1 taken 61 times.
✗ Branch 2 not taken.
61 names.reserve(number);
226
227
2/2
✓ Branch 0 taken 476 times.
✓ Branch 1 taken 61 times.
537 for (size_t i = 1; i <= number; i++) {
228
1/2
✓ Branch 1 taken 476 times.
✗ Branch 2 not taken.
952 names.emplace_back(base + std::to_string(i));
229 }
230
231 return names;
232 }
233 }
234
235 #endif // OPENVDB_AX_UNITTEST_UTIL_HAS_BEEN_INCLUDED
236
237