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 BB_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 BB_ASSERT_DEBUG(expression, ...) BB_ASSERT(expression, __VA_ARGS__)
55#endif // NDEBUG
56
57#ifdef __wasm__
58#define BB_ASSERT(expression, ...) DONT_EVALUATE((expression))
59
60#define BB_ASSERT_EQ(actual, expected, ...) DONT_EVALUATE((actual) == (expected))
61#define BB_ASSERT_NEQ(actual, expected, ...) DONT_EVALUATE((actual) != (expected))
62#define BB_ASSERT_GT(left, right, ...) DONT_EVALUATE((left) > (right))
63#define BB_ASSERT_GTE(left, right, ...) DONT_EVALUATE((left) >= (right))
64#define BB_ASSERT_LT(left, right, ...) DONT_EVALUATE((left) < (right))
65#define BB_ASSERT_LTE(left, right, ...) DONT_EVALUATE((left) <= (right))
66#else
67#define BB_ASSERT(expression, ...) \
68 do { \
69 BB_BENCH_ASSERT("BB_ASSERT" #expression); \
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 BB_ASSERT_EQ(actual, expected, ...) \
78 do { \
79 BB_BENCH_ASSERT("BB_ASSERT_EQ" #actual " == " #expected); \
80 auto _actual = (actual); \
81 auto _expected = (expected); \
82 if (!(BB_LIKELY(_actual == _expected))) { \
83 std::ostringstream oss; \
84 oss << "Assertion failed: (" #actual " == " #expected ")\n"; \
85 oss << " Actual : " << _actual << "\n"; \
86 oss << " Expected: " << _expected; \
87 __VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
88 bb::assert_failure(oss.str()); \
89 } \
90 } while (0)
91
92#define BB_ASSERT_NEQ(actual, expected, ...) \
93 do { \
94 BB_BENCH_ASSERT("BB_ASSERT_NEQ" #actual " != " #expected); \
95 auto _actual = (actual); \
96 auto _expected = (expected); \
97 if (!(BB_LIKELY(_actual != _expected))) { \
98 std::ostringstream oss; \
99 oss << "Assertion failed: (" #actual " != " #expected ")\n"; \
100 oss << " Actual : " << _actual << "\n"; \
101 oss << " Not expected: " << _expected; \
102 __VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
103 bb::assert_failure(oss.str()); \
104 } \
105 } while (0)
106
107#define BB_ASSERT_GT(left, right, ...) \
108 do { \
109 BB_BENCH_ASSERT("BB_ASSERT_GT" #left " > " #right); \
110 auto _left = (left); \
111 auto _right = (right); \
112 if (!(BB_LIKELY(_left > _right))) { \
113 std::ostringstream oss; \
114 oss << "Assertion failed: (" #left " > " #right ")\n"; \
115 oss << " Left : " << _left << "\n"; \
116 oss << " Right : " << _right; \
117 __VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
118 bb::assert_failure(oss.str()); \
119 } \
120 } while (0)
121
122#define BB_ASSERT_GTE(left, right, ...) \
123 do { \
124 BB_BENCH_ASSERT("BB_ASSERT_GTE" #left " >= " #right); \
125 auto _left = (left); \
126 auto _right = (right); \
127 if (!(BB_LIKELY(_left >= _right))) { \
128 std::ostringstream oss; \
129 oss << "Assertion failed: (" #left " >= " #right ")\n"; \
130 oss << " Left : " << _left << "\n"; \
131 oss << " Right : " << _right; \
132 __VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
133 bb::assert_failure(oss.str()); \
134 } \
135 } while (0)
136
137#define BB_ASSERT_LT(left, right, ...) \
138 do { \
139 BB_BENCH_ASSERT("BB_ASSERT_LT" #left " < " #right); \
140 auto _left = (left); \
141 auto _right = (right); \
142 if (!(BB_LIKELY(_left < _right))) { \
143 std::ostringstream oss; \
144 oss << "Assertion failed: (" #left " < " #right ")\n"; \
145 oss << " Left : " << _left << "\n"; \
146 oss << " Right : " << _right; \
147 __VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
148 bb::assert_failure(oss.str()); \
149 } \
150 } while (0)
151
152#define BB_ASSERT_LTE(left, right, ...) \
153 do { \
154 BB_BENCH_ASSERT("BB_ASSERT_LTE" #left " <= " #right); \
155 auto _left = (left); \
156 auto _right = (right); \
157 if (!(BB_LIKELY(_left <= _right))) { \
158 std::ostringstream oss; \
159 oss << "Assertion failed: (" #left " <= " #right ")\n"; \
160 oss << " Left : " << _left << "\n"; \
161 oss << " Right : " << _right; \
162 __VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
163 bb::assert_failure(oss.str()); \
164 } \
165 } while (0)
166#endif // __wasm__
167
168// These are used in tests.
169#ifdef BB_NO_EXCEPTIONS
170#define ASSERT_THROW_OR_ABORT(statement, matcher) ASSERT_DEATH(statement, matcher)
171#define EXPECT_THROW_OR_ABORT(statement, matcher) EXPECT_DEATH(statement, matcher)
172#else
173#define ASSERT_THROW_OR_ABORT(statement, matcher) ASSERT_THROW(statement, std::runtime_error)
174#define EXPECT_THROW_OR_ABORT(statement, matcher) EXPECT_THROW(statement, std::runtime_error)
175#endif // BB_NO_EXCEPTIONS
176// 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: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