ZeroErr
Loading...
Searching...
No Matches
assert.h
Go to the documentation of this file.
1#pragma once
2
4
5#include "zeroerr/format.h"
8#include "zeroerr/print.h"
9
10#include <cstdint>
11#include <exception>
12#include <iostream>
13
15
16
17// This macro will be redefined in the log.h header, so the global context variable could be
18// envolved only when we use log.h at the same time. If you didn't use log.h, this is still a
19// header-only library.
20#ifndef ZEROERR_G_CONTEXT_SCOPE
21#define ZEROERR_G_CONTEXT_SCOPE(x)
22#endif
23
24
34#ifndef ZEROERR_PRINT_ASSERT_DEFAULT_PRINTER
35#define ZEROERR_PRINT_ASSERT_DEFAULT_PRINTER(cond, level, ...) \
36 do { \
37 if (cond) { \
38 switch (zeroerr::assert_level::ZEROERR_CAT(level, _l)) { \
39 case zeroerr::assert_level::ZEROERR_WARN_l: \
40 std::cerr << zeroerr::FgYellow << "WARN" << zeroerr::Reset; \
41 break; \
42 case zeroerr::assert_level::ZEROERR_ERROR_l: \
43 std::cerr << zeroerr::FgRed << "ERROR" << zeroerr::Reset; \
44 break; \
45 case zeroerr::assert_level::ZEROERR_FATAL_l: \
46 std::cerr << zeroerr::FgMagenta << "FATAL" << zeroerr::Reset; \
47 break; \
48 } \
49 std::cerr << zeroerr::format(__VA_ARGS__) << std::endl; \
50 } \
51 } while (0)
52#endif
53
54// pattern is optional: call sites pass "" __VA_ARGS__ so empty message args still
55// provide a pattern token. ##__VA_ARGS__ drops the trailing comma when there are no
56// extra format arguments (required for clang/clangd; MSVC accepts the extension too).
57ZEROERR_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wgnu-zero-variadic-macro-arguments")
58#define ZEROERR_PRINT_ASSERT(cond, level, pattern, ...) \
59 ZEROERR_PRINT_ASSERT_DEFAULT_PRINTER(cond, level, " Assertion Failed:\n{msg}" pattern, \
60 assertion_data.log(), ##__VA_ARGS__)
62
63#define ZEROERR_ASSERT_EXP(cond, level, expect_throw, is_false, ...) \
64 ZEROERR_FUNC_SCOPE_BEGIN { \
65 zeroerr::assert_info info{zeroerr::assert_level::ZEROERR_CAT(level, _l), \
66 zeroerr::assert_throw::expect_throw, is_false}; \
67 \
68 zeroerr::AssertionData assertion_data(__FILE__, __LINE__, #cond, info); \
69 try { \
70 assertion_data.setResult(zeroerr::ExpressionDecomposer() << cond); \
71 } catch (const std::exception& e) { \
72 assertion_data.setException(e); \
73 } \
74 zeroerr::detail::context_helper< \
75 decltype(_ZEROERR_TEST_CONTEXT), \
76 std::is_same<decltype(_ZEROERR_TEST_CONTEXT), \
77 const bool>::value>::setContext(assertion_data, _ZEROERR_TEST_CONTEXT); \
78 ZEROERR_PRINT_ASSERT(assertion_data.passed == false, level, "" __VA_ARGS__); \
79 if (false) debug_break(); \
80 assertion_data(); \
81 ZEROERR_FUNC_SCOPE_RET(assertion_data.passed); \
82 } \
83 ZEROERR_FUNC_SCOPE_END
84
85
86#define ZEROERR_ASSERT_CMP(lhs, op, rhs, level, expect_throw, is_false, ...) \
87 ZEROERR_FUNC_SCOPE_BEGIN { \
88 zeroerr::assert_info info{zeroerr::assert_level::ZEROERR_CAT(level, _l), \
89 zeroerr::assert_throw::expect_throw, is_false}; \
90 \
91 zeroerr::Printer print; \
92 print.isQuoted = false; \
93 zeroerr::AssertionData assertion_data(__FILE__, __LINE__, #lhs " " #op " " #rhs, info); \
94 try { \
95 assertion_data.setResult({(lhs)op(rhs), print(lhs, #op, rhs)}); \
96 } catch (const std::exception& e) { \
97 assertion_data.setException(e); \
98 } \
99 zeroerr::detail::context_helper< \
100 decltype(_ZEROERR_TEST_CONTEXT), \
101 std::is_same<decltype(_ZEROERR_TEST_CONTEXT), \
102 const bool>::value>::setContext(assertion_data, _ZEROERR_TEST_CONTEXT); \
103 ZEROERR_PRINT_ASSERT(assertion_data.passed == false, level, "" __VA_ARGS__); \
104 if (false) debug_break(); \
105 assertion_data(); \
106 ZEROERR_FUNC_SCOPE_RET(assertion_data.passed); \
107 } \
108 ZEROERR_FUNC_SCOPE_END
109
110
111#ifdef ZEROERR_NO_ASSERT
112
113#define CHECK(...)
114#define CHECK_NOT(...)
115#define CHECK_THROWS(...)
116#define REQUIRE(...)
117#define REQUIRE_NOT(...)
118#define REQUIRE_THROWS(...)
119#define ASSERT(...)
120#define ASSERT_NOT(...)
121#define ASSERT_THROWS(...)
122
123#define CHECK_EQ(...)
124#define CHECK_NE(...)
125#define CHECK_LT(...)
126#define CHECK_LE(...)
127#define CHECK_GT(...)
128#define CHECK_GE(...)
129
130#define REQUIRE_EQ(...)
131#define REQUIRE_NE(...)
132#define REQUIRE_LT(...)
133#define REQUIRE_LE(...)
134#define REQUIRE_GT(...)
135#define REQUIRE_GE(...)
136
137#define ASSERT_EQ(...)
138#define ASSERT_NE(...)
139#define ASSERT_LT(...)
140#define ASSERT_LE(...)
141#define ASSERT_GT(...)
142#define ASSERT_GE(...)
143
144#else
145// clang-format off
146ZEROERR_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wgnu-zero-variadic-macro-arguments")
147
148#define ZEROERR_CHECK(cond, ...) ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_WARN, no_throw, false, __VA_ARGS__))
149#define ZEROERR_CHECK_NOT(cond, ...) ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_WARN, no_throw, true, __VA_ARGS__))
150#define ZEROERR_CHECK_THROWS(cond, ...) ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_WARN, throws, false, __VA_ARGS__))
151#define ZEROERR_REQUIRE(cond, ...) ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_ERROR, no_throw, false, __VA_ARGS__))
152#define ZEROERR_REQUIRE_NOT(cond, ...) ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_ERROR, no_throw, true, __VA_ARGS__))
153#define ZEROERR_REQUIRE_THROWS(cond, ...) ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_ERROR, throws, false, __VA_ARGS__))
154#define ZEROERR_ASSERT(cond, ...) ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_FATAL, no_throw, false, __VA_ARGS__))
155#define ZEROERR_ASSERT_NOT(cond, ...) ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_FATAL, no_throw, true, __VA_ARGS__))
156#define ZEROERR_ASSERT_THROWS(cond, ...) ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_FATAL, throws, false, __VA_ARGS__))
157
158#define CHECK(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_CHECK(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
159#define CHECK_NOT(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_CHECK_NOT(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
160#define CHECK_THROWS(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_CHECK_THROWS(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
161#define REQUIRE(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_REQUIRE(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
162#define REQUIRE_NOT(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_REQUIRE_NOT(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
163#define REQUIRE_THROWS(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_REQUIRE_THROWS(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
164#define ASSERT(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_ASSERT(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
165#define ASSERT_NOT(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_ASSERT_NOT(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
166#define ASSERT_THROWS(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_ASSERT_THROWS(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
167
168#define ZEROERR_CHECK_EQ(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, ==, rhs, ZEROERR_WARN, no_throw, false, __VA_ARGS__)
169#define ZEROERR_CHECK_NE(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, !=, rhs, ZEROERR_WARN, no_throw, false, __VA_ARGS__)
170#define ZEROERR_CHECK_LT(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, <, rhs, ZEROERR_WARN, no_throw, false, __VA_ARGS__)
171#define ZEROERR_CHECK_LE(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, <=, rhs, ZEROERR_WARN, no_throw, false, __VA_ARGS__)
172#define ZEROERR_CHECK_GT(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, >, rhs, ZEROERR_WARN, no_throw, false, __VA_ARGS__)
173#define ZEROERR_CHECK_GE(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, >=, rhs, ZEROERR_WARN, no_throw, false, __VA_ARGS__)
174
175#define ZEROERR_REQUIRE_EQ(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, ==, rhs, ZEROERR_ERROR, no_throw, false, __VA_ARGS__)
176#define ZEROERR_REQUIRE_NE(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, !=, rhs, ZEROERR_ERROR, no_throw, false, __VA_ARGS__)
177#define ZEROERR_REQUIRE_LT(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, <, rhs, ZEROERR_ERROR, no_throw, false, __VA_ARGS__)
178#define ZEROERR_REQUIRE_LE(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, <=, rhs, ZEROERR_ERROR, no_throw, false, __VA_ARGS__)
179#define ZEROERR_REQUIRE_GT(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, >, rhs, ZEROERR_ERROR, no_throw, false, __VA_ARGS__)
180#define ZEROERR_REQUIRE_GE(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, >=, rhs, ZEROERR_ERROR, no_throw, false, __VA_ARGS__)
181
182#define ZEROERR_ASSERT_EQ(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, ==, rhs, ZEROERR_FATAL, no_throw, false, __VA_ARGS__)
183#define ZEROERR_ASSERT_NE(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, !=, rhs, ZEROERR_FATAL, no_throw, false, __VA_ARGS__)
184#define ZEROERR_ASSERT_LT(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, <, rhs, ZEROERR_FATAL, no_throw, false, __VA_ARGS__)
185#define ZEROERR_ASSERT_LE(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, <=, rhs, ZEROERR_FATAL, no_throw, false, __VA_ARGS__)
186#define ZEROERR_ASSERT_GT(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, >, rhs, ZEROERR_FATAL, no_throw, false, __VA_ARGS__)
187#define ZEROERR_ASSERT_GE(lhs, rhs, ...) ZEROERR_ASSERT_CMP(lhs, >=, rhs, ZEROERR_FATAL, no_throw, false, __VA_ARGS__)
188
189#define CHECK_EQ(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_CHECK_EQ(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
190#define CHECK_NE(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_CHECK_NE(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
191#define CHECK_LT(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_CHECK_LT(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
192#define CHECK_LE(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_CHECK_LE(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
193#define CHECK_GT(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_CHECK_GT(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
194#define CHECK_GE(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_CHECK_GE(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
195#define REQUIRE_EQ(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_REQUIRE_EQ(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
196#define REQUIRE_NE(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_REQUIRE_NE(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
197#define REQUIRE_LT(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_REQUIRE_LT(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
198#define REQUIRE_LE(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_REQUIRE_LE(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
199#define REQUIRE_GT(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_REQUIRE_GT(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
200#define REQUIRE_GE(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_REQUIRE_GE(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
201#define ASSERT_EQ(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_ASSERT_EQ(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
202#define ASSERT_NE(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_ASSERT_NE(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
203#define ASSERT_LT(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_ASSERT_LT(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
204#define ASSERT_LE(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_ASSERT_LE(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
205#define ASSERT_GT(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_ASSERT_GT(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
206#define ASSERT_GE(...) ZEROERR_SUPPRESS_VARIADIC_MACRO ZEROERR_EXPAND(ZEROERR_ASSERT_GE(__VA_ARGS__)) ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
207
209// clang-format on
210#endif
211
212
213// This symbol must be in the global namespace or anonymous namespace
214// used for checking the assert is inside testing or not
215namespace {
216constexpr bool _ZEROERR_TEST_CONTEXT = false;
217} // namespace
218
219
220namespace zeroerr {
221
223enum class assert_throw : uint8_t { no_throw, throws, throws_as };
224enum class assert_cmp : uint8_t { eq, ne, gt, ge, lt, le };
225
235
236
241struct AssertionData : std::exception {
242 const char* file; // file name
243 unsigned line; // line number
244 assert_info info; // assert info
245 bool passed; // if the assertion passed
246 std::string message; // the message of the assertion
247 std::string cond; // the condition of the assertion
248
249 AssertionData(const char* file, unsigned line, const char* cond, assert_info info)
250 : file(file), line(line), info(info) {
251 static std::string pattern = "zeroerr::ExpressionDecomposer() << ";
252 std::string cond_str(cond);
253 size_t pos = cond_str.find(pattern);
254 if (pos != std::string::npos) cond_str.replace(pos, pos + pattern.size(), "");
255 this->cond = cond_str;
256 }
257
258 void setResult(ExprResult&& result) {
259 ExprResult r(std::move(result));
260 if (info.is_false)
261 passed = !r.res;
262 else
263 passed = r.res;
264 message = r.decomp;
265 }
266
267 void setException(const std::exception& e) {
269 message = e.what();
270 }
271
272 std::string log() {
273 std::stringstream ss;
274 ss << " " << cond << " expands to " << message << std::endl;
275 ss << Dim << "(" << file << ":" << line << ")" << Reset << std::endl;
276 return ss.str();
277 }
278
279 // throw the exception
280 void operator()() {
281 if (passed) return;
282 if (shouldThrow()) throw *this;
283 }
284
286};
287
288namespace detail {
289// This struct is used for handle constexpr if in C++11
290// https://stackoverflow.com/questions/43587405/constexpr-if-alternative
291template <typename T, bool>
293
294template <typename T>
295struct context_helper<T, true> {
296 static void setContext(AssertionData& data, T) {
297 if (data.passed) return;
298 }
299};
300
301template <typename T>
302struct context_helper<T, false> {
303 static void setContext(AssertionData& data, T ctx) {
304 if (data.passed) {
305 ctx->passed_as++;
306 return;
307 }
308 switch (data.info.level) {
310 case assert_level::ZEROERR_ERROR_l: ctx->failed_as++; break;
311 case assert_level::ZEROERR_WARN_l: ctx->warning_as++; break;
312 }
313 }
314};
315} // namespace detail
316
317
318} // namespace zeroerr
319
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
Definition config.h:272
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
Definition config.h:224
#define ZEROERR_CLANG_SUPPRESS_WARNING_WITH_PUSH(w)
Definition config.h:188
Definition assert.h:215
constexpr bool _ZEROERR_TEST_CONTEXT
Definition assert.h:216
Definition benchmark.cpp:17
const char * Dim
Definition color.cpp:36
assert_throw
Definition assert.h:223
assert_level
Definition assert.h:222
assert_cmp
Definition assert.h:224
const char * Reset
Definition color.cpp:34
AssertionData is a struct that contains all the information of an assertion. It will be thrown as an ...
Definition assert.h:241
assert_info info
Definition assert.h:244
std::string log()
Definition assert.h:272
std::string message
Definition assert.h:246
const char * file
Definition assert.h:242
void operator()()
Definition assert.h:280
void setResult(ExprResult &&result)
Definition assert.h:258
bool shouldThrow()
Definition assert.h:285
AssertionData(const char *file, unsigned line, const char *cond, assert_info info)
Definition assert.h:249
void setException(const std::exception &e)
Definition assert.h:267
bool passed
Definition assert.h:245
unsigned line
Definition assert.h:243
std::string cond
Definition assert.h:247
Definition decomposition.h:87
std::string decomp
Definition decomposition.h:89
bool res
Definition decomposition.h:88
This is a one-byte assert info struct, which is used to collect the meta info of an assertion.
Definition assert.h:230
assert_level level
Definition assert.h:231
assert_throw throw_type
Definition assert.h:232
bool is_false
Definition assert.h:233
static void setContext(AssertionData &data, T ctx)
Definition assert.h:303
static void setContext(AssertionData &data, T)
Definition assert.h:296
Definition assert.h:292