OGS
anonymous_namespace{TestDefinition.cpp} Namespace Reference

Functions

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.
std::string findVtkdiff ()

Function Documentation

◆ findVtkdiff()

std::string anonymous_namespace{TestDefinition.cpp}::findVtkdiff ( )

Tries to find a vtkdiff executable by testing 'path/vtkdiff –version' calls for various paths.

Definition at line 63 of file TestDefinition.cpp.

64{
65 // Try to read the VTKDIFF_EXE environment variable.
66 if (const char* vtkdiff_exe_environment_variable =
67 std::getenv("VTKDIFF_EXE"))
68 {
69 std::string const vtkdiff_exe{vtkdiff_exe_environment_variable};
70 DBUG("VTKDIFF_EXE set to {:s}.", vtkdiff_exe);
71
72 //
73 // Sanity checks.
74 //
75 { // The base name is 'vtkdiff'
76 auto const& base_name =
78 if (base_name != "vtkdiff")
79 {
81 "The VTKDIFF_EXE environment variable does not point to "
82 "'vtkdiff'. VTKDIFF_EXE='{:s}'",
83 vtkdiff_exe);
84 }
85 }
86 { // vtkdiff must exist.
87 if (!BaseLib::IsFileExisting(vtkdiff_exe))
88 {
90 "The VTKDIFF_EXE points to a non-existing file. "
91 "VTKDIFF_EXE='{:s}'",
92 vtkdiff_exe);
93 }
94 }
95
96 //
97 // Test the actual call.
98 //
99 int const return_value =
100 // TODO (naumov) replace system call with output consuming call
101 // (fork + execl seems to be more safe), and extract the vtkdiff
102 // call to common function. Also properly escape all strings in
103 // command lines.
104 // Reference for POSIX and Windows:
105 // https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87152177
106 // Take care when using fork, which might copy resources.
107 std::system((vtkdiff_exe + " --version").c_str());
108 if (return_value == 0)
109 {
110 return vtkdiff_exe;
111 }
112 WARN(
113 "Calling {:s} from the VTKDIFF_EXE environment variable didn't "
114 "work as expected. Return value was {:d}.",
115 vtkdiff_exe, return_value);
116 }
117
118 std::string const vtkdiff_exe{"vtkdiff"};
119 std::vector<std::string> const paths = {"", "bin"};
120 auto const path =
121 find_if(begin(paths), end(paths),
122 [&vtkdiff_exe](std::string const& path)
123 {
124 int const return_value =
125 // TODO (naumov) replace system call with output
126 // consuming call as in an above todo comment.
127 std::system((BaseLib::joinPaths(path, vtkdiff_exe) +
128 " --version")
129 .c_str());
130 return return_value == 0;
131 });
132 if (path == end(paths))
133 {
134 OGS_FATAL("vtkdiff not found.");
135 }
136 return BaseLib::joinPaths(*path, vtkdiff_exe);
137}
#define OGS_FATAL(...)
Definition Error.h:19
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists.
Definition FileTools.cpp:23
std::string extractBaseNameWithoutExtension(std::string const &pathname)
std::string joinPaths(std::string const &pathA, std::string const &pathB)

References DBUG(), BaseLib::extractBaseNameWithoutExtension(), BaseLib::IsFileExisting(), BaseLib::joinPaths(), OGS_FATAL, and WARN().

◆ isConvertibleToDouble()

bool anonymous_namespace{TestDefinition.cpp}::isConvertibleToDouble ( std::string const & s)

Test if the given string is convertible to a valid double value, not a NaN.

Definition at line 24 of file TestDefinition.cpp.

25{
26 std::size_t pos = 0;
27 double value;
28 try
29 {
30 value = std::stod(s, &pos);
31 }
32 catch (...)
33 {
34 ERR("The given string '{:s}' is not convertible to double.", s);
35 return false;
36 }
37 if (pos != s.size())
38 {
39 ERR("Only {:d} characters were used for double conversion of string "
40 "'{:s}'",
41 pos, s);
42 return false;
43 }
44
45 if (std::isnan(value))
46 {
47 ERR("The given string '{:s}' results in a NaN value.", s);
48 return false;
49 }
50 return true;
51}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40

References ERR().

◆ safeString()

std::string anonymous_namespace{TestDefinition.cpp}::safeString ( std::string const & s)

Wraps a string into double ticks.

Definition at line 54 of file TestDefinition.cpp.

55{
56 std::stringstream ss;
57 ss << std::quoted(s);
58 return ss.str();
59}