46 using C = std::remove_cvref_t<T>;
47 if constexpr (std::is_floating_point_v<C>)
49 return std::numeric_limits<C>::digits10;
51 else if constexpr (std::is_integral_v<C>)
53 return std::numeric_limits<C>::digits10;
55 else if constexpr (std::is_class_v<C> && std::is_convertible_v<const C&, double>)
57 return std::numeric_limits<double>::digits10;
74 using C = std::remove_cvref_t<T>;
75 return std::is_floating_point_v<C> || (std::is_class_v<C> && std::is_convertible_v<const C&, double>);
81 inline void trim(std::string& s)
83 const char* ws =
" \t\n\r";
84 const size_t b = s.find_first_not_of(ws);
85 if (b == std::string::npos)
90 const size_t e = s.find_last_not_of(ws);
91 s = s.substr(b, e - b + 1);
97 inline std::vector<std::string>
split(
const std::string& s,
char splitter)
99 std::vector<std::string> out;
100 if (s.empty())
return out;
102 for (
size_t i = 0; i < s.size(); ++i)
104 if (s[i] == splitter)
106 out.emplace_back(s, begin, i - begin);
110 out.emplace_back(s, begin, s.size() - begin);
115 inline std::string
join(
const std::vector<std::string>& v,
const char* sep)
118 for (
size_t i = 0; i < v.size(); ++i)
130 template <
typename T>
131 inline void appendNumeric(T value, std::string& target,
int precision,
bool fixed_format)
133 if (std::isnan(value)) { target +=
"NaN";
return; }
134 if (std::isinf(value)) { target += (value < T(0)) ?
"-inf" :
"inf";
return; }
136 std::to_chars_result fc;
140 T abs_val = (value < 0) ? -value : value;
141 bool use_scientific = fixed_format
142 ? (abs_val != T(0) && (abs_val >= T(1e4) || abs_val < T(1e-2)))
143 : (abs_val != T(0) && (abs_val >= T(1e4) || abs_val < T(1e-2)));
149 fc = std::to_chars(buf, buf +
sizeof(buf), value, std::chars_format::scientific, 3);
155 fc = std::to_chars(buf, buf +
sizeof(buf), value, std::chars_format::scientific);
158 else if (fixed_format)
160 fc = std::to_chars(buf, buf +
sizeof(buf), value, std::chars_format::fixed, precision);
164 fc = std::to_chars(buf, buf +
sizeof(buf), value, std::chars_format::fixed, precision);
167 if (fc.ec == std::errc{})
169 const char* end = fc.ptr;
172 const char* e_pos =
reinterpret_cast<const char*
>(std::memchr(buf,
'e', end - buf));
175 const char* e_orig = e_pos;
178 const char* dot =
reinterpret_cast<const char*
>(std::memchr(buf,
'.', e_orig - buf));
181 while (e_pos > dot + 1 && *(e_pos - 1) ==
'0') --e_pos;
182 if (e_pos == dot + 1) e_pos = dot + 2;
184 target.append(buf,
static_cast<size_t>(e_pos - buf));
185 if (!dot) target +=
".0";
189 const char* exp_start = e_orig + 1;
190 if (exp_start < end && *exp_start ==
'+')
192 size_t exp_len =
static_cast<size_t>(end - exp_start);
193 target.append(exp_start, exp_len);
198 const char* dot =
reinterpret_cast<const char*
>(std::memchr(buf,
'.', end - buf));
201 while (end > dot + 1 && *(end - 1) ==
'0') --end;
202 if (end == dot + 1) end = dot + 2;
204 else if constexpr (std::is_floating_point_v<T>)
206 target.append(buf,
static_cast<size_t>(end - buf));
210 target.append(buf,
static_cast<size_t>(end - buf));
214 target += std::to_string(
static_cast<double>(value));
222 inline std::string
toString(
const std::string& s) {
return s; }
223 inline std::string
toString(std::string_view sv) {
return std::string(sv); }
224 inline std::string
toString(
const char* s) {
return std::string(s); }
225 inline std::string
toString(
char c) {
return std::string(1, c); }
226 template <
typename T>
229 static_assert(std::is_arithmetic_v<T>,
230 "TEST_EQUAL(std::string, x): x must be a string, character or number. "
231 "For other types, stringify explicitly in the test.");
233 if constexpr (std::is_same_v<T, bool>)
237 else if constexpr (std::is_floating_point_v<T>)
239 appendNumeric(v, r, std::numeric_limits<T>::digits10,
false);
244 auto [p, ec] = std::to_chars(buf, buf +
sizeof(buf), v);
245 if (ec == std::errc{}) r.append(buf, p);
253 template <
typename T>
254 concept Streamable =
requires(std::ostream& os,
const T& t) { os << t; };
258 template <
typename T>
261 using C = std::remove_cvref_t<T>;
266 else if constexpr (std::ranges::input_range<C>)
270 for (
const auto& e : v)
272 if (!
first) os <<
", ";
274 using E = std::remove_cvref_t<
decltype(e)>;
275 if constexpr (std::is_arithmetic_v<E>)
292 os <<
"<value of unprintable type - provide operator<< or compare via TEST_TRUE>";
299 inline std::filesystem::path
to_path(
const std::string& s)
304 return std::filesystem::path(std::u8string(
reinterpret_cast<const char8_t*
>(s.data()), s.size()));
306 catch (
const std::system_error&)
308 return std::filesystem::path(s);
311 return std::filesystem::path(std::u8string(
reinterpret_cast<const char8_t*
>(s.data()), s.size()));
323 template <
typename FloatingPo
intType>
329 template <
typename FloatingPo
intType>
335 template <
typename FloatingPo
intType>
OpenMS looks in the< code > OPENMS_THERMO_MANAGED_DIR</code > environment variable first
Definition common-cmake-parameters.doxygen:179
Definition ClassTestUtils.h:254
@ C
c-ion (N-terminal, ETD)
std::string join(const std::vector< std::string > &v, const char *sep)
Join with a separator (display purposes only).
Definition ClassTestUtils.h:115
std::string toString(const std::string &s)
Definition ClassTestUtils.h:222
std::filesystem::path to_path(const std::string &s)
Definition ClassTestUtils.h:299
void trim(std::string &s)
Remove leading and trailing whitespace (' ', '\t', '\n', '\r'), like StringUtils::trim.
Definition ClassTestUtils.h:81
void appendNumeric(T value, std::string &target, int precision, bool fixed_format)
Definition ClassTestUtils.h:131
std::vector< std::string > split(const std::string &s, char splitter)
Definition ClassTestUtils.h:97
void printValue(std::ostream &os, const T &v)
Definition ClassTestUtils.h:259
constexpr bool isRealType(const T &)
Whether TEST_REAL_SIMILAR may convert a value of this type to double.
Definition ClassTestUtils.h:72
constexpr int writtenDigits(const T &=T())
Decimal digits to print for a value in TEST_REAL_SIMILAR reports.
Definition ClassTestUtils.h:44
FloatingPointType value
Definition ClassTestUtils.h:326
PrecisionWrapped< FloatingPointType > precisionWrapper(const FloatingPointType value)
Definition ClassTestUtils.h:330
std::ostream & operator<<(std::ostream &os, const PrecisionWrapped< FloatingPointType > &p)
Definition ClassTestUtils.h:336
Stream wrapper printing a floating point value with full precision.
Definition ClassTestUtils.h:325
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19