10 #ifndef OPENVDB_AX_UNITTEST_UTIL_HAS_BEEN_INCLUDED
11 #define OPENVDB_AX_UNITTEST_UTIL_HAS_BEEN_INCLUDED
24 #include <type_traits>
26 #define ERROR_MSG(Msg, Code) Msg + std::string(": \"") + Code + std::string("\"")
28 #define TEST_SYNTAX_PASSES(Tests) \
30 openvdb::ax::Logger logger;\
31 for (const auto& test : Tests) { \
33 const std::string& code = test.first; \
34 openvdb::ax::ast::Tree::ConstPtr tree = openvdb::ax::ast::parse(code.c_str(), logger);\
35 std::stringstream str; \
36 CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("Unexpected parsing error(s)\n", str.str()), tree); \
40 #define TEST_SYNTAX_FAILS(Tests) \
42 openvdb::ax::Logger logger([](const std::string&) {});\
43 for (const auto& test : Tests) { \
45 const std::string& code = test.first; \
46 openvdb::ax::ast::Tree::ConstPtr tree = openvdb::ax::ast::parse(code.c_str(), logger);\
47 CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("Expected parsing error", code), logger.hasError()); \
56 using CodeTests = std::vector<std::pair<std::string, openvdb::ax::ast::Node::Ptr>>;
61 inline void replace(std::string& str,
const std::string& oldStr,
const std::string& newStr)
63 std::string::size_type pos = 0u;
64 while ((pos = str.find(oldStr, pos)) != std::string::npos) {
65 str.replace(pos, oldStr.length(), newStr);
66 pos += newStr.length();
73 const std::vector<const openvdb::ax::ast::Node*>& b,
const bool allowEmpty =
false)
75 if (!allowEmpty && (a.empty() || b.empty()))
return false;
76 if (a.size() != b.size())
return false;
77 const size_t size = a.size();
78 for (
size_t i = 0; i < size; ++i) {
79 if ((a[i] ==
nullptr) ^ (b[i] ==
nullptr))
return false;
80 if (a[i] ==
nullptr)
continue;
81 if (a[i]->nodetype() != b[i]->nodetype())
return false;
87 if (a[i]->nodetype() == openvdb::ax::ast::Node::AssignExpressionNode) {
88 if (
static_cast<const openvdb::ax::ast::AssignExpression*
>(a[i])->operation() !=
89 static_cast<const openvdb::ax::ast::AssignExpression*
>(b[i])->operation()) {
93 else if (a[i]->nodetype() == openvdb::ax::ast::Node::BinaryOperatorNode) {
94 if (
static_cast<const openvdb::ax::ast::BinaryOperator*
>(a[i])->operation() !=
95 static_cast<const openvdb::ax::ast::BinaryOperator*
>(b[i])->operation()) {
99 else if (a[i]->nodetype() == openvdb::ax::ast::Node::CrementNode) {
100 if (
static_cast<const openvdb::ax::ast::Crement*
>(a[i])->operation() !=
101 static_cast<const openvdb::ax::ast::Crement*
>(b[i])->operation()) {
104 if (
static_cast<const openvdb::ax::ast::Crement*
>(a[i])->post() !=
105 static_cast<const openvdb::ax::ast::Crement*
>(b[i])->post()) {
109 else if (a[i]->nodetype() == openvdb::ax::ast::Node::CastNode) {
110 if (
static_cast<const openvdb::ax::ast::Cast*
>(a[i])->type() !=
111 static_cast<const openvdb::ax::ast::Cast*
>(b[i])->type()) {
115 else if (a[i]->nodetype() == openvdb::ax::ast::Node::FunctionCallNode) {
116 if (
static_cast<const openvdb::ax::ast::FunctionCall*
>(a[i])->name() !=
117 static_cast<const openvdb::ax::ast::FunctionCall*
>(b[i])->name()) {
121 else if (a[i]->nodetype() == openvdb::ax::ast::Node::LoopNode) {
122 if (
static_cast<const openvdb::ax::ast::Loop*
>(a[i])->loopType() !=
123 static_cast<const openvdb::ax::ast::Loop*
>(b[i])->loopType()) {
127 else if (a[i]->nodetype() == openvdb::ax::ast::Node::KeywordNode) {
128 if (
static_cast<const openvdb::ax::ast::Keyword*
>(a[i])->keyword() !=
129 static_cast<const openvdb::ax::ast::Keyword*
>(b[i])->keyword()) {
133 else if (a[i]->nodetype() == openvdb::ax::ast::Node::AttributeNode) {
134 if (
static_cast<const openvdb::ax::ast::Attribute*
>(a[i])->type() !=
135 static_cast<const openvdb::ax::ast::Attribute*
>(b[i])->type()) {
138 if (
static_cast<const openvdb::ax::ast::Attribute*
>(a[i])->name() !=
139 static_cast<const openvdb::ax::ast::Attribute*
>(b[i])->name()) {
142 if (
static_cast<const openvdb::ax::ast::Attribute*
>(a[i])->inferred() !=
143 static_cast<const openvdb::ax::ast::Attribute*
>(b[i])->inferred()) {
147 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ExternalVariableNode) {
148 if (
static_cast<const openvdb::ax::ast::ExternalVariable*
>(a[i])->type() !=
149 static_cast<const openvdb::ax::ast::ExternalVariable*
>(b[i])->type()) {
152 if (
static_cast<const openvdb::ax::ast::ExternalVariable*
>(a[i])->name() !=
153 static_cast<const openvdb::ax::ast::ExternalVariable*
>(b[i])->name()) {
157 else if (a[i]->nodetype() == openvdb::ax::ast::Node::DeclareLocalNode) {
158 if (
static_cast<const openvdb::ax::ast::DeclareLocal*
>(a[i])->type() !=
159 static_cast<const openvdb::ax::ast::DeclareLocal*
>(b[i])->type()) {
163 else if (a[i]->nodetype() == openvdb::ax::ast::Node::LocalNode) {
164 if (
static_cast<const openvdb::ax::ast::Local*
>(a[i])->name() !=
165 static_cast<const openvdb::ax::ast::Local*
>(b[i])->name()) {
170 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueBoolNode) {
171 if (
static_cast<const openvdb::ax::ast::Value<bool>*
>(a[i])->value() !=
172 static_cast<const openvdb::ax::ast::Value<bool>*
>(b[i])->value()) {
176 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueInt16Node) {
177 if (
static_cast<const openvdb::ax::ast::Value<int16_t>*
>(a[i])->value() !=
178 static_cast<const openvdb::ax::ast::Value<int16_t>*
>(b[i])->value()) {
182 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueInt32Node) {
183 if (
static_cast<const openvdb::ax::ast::Value<int32_t>*
>(a[i])->value() !=
184 static_cast<const openvdb::ax::ast::Value<int32_t>*
>(b[i])->value()) {
188 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueInt64Node) {
189 if (
static_cast<const openvdb::ax::ast::Value<int64_t>*
>(a[i])->value() !=
190 static_cast<const openvdb::ax::ast::Value<int64_t>*
>(b[i])->value()) {
194 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueFloatNode) {
195 if (
static_cast<const openvdb::ax::ast::Value<float>*
>(a[i])->value() !=
196 static_cast<const openvdb::ax::ast::Value<float>*
>(b[i])->value()) {
200 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueDoubleNode) {
201 if (
static_cast<const openvdb::ax::ast::Value<double>*
>(a[i])->value() !=
202 static_cast<const openvdb::ax::ast::Value<double>*
>(b[i])->value()) {
206 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueStrNode) {
207 if (
static_cast<const openvdb::ax::ast::Value<std::string>*
>(a[i])->value() !=
208 static_cast<const openvdb::ax::ast::Value<std::string>*
>(b[i])->value()) {
216 inline std::vector<std::string>
219 std::vector<std::string> names;
220 if (number <= 0)
return names;
221 names.reserve(number);
223 for (
size_t i = 1; i <= number; i++) {
224 names.emplace_back(base + std::to_string(i));
231 #endif // OPENVDB_AX_UNITTEST_UTIL_HAS_BEEN_INCLUDED