OGS
ApplicationsLib::TestDefinition Class Referencefinal

Detailed Description

Definition at line 19 of file TestDefinition.h.

#include <TestDefinition.h>

Public Member Functions

 TestDefinition (BaseLib::ConfigTree const &config_tree, std::string const &reference_path, std::string const &output_directory)
OGS_EXPORT_SYMBOL bool runTests () const
std::vector< std::string > const & getOutputFiles () const
std::size_t numberOfTests () const

Private Attributes

std::vector< std::string > _command_lines
std::vector< std::string > _output_files

Constructor & Destructor Documentation

◆ TestDefinition()

ApplicationsLib::TestDefinition::TestDefinition ( BaseLib::ConfigTree const & config_tree,
std::string const & reference_path,
std::string const & output_directory )

Constructs test definition from the config and reference path essentially constructing the command lines to be run on run() function call.

Input File Parameter
prj__test_definition__vtkdiff
Input File Parameter
prj__test_definition__vtkdiff__field
Input File Parameter
prj__test_definition__vtkdiff__regex
Input File Parameter
prj__test_definition__vtkdiff__file
Input File Parameter
prj__test_definition__vtkdiff__absolute_tolerance
Input File Parameter
prj__test_definition__vtkdiff__relative_tolerance

Definition at line 143 of file TestDefinition.cpp.

146{
147 if (reference_path.empty())
148 {
149 OGS_FATAL(
150 "Reference path containing expected result files can not be "
151 "empty.");
152 }
153
154 std::string const vtkdiff = findVtkdiff();
155
156 // Construct command lines for each entry.
158 auto const& vtkdiff_configs = config_tree.getConfigSubtreeList("vtkdiff");
159 _command_lines.reserve(vtkdiff_configs.size());
160 for (auto const& vtkdiff_config : vtkdiff_configs)
161 {
162 std::string const& field_name =
164 vtkdiff_config.getConfigParameter<std::string>("field");
165 DBUG("vtkdiff will compare field '{:s}'.", field_name);
166
167 std::vector<std::string> filenames;
168 if (auto const regex_string =
170 vtkdiff_config.getConfigParameterOptional<std::string>("regex"))
171 {
172 // TODO: insert rank into regex for mpi case
173 DBUG("vtkdiff regex is '{}'.", *regex_string);
174 auto const regex = std::regex(*regex_string);
175 for (auto const& p : std::filesystem::directory_iterator(
176 std::filesystem::path(reference_path)))
177 {
178 auto const filename = p.path().filename().string();
179 if (std::regex_match(filename, regex))
180 {
181 DBUG(" -> matched '{}'", filename);
182 filenames.push_back(filename);
183 }
184 }
185 }
186 else
187 {
188 std::string filename =
190 vtkdiff_config.getConfigParameter<std::string>("file");
191#ifdef USE_PETSC
192 BaseLib::MPI::Mpi mpi;
193 if (mpi.size > 1)
194 {
195 filename =
197 filename) +
198 "_" + std::to_string(mpi.rank) + ".vtu";
199 }
200#endif // OGS_USE_PETSC
201 filenames.push_back(filename);
202 }
203
204 if (empty(filenames))
205 {
206 OGS_FATAL(
207 "No files from test definitions were added for tests but {} "
208 "{:s} specified.",
209 std::size(vtkdiff_configs),
210 (std::size(vtkdiff_configs) == 1 ? "test was" : "tests were"));
211 }
212
213 auto const absolute_tolerance =
215 vtkdiff_config.getConfigParameter<std::string>("absolute_tolerance",
216 "");
217 if (!absolute_tolerance.empty() &&
218 !isConvertibleToDouble(absolute_tolerance))
219 {
220 OGS_FATAL(
221 "The absolute tolerance value '{:s}' is not convertible to "
222 "double.",
223 absolute_tolerance);
224 }
225 std::string const absolute_tolerance_parameter =
226 "--abs " + absolute_tolerance;
227 auto const relative_tolerance =
229 vtkdiff_config.getConfigParameter<std::string>("relative_tolerance",
230 "");
231 if (!relative_tolerance.empty() &&
232 !isConvertibleToDouble(relative_tolerance))
233 {
234 OGS_FATAL(
235 "The relative tolerance value '{:s}' is not convertible to "
236 "double.",
237 relative_tolerance);
238 }
239 std::string const relative_tolerance_parameter =
240 "--rel " + relative_tolerance;
241
242 for (auto const& filename : filenames)
243 {
244 std::string output_filename =
245 BaseLib::joinPaths(output_directory, filename);
246 _output_files.push_back(output_filename);
247 std::string reference_filename =
248 BaseLib::joinPaths(reference_path, filename);
249#if _WIN32
250 // vtk does not handle Windows long paths:
251 // https://gitlab.kitware.com/vtk/vtk/-/blob/master/Utilities/KWSys/vtksys/SystemTools.cxx#L1519-1521
252 // workaround is to make path absolute and prefix with a special
253 // marker and put everything in quotes, see
254 // https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=powershell
255 auto const& long_path_indicator = R"(\\?\)";
256
257 reference_filename =
258 std::filesystem::absolute(reference_filename).string();
259 reference_filename = std::format("\"{}{}\"", long_path_indicator,
260 reference_filename);
261 output_filename =
262 std::filesystem::absolute(output_filename).string();
263 output_filename =
264 std::format("\"{}{}\"", long_path_indicator, output_filename);
265
266#else
267 output_filename = safeString(output_filename);
268 reference_filename = safeString(reference_filename);
269#endif
270 //
271 // Construct command line.
272 //
273 std::string command_line =
274 vtkdiff + " -a " + safeString(field_name) + " -b " +
275 safeString(field_name) + " " + reference_filename + " " +
276 output_filename + " " + absolute_tolerance_parameter + " " +
277 relative_tolerance_parameter;
278 INFO("Will run '{:s}'", command_line);
279 _command_lines.emplace_back(std::move(command_line));
280 }
281 }
282}
#define OGS_FATAL(...)
Definition Error.h:19
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
std::vector< std::string > _output_files
std::vector< std::string > _command_lines
std::string joinPaths(std::string const &pathA, std::string const &pathB)
std::string getVtuFileNameForPetscOutputWithoutExtension(std::string const &file_name)
bool isConvertibleToDouble(std::string const &s)
Test if the given string is convertible to a valid double value, not a NaN.
std::string safeString(std::string const &s)
Wraps a string into double ticks.

References _command_lines, _output_files, DBUG(), BaseLib::ConfigTree::getConfigSubtreeList(), MeshLib::IO::getVtuFileNameForPetscOutputWithoutExtension(), INFO(), BaseLib::joinPaths(), and OGS_FATAL.

Member Function Documentation

◆ getOutputFiles()

std::vector< std::string > const & ApplicationsLib::TestDefinition::getOutputFiles ( ) const

Definition at line 306 of file TestDefinition.cpp.

307{
308 return _output_files;
309}

References _output_files.

◆ numberOfTests()

std::size_t ApplicationsLib::TestDefinition::numberOfTests ( ) const

Definition at line 311 of file TestDefinition.cpp.

312{
313 return size(_command_lines);
314}
constexpr int size(int const displacement_dim)
Vectorized tensor size for given displacement dimension.

References _command_lines.

◆ runTests()

bool ApplicationsLib::TestDefinition::runTests ( ) const

Definition at line 284 of file TestDefinition.cpp.

285{
286 std::vector<int> return_values;
287 transform(begin(_command_lines), end(_command_lines),
288 back_inserter(return_values),
289 [](std::string const& command_line)
290 {
291 INFO("---------- vtkdiff begin ----------");
292 int const return_value = std::system(command_line.c_str());
293 if (return_value != 0)
294 {
295 WARN("Return value {:d} was returned by '{:s}'.",
296 return_value, command_line);
297 }
298 INFO("---------- vtkdiff end ----------\n");
299 return return_value;
300 });
301 return !return_values.empty() &&
302 all_of(begin(return_values), end(return_values),
303 [](int const& return_value) { return return_value == 0; });
304}
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
constexpr bool all_of(List const &values)
Checks if all of the elements in the given list are true.
Definition Algorithm.h:302

References _command_lines, INFO(), and WARN().

Member Data Documentation

◆ _command_lines

std::vector<std::string> ApplicationsLib::TestDefinition::_command_lines
private

Definition at line 34 of file TestDefinition.h.

Referenced by TestDefinition(), numberOfTests(), and runTests().

◆ _output_files

std::vector<std::string> ApplicationsLib::TestDefinition::_output_files
private

Definition at line 35 of file TestDefinition.h.

Referenced by TestDefinition(), and getOutputFiles().


The documentation for this class was generated from the following files: