1#ifndef DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH
2#define DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH
18#include <dune/common/test/testsuite.hh>
26inline std::string pyq(
const std::string &s)
28 std::ostringstream result;
29 for(std::size_t i = 0; i < s.size(); ++i)
33 case '\'': result <<
"\\'";
break;
34 case '\\': result <<
"\\\\";
break;
35 case '\n': result <<
"\\n";
break;
37 if(c < 32 || c >= 127)
38 result <<
"\\x" << std::hex << std::setfill(
'0') << std::setw(2)
39 <<
static_cast<int>(c);
49inline std::string shq(
const std::string &s)
51 std::ostringstream result;
53 for(std::size_t i = 0; i < s.size(); ++i)
58 "Can't pass \\0 through the shell");
59 case '\'': result << (pend ?
"" :
"'") <<
"\\'"; pend =
true;
break;
60 default: result << (pend ?
"'" :
"") << c; pend =
false;
break;
63 if(pend) result <<
"'";
67inline int runShell(
const std::string &code)
69 int result = std::system(code.c_str());
74 return WEXITSTATUS(result);
75 if(WIFSIGNALED(result))
76 return WTERMSIG(result) + 256;
81inline int runPython(
const std::string &code)
83 return runShell(
"python3 -c '"+shq(code)+
"'");
86inline bool is_suffix(
const std::string &s,
const std::string &suffix)
88 return s.size() >= suffix.size() &&
89 s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0;
92inline bool havePythonVTK()
94 static const bool result = [] {
97 if(runPython(
"from vtk import *") == 0)
99 std::cerr <<
"warning: python or python vtk module not available. This "
100 <<
"will result in skipped tests, since we cannot check that "
101 <<
"vtk can read the files we wrote." << std::endl;
108inline std::string pythonVTKReader(
const std::string& filename)
110 if (is_suffix(filename,
".vtu"))
return "vtkXMLUnstructuredGridReader";
111 else if(is_suffix(filename,
".pvtu"))
return "vtkXMLPUnstructuredGridReader";
112 else if(is_suffix(filename,
".vtp"))
return "vtkXMLPolyDataReader";
113 else if(is_suffix(filename,
".pvtp"))
return "vtkXMLPPolyDataReader";
115 "Unknown vtk file extension: " << filename);
123 void push(
const std::string& file)
125 auto res = files_.insert(file);
126 if (not res.second) {
127 testSuite_.check(
false,
"VTKChecker")
128 <<
"'" << file <<
"' was added multiple times";
134 if (not Impl::havePythonVTK()) {
137 else if (not files_.empty()) {
138 const int result = Impl::runPython(generatePythonCode());
139 testSuite_.check(result == 0);
141 return testSuite_.exit();
144 const TestSuite& testSuite()
const
150 std::string generatePythonCode()
const
152 std::stringstream code;
154 code <<
"from vtk import *\n"
156 <<
"passed = True\n";
158 for (
const auto& file : files_) {
159 code <<
"reader = " << Impl::pythonVTKReader(file) <<
"()\n"
160 <<
"reader.SetFileName('" << Impl::pyq(file) <<
"')\n"
161 <<
"reader.Update()\n"
162 <<
"if (not (reader.GetOutput().GetNumberOfCells() > 0)):\n"
163 <<
" print('ERROR in {}'.format('" << Impl::pyq(file) <<
"'))\n"
164 <<
" passed = False\n";
167 code <<
"sys.exit(0 if passed else 1)\n";
172 std::set< std::string > files_;
173 TestSuite testSuite_;
Default exception for dummy implementations.
Definition: exceptions.hh:261
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignedallocator.hh:11