Skip to content

Commit 8dcdea6

Browse files
authored
chore(bb): allow disabling asserts in tests (#17148)
1 parent aa31516 commit 8dcdea6

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "barretenberg/common/assert.hpp"
2+
#include "barretenberg/common/throw_or_abort.hpp"
3+
4+
namespace bb {
5+
AssertMode& get_assert_mode()
6+
{
7+
static AssertMode current_mode = AssertMode::ABORT;
8+
return current_mode;
9+
}
10+
11+
void assert_failure(std::string const& err)
12+
{
13+
if (get_assert_mode() == AssertMode::WARN) {
14+
info("NOT FOR PROD - assert as warning: ", err);
15+
return;
16+
}
17+
throw_or_abort(err);
18+
}
19+
} // namespace bb

barretenberg/cpp/src/barretenberg/common/assert.hpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
#pragma once
22

3-
#include "barretenberg/common/throw_or_abort.hpp"
3+
#include <cstdint>
44
#include <sstream>
55

6+
namespace bb {
7+
enum class AssertMode : std::uint8_t { ABORT, WARN };
8+
AssertMode& get_assert_mode();
9+
void assert_failure(std::string const& err);
10+
11+
// NOTE do not use in threaded contexts!
12+
struct AssertGuard {
13+
AssertGuard(AssertMode mode)
14+
: previous_mode(get_assert_mode())
15+
{
16+
get_assert_mode() = mode;
17+
}
18+
~AssertGuard() { get_assert_mode() = (previous_mode); }
19+
AssertMode previous_mode;
20+
};
21+
} // namespace bb
22+
23+
// NOTE do not use in threaded contexts!
24+
#define BB_DISABLE_ASSERTS() bb::AssertGuard __bb_assert_guard(bb::AssertMode::WARN)
25+
626
// NOLINTBEGIN
727
// Compiler should optimize this out in release builds, without triggering unused-variable warnings.
828
#define DONT_EVALUATE(expression) \
@@ -42,7 +62,7 @@
4262
if (!(expression)) { \
4363
info("Assertion failed: (" #expression ")"); \
4464
__VA_OPT__(info("Reason : ", __VA_ARGS__);) \
45-
throw_or_abort(""); \
65+
bb::assert_failure(""); \
4666
} \
4767
} while (0)
4868

@@ -52,7 +72,7 @@
5272
std::ostringstream oss; \
5373
oss << "Assertion failed: (" #expression ")"; \
5474
__VA_OPT__(oss << " | Reason: " << __VA_ARGS__;) \
55-
throw_or_abort(oss.str()); \
75+
bb::assert_failure(oss.str()); \
5676
} \
5777
} while (0)
5878

@@ -66,7 +86,7 @@
6686
oss << " Actual : " << _actual << "\n"; \
6787
oss << " Expected: " << _expected; \
6888
__VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
69-
throw_or_abort(oss.str()); \
89+
bb::assert_failure(oss.str()); \
7090
} \
7191
} while (0)
7292

@@ -80,7 +100,7 @@
80100
oss << " Actual : " << _actual << "\n"; \
81101
oss << " Not expected: " << _expected; \
82102
__VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
83-
throw_or_abort(oss.str()); \
103+
bb::assert_failure(oss.str()); \
84104
} \
85105
} while (0)
86106

@@ -94,7 +114,7 @@
94114
oss << " Left : " << _left << "\n"; \
95115
oss << " Right : " << _right; \
96116
__VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
97-
throw_or_abort(oss.str()); \
117+
bb::assert_failure(oss.str()); \
98118
} \
99119
} while (0)
100120

@@ -108,7 +128,7 @@
108128
oss << " Left : " << _left << "\n"; \
109129
oss << " Right : " << _right; \
110130
__VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
111-
throw_or_abort(oss.str()); \
131+
bb::assert_failure(oss.str()); \
112132
} \
113133
} while (0)
114134

@@ -122,7 +142,7 @@
122142
oss << " Left : " << _left << "\n"; \
123143
oss << " Right : " << _right; \
124144
__VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
125-
throw_or_abort(oss.str()); \
145+
bb::assert_failure(oss.str()); \
126146
} \
127147
} while (0)
128148

@@ -136,7 +156,7 @@
136156
oss << " Left : " << _left << "\n"; \
137157
oss << " Right : " << _right; \
138158
__VA_OPT__(oss << "\n Reason : " << __VA_ARGS__;) \
139-
throw_or_abort(oss.str()); \
159+
bb::assert_failure(oss.str()); \
140160
} \
141161
} while (0)
142162
#endif // __wasm__

0 commit comments

Comments
 (0)