Iros
 
Loading...
Searching...
No Matches
assert_binary.h
Go to the documentation of this file.
1#pragma once
2
8#include "di/function/equal.h"
11#include "di/function/greater.h"
12#include "di/function/less.h"
14#include "di/math/functions.h"
15#include "di/math/to_unsigned.h"
16#include "di/meta/compare.h"
19
21template<typename T, typename U>
22void binary_assert_fail(char const* expression, T&& a, U&& b, util::SourceLocation loc) {
23 // Allocate a 512 byte buffer on the stack to stringify a and b. We'd don't want to allocate here because this
24 // assertion could indicate heap corruption, or even be triggered before the heap is initialized.
27
28 auto lhs_context = TargetContext {};
29 auto const* lhs_data_pointer = static_cast<char const*>(nullptr);
30 if constexpr (concepts::Formattable<T>) {
32 di::container::string::StringViewImpl<Enc>(encoding::assume_valid, u8"{}", 2),
34 lhs_data_pointer = reinterpret_cast<char const*>(lhs_context.output().span().data());
35 }
36 auto rhs_context = TargetContext {};
37 auto const* rhs_data_pointer = static_cast<char const*>(nullptr);
38 if constexpr (concepts::Formattable<U>) {
40 di::container::string::StringViewImpl<Enc>(encoding::assume_valid, u8"{}", 2),
42 rhs_data_pointer = reinterpret_cast<char const*>(rhs_context.output().span().data());
43 }
44 assert_fail(expression, lhs_data_pointer, rhs_data_pointer, loc);
45}
46
47template<typename F, typename T, typename U>
48constexpr void binary_assert(F op, char const* expression, T&& a, U&& b, util::SourceLocation loc) {
49 if (!op(a, b)) {
50 if consteval {
52 } else {
53 binary_assert_fail(expression, util::forward<T>(a), util::forward<U>(b), loc);
54 }
55 }
56}
57}
58
59#define DI_ASSERT_EQ(a, b) \
60 ::di::assert::detail::binary_assert(::di::function::equal, "" #a " == " #b, (a), (b), \
61 ::di::util::SourceLocation::current())
62
63#define DI_ASSERT_APPROX_EQ(a, b) \
64 ::di::assert::detail::binary_assert(::di::approximately_equal, "" #a " ~= " #b, (a), (b), \
65 ::di::util::SourceLocation::current())
66
67#define DI_ASSERT_NOT_EQ(a, b) \
68 ::di::assert::detail::binary_assert(::di::function::not_equal, "" #a " != " #b, (a), (b), \
69 ::di::util::SourceLocation::current())
70#define DI_ASSERT_LT(a, b) \
71 ::di::assert::detail::binary_assert(::di::function::less, "" #a " < " #b, (a), (b), \
72 ::di::util::SourceLocation::current())
73#define DI_ASSERT_LT_EQ(a, b) \
74 ::di::assert::detail::binary_assert(::di::function::equal_or_less, "" #a " <= " #b, (a), (b), \
75 ::di::util::SourceLocation::current())
76#define DI_ASSERT_GT(a, b) \
77 ::di::assert::detail::binary_assert(::di::function::greater, "" #a " > " #b, (a), (b), \
78 ::di::util::SourceLocation::current())
79#define DI_ASSERT_GT_EQ(a, b) \
80 ::di::assert::detail::binary_assert(::di::function::equal_or_greater, "" #a " >= " #b, (a), (b), \
81 ::di::util::SourceLocation::current())
82
83#if !defined(DI_NO_GLOBALS) && !defined(DI_NO_GLOBAL_ASSERT)
84#define ASSERT_EQ DI_ASSERT_EQ
85#define ASSERT_APPROX_EQ DI_ASSERT_APPROX_EQ
86#define ASSERT_NOT_EQ DI_ASSERT_NOT_EQ
87#define ASSERT_LT DI_ASSERT_LT
88#define ASSERT_LT_EQ DI_ASSERT_LT_EQ
89#define ASSERT_GT DI_ASSERT_GT
90#define ASSERT_GT_EQ DI_ASSERT_GT_EQ
91#endif
Definition string_view_impl_forward_declaration.h:7
Definition utf8_encoding.h:107
Definition bounded_format_context.h:9
Definition source_location.h:10
Definition formattable.h:9
Definition assert_binary.h:20
constexpr void binary_assert(F op, char const *expression, T &&a, U &&b, util::SourceLocation loc)
Definition assert_binary.h:48
void assert_fail(char const *source_text, char const *lhs_message, char const *rhs_message, util::SourceLocation loc)
Definition assert_interface.h:13
void binary_assert_fail(char const *expression, T &&a, U &&b, util::SourceLocation loc)
Definition assert_binary.h:22
constexpr auto make_format_args(Types &&... values)
Definition make_format_args.h:9
constexpr auto vpresent_encoded_context
Definition vpresent_encoded_context.h:61
__UINT8_TYPE__ u8
Definition integers.h:9
void compile_time_fail()
Definition compile_time_fail.h:7
constexpr auto data
Definition data.h:51