kaycxx-assert
C++ assertion library
Loading...
Searching...
No Matches
assert_not_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 <string_view>
13#include <utility>
14
16#include <kaycxx/assert/detail/source_location.hpp>
17#include <kaycxx/assert/detail/close.hpp>
18#include <kaycxx/assert/detail/to_string.hpp>
19
20namespace kaycxx::assert {
21
39template <typename Actual, typename Expected>
40requires detail::close_supported<Actual, Expected>
42 Actual const& actual,
43 Expected const& expected,
44 int precision = 2,
45 std::string_view reason = {},
46 std::optional<std::source_location> location = KAYCXX_ASSERT_DEFAULT_SOURCE
47) {
48 if (detail::close(actual, expected, precision)) {
49 auto actual_string = detail::to_string(actual);
50 auto expected_string = detail::to_string(expected);
51 auto message = std::format(
52 "Expected <{}> not to be close to <{}> (precision: {} decimals)",
53 actual_string,
54 expected_string,
55 precision
56 );
57 throw assertion_error(
58 message,
59 reason,
60 std::move(actual_string),
61 std::move(expected_string),
62 location
63 );
64 }
65}
66
67} // namespace kaycxx::assert
Defines the assertion_error type.
Assertion functions and related types.
Definition assert.hpp:15
void assert_not_close(Actual const &actual, Expected const &expected, int precision=2, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE)
Asserts that two numeric values are not close enough.
Definition assert_not_close.hpp:41