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 71 of file TestDefinition.cpp.

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

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

References ERR().

◆ safeString()

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

Wraps a string into double ticks.

Definition at line 62 of file TestDefinition.cpp.

63{
64 std::stringstream ss;
65 ss << std::quoted(s);
66 return ss.str();
67}