6#ifndef MIMICPP_MATCHERS_FLOATING_POINT_MATCHERS_HPP
7#define MIMICPP_MATCHERS_FLOATING_POINT_MATCHERS_HPP
33 consteval auto NaN() noexcept
36 []<std::floating_point T>(
const T target)
noexcept {
37 return std::isnan(target);
39 "is not a number (NaN)",
45 void check_fp_value(
const std::floating_point
auto value)
50 throw std::runtime_error{
"Value must be not NaN and not infinity."};
54 void check_fp_epsilon(
const std::floating_point
auto epsilon)
56 if (std::isnan(epsilon)
57 || std::isinf(epsilon)
60 throw std::runtime_error{
"Epsilon must be not NaN, not infinity and not less or equal 0."};
75 const std::floating_point
auto value,
76 const std::floating_point
auto epsilon)
78 detail::check_fp_value(value);
79 detail::check_fp_epsilon(epsilon);
82 [](
const std::floating_point
auto target,
const auto val,
const auto eps) {
83 return std::abs(target - val) <= eps;
85 "is approximately {} +- {}",
86 "is not approximately {} +- {}",
87 std::make_tuple(value, epsilon)};
110 const std::floating_point
auto value,
111 const std::floating_point
auto relEpsilon)
113 detail::check_fp_value(value);
114 detail::check_fp_epsilon(relEpsilon);
117 [](
const std::floating_point
auto target,
const auto val,
const auto rel) {
119 if (!std::isinf(target))
121 const auto absDiff = std::abs(target - val);
122 const auto scaledEpsilon = rel * std::max(std::abs(target), std::abs(val));
123 return absDiff <= scaledEpsilon;
128 "is approximately {} +- ({} * max(|lhs|, |rhs|))",
129 "is not approximately {} +- ({} * max(|lhs|, |rhs|))",
130 std::make_tuple(value, relEpsilon)};
145 template <std::
floating_po
int Float>
151 std::numeric_limits<Float>::epsilon() * Float{100});
Generic matcher and the basic building block of most of the built-in matchers.
Definition GeneralMatchers.hpp:68
consteval auto NaN() noexcept
Tests, whether the floating-point target is NaN.
Definition FloatingPointMatchers.hpp:33
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:109
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:74
Definition FloatingPointMatchers.hpp:22