3#ifndef DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH
4#define DUNE_GRID_IO_FILE_TEST_CHECKVTKFILE_HH
20#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);
51inline std::string shq(
const std::string &s)
53 std::ostringstream result;
55 for(std::size_t i = 0; i < s.size(); ++i)
60 "Can't pass \\0 through the shell");
61 case '\'': result << (pend ?
"" :
"'") <<
"\\'"; pend =
true;
break;
62 default: result << (pend ?
"'" :
"") << c; pend =
false;
break;
65 if(pend) result <<
"'";
69inline int runShell(
const std::string &code)
71 int result = std::system(code.c_str());
76 return WEXITSTATUS(result);
77 if(WIFSIGNALED(result))
78 return WTERMSIG(result) + 256;
83inline int runPython(
const std::string &code)
85 return runShell(
"python3 -c '"+shq(code)+
"'");
88inline bool is_suffix(
const std::string &s,
const std::string &suffix)
90 return s.size() >= suffix.size() &&
91 s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0;
94inline bool havePythonVTK()
96 static const bool result = [] {
99 if(runPython(
"from vtk import *") == 0)
101 std::cerr <<
"warning: python or python vtk module not available. This "
102 <<
"will result in skipped tests, since we cannot check that "
103 <<
"vtk can read the files we wrote." << std::endl;
110inline std::string pythonVTKReader(
const std::string& filename)
112 if (is_suffix(filename,
".vtu"))
return "vtkXMLUnstructuredGridReader";
113 else if(is_suffix(filename,
".pvtu"))
return "vtkXMLPUnstructuredGridReader";
114 else if(is_suffix(filename,
".vtp"))
return "vtkXMLPolyDataReader";
115 else if(is_suffix(filename,
".pvtp"))
return "vtkXMLPPolyDataReader";
117 "Unknown vtk file extension: " << filename);
125 void push(
const std::string& file)
127 auto res = files_.insert(file);
128 if (not res.second) {
129 testSuite_.check(
false,
"VTKChecker")
130 <<
"'" << file <<
"' was added multiple times";
136 if (not Impl::havePythonVTK()) {
139 else if (not files_.empty()) {
140 const int result = Impl::runPython(generatePythonCode());
141 testSuite_.check(result == 0);
143 return testSuite_.exit();
146 const TestSuite& testSuite()
const
152 std::string generatePythonCode()
const
154 std::stringstream code;
156 code <<
"from vtk import *\n"
158 <<
"passed = True\n";
160 for (
const auto& file : files_) {
161 code <<
"reader = " << Impl::pythonVTKReader(file) <<
"()\n"
162 <<
"reader.SetFileName('" << Impl::pyq(file) <<
"')\n"
163 <<
"reader.Update()\n"
164 <<
"if (not (reader.GetOutput().GetNumberOfCells() > 0)):\n"
165 <<
" print('ERROR in {}'.format('" << Impl::pyq(file) <<
"'))\n"
166 <<
" passed = False\n";
169 code <<
"sys.exit(0 if passed else 1)\n";
174 std::set< std::string > files_;
175 TestSuite testSuite_;
Default exception for dummy implementations.
Definition: exceptions.hh:263
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13