Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
assert.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include "
barretenberg/common/throw_or_abort.hpp
"
4
#include <sstream>
5
6
// NOLINTBEGIN
7
// Compiler should optimize this out in release builds, without triggering unused-variable warnings.
8
#define DONT_EVALUATE(expression) \
9
{ \
10
true ? static_cast<void>(0) : static_cast<void>((expression)); \
11
}
12
13
#if NDEBUG
14
15
// All assertion macros accept an optional message but do nothing in release.
16
#define ASSERT_DEBUG(expression, ...) DONT_EVALUATE((expression))
17
18
#else
19
#include "
barretenberg/common/log.hpp
"
20
#include <cassert>
21
#include <cstdlib>
22
#include <iostream>
23
#include <string>
24
25
// Basic assert with optional error message
26
#define ASSERT_DEBUG(expression, ...) ASSERT(expression, __VA_ARGS__)
27
#endif
// NDEBUG
28
29
#ifdef __wasm__
30
#define ASSERT_IN_CONSTEXPR(expression, ...) DONT_EVALUATE((expression))
31
#define ASSERT(expression, ...) DONT_EVALUATE((expression))
32
33
#define BB_ASSERT_EQ(actual, expected, ...) DONT_EVALUATE((actual) == (expected))
34
#define BB_ASSERT_NEQ(actual, expected, ...) DONT_EVALUATE((actual) != (expected))
35
#define BB_ASSERT_GT(left, right, ...) DONT_EVALUATE((left) > (right))
36
#define BB_ASSERT_GTE(left, right, ...) DONT_EVALUATE((left) >= (right))
37
#define BB_ASSERT_LT(left, right, ...) DONT_EVALUATE((left) < (right))
38
#define BB_ASSERT_LTE(left, right, ...) DONT_EVALUATE((left) <= (right))
39
#else
40
#define ASSERT_IN_CONSTEXPR(expression, ...) \
41
do { \
42
if (!(expression)) { \
43
info("Assertion failed: (" #expression ")"); \
44
__VA_OPT__(info("Reason : ", __VA_ARGS__);) \
45
throw_or_abort(""); \
46
} \
47
} while (0)
48
49
#define ASSERT(expression, ...) \
50
do { \
51
if (!(expression)) { \
52
std::ostringstream oss; \
53
oss << "Assertion failed: (" #expression ")"; \
54
__VA_OPT__(oss << " | Reason: " << __VA_ARGS__;) \
55
throw_or_abort(oss.str()); \
56
} \
57
} while (0)
58
59
#define BB_ASSERT_EQ(actual, expected, ...) \
60
do { \
61
auto _actual = (actual); \
62
auto _expected = (expected); \
63
if (!(_actual == _expected)) { \
64
std::ostringstream oss; \
65
oss << "Assertion failed: (" #actual " == " #expected ")\n"; \
66
oss << " Actual : " << _actual << "\n"; \
67
oss << " Expected: " << _expected; \
68
__VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
69
throw_or_abort(oss.str()); \
70
} \
71
} while (0)
72
73
#define BB_ASSERT_NEQ(actual, expected, ...) \
74
do { \
75
auto _actual = (actual); \
76
auto _expected = (expected); \
77
if (!(_actual != _expected)) { \
78
std::ostringstream oss; \
79
oss << "Assertion failed: (" #actual " != " #expected ")\n"; \
80
oss << " Actual : " << _actual << "\n"; \
81
oss << " Not expected: " << _expected; \
82
__VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
83
throw_or_abort(oss.str()); \
84
} \
85
} while (0)
86
87
#define BB_ASSERT_GT(left, right, ...) \
88
do { \
89
auto _left = (left); \
90
auto _right = (right); \
91
if (!(_left > _right)) { \
92
std::ostringstream oss; \
93
oss << "Assertion failed: (" #left " > " #right ")\n"; \
94
oss << " Left : " << _left << "\n"; \
95
oss << " Right : " << _right; \
96
__VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
97
throw_or_abort(oss.str()); \
98
} \
99
} while (0)
100
101
#define BB_ASSERT_GTE(left, right, ...) \
102
do { \
103
auto _left = (left); \
104
auto _right = (right); \
105
if (!(_left >= _right)) { \
106
std::ostringstream oss; \
107
oss << "Assertion failed: (" #left " >= " #right ")\n"; \
108
oss << " Left : " << _left << "\n"; \
109
oss << " Right : " << _right; \
110
__VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
111
throw_or_abort(oss.str()); \
112
} \
113
} while (0)
114
115
#define BB_ASSERT_LT(left, right, ...) \
116
do { \
117
auto _left = (left); \
118
auto _right = (right); \
119
if (!(_left < _right)) { \
120
std::ostringstream oss; \
121
oss << "Assertion failed: (" #left " < " #right ")\n"; \
122
oss << " Left : " << _left << "\n"; \
123
oss << " Right : " << _right; \
124
__VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
125
throw_or_abort(oss.str()); \
126
} \
127
} while (0)
128
129
#define BB_ASSERT_LTE(left, right, ...) \
130
do { \
131
auto _left = (left); \
132
auto _right = (right); \
133
if (!(_left <= _right)) { \
134
std::ostringstream oss; \
135
oss << "Assertion failed: (" #left " <= " #right ")\n"; \
136
oss << " Left : " << _left << "\n"; \
137
oss << " Right : " << _right; \
138
__VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
139
throw_or_abort(oss.str()); \
140
} \
141
} while (0)
142
#endif
// __wasm__
143
144
// These are used in tests.
145
#ifdef BB_NO_EXCEPTIONS
146
#define ASSERT_THROW_OR_ABORT(statement, matcher) ASSERT_DEATH(statement, matcher)
147
#define EXPECT_THROW_OR_ABORT(statement, matcher) EXPECT_DEATH(statement, matcher)
148
#else
149
#define ASSERT_THROW_OR_ABORT(statement, matcher) ASSERT_THROW(statement, std::runtime_error)
150
#define EXPECT_THROW_OR_ABORT(statement, matcher) EXPECT_THROW(statement, std::runtime_error)
151
#endif
// BB_NO_EXCEPTIONS
152
// NOLINTEND
log.hpp
throw_or_abort.hpp
src
barretenberg
common
assert.hpp
Generated by
1.9.8