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