10 #ifndef OPENVDB_AX_UNITTEST_UTIL_HAS_BEEN_INCLUDED 11 #define OPENVDB_AX_UNITTEST_UTIL_HAS_BEEN_INCLUDED 25 #include <type_traits> 28 #define ERROR_MSG(Msg, Code) Msg + std::string(": \"") + Code + std::string("\"") 30 #define TEST_SYNTAX_PASSES(Tests) \ 32 openvdb::ax::Logger logger;\ 33 for (const auto& test : Tests) { \ 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()); \ 42 #define TEST_SYNTAX_FAILS(Tests) \ 44 openvdb::ax::Logger logger([](const std::string&) {});\ 45 for (const auto& test : Tests) { \ 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()); \ 58 using CodeTests = std::vector<std::pair<std::string, openvdb::ax::ast::Node::Ptr>>;
60 using ConfigMap = std::map<std::string, std::map<std::string, std::string>>;
65 inline void replace(std::string& str,
const std::string& oldStr,
const std::string& newStr)
67 std::string::size_type pos = 0u;
68 while ((pos = str.find(oldStr, pos)) != std::string::npos) {
69 str.replace(pos, oldStr.length(), newStr);
70 pos += newStr.length();
77 const std::vector<const openvdb::ax::ast::Node*>& b,
const bool allowEmpty =
false)
79 if (!allowEmpty && (a.empty() || b.empty()))
return false;
80 if (a.size() != b.size())
return false;
81 const size_t size = a.size();
82 for (
size_t i = 0; i < size; ++i) {
83 if ((a[i] ==
nullptr) ^ (b[i] ==
nullptr))
return false;
84 if (a[i] ==
nullptr)
continue;
85 if (a[i]->nodetype() != b[i]->nodetype())
return false;
91 if (a[i]->nodetype() == openvdb::ax::ast::Node::AssignExpressionNode) {
92 if (static_cast<const openvdb::ax::ast::AssignExpression*>(a[i])->operation() !=
93 static_cast<const openvdb::ax::ast::AssignExpression*>(b[i])->operation()) {
97 else if (a[i]->nodetype() == openvdb::ax::ast::Node::BinaryOperatorNode) {
98 if (static_cast<const openvdb::ax::ast::BinaryOperator*>(a[i])->operation() !=
99 static_cast<const openvdb::ax::ast::BinaryOperator*>(b[i])->operation()) {
103 else if (a[i]->nodetype() == openvdb::ax::ast::Node::CrementNode) {
104 if (static_cast<const openvdb::ax::ast::Crement*>(a[i])->operation() !=
105 static_cast<const openvdb::ax::ast::Crement*>(b[i])->operation()) {
108 if (static_cast<const openvdb::ax::ast::Crement*>(a[i])->post() !=
109 static_cast<const openvdb::ax::ast::Crement*>(b[i])->post()) {
113 else if (a[i]->nodetype() == openvdb::ax::ast::Node::CastNode) {
114 if (static_cast<const openvdb::ax::ast::Cast*>(a[i])->type() !=
115 static_cast<const openvdb::ax::ast::Cast*>(b[i])->type()) {
119 else if (a[i]->nodetype() == openvdb::ax::ast::Node::FunctionCallNode) {
120 if (static_cast<const openvdb::ax::ast::FunctionCall*>(a[i])->name() !=
121 static_cast<const openvdb::ax::ast::FunctionCall*>(b[i])->name()) {
125 else if (a[i]->nodetype() == openvdb::ax::ast::Node::LoopNode) {
126 if (static_cast<const openvdb::ax::ast::Loop*>(a[i])->loopType() !=
127 static_cast<const openvdb::ax::ast::Loop*>(b[i])->loopType()) {
131 else if (a[i]->nodetype() == openvdb::ax::ast::Node::KeywordNode) {
132 if (static_cast<const openvdb::ax::ast::Keyword*>(a[i])->keyword() !=
133 static_cast<const openvdb::ax::ast::Keyword*>(b[i])->keyword()) {
137 else if (a[i]->nodetype() == openvdb::ax::ast::Node::AttributeNode) {
138 if (static_cast<const openvdb::ax::ast::Attribute*>(a[i])->type() !=
139 static_cast<const openvdb::ax::ast::Attribute*>(b[i])->type()) {
142 if (static_cast<const openvdb::ax::ast::Attribute*>(a[i])->name() !=
143 static_cast<const openvdb::ax::ast::Attribute*>(b[i])->name()) {
146 if (static_cast<const openvdb::ax::ast::Attribute*>(a[i])->inferred() !=
147 static_cast<const openvdb::ax::ast::Attribute*>(b[i])->inferred()) {
151 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ExternalVariableNode) {
152 if (static_cast<const openvdb::ax::ast::ExternalVariable*>(a[i])->type() !=
153 static_cast<const openvdb::ax::ast::ExternalVariable*>(b[i])->type()) {
156 if (static_cast<const openvdb::ax::ast::ExternalVariable*>(a[i])->name() !=
157 static_cast<const openvdb::ax::ast::ExternalVariable*>(b[i])->name()) {
161 else if (a[i]->nodetype() == openvdb::ax::ast::Node::DeclareLocalNode) {
162 if (static_cast<const openvdb::ax::ast::DeclareLocal*>(a[i])->type() !=
163 static_cast<const openvdb::ax::ast::DeclareLocal*>(b[i])->type()) {
167 else if (a[i]->nodetype() == openvdb::ax::ast::Node::LocalNode) {
168 if (static_cast<const openvdb::ax::ast::Local*>(a[i])->name() !=
169 static_cast<const openvdb::ax::ast::Local*>(b[i])->name()) {
174 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueBoolNode) {
175 if (
static_cast<const openvdb::ax::ast::Value<bool>*
>(a[i])->value() !=
176 static_cast<const openvdb::ax::ast::Value<bool>*
>(b[i])->value()) {
180 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()) {
186 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueInt32Node) {
187 if (
static_cast<const openvdb::ax::ast::Value<int32_t>*
>(a[i])->value() !=
188 static_cast<const openvdb::ax::ast::Value<int32_t>*
>(b[i])->value()) {
192 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueInt64Node) {
193 if (
static_cast<const openvdb::ax::ast::Value<int64_t>*
>(a[i])->value() !=
194 static_cast<const openvdb::ax::ast::Value<int64_t>*
>(b[i])->value()) {
198 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueFloatNode) {
199 if (
static_cast<const openvdb::ax::ast::Value<float>*
>(a[i])->value() !=
200 static_cast<const openvdb::ax::ast::Value<float>*
>(b[i])->value()) {
204 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueDoubleNode) {
205 if (
static_cast<const openvdb::ax::ast::Value<double>*
>(a[i])->value() !=
206 static_cast<const openvdb::ax::ast::Value<double>*
>(b[i])->value()) {
210 else if (a[i]->nodetype() == openvdb::ax::ast::Node::ValueStrNode) {
211 if (
static_cast<const openvdb::ax::ast::Value<std::string>*
>(a[i])->value() !=
212 static_cast<const openvdb::ax::ast::Value<std::string>*
>(b[i])->value()) {
220 inline std::vector<std::string>
223 std::vector<std::string> names;
224 if (number <= 0)
return names;
225 names.reserve(number);
227 for (
size_t i = 1; i <= number; i++) {
228 names.emplace_back(base + std::to_string(i));
235 #endif // OPENVDB_AX_UNITTEST_UTIL_HAS_BEEN_INCLUDED Provides the definition for every abstract and concrete derived class which represent a particular ab...
Parsing methods for creating abstract syntax trees out of AX code.
std::map< std::string, std::map< std::string, std::string >> ConfigMap
Definition: util.h:60
std::vector< std::pair< std::string, openvdb::ax::ast::Node::Ptr >> CodeTests
Definition: util.h:58
void replace(std::string &str, const std::string &oldStr, const std::string &newStr)
Definition: util.h:65
Logging system to collect errors and warnings throughout the different stages of parsing and compilat...
Various function and operator tokens used throughout the AST and code generation. ...
Various tools which traverse an AX AST and report information back to a std::ostream.
std::vector< std::string > nameSequence(const std::string &base, const size_t number)
Definition: util.h:221
bool compareLinearTrees(const std::vector< const openvdb::ax::ast::Node * > &a, const std::vector< const openvdb::ax::ast::Node * > &b, const bool allowEmpty=false)
Definition: util.h:76