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
6#include <cstdint>
7#include <sstream>
8
9// Enable this for (VERY SLOW) stats on which asserts are hit the most. Note that the time measured will be very
10// inaccurate, but you can still see what is called too often to be in a release build.
11// #define BB_BENCH_ASSERT(x) BB_BENCH_NAME(x)
12#define BB_BENCH_ASSERT(x)
13
14namespace bb {
17void assert_failure(std::string const& err);
18
19// NOTE do not use in threaded contexts!
29} // namespace bb
30
31// NOTE do not use in threaded contexts!
32#define BB_DISABLE_ASSERTS() bb::AssertGuard __bb_assert_guard(bb::AssertMode::WARN)
33
34// NOLINTBEGIN
35// Compiler should optimize this out in release builds, without triggering unused-variable warnings.
36#define DONT_EVALUATE(expression) \
37 { \
38 true ? static_cast<void>(0) : static_cast<void>((expression)); \
39 }
40
41#if NDEBUG
42
43// All assertion macros accept an optional message but do nothing in release.
44#define ASSERT_DEBUG(expression, ...) DONT_EVALUATE((expression))
45
46#else
48#include <cassert>
49#include <cstdlib>
50#include <iostream>
51#include <string>
52
53// Basic assert with optional error message
54#define ASSERT_DEBUG(expression, ...) ASSERT(expression, __VA_ARGS__)
55#endif // NDEBUG
56
57#ifdef __wasm__
58#define ASSERT_IN_CONSTEXPR(expression, ...) DONT_EVALUATE((expression))
59#define ASSERT(expression, ...) DONT_EVALUATE((expression))
60
61#define BB_ASSERT_EQ(actual, expected, ...) DONT_EVALUATE((actual) == (expected))
62#define BB_ASSERT_NEQ(actual, expected, ...) DONT_EVALUATE((actual) != (expected))
63#define BB_ASSERT_GT(left, right, ...) DONT_EVALUATE((left) > (right))
64#define BB_ASSERT_GTE(left, right, ...) DONT_EVALUATE((left) >= (right))
65#define BB_ASSERT_LT(left, right, ...) DONT_EVALUATE((left) < (right))
66#define BB_ASSERT_LTE(left, right, ...) DONT_EVALUATE((left) <= (right))
67#else
68#define ASSERT_IN_CONSTEXPR(expression, ...) \
69 do { \
70 if (!(BB_LIKELY(expression))) { \
71 info("Assertion failed: (" #expression ")"); \
72 __VA_OPT__(info("Reason : ", __VA_ARGS__);) \
73 bb::assert_failure(""); \
74 } \
75 } while (0)
76
77#define ASSERT(expression, ...) \
78 do { \
79 BB_BENCH_ASSERT("ASSERT" #expression); \
80 if (!(BB_LIKELY(expression))) { \
81 std::ostringstream oss; \
82 oss << "Assertion failed: (" #expression ")"; \
83 __VA_OPT__(oss << " | Reason: " << __VA_ARGS__;) \
84 bb::assert_failure(oss.str()); \
85 } \
86 } while (0)
87
88#define BB_ASSERT_EQ(actual, expected, ...) \
89 do { \
90 BB_BENCH_ASSERT("BB_ASSERT_EQ" #actual " == " #expected); \
91 auto _actual = (actual); \
92 auto _expected = (expected); \
93 if (!(BB_LIKELY(_actual == _expected))) { \
94 std::ostringstream oss; \
95 oss << "Assertion failed: (" #actual " == " #expected ")\n"; \
96 oss << " Actual : " << _actual << "\n"; \
97 oss << " Expected: " << _expected; \
98 __VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
99 bb::assert_failure(oss.str()); \
100 } \
101 } while (0)
102
103#define BB_ASSERT_NEQ(actual, expected, ...) \
104 do { \
105 BB_BENCH_ASSERT("BB_ASSERT_NEQ" #actual " != " #expected); \
106 auto _actual = (actual); \
107 auto _expected = (expected); \
108 if (!(BB_LIKELY(_actual != _expected))) { \
109 std::ostringstream oss; \
110 oss << "Assertion failed: (" #actual " != " #expected ")\n"; \
111 oss << " Actual : " << _actual << "\n"; \
112 oss << " Not expected: " << _expected; \
113 __VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
114 bb::assert_failure(oss.str()); \
115 } \
116 } while (0)
117
118#define BB_ASSERT_GT(left, right, ...) \
119 do { \
120 BB_BENCH_ASSERT("BB_ASSERT_GT" #left " > " #right); \
121 auto _left = (left); \
122 auto _right = (right); \
123 if (!(BB_LIKELY(_left > _right))) { \
124 std::ostringstream oss; \
125 oss << "Assertion failed: (" #left " > " #right ")\n"; \
126 oss << " Left : " << _left << "\n"; \
127 oss << " Right : " << _right; \
128 __VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
129 bb::assert_failure(oss.str()); \
130 } \
131 } while (0)
132
133#define BB_ASSERT_GTE(left, right, ...) \
134 do { \
135 BB_BENCH_ASSERT("BB_ASSERT_GTE" #left " >= " #right); \
136 auto _left = (left); \
137 auto _right = (right); \
138 if (!(BB_LIKELY(_left >= _right))) { \
139 std::ostringstream oss; \
140 oss << "Assertion failed: (" #left " >= " #right ")\n"; \
141 oss << " Left : " << _left << "\n"; \
142 oss << " Right : " << _right; \
143 __VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
144 bb::assert_failure(oss.str()); \
145 } \
146 } while (0)
147
148#define BB_ASSERT_LT(left, right, ...) \
149 do { \
150 BB_BENCH_ASSERT("BB_ASSERT_LT" #left " < " #right); \
151 auto _left = (left); \
152 auto _right = (right); \
153 if (!(BB_LIKELY(_left < _right))) { \
154 std::ostringstream oss; \
155 oss << "Assertion failed: (" #left " < " #right ")\n"; \
156 oss << " Left : " << _left << "\n"; \
157 oss << " Right : " << _right; \
158 __VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
159 bb::assert_failure(oss.str()); \
160 } \
161 } while (0)
162
163#define BB_ASSERT_LTE(left, right, ...) \
164 do { \
165 BB_BENCH_ASSERT("BB_ASSERT_LTE" #left " <= " #right); \
166 auto _left = (left); \
167 auto _right = (right); \
168 if (!(BB_LIKELY(_left <= _right))) { \
169 std::ostringstream oss; \
170 oss << "Assertion failed: (" #left " <= " #right ")\n"; \
171 oss << " Left : " << _left << "\n"; \
172 oss << " Right : " << _right; \
173 __VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
174 bb::assert_failure(oss.str()); \
175 } \
176 } while (0)
177#endif // __wasm__
178
179// These are used in tests.
180#ifdef BB_NO_EXCEPTIONS
181#define ASSERT_THROW_OR_ABORT(statement, matcher) ASSERT_DEATH(statement, matcher)
182#define EXPECT_THROW_OR_ABORT(statement, matcher) EXPECT_DEATH(statement, matcher)
183#else
184#define ASSERT_THROW_OR_ABORT(statement, matcher) ASSERT_THROW(statement, std::runtime_error)
185#define EXPECT_THROW_OR_ABORT(statement, matcher) EXPECT_THROW(statement, std::runtime_error)
186#endif // BB_NO_EXCEPTIONS
187// NOLINTEND
Entry point for Barretenberg command-line interface.
AssertMode & get_assert_mode()
Definition assert.cpp:5
void assert_failure(std::string const &err)
Definition assert.cpp:11
AssertMode
Definition assert.hpp:15
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
AssertGuard(AssertMode mode)
Definition assert.hpp:21
AssertMode previous_mode
Definition assert.hpp:27