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>
28inline std::string pyq(
const std::string &s)
30 std::ostringstream result;
31 for(std::size_t i = 0; i < s.size(); ++i)
35 case '\'': result <<
"\\'";
break;
36 case '\\': result <<
"\\\\";
break;
37 case '\n': result <<
"\\n";
break;
39 if(c < 32 || c >= 127)
40 result <<
"\\x" << std::hex << std::setfill(
'0') << std::setw(2)
41 <<
static_cast<int>(c);
49inline int runPython(
const std::string &code)
51#if (PY_MAJOR_VERSION >= 3) && (PY_MINOR_VERSION >= 8)
53 PyConfig_InitPythonConfig(&config);
54 PyConfig_SetString(&config, &config.program_name, PYTHON_INTERPRETER);
55 PyStatus status = Py_InitializeFromConfig(&config);
56 PyConfig_Clear(&config);
57 if (PyStatus_Exception(status)) {
58 Py_ExitStatusException(status);
59 return PyStatus_IsExit(status) ? status.exitcode : EXIT_FAILURE;
62 Py_SetProgramName(PYTHON_INTERPRETER);
66 int exitcode = PyRun_SimpleString(code.c_str());
67 int finalizecode = Py_FinalizeEx();
68 return (exitcode + finalizecode) ? EXIT_FAILURE : EXIT_SUCCESS;
71inline bool is_suffix(
const std::string &s,
const std::string &suffix)
73 return s.size() >= suffix.size() &&
74 s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0;
77inline bool havePythonVTK()
79 static const bool result = [] {
82 if(runPython(
"from vtk import *") == EXIT_SUCCESS)
84 std::cerr <<
"warning: python or python vtk module not available. This "
85 <<
"will result in skipped tests, since we cannot check that "
86 <<
"vtk can read the files we wrote." << std::endl;
93inline std::string pythonVTKReader(
const std::string& filename)
95 if (is_suffix(filename,
".vtu"))
return "vtkXMLUnstructuredGridReader";
96 else if(is_suffix(filename,
".pvtu"))
return "vtkXMLPUnstructuredGridReader";
97 else if(is_suffix(filename,
".vtp"))
return "vtkXMLPolyDataReader";
98 else if(is_suffix(filename,
".pvtp"))
return "vtkXMLPPolyDataReader";
100 "Unknown vtk file extension: " << filename);
108 void push(
const std::string& file)
110 auto res = files_.insert(file);
111 if (not res.second) {
112 testSuite_.check(
false,
"VTKChecker")
113 <<
"'" << file <<
"' was added multiple times";
119 if (not Impl::havePythonVTK()) {
122 else if (not files_.empty()) {
123 const int result = Impl::runPython(generatePythonCode());
124 testSuite_.check(result == EXIT_SUCCESS);
126 return testSuite_.exit();
129 const TestSuite& testSuite()
const
135 std::string generatePythonCode()
const
137 std::stringstream code;
139 code <<
"from vtk import *\n"
141 <<
"passed = True\n";
143 for (
const auto& file : files_) {
144 code <<
"reader = " << Impl::pythonVTKReader(file) <<
"()\n"
145 <<
"reader.SetFileName('" << Impl::pyq(file) <<
"')\n"
146 <<
"reader.Update()\n"
147 <<
"if (not (reader.GetOutput().GetNumberOfCells() > 0)):\n"
148 <<
" print('ERROR in {}'.format('" << Impl::pyq(file) <<
"'))\n"
149 <<
" passed = False\n";
152 code <<
"sys.exit(0 if passed else 1)\n";
157 std::set< std::string > files_;
158 TestSuite testSuite_;
Default exception for dummy implementations.
Definition: exceptions.hh:355
A few common exception classes.
#define DUNE_THROW(E,...)
Definition: exceptions.hh:312
Dune namespace.
Definition: alignedallocator.hh:13