OGS
ApplicationsLib::TestDefinition Class Referencefinal

Detailed Description

Definition at line 20 of file TestDefinition.h.

#include <TestDefinition.h>

Classes

struct  CommandLine

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
 Runs all configured test command lines.
OGS_EXPORT_SYMBOL bool runTests (std::string_view diff_tool_name) const
 Runs only test command lines for the given diff tool.
OGS_EXPORT_SYMBOL bool runTestsExcluding (std::string_view diff_tool_name) const
 Runs all test command lines except those for the given diff tool.
OGS_EXPORT_SYMBOL bool hasTests (std::string_view diff_tool_name) const
 Returns true if at least one test command line uses the given diff tool.
std::vector< std::string > const & getOutputFiles () const
 Returns all output files referenced by the configured test definitions.
std::size_t numberOfTests () const
 Returns the number of configured test command lines.

Static Private Member Functions

static bool runCommandLines (std::vector< CommandLine > const &command_lines, bool fail_on_empty)

Private Attributes

std::vector< CommandLine_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__xdmfdiff
Input File Parameter
prj__test_definition__vtkdiff__field
Input File Parameter
prj__test_definition__vtkdiff__reference_field
Input File Parameter
prj__test_definition__vtkdiff__regex
Input File Parameter
prj__test_definition__vtkdiff__file
Input File Parameter
prj__test_definition__vtkdiff__reference_file
Input File Parameter
prj__test_definition__vtkdiff__absolute_tolerance
Input File Parameter
prj__test_definition__vtkdiff__relative_tolerance
Input File Parameter
prj__test_definition__xdmfdiff__field
Input File Parameter
prj__test_definition__xdmfdiff__reference_field
Input File Parameter
prj__test_definition__xdmfdiff__regex
Input File Parameter
prj__test_definition__xdmfdiff__file
Input File Parameter
prj__test_definition__xdmfdiff__reference_file
Input File Parameter
prj__test_definition__xdmfdiff__absolute_tolerance
Input File Parameter
prj__test_definition__xdmfdiff__relative_tolerance
Input File Parameter
prj__test_definition__xdmfdiff__timestep
Input File Parameter
prj__test_definition__xdmfdiff__reference_timestep

Definition at line 200 of file TestDefinition.cpp.

203{
204 if (reference_path.empty())
205 {
206 OGS_FATAL(
207 "Reference path containing expected result files can not be "
208 "empty.");
209 }
210
211 // Construct command lines for each entry.
213 auto const& vtkdiff_configs = config_tree.getConfigSubtreeList("vtkdiff");
215 auto const& xdmfdiff_configs = config_tree.getConfigSubtreeList("xdmfdiff");
216 _command_lines.reserve(vtkdiff_configs.size() + xdmfdiff_configs.size());
217
218 // Constructs one command line per file name pair from the entries common
219 // to all diff tools and appends it together with the corresponding output
220 // file.
221 auto const append_tests =
222 [&](std::string const& tool, std::string const& tool_path,
223 std::size_t const number_of_configs, FilePairs const& filenames,
224 std::string const& field_name,
225 std::string const& reference_field_name,
226 std::string const& absolute_tolerance,
227 std::string const& relative_tolerance,
228 std::string const& extra_options)
229 {
230 if (filenames.empty())
231 {
232 OGS_FATAL(
233 "No files from test definitions were added for tests but {} "
234 "{:s} specified.",
235 number_of_configs,
236 (number_of_configs == 1 ? "test was" : "tests were"));
237 }
238 checkTolerance("absolute", absolute_tolerance);
239 checkTolerance("relative", relative_tolerance);
240
241 for (auto const& [reference_file, output_file] : filenames)
242 {
243 auto const output_filename =
244 BaseLib::joinPaths(output_directory, output_file);
245 _output_files.push_back(output_filename);
246
247 std::string command_line = fmt::format(
248 "{} -a {} -b {} {} {} --abs {} --rel {}{}", tool_path,
249 safeString(reference_field_name), safeString(field_name),
251 BaseLib::joinPaths(reference_path, reference_file)),
252 formatPathForDiffTool(output_filename), absolute_tolerance,
253 relative_tolerance, extra_options);
254 INFO("Will run '{:s}'", command_line);
255 _command_lines.push_back({tool, std::move(command_line)});
256 }
257 };
258
259 std::string const vtkdiff =
260 vtkdiff_configs.empty() ? "" : findDiffTool("vtkdiff", "VTKDIFF_EXE");
261 for (auto const& vtkdiff_config : vtkdiff_configs)
262 {
263 std::string const& field_name =
265 vtkdiff_config.getConfigParameter<std::string>("field");
266 DBUG("vtkdiff will compare field '{:s}'.", field_name);
267 std::string const reference_field_name =
268 vtkdiff_config
270 .getConfigParameterOptional<std::string>("reference_field")
271 .value_or(field_name);
272
273 FilePairs filenames;
274 if (auto const regex_string =
276 vtkdiff_config.getConfigParameterOptional<std::string>("regex"))
277 {
278 // TODO: insert rank into regex for mpi case
279 filenames = findFilesMatchingRegex(*regex_string, reference_path);
280 }
281 else
282 {
283 std::string filename =
285 vtkdiff_config.getConfigParameter<std::string>("file");
286 std::string reference_filename =
287 vtkdiff_config
289 .getConfigParameterOptional<std::string>("reference_file")
290 .value_or(filename);
291#ifdef USE_PETSC
292 BaseLib::MPI::Mpi mpi;
293 if (mpi.size > 1)
294 {
295 filename =
297 filename) +
298 "_" + std::to_string(mpi.rank) + ".vtu";
299 reference_filename =
301 reference_filename) +
302 "_" + std::to_string(mpi.rank) + ".vtu";
303 }
304#endif // OGS_USE_PETSC
305 filenames.emplace_back(reference_filename, filename);
306 }
307
308 auto const absolute_tolerance =
310 vtkdiff_config.getConfigParameter<std::string>("absolute_tolerance",
311 "");
312 auto const relative_tolerance =
314 vtkdiff_config.getConfigParameter<std::string>("relative_tolerance",
315 "");
316
317 append_tests("vtkdiff", vtkdiff, vtkdiff_configs.size(), filenames,
318 field_name, reference_field_name, absolute_tolerance,
319 relative_tolerance, "");
320 }
321
322 std::string const xdmfdiff = xdmfdiff_configs.empty()
323 ? ""
324 : findDiffTool("xdmfdiff", "XDMFDIFF_EXE");
325 for (auto const& xdmfdiff_config : xdmfdiff_configs)
326 {
327 std::string const& field_name =
329 xdmfdiff_config.getConfigParameter<std::string>("field");
330 DBUG("xdmfdiff will compare field '{:s}'.", field_name);
331 std::string const reference_field_name =
332 xdmfdiff_config
334 .getConfigParameterOptional<std::string>("reference_field")
335 .value_or(field_name);
336
337 FilePairs filenames;
338 if (auto const regex_string =
340 xdmfdiff_config.getConfigParameterOptional<std::string>("regex"))
341 {
342 filenames = findFilesMatchingRegex(*regex_string, reference_path);
343 }
344 else
345 {
346 std::string const filename =
348 xdmfdiff_config.getConfigParameter<std::string>("file");
349 std::string const reference_filename =
350 xdmfdiff_config
352 .getConfigParameterOptional<std::string>("reference_file")
353 .value_or(filename);
354 filenames.emplace_back(reference_filename, filename);
355 }
356
357 auto const absolute_tolerance =
359 xdmfdiff_config.getConfigParameter<std::string>(
360 "absolute_tolerance", "");
361 auto const relative_tolerance =
363 xdmfdiff_config.getConfigParameter<std::string>(
364 "relative_tolerance", "");
365
366 std::string const timestep =
368 xdmfdiff_config.getConfigParameter<std::string>("timestep");
369 std::string const reference_timestep =
370 xdmfdiff_config
372 .getConfigParameterOptional<std::string>("reference_timestep")
373 .value_or(timestep);
374
375 append_tests("xdmfdiff", xdmfdiff, xdmfdiff_configs.size(), filenames,
376 field_name, reference_field_name, absolute_tolerance,
377 relative_tolerance,
378 fmt::format(" --timestep-a {} --timestep-b {}",
379 reference_timestep, timestep));
380 }
381}
#define OGS_FATAL(...)
Definition Error.h:10
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< CommandLine > _command_lines
std::string joinPaths(std::string const &pathA, std::string const &pathB)
std::string getVtuFileNameForPetscOutputWithoutExtension(std::string const &file_name)
std::string findDiffTool(std::string const &executable_name, std::string const &environment_variable_name)
std::string formatPathForDiffTool(std::string const &filename)
void checkTolerance(std::string const &tolerance_name, std::string const &tolerance)
std::vector< std::pair< std::string, std::string > > FilePairs
Pairs of reference and output file names to be compared.
std::string safeString(std::string const &s)
Wraps a string into double ticks.
FilePairs findFilesMatchingRegex(std::string const &regex_string, std::string const &reference_path)
Collects all file names in reference_path matching the regex.

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

Member Function Documentation

◆ getOutputFiles()

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

Returns all output files referenced by the configured test definitions.

Definition at line 444 of file TestDefinition.cpp.

445{
446 return _output_files;
447}

References _output_files.

◆ hasTests()

bool ApplicationsLib::TestDefinition::hasTests ( std::string_view diff_tool_name) const

Returns true if at least one test command line uses the given diff tool.

Definition at line 411 of file TestDefinition.cpp.

412{
413 return any_of(begin(_command_lines), end(_command_lines),
414 [diff_tool_name](CommandLine const& command_line)
415 { return command_line.diff_tool_name == diff_tool_name; });
416}
constexpr bool any_of(List const &values)
Checks if any of the elements in the given list is true.
Definition Algorithm.h:311

References _command_lines, and ApplicationsLib::TestDefinition::CommandLine::diff_tool_name.

◆ numberOfTests()

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

Returns the number of configured test command lines.

Definition at line 449 of file TestDefinition.cpp.

450{
451 return size(_command_lines);
452}
constexpr int size(int const displacement_dim)
Vectorized tensor size for given displacement dimension.

References _command_lines.

◆ runCommandLines()

bool ApplicationsLib::TestDefinition::runCommandLines ( std::vector< CommandLine > const & command_lines,
bool fail_on_empty )
staticprivate

Definition at line 418 of file TestDefinition.cpp.

420{
421 std::vector<int> return_values;
422 transform(begin(command_lines), end(command_lines),
423 back_inserter(return_values),
424 [](CommandLine const& command_line)
425 {
426 INFO("---------- {:s} begin ----------",
427 command_line.diff_tool_name);
428 int const return_value =
429 std::system(command_line.command_line.c_str());
430 if (return_value != 0)
431 {
432 WARN("Value {:d} was returned by '{:s}'.", return_value,
433 command_line.command_line);
434 }
435 INFO("---------- {:s} end ----------\n",
436 command_line.diff_tool_name);
437 return return_value;
438 });
439 return (!return_values.empty() || !fail_on_empty) &&
440 all_of(begin(return_values), end(return_values),
441 [](int const return_value) { return return_value == 0; });
442}
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:327

References ApplicationsLib::TestDefinition::CommandLine::command_line, ApplicationsLib::TestDefinition::CommandLine::diff_tool_name, INFO(), and WARN().

Referenced by runTests(), runTests(), and runTestsExcluding().

◆ runTests() [1/2]

bool ApplicationsLib::TestDefinition::runTests ( ) const

Runs all configured test command lines.

Definition at line 383 of file TestDefinition.cpp.

384{
385 return runCommandLines(_command_lines, true);
386}
static bool runCommandLines(std::vector< CommandLine > const &command_lines, bool fail_on_empty)

References _command_lines, and runCommandLines().

◆ runTests() [2/2]

bool ApplicationsLib::TestDefinition::runTests ( std::string_view diff_tool_name) const

Runs only test command lines for the given diff tool.

Definition at line 388 of file TestDefinition.cpp.

389{
390 std::vector<CommandLine> command_lines;
391 copy_if(begin(_command_lines), end(_command_lines),
392 back_inserter(command_lines),
393 [diff_tool_name](CommandLine const& command_line)
394 { return command_line.diff_tool_name == diff_tool_name; });
395
396 return runCommandLines(command_lines, true);
397}

References _command_lines, ApplicationsLib::TestDefinition::CommandLine::diff_tool_name, and runCommandLines().

◆ runTestsExcluding()

bool ApplicationsLib::TestDefinition::runTestsExcluding ( std::string_view diff_tool_name) const

Runs all test command lines except those for the given diff tool.

Definition at line 399 of file TestDefinition.cpp.

401{
402 std::vector<CommandLine> command_lines;
403 copy_if(begin(_command_lines), end(_command_lines),
404 back_inserter(command_lines),
405 [diff_tool_name](CommandLine const& command_line)
406 { return command_line.diff_tool_name != diff_tool_name; });
407
408 return runCommandLines(command_lines, false);
409}

References _command_lines, ApplicationsLib::TestDefinition::CommandLine::diff_tool_name, and runCommandLines().

Member Data Documentation

◆ _command_lines

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

◆ _output_files

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

Definition at line 60 of file TestDefinition.h.

Referenced by TestDefinition(), and getOutputFiles().


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