kaycxx-test
C++ unit test framework
Loading...
Searching...
No Matches
assert_close.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Klaus Reimer <k@ailis.de>
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
11#include <format>
12#include <optional>
13#include <source_location>
14#include <string_view>
15#include <utility>
16
18#include <kaycxx/test/detail/close.hpp>
19#include <kaycxx/test/detail/to_string.hpp>
20
21namespace kaycxx::test {
22
40template <typename Actual, typename Expected>
41requires detail::close_supported<Actual, Expected>
43 Actual const& actual,
44 Expected const& expected,
45 int precision = 2,
46 std::string_view reason = {},
47 std::optional<std::source_location> location = std::source_location::current()
48) {
49 if (!detail::close(actual, expected, precision)) {
50 auto actual_string = detail::to_string(actual);
51 auto expected_string = detail::to_string(expected);
52 auto message = std::format(
53 "Expected <{}> to be close to <{}> (precision: {} decimals)",
54 actual_string,
55 expected_string,
56 precision
57 );
58 throw assertion_error(
59 message,
60 reason,
61 std::move(actual_string),
62 std::move(expected_string),
63 location
64 );
65 }
66}
67
68} // namespace kaycxx::test
Defines the assertion_error type.
Unit test framework functions and types.
Definition test.hpp:15
void assert_close(Actual const &actual, Expected const &expected, int precision=2, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
Asserts that two numeric values are close enough.
Definition assert_close.hpp:42