OGS
ApplicationsLib::TestDefinition Class Referencefinal

Detailed Description

Definition at line 25 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)
 
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 149 of file TestDefinition.cpp.

152 {
153  if (reference_path.empty())
154  {
155  OGS_FATAL(
156  "Reference path containing expected result files can not be "
157  "empty.");
158  }
159 
160  std::string const vtkdiff = findVtkdiff();
161 
162  // Construct command lines for each entry.
164  auto const& vtkdiff_configs = config_tree.getConfigSubtreeList("vtkdiff");
165  _command_lines.reserve(vtkdiff_configs.size());
166  for (auto const& vtkdiff_config : vtkdiff_configs)
167  {
168  std::string const& field_name =
170  vtkdiff_config.getConfigParameter<std::string>("field");
171  DBUG("vtkdiff will compare field '{:s}'.", field_name);
172 
173  std::vector<std::string> filenames;
174  if (auto const regex_string =
176  vtkdiff_config.getConfigParameterOptional<std::string>("regex"))
177  {
178  // TODO: insert rank into regex for mpi case
179  DBUG("vtkdiff regex is '{}'.", *regex_string);
180  auto const regex = std::regex(*regex_string);
181  for (auto const& p : std::filesystem::directory_iterator(
182  std::filesystem::path(reference_path)))
183  {
184  auto const filename = p.path().filename().string();
185  if (std::regex_match(filename, regex))
186  {
187  DBUG(" -> matched '{}'", filename);
188  filenames.push_back(filename);
189  }
190  }
191  }
192  else
193  {
194  std::string filename =
196  vtkdiff_config.getConfigParameter<std::string>("file");
197 #ifdef USE_PETSC
198  int mpi_size;
199  MPI_Comm_size(PETSC_COMM_WORLD, &mpi_size);
200  if (mpi_size > 1)
201  {
202  int rank;
203  MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
204  filename =
206  filename) +
207  "_" + std::to_string(rank) + ".vtu";
208  }
209 #endif // OGS_USE_PETSC
210  filenames.push_back(filename);
211  }
212 
213  if (empty(filenames))
214  {
215  OGS_FATAL(
216  "No files from test definitions were added for tests but {} "
217  "{:s} specified.",
218  std::size(vtkdiff_configs),
219  (std::size(vtkdiff_configs) == 1 ? "test was" : "tests were"));
220  }
221 
222  auto const absolute_tolerance =
224  vtkdiff_config.getConfigParameter<std::string>("absolute_tolerance",
225  "");
226  if (!absolute_tolerance.empty() &&
227  !isConvertibleToDouble(absolute_tolerance))
228  {
229  OGS_FATAL(
230  "The absolute tolerance value '{:s}' is not convertible to "
231  "double.",
232  absolute_tolerance);
233  }
234  std::string const absolute_tolerance_parameter =
235  "--abs " + absolute_tolerance;
236  auto const relative_tolerance =
238  vtkdiff_config.getConfigParameter<std::string>("relative_tolerance",
239  "");
240  if (!relative_tolerance.empty() &&
241  !isConvertibleToDouble(relative_tolerance))
242  {
243  OGS_FATAL(
244  "The relative tolerance value '{:s}' is not convertible to "
245  "double.",
246  relative_tolerance);
247  }
248  std::string const relative_tolerance_parameter =
249  "--rel " + relative_tolerance;
250 
251  for (auto const& filename : filenames)
252  {
253  std::string const& output_filename =
254  BaseLib::joinPaths(output_directory, filename);
255  _output_files.push_back(output_filename);
256  std::string const& reference_filename =
257  BaseLib::joinPaths(reference_path, filename);
258 
259  //
260  // Construct command line.
261  //
262  std::string command_line =
263  vtkdiff + " -a " + safeString(field_name) + " -b " +
264  safeString(field_name) + " " + safeString(reference_filename) +
265  " " + safeString(output_filename) + " " +
266  absolute_tolerance_parameter + " " +
267  relative_tolerance_parameter;
268  INFO("Will run '{:s}'", command_line);
269  _command_lines.emplace_back(std::move(command_line));
270  }
271  }
272 }
#define OGS_FATAL(...)
Definition: Error.h:26
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition: Logging.h:34
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition: Logging.h:29
std::vector< std::string > _output_files
std::vector< std::string > _command_lines
std::string joinPaths(std::string const &pathA, std::string const &pathB)
Definition: FileTools.cpp:213
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(), anonymous_namespace{TestDefinition.cpp}::findVtkdiff(), BaseLib::ConfigTree::getConfigSubtreeList(), MeshLib::IO::getVtuFileNameForPetscOutputWithoutExtension(), INFO(), anonymous_namespace{TestDefinition.cpp}::isConvertibleToDouble(), BaseLib::joinPaths(), OGS_FATAL, and anonymous_namespace{TestDefinition.cpp}::safeString().

Member Function Documentation

◆ getOutputFiles()

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

Definition at line 296 of file TestDefinition.cpp.

297 {
298  return _output_files;
299 }

References _output_files.

◆ numberOfTests()

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

Definition at line 301 of file TestDefinition.cpp.

302 {
303  return size(_command_lines);
304 }

References _command_lines.

◆ runTests()

bool ApplicationsLib::TestDefinition::runTests ( ) const

Definition at line 274 of file TestDefinition.cpp.

275 {
276  std::vector<int> return_values;
277  transform(begin(_command_lines), end(_command_lines),
278  back_inserter(return_values),
279  [](std::string const& command_line)
280  {
281  INFO("---------- vtkdiff begin ----------");
282  int const return_value = std::system(command_line.c_str());
283  if (return_value != 0)
284  {
285  WARN("Return value {:d} was returned by '{:s}'.",
286  return_value, command_line);
287  }
288  INFO("---------- vtkdiff end ----------\n");
289  return return_value;
290  });
291  return !return_values.empty() &&
292  all_of(begin(return_values), end(return_values),
293  [](int const& return_value) { return return_value == 0; });
294 }
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition: Logging.h:39
constexpr bool all_of(List const &values)
Checks if all of the elements in the given list are true.
Definition: Algorithm.h:341

References _command_lines, BaseLib::all_of(), INFO(), and WARN().

Member Data Documentation

◆ _command_lines

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

Definition at line 40 of file TestDefinition.h.

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

◆ _output_files

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

Definition at line 41 of file TestDefinition.h.

Referenced by TestDefinition(), and getOutputFiles().


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