diff options
Diffstat (limited to 'tests/dumptest.cc')
-rw-r--r-- | tests/dumptest.cc | 96 |
1 files changed, 51 insertions, 45 deletions
diff --git a/tests/dumptest.cc b/tests/dumptest.cc index 3782446..5f2f22f 100644 --- a/tests/dumptest.cc +++ b/tests/dumptest.cc @@ -24,15 +24,14 @@ * */ +#include "tests-common.h" #include "openscad.h" -#include "handle_dep.h" #include "node.h" #include "module.h" #include "context.h" #include "value.h" #include "export.h" #include "builtin.h" -#include "nodedumper.h" #include "Tree.h" #include <QApplication> @@ -54,8 +53,22 @@ QString currentdir; QString examplesdir; QString librarydir; +string dumptree(const Tree &tree, const AbstractNode &node) +{ + std::stringstream str; + const std::vector<AbstractNode*> &children = node.getChildren(); + for (std::vector<AbstractNode*>::const_iterator iter = children.begin(); iter != children.end(); iter++) { + str << tree.getString(**iter) << "\n"; + } + return str.str(); +} + int main(int argc, char **argv) { +#ifdef WIN32 + _set_output_format(_TWO_DIGIT_EXPONENT); +#endif + if (argc != 3) { fprintf(stderr, "Usage: %s <file.scad> <output.txt>\n", argv[0]); exit(1); @@ -66,8 +79,7 @@ int main(int argc, char **argv) int rc = 0; - initialize_builtin_functions(); - initialize_builtin_modules(); + Builtins::instance()->initialize(); QApplication app(argc, argv, false); QDir original_path = QDir::current(); @@ -94,69 +106,63 @@ int main(int argc, char **argv) } Context root_ctx; - root_ctx.functions_p = &builtin_functions; - root_ctx.modules_p = &builtin_modules; - root_ctx.set_variable("$fn", Value(0.0)); - root_ctx.set_variable("$fs", Value(1.0)); - root_ctx.set_variable("$fa", Value(12.0)); - root_ctx.set_variable("$t", Value(0.0)); - - Value zero3; - zero3.type = Value::VECTOR; - zero3.append(new Value(0.0)); - zero3.append(new Value(0.0)); - zero3.append(new Value(0.0)); - root_ctx.set_variable("$vpt", zero3); - root_ctx.set_variable("$vpr", zero3); - + register_builtin(root_ctx); AbstractModule *root_module; ModuleInstantiation root_inst; AbstractNode *root_node; - QFileInfo fileInfo(filename); - handle_dep(filename); - FILE *fp = fopen(filename, "rt"); - if (!fp) { - fprintf(stderr, "Can't open input file `%s'!\n", filename); + root_module = parsefile(filename); + if (!root_module) { exit(1); - } else { - std::stringstream text; - char buffer[513]; - int ret; - while ((ret = fread(buffer, 1, 512, fp)) > 0) { - buffer[ret] = 0; - text << buffer; - } - fclose(fp); - text << commandline_commands; - root_module = parse(text.str().c_str(), fileInfo.absolutePath().toLocal8Bit(), false); - if (!root_module) { - exit(1); - } } + QFileInfo fileInfo(filename); QDir::setCurrent(fileInfo.absolutePath()); AbstractNode::resetIndexCounter(); root_node = root_module->evaluate(&root_ctx, &root_inst); - // Cache test - QString teststr("test"); Tree tree; tree.setRoot(root_node); - string dumpstdstr = tree.getString(*root_node); - string dumpstdstr_cached = tree.getString(*root_node); - assert(dumpstdstr == dumpstdstr_cached); + string dumpstdstr = dumptree(tree, *root_node); + string dumpstdstr_cached = dumptree(tree, *root_node); + if (dumpstdstr != dumpstdstr_cached) { + fprintf(stderr, "Error: Dump cached failed\n"); + exit(1); + } + QDir::setCurrent(original_path.absolutePath()); std::ofstream outfile; outfile.open(outfilename); outfile << dumpstdstr << "\n"; outfile.close(); - destroy_builtin_functions(); - destroy_builtin_modules(); + root_module = parsefile(outfilename); + if (!root_module) { + fprintf(stderr, "Error: Unable to read back dumped file\n"); + exit(1); + } + fileInfo = QFileInfo(outfilename); + QDir::setCurrent(fileInfo.absolutePath()); + + AbstractNode::resetIndexCounter(); + root_node = root_module->evaluate(&root_ctx, &root_inst); + + tree.setRoot(root_node); + + string readbackstr = dumptree(tree, *root_node); + if (dumpstdstr != readbackstr) { + fprintf(stderr, "Error: Readback is different from original dump:\n"); + fprintf(stderr, "Original:\n"); + fprintf(stderr, dumpstdstr.c_str()); + fprintf(stderr, "Readback:\n"); + fprintf(stderr, readbackstr.c_str()); + exit(1); + } + + Builtins::instance(true); return rc; } |