6#ifndef MIMICPP_MATCHERS_FLOATING_POINT_MATCHERS_HPP
7#define MIMICPP_MATCHERS_FLOATING_POINT_MATCHERS_HPP
15#ifndef MIMICPP_DETAIL_IS_MODULE
36 consteval auto NaN() noexcept
39 []<std::floating_point T>(
const T target)
noexcept {
40 return std::isnan(target);
42 "is not a number (NaN)",
48 void check_fp_value(
const std::floating_point
auto value)
53 throw std::runtime_error{
"Value must be not NaN and not infinity."};
57 void check_fp_epsilon(
const std::floating_point
auto epsilon)
59 if (std::isnan(epsilon)
60 || std::isinf(epsilon)
63 throw std::runtime_error{
"Epsilon must be not NaN, not infinity and not less or equal 0."};
78 const std::floating_point
auto value,
79 const std::floating_point
auto epsilon)
81 detail::check_fp_value(value);
82 detail::check_fp_epsilon(epsilon);
85 [](
const std::floating_point
auto target,
const auto val,
const auto eps) {
86 return std::abs(target - val) <= eps;
88 "is approximately {} +- {}",
89 "is not approximately {} +- {}",
90 std::make_tuple(value, epsilon)};
113 const std::floating_point
auto value,
114 const std::floating_point
auto relEpsilon)
116 detail::check_fp_value(value);
117 detail::check_fp_epsilon(relEpsilon);
120 [](
const std::floating_point
auto target,
const auto val,
const auto rel) {
122 if (!std::isinf(target))
124 const auto absDiff = std::abs(target - val);
125 const auto scaledEpsilon = rel * std::max(std::abs(target), std::abs(val));
126 return absDiff <= scaledEpsilon;
131 "is approximately {} +- ({} * max(|lhs|, |rhs|))",
132 "is not approximately {} +- ({} * max(|lhs|, |rhs|))",
133 std::make_tuple(value, relEpsilon)};
148 template <std::
floating_po
int Float>
154 std::numeric_limits<Float>::epsilon() * Float{100});
#define MIMICPP_DETAIL_MODULE_EXPORT
Definition Config.hpp:19
Generic matcher and the basic building block of most of the built-in matchers.
Definition GeneralMatchers.hpp:75
consteval auto NaN() noexcept
Tests, whether the floating-point target is NaN.
Definition FloatingPointMatchers.hpp:36
constexpr auto approx_rel(const std::floating_point auto value, const std::floating_point auto relEpsilon)
Tests, whether the floating-point target is approximately equal to value.
Definition FloatingPointMatchers.hpp:112
constexpr auto approx_abs(const std::floating_point auto value, const std::floating_point auto epsilon)
Tests, whether the floating-point target is approximately equal to value.
Definition FloatingPointMatchers.hpp:77
Definition FloatingPointMatchers.hpp:25