3#ifndef DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH
4#define DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH
18#include <dune/common/test/testsuite.hh>
20#ifdef PYTHON_INTERPRETER
30inline std::string pyq(
const std::string &s)
32 std::ostringstream result;
33 for(std::size_t i = 0; i < s.size(); ++i)
37 case '\'': result <<
"\\'";
break;
38 case '\\': result <<
"\\\\";
break;
39 case '\n': result <<
"\\n";
break;
41 if(c < 32 || c >= 127)
42 result <<
"\\x" << std::hex << std::setfill(
'0') << std::setw(2)
43 <<
static_cast<int>(c);
51inline int runPython(
const std::string &code)
53#ifdef PYTHON_INTERPRETER
54#if (PY_MAJOR_VERSION >= 3) && (PY_MINOR_VERSION >= 8)
56 PyConfig_InitPythonConfig(&config);
57 PyConfig_SetString(&config, &config.program_name, PYTHON_INTERPRETER);
58 PyStatus status = Py_InitializeFromConfig(&config);
59 PyConfig_Clear(&config);
60 if (PyStatus_Exception(status)) {
61 Py_ExitStatusException(status);
62 return PyStatus_IsExit(status) ? status.exitcode : EXIT_FAILURE;
65 Py_SetProgramName(PYTHON_INTERPRETER);
69 int exitcode = PyRun_SimpleString(code.c_str());
70 int finalizecode = Py_FinalizeEx();
71 return (exitcode + finalizecode) ? EXIT_FAILURE : EXIT_SUCCESS;
77inline bool is_suffix(
const std::string &s,
const std::string &suffix)
79 return s.size() >= suffix.size() &&
80 s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0;
83inline bool havePythonVTK()
85 static const bool result = [] {
88 if(runPython(
"from vtk import *") == EXIT_SUCCESS)
90 std::cerr <<
"warning: python or python vtk module not available. This "
91 <<
"will result in skipped tests, since we cannot check that "
92 <<
"vtk can read the files we wrote." << std::endl;
99inline std::string pythonVTKReader(
const std::string& filename)
101 if (is_suffix(filename,
".vtu"))
return "vtkXMLUnstructuredGridReader";
102 else if(is_suffix(filename,
".pvtu"))
return "vtkXMLPUnstructuredGridReader";
103 else if(is_suffix(filename,
".vtp"))
return "vtkXMLPolyDataReader";
104 else if(is_suffix(filename,
".pvtp"))
return "vtkXMLPPolyDataReader";
106 "Unknown vtk file extension: " << filename);
114 void push(
const std::string& file)
116 auto res = files_.insert(file);
117 if (not res.second) {
118 testSuite_.check(
false,
"VTKChecker")
119 <<
"'" << file <<
"' was added multiple times";
125 if (not Impl::havePythonVTK()) {
128 else if (not files_.empty()) {
129 const int result = Impl::runPython(generatePythonCode());
130 testSuite_.check(result == EXIT_SUCCESS);
132 return testSuite_.exit();
135 const TestSuite& testSuite()
const
141 std::string generatePythonCode()
const
143 std::stringstream code;
145 code <<
"from vtk import *\n"
147 <<
"passed = True\n";
149 for (
const auto& file : files_) {
150 code <<
"reader = " << Impl::pythonVTKReader(file) <<
"()\n"
151 <<
"reader.SetFileName('" << Impl::pyq(file) <<
"')\n"
152 <<
"reader.Update()\n"
153 <<
"if (not (reader.GetOutput().GetNumberOfCells() > 0)):\n"
154 <<
" print('ERROR in {}'.format('" << Impl::pyq(file) <<
"'))\n"
155 <<
" passed = False\n";
158 code <<
"sys.exit(0 if passed else 1)\n";
163 std::set< std::string > files_;
164 TestSuite testSuite_;
Default exception for dummy implementations.
Definition: exceptions.hh:357
A few common exception classes.
#define DUNE_THROW(E,...)
Definition: exceptions.hh:314
Dune namespace.
Definition: alignedallocator.hh:13