ZeroErr
Loading...
Searching...
No Matches
assert.h File Reference
#include "zeroerr/internal/config.h"
#include "zeroerr/format.h"
#include "zeroerr/internal/debugbreak.h"
#include "zeroerr/internal/decomposition.h"
#include "zeroerr/print.h"
#include <cstdint>
#include <exception>
Include dependency graph for assert.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  zeroerr::assert_info
 This is a one-byte assert info struct, which is used to collect the meta info of an assertion. More...
struct  zeroerr::AssertionData
 AssertionData is a struct that contains all the information of an assertion. It will be thrown as an exception when the assertion failed. More...
struct  zeroerr::detail::context_helper< T, true >
struct  zeroerr::detail::context_helper< T, false >

Namespaces

namespace  ZEROERR_CLANG_SUPPRESS_WARNING_POP
namespace  zeroerr
namespace  zeroerr::detail

Macros

#define ZEROERR_G_CONTEXT_SCOPE(x)
#define ZEROERR_PRINT_ASSERT_DEFAULT_PRINTER(cond, level, ...)
 Default printer for assertion messages.
#define ZEROERR_PRINT_ASSERT(cond, level, pattern, ...)
#define ZEROERR_ASSERT_EXP(cond, level, expect_throw, is_false, ...)
#define ZEROERR_ASSERT_CMP(lhs, op, rhs, level, expect_throw, is_false, ...)
#define ZEROERR_CHECK_LEVEL   ZEROERR_WARN
#define ZEROERR_CHECK(cond, ...)
#define ZEROERR_CHECK_NOT(cond, ...)
#define ZEROERR_CHECK_THROWS(cond, ...)
#define ZEROERR_REQUIRE(cond, ...)
#define ZEROERR_REQUIRE_NOT(cond, ...)
#define ZEROERR_REQUIRE_THROWS(cond, ...)
#define ZEROERR_ASSERT(cond, ...)
#define ZEROERR_ASSERT_NOT(cond, ...)
#define ZEROERR_ASSERT_THROWS(cond, ...)
#define CHECK(...)
#define CHECK_NOT(...)
#define CHECK_THROWS(...)
#define REQUIRE(...)
#define REQUIRE_NOT(...)
#define REQUIRE_THROWS(...)
#define ASSERT(...)
#define ASSERT_NOT(...)
#define ASSERT_THROWS(...)
#define ZEROERR_CHECK_EQ(lhs, rhs, ...)
#define ZEROERR_CHECK_NE(lhs, rhs, ...)
#define ZEROERR_CHECK_LT(lhs, rhs, ...)
#define ZEROERR_CHECK_LE(lhs, rhs, ...)
#define ZEROERR_CHECK_GT(lhs, rhs, ...)
#define ZEROERR_CHECK_GE(lhs, rhs, ...)
#define ZEROERR_REQUIRE_EQ(lhs, rhs, ...)
#define ZEROERR_REQUIRE_NE(lhs, rhs, ...)
#define ZEROERR_REQUIRE_LT(lhs, rhs, ...)
#define ZEROERR_REQUIRE_LE(lhs, rhs, ...)
#define ZEROERR_REQUIRE_GT(lhs, rhs, ...)
#define ZEROERR_REQUIRE_GE(lhs, rhs, ...)
#define ZEROERR_ASSERT_EQ(lhs, rhs, ...)
#define ZEROERR_ASSERT_NE(lhs, rhs, ...)
#define ZEROERR_ASSERT_LT(lhs, rhs, ...)
#define ZEROERR_ASSERT_LE(lhs, rhs, ...)
#define ZEROERR_ASSERT_GT(lhs, rhs, ...)
#define ZEROERR_ASSERT_GE(lhs, rhs, ...)
#define CHECK_EQ(...)
#define CHECK_NE(...)
#define CHECK_LT(...)
#define CHECK_LE(...)
#define CHECK_GT(...)
#define CHECK_GE(...)
#define REQUIRE_EQ(...)
#define REQUIRE_NE(...)
#define REQUIRE_LT(...)
#define REQUIRE_LE(...)
#define REQUIRE_GT(...)
#define REQUIRE_GE(...)
#define ASSERT_EQ(...)
#define ASSERT_NE(...)
#define ASSERT_LT(...)
#define ASSERT_LE(...)
#define ASSERT_GT(...)
#define ASSERT_GE(...)

Enumerations

enum class  zeroerr::assert_level : uint8_t { zeroerr::ZEROERR_WARN_l , zeroerr::ZEROERR_ERROR_l , zeroerr::ZEROERR_FATAL_l }
enum class  zeroerr::assert_throw : uint8_t { zeroerr::no_throw , zeroerr::throws , zeroerr::throws_as }
enum class  zeroerr::assert_cmp : uint8_t {
  zeroerr::eq , zeroerr::ne , zeroerr::gt , zeroerr::ge ,
  zeroerr::lt , zeroerr::le
}

Variables

constexpr bool ZEROERR_CLANG_SUPPRESS_WARNING_POP::_ZEROERR_TEST_CONTEXT = false

Macro Definition Documentation

◆ ASSERT

#define ASSERT ( ...)
Value:
#define ZEROERR_ASSERT(cond,...)
Definition assert.h:163
#define ZEROERR_SUPPRESS_VARIADIC_MACRO_POP
Definition config.h:316
#define ZEROERR_EXPAND(x)
Definition config.h:131
#define ZEROERR_SUPPRESS_VARIADIC_MACRO
Definition config.h:312

◆ ASSERT_EQ

#define ASSERT_EQ ( ...)
Value:
#define ZEROERR_ASSERT_EQ(lhs, rhs,...)
Definition assert.h:191

◆ ASSERT_GE

#define ASSERT_GE ( ...)
Value:
#define ZEROERR_ASSERT_GE(lhs, rhs,...)
Definition assert.h:196

◆ ASSERT_GT

#define ASSERT_GT ( ...)
Value:
#define ZEROERR_ASSERT_GT(lhs, rhs,...)
Definition assert.h:195

◆ ASSERT_LE

#define ASSERT_LE ( ...)
Value:
#define ZEROERR_ASSERT_LE(lhs, rhs,...)
Definition assert.h:194

◆ ASSERT_LT

#define ASSERT_LT ( ...)
Value:
#define ZEROERR_ASSERT_LT(lhs, rhs,...)
Definition assert.h:193

◆ ASSERT_NE

#define ASSERT_NE ( ...)
Value:
#define ZEROERR_ASSERT_NE(lhs, rhs,...)
Definition assert.h:192

◆ ASSERT_NOT

#define ASSERT_NOT ( ...)
Value:
#define ZEROERR_ASSERT_NOT(cond,...)
Definition assert.h:164

◆ ASSERT_THROWS

#define ASSERT_THROWS ( ...)
Value:

◆ CHECK

#define CHECK ( ...)
Value:
#define ZEROERR_CHECK(cond,...)
Definition assert.h:157

◆ CHECK_EQ

#define CHECK_EQ ( ...)
Value:
#define ZEROERR_CHECK_EQ(lhs, rhs,...)
Definition assert.h:177

◆ CHECK_GE

#define CHECK_GE ( ...)
Value:
#define ZEROERR_CHECK_GE(lhs, rhs,...)
Definition assert.h:182

◆ CHECK_GT

#define CHECK_GT ( ...)
Value:
#define ZEROERR_CHECK_GT(lhs, rhs,...)
Definition assert.h:181

◆ CHECK_LE

#define CHECK_LE ( ...)
Value:
#define ZEROERR_CHECK_LE(lhs, rhs,...)
Definition assert.h:180

◆ CHECK_LT

#define CHECK_LT ( ...)
Value:
#define ZEROERR_CHECK_LT(lhs, rhs,...)
Definition assert.h:179

◆ CHECK_NE

#define CHECK_NE ( ...)
Value:
#define ZEROERR_CHECK_NE(lhs, rhs,...)
Definition assert.h:178

◆ CHECK_NOT

#define CHECK_NOT ( ...)
Value:
#define ZEROERR_CHECK_NOT(cond,...)
Definition assert.h:158

◆ CHECK_THROWS

#define CHECK_THROWS ( ...)
Value:
#define ZEROERR_CHECK_THROWS(cond,...)
Definition assert.h:159

◆ REQUIRE

#define REQUIRE ( ...)
Value:
#define ZEROERR_REQUIRE(cond,...)
Definition assert.h:160

◆ REQUIRE_EQ

#define REQUIRE_EQ ( ...)
Value:
#define ZEROERR_REQUIRE_EQ(lhs, rhs,...)
Definition assert.h:184

◆ REQUIRE_GE

#define REQUIRE_GE ( ...)
Value:
#define ZEROERR_REQUIRE_GE(lhs, rhs,...)
Definition assert.h:189

◆ REQUIRE_GT

#define REQUIRE_GT ( ...)
Value:
#define ZEROERR_REQUIRE_GT(lhs, rhs,...)
Definition assert.h:188

◆ REQUIRE_LE

#define REQUIRE_LE ( ...)
Value:
#define ZEROERR_REQUIRE_LE(lhs, rhs,...)
Definition assert.h:187

◆ REQUIRE_LT

#define REQUIRE_LT ( ...)
Value:
#define ZEROERR_REQUIRE_LT(lhs, rhs,...)
Definition assert.h:186

◆ REQUIRE_NE

#define REQUIRE_NE ( ...)
Value:
#define ZEROERR_REQUIRE_NE(lhs, rhs,...)
Definition assert.h:185

◆ REQUIRE_NOT

#define REQUIRE_NOT ( ...)
Value:
#define ZEROERR_REQUIRE_NOT(cond,...)
Definition assert.h:161

◆ REQUIRE_THROWS

#define REQUIRE_THROWS ( ...)
Value:

◆ ZEROERR_ASSERT

#define ZEROERR_ASSERT ( cond,
... )
Value:
ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_FATAL, no_throw, false, __VA_ARGS__))
#define ZEROERR_ASSERT_EXP(cond, level, expect_throw, is_false,...)
Definition assert.h:63
#define ZEROERR_FATAL(...)
Definition log.h:39

◆ ZEROERR_ASSERT_CMP

#define ZEROERR_ASSERT_CMP ( lhs,
op,
rhs,
level,
expect_throw,
is_false,
... )
Value:
zeroerr::assert_info info{zeroerr::assert_level::ZEROERR_CAT(level, _l), \
zeroerr::assert_throw::expect_throw, is_false}; \
\
print.isQuoted = false; \
zeroerr::AssertionData assertion_data(__FILE__, __LINE__, #lhs " " #op " " #rhs, info); \
try { \
ZEROERR_SUPPRESS_COMPARE \
assertion_data.setResult(zeroerr::detail::makeComparisonLhs((lhs), print) op (rhs)); \
} catch (const std::exception& e) { \
assertion_data.setException(e); \
} \
zeroerr::detail::context_helper< \
decltype(_ZEROERR_TEST_CONTEXT), \
std::is_same<decltype(_ZEROERR_TEST_CONTEXT), \
const bool>::value>::setContext(assertion_data, _ZEROERR_TEST_CONTEXT); \
ZEROERR_PRINT_ASSERT(assertion_data.passed == false, level, "" __VA_ARGS__); \
if (false) debug_break(); \
assertion_data(); \
ZEROERR_FUNC_SCOPE_RET(assertion_data.passed); \
} \
#define ZEROERR_FUNC_SCOPE_RET(v)
Definition config.h:124
#define ZEROERR_SUPPRESS_COMPARE_POP
Definition config.h:331
#define ZEROERR_FUNC_SCOPE_END
Definition config.h:123
#define ZEROERR_FUNC_SCOPE_BEGIN
Definition config.h:122
constexpr bool _ZEROERR_TEST_CONTEXT
Definition assert.h:225
ComparisonLhs< L > makeComparisonLhs(const L &lhs, Printer &printer)
Definition decomposition.h:124
AssertionData is a struct that contains all the information of an assertion. It will be thrown as an ...
Definition assert.h:250
A functor class Printer for printing a value of any type.
Definition print.h:87
bool isQuoted
Definition print.h:131
This is a one-byte assert info struct, which is used to collect the meta info of an assertion.
Definition assert.h:239

◆ ZEROERR_ASSERT_EQ

#define ZEROERR_ASSERT_EQ ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, ==, rhs, ZEROERR_FATAL, no_throw, false, __VA_ARGS__)
#define ZEROERR_ASSERT_CMP(lhs, op, rhs, level, expect_throw, is_false,...)
Definition assert.h:86

◆ ZEROERR_ASSERT_EXP

#define ZEROERR_ASSERT_EXP ( cond,
level,
expect_throw,
is_false,
... )
Value:
zeroerr::assert_info info{zeroerr::assert_level::ZEROERR_CAT(level, _l), \
zeroerr::assert_throw::expect_throw, is_false}; \
\
zeroerr::AssertionData assertion_data(__FILE__, __LINE__, #cond, info); \
try { \
assertion_data.setResult(zeroerr::ExpressionDecomposer() << cond); \
} catch (const std::exception& e) { \
assertion_data.setException(e); \
} \
zeroerr::detail::context_helper< \
decltype(_ZEROERR_TEST_CONTEXT), \
std::is_same<decltype(_ZEROERR_TEST_CONTEXT), \
const bool>::value>::setContext(assertion_data, _ZEROERR_TEST_CONTEXT); \
ZEROERR_PRINT_ASSERT(assertion_data.passed == false, level, "" __VA_ARGS__); \
if (false) debug_break(); \
assertion_data(); \
ZEROERR_FUNC_SCOPE_RET(assertion_data.passed); \
} \
Definition decomposition.h:207

◆ ZEROERR_ASSERT_GE

#define ZEROERR_ASSERT_GE ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, >=, rhs, ZEROERR_FATAL, no_throw, false, __VA_ARGS__)

◆ ZEROERR_ASSERT_GT

#define ZEROERR_ASSERT_GT ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, >, rhs, ZEROERR_FATAL, no_throw, false, __VA_ARGS__)

◆ ZEROERR_ASSERT_LE

#define ZEROERR_ASSERT_LE ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, <=, rhs, ZEROERR_FATAL, no_throw, false, __VA_ARGS__)

◆ ZEROERR_ASSERT_LT

#define ZEROERR_ASSERT_LT ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, <, rhs, ZEROERR_FATAL, no_throw, false, __VA_ARGS__)

◆ ZEROERR_ASSERT_NE

#define ZEROERR_ASSERT_NE ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, !=, rhs, ZEROERR_FATAL, no_throw, false, __VA_ARGS__)

◆ ZEROERR_ASSERT_NOT

#define ZEROERR_ASSERT_NOT ( cond,
... )
Value:
ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_FATAL, no_throw, true, __VA_ARGS__))

◆ ZEROERR_ASSERT_THROWS

#define ZEROERR_ASSERT_THROWS ( cond,
... )
Value:
ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_FATAL, throws, false, __VA_ARGS__))

◆ ZEROERR_CHECK

#define ZEROERR_CHECK ( cond,
... )
Value:
ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_CHECK_LEVEL, no_throw, false, __VA_ARGS__))
#define ZEROERR_CHECK_LEVEL
Definition assert.h:155

◆ ZEROERR_CHECK_EQ

#define ZEROERR_CHECK_EQ ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, ==, rhs, ZEROERR_CHECK_LEVEL, no_throw, false, __VA_ARGS__)

◆ ZEROERR_CHECK_GE

#define ZEROERR_CHECK_GE ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, >=, rhs, ZEROERR_CHECK_LEVEL, no_throw, false, __VA_ARGS__)

◆ ZEROERR_CHECK_GT

#define ZEROERR_CHECK_GT ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, >, rhs, ZEROERR_CHECK_LEVEL, no_throw, false, __VA_ARGS__)

◆ ZEROERR_CHECK_LE

#define ZEROERR_CHECK_LE ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, <=, rhs, ZEROERR_CHECK_LEVEL, no_throw, false, __VA_ARGS__)

◆ ZEROERR_CHECK_LEVEL

#define ZEROERR_CHECK_LEVEL   ZEROERR_WARN

◆ ZEROERR_CHECK_LT

#define ZEROERR_CHECK_LT ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, <, rhs, ZEROERR_CHECK_LEVEL, no_throw, false, __VA_ARGS__)

◆ ZEROERR_CHECK_NE

#define ZEROERR_CHECK_NE ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, !=, rhs, ZEROERR_CHECK_LEVEL, no_throw, false, __VA_ARGS__)

◆ ZEROERR_CHECK_NOT

#define ZEROERR_CHECK_NOT ( cond,
... )
Value:
ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_CHECK_LEVEL, no_throw, true, __VA_ARGS__))

◆ ZEROERR_CHECK_THROWS

#define ZEROERR_CHECK_THROWS ( cond,
... )
Value:
ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_CHECK_LEVEL, throws, false, __VA_ARGS__))

◆ ZEROERR_G_CONTEXT_SCOPE

#define ZEROERR_G_CONTEXT_SCOPE ( x)

◆ ZEROERR_PRINT_ASSERT

#define ZEROERR_PRINT_ASSERT ( cond,
level,
pattern,
... )
Value:
ZEROERR_PRINT_ASSERT_DEFAULT_PRINTER(cond, level, " Assertion Failed:\n{msg}" pattern, \
assertion_data.log(), ##__VA_ARGS__)
#define ZEROERR_PRINT_ASSERT_DEFAULT_PRINTER(cond, level,...)
Default printer for assertion messages.
Definition assert.h:35

◆ ZEROERR_PRINT_ASSERT_DEFAULT_PRINTER

#define ZEROERR_PRINT_ASSERT_DEFAULT_PRINTER ( cond,
level,
... )
Value:
do { \
if (cond) { \
switch (zeroerr::assert_level::ZEROERR_CAT(level, _l)) { \
zeroerr::getStderrPrinter().os << zeroerr::FgYellow << "WARN" << zeroerr::Reset; \
break; \
zeroerr::getStderrPrinter().os << zeroerr::FgRed << "ERROR" << zeroerr::Reset; \
break; \
zeroerr::getStderrPrinter().os << zeroerr::FgMagenta << "FATAL" << zeroerr::Reset; \
break; \
} \
zeroerr::getStderrPrinter().os << zeroerr::format(__VA_ARGS__) << '\n'; \
} \
} while (0)
const char * FgMagenta
Definition color.cpp:47
ZEROERR_DIAGNOSTIC_NOINLINE zeroerr::String format(const char *fmt, T &&... args)
Format a string with arguments.
Definition format.h:27
@ ZEROERR_FATAL_l
Definition assert.h:231
@ ZEROERR_ERROR_l
Definition assert.h:231
@ ZEROERR_WARN_l
Definition assert.h:231
const char * FgRed
Definition color.cpp:43
const char * FgYellow
Definition color.cpp:45
const char * Reset
Definition color.cpp:34

Default printer for assertion messages.

This macro defines the default printer for assertion messages. It prints the assertion message in different colors based on the assertion level.

The macro can be overridden by defining ZEROERR_PRINT_ASSERT_DEFAULT_PRINTER before including this header. (Or undefine it and implement your own printer.)

◆ ZEROERR_REQUIRE

#define ZEROERR_REQUIRE ( cond,
... )
Value:
ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_ERROR, no_throw, false, __VA_ARGS__))
#define ZEROERR_ERROR(...)
Definition log.h:38

◆ ZEROERR_REQUIRE_EQ

#define ZEROERR_REQUIRE_EQ ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, ==, rhs, ZEROERR_ERROR, no_throw, false, __VA_ARGS__)

◆ ZEROERR_REQUIRE_GE

#define ZEROERR_REQUIRE_GE ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, >=, rhs, ZEROERR_ERROR, no_throw, false, __VA_ARGS__)

◆ ZEROERR_REQUIRE_GT

#define ZEROERR_REQUIRE_GT ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, >, rhs, ZEROERR_ERROR, no_throw, false, __VA_ARGS__)

◆ ZEROERR_REQUIRE_LE

#define ZEROERR_REQUIRE_LE ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, <=, rhs, ZEROERR_ERROR, no_throw, false, __VA_ARGS__)

◆ ZEROERR_REQUIRE_LT

#define ZEROERR_REQUIRE_LT ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, <, rhs, ZEROERR_ERROR, no_throw, false, __VA_ARGS__)

◆ ZEROERR_REQUIRE_NE

#define ZEROERR_REQUIRE_NE ( lhs,
rhs,
... )
Value:
ZEROERR_ASSERT_CMP(lhs, !=, rhs, ZEROERR_ERROR, no_throw, false, __VA_ARGS__)

◆ ZEROERR_REQUIRE_NOT

#define ZEROERR_REQUIRE_NOT ( cond,
... )
Value:
ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_ERROR, no_throw, true, __VA_ARGS__))

◆ ZEROERR_REQUIRE_THROWS

#define ZEROERR_REQUIRE_THROWS ( cond,
... )
Value:
ZEROERR_EXPAND(ZEROERR_ASSERT_EXP(cond, ZEROERR_ERROR, throws, false, __VA_ARGS__))