OGS
ApplicationsLib::TestDefinition Class Referencefinal

Detailed Description

Definition at line 27 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 151 of file TestDefinition.cpp.

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

315{
316 return _output_files;
317}

References _output_files.

◆ numberOfTests()

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

Definition at line 319 of file TestDefinition.cpp.

320{
321 return size(_command_lines);
322}
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 292 of file TestDefinition.cpp.

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

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

Member Data Documentation

◆ _command_lines

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

Definition at line 42 of file TestDefinition.h.

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

◆ _output_files

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

Definition at line 43 of file TestDefinition.h.

Referenced by TestDefinition(), and getOutputFiles().


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