forked from vmware/concord-bft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXAssert.h
272 lines (240 loc) · 15 KB
/
XAssert.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Concord
//
// Copyright (c) 2018 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Apache 2.0 license (the "License").
// You may not use this product except in compliance with the Apache 2.0 License.
//
// This product may include a number of subcomponents with separate copyright
// notices and license terms. Your use of these subcomponents is subject to the
// terms and conditions of the subcomponent's license, as noted in the
// LICENSE file.
#pragma once
#include <cassert>
#include <cstdlib>
#include <typeinfo>
namespace XAssert {
/**
* Dereferences a null pointer and causes the program to coredump so that
* the state can be inspected.
*/
bool coredump();
/**
* Makes sure asserts are enabled or segfaults.
*/
// void assertAssertsEnabled();
} // namespace XAssert
#ifndef XASSERT_OSTREAM
// This is the std::ostream object where assert error messages go to
#include <iostream>
#define XASSERT_OSTREAM std::cerr
#endif
// Assert error messages start with this
#define XASSERT_START \
XASSERT_OSTREAM << "Assertion failed in function '" << __FUNCTION__ << "', " << __FILE__ << ":" << __LINE__ << ": "
// Assert errors cause the program to exit with the following call
#define XASSERT_EXIT \
{ \
XAssert::coredump(); \
exit(1); \
}
#define XASSERT_NO_EVAL(expr) \
{ true ? static_cast<void>(0) : static_cast<void>((expr)); }
/**
* Here we define all our assert macros, after which we define macros that call them!
*/
#define XASSERT_InclusiveRange(start, middle, end) \
{ \
assertGreaterThanOrEqual(middle, start); \
assertLessThanOrEqual(middle, end); \
}
// Checks if 1st > 2nd
#define XASSERT_StrictlyGreaterThan(bigger, smaller) \
{ \
if ((bigger) <= (smaller)) { \
XASSERT_START << "Expected '" << #bigger << "' (" << (bigger) << ") to be strictly greater than '" << #smaller \
<< "' (" << (smaller) << ")" << std::endl; \
XASSERT_EXIT; \
} \
}
// Checks if 1st < 2nd
#define XASSERT_StrictlyLessThan(smaller, bigger) \
{ \
if ((smaller) >= (bigger)) { \
XASSERT_START << "Expected '" << #smaller << "' (" << (smaller) << ") to be strictly less than '" << #bigger \
<< "' (" << (bigger) << ")" << std::endl; \
XASSERT_EXIT; \
} \
}
// Checks if 1st >= 2nd
#define XASSERT_GreaterThanOrEqual(bigger, smaller) \
{ \
if ((bigger) < (smaller)) { \
XASSERT_START << "Expected '" << #bigger << "' (" << (bigger) << ") to be greater than or equal to '" \
<< #smaller << "' (" << (smaller) << ")" << std::endl; \
XASSERT_EXIT; \
} \
}
// Checks if 1st <= 2nd
#define XASSERT_LessThanOrEqual(smaller, bigger) \
{ \
if ((smaller) > (bigger)) { \
XASSERT_START << "Expected '" << #smaller << "' (" << (smaller) << ") to be less than or equal to '" << #bigger \
<< "' (" << (bigger) << ")" << std::endl; \
XASSERT_EXIT; \
} \
}
// Checks if 1st == 2nd
#define XASSERT_Equal(first, second) \
{ \
if ((first) != (second)) { \
XASSERT_START << "Expected '" << #first << "' (" << (first) << ") to be equal to '" << #second << "' (" \
<< (second) << ")" << std::endl; \
XASSERT_EXIT; \
} \
}
// Checks if 1st != 2nd
#define XASSERT_NotEqual(first, second) \
{ \
if ((first) == (second)) { \
XASSERT_START << "Expected '" << #first << "' (" << (first) << ") to NOT be equal to '" << #second << "' (" \
<< (second) << ")" << std::endl; \
XASSERT_EXIT; \
} \
}
// Checks if first == 0
#define XASSERT_IsZero(first) \
{ \
if ((first)) { \
XASSERT_START << "Expected '" << #first << "' (" << (first) << ") to be zero" << std::endl; \
XASSERT_EXIT; \
} \
}
// Checks if first != 0
#define XASSERT_NotZero(first) \
{ \
if ((first) == 0) { \
XASSERT_START << "Expected '" << #first << "' (" << (first) << ") to NOT be zero" << std::endl; \
XASSERT_EXIT; \
} \
}
// Exits with the specified error message
#define XASSERT_Fail(msg) \
{ \
XASSERT_START << (msg) << std::endl; \
XASSERT_EXIT; \
}
// Checks if the specified property is true for the specified value
// Useful when you're checking a complex assertion and you want to see the value that failed that assertion.
// e.g., assertProperty(finalSign, finalSign == -1 || finalSign == 1)
#define XASSERT_Property(value, property) \
{ \
if ((property) == false) { \
XASSERT_START << "'" << #property << "' is NOT true for '" << #value << "' (" << (value) << ")" << std::endl; \
XASSERT_EXIT; \
} \
}
// Checks if p != NULL
#define XASSERT_NotNull(p) \
{ \
if ((p) == nullptr) { \
XASSERT_START << "'" << #p << "' is NULL" << std::endl; \
XASSERT_EXIT; \
} \
}
// Checks if x > 0
#define XASSERT_StrictlyPositive(x) \
{ \
if ((x) <= 0) { \
XASSERT_START << "'" << #x << "' is NOT greater than zero" << std::endl; \
XASSERT_EXIT; \
} \
}
// Checks if p == NULL
#define XASSERT_Null(p) \
{ \
if ((p) != nullptr) { \
XASSERT_START << "'" << #p << "' is NOT NULL" << std::endl; \
XASSERT_EXIT; \
} \
}
#define XASSERT_True(e) \
{ \
if ((e) != true) { \
XASSERT_START << "'" << #e << "' is NOT true" << std::endl; \
XASSERT_EXIT; \
} \
}
#define XASSERT_False(e) \
{ \
if ((e) != false) { \
XASSERT_START << "'" << #e << "' is NOT false (i.e., it's true)" << std::endl; \
XASSERT_EXIT; \
} \
}
/**
* Defining assert* calls during DEBUG builds!
*/
#ifndef NDEBUG
template <class T, class S>
T& checked_ref_cast(S& source) {
return dynamic_cast<T&>(source);
}
#define assertFalse(expr) XASSERT_False(expr)
#define assertTrue(expr) XASSERT_True(expr)
#define assertInclusiveRange(start, middle, end) XASSERT_InclusiveRange(start, middle, end)
#define assertStrictlyPositive(x) XASSERT_StrictlyPositive(x)
#define assertStrictlyGreaterThan(bigger, smaller) XASSERT_StrictlyGreaterThan(bigger, smaller)
#define assertStrictlyLessThan(smaller, bigger) XASSERT_StrictlyLessThan(smaller, bigger)
#define assertGreaterThanOrEqual(bigger, smaller) XASSERT_GreaterThanOrEqual(bigger, smaller)
#define assertLessThanOrEqual(smaller, bigger) XASSERT_LessThanOrEqual(smaller, bigger)
#define assertEqual(first, second) XASSERT_Equal(first, second)
#define assertIsZero(first) XASSERT_IsZero(first)
#define assertNotZero(first) XASSERT_NotZero(first)
#define assertFail(msg) XASSERT_Fail(msg)
#define assertProperty(value, property) XASSERT_Property(value, property)
#define assertNotNull(p) XASSERT_NotNull(p)
#define assertNotEqual(first, second) XASSERT_NotEqual(first, second)
#define assertNull(p) XASSERT_Null(p)
#else
template <class T, class S>
T& checked_ref_cast(S& source) {
return reinterpret_cast<T&>(source);
}
/**
* WARNING: You need the (void)arg around every argument 'arg.' On some compilers, not having it
* will trigger an "error: right operand of comma operator has no effect [-Werror=unused-value]"
*/
#define assertFalse(expr) XASSERT_NO_EVAL(expr)
#define assertTrue(expr) XASSERT_NO_EVAL(expr)
#define assertInclusiveRange(start, middle, end) XASSERT_NO_EVAL(start) XASSERT_NO_EVAL(middle) XASSERT_NO_EVAL(end)
#define assertStrictlyPositive(x) XASSERT_NO_EVAL(x)
#define assertStrictlyGreaterThan(bigger, smaller) XASSERT_NO_EVAL(bigger) XASSERT_NO_EVAL(smaller)
#define assertStrictlyLessThan(smaller, bigger) XASSERT_NO_EVAL(smaller) XASSERT_NO_EVAL(bigger)
#define assertGreaterThanOrEqual(bigger, smaller) XASSERT_NO_EVAL(bigger) XASSERT_NO_EVAL(smaller)
#define assertLessThanOrEqual(smaller, bigger) XASSERT_NO_EVAL(smaller) XASSERT_NO_EVAL(bigger)
#define assertEqual(first, second) XASSERT_NO_EVAL(first) XASSERT_NO_EVAL(second)
#define assertIsZero(first) XASSERT_NO_EVAL(first)
#define assertNotZero(first) XASSERT_NO_EVAL(first)
#define assertFail(msg) XASSERT_NO_EVAL(msg)
#define assertProperty(value, property) XASSERT_NO_EVAL(value) XASSERT_NO_EVAL(property)
#define assertNotNull(p) XASSERT_NO_EVAL(p)
#define assertNotEqual(first, second) XASSERT_NO_EVAL(first) XASSERT_NO_EVAL(second)
#define assertNull(p) XASSERT_NO_EVAL(p)
#endif
#define testAssertFalse(expr) XASSERT_False(expr)
#define testAssertTrue(expr) XASSERT_True(expr)
#define testAssertInclusiveRange(start, middle, end) XASSERT_InclusiveRange(start, middle, end)
#define testAssertStrictlyPositive(x) XASSERT_StrictlyPositive(x)
#define testAssertStrictlyGreaterThan(bigger, smaller) XASSERT_StrictlyGreaterThan(bigger, smaller)
#define testAssertStrictlyLessThan(smaller, bigger) XASSERT_StrictlyLessThan(smaller, bigger)
#define testAssertGreaterThanOrEqual(bigger, smaller) XASSERT_GreaterThanOrEqual(bigger, smaller)
#define testAssertLessThanOrEqual(smaller, bigger) XASSERT_LessThanOrEqual(smaller, bigger)
#define testAssertEqual(first, second) XASSERT_Equal(first, second)
#define testAssertIsZero(first) XASSERT_IsZero(first)
#define testAssertNotZero(first) XASSERT_NotZero(first)
#define testAssertFail(msg) XASSERT_Fail(msg)
#define testAssertProperty(value, property) XASSERT_Property(value, property)
#define testAssertNotNull(p) XASSERT_NotNull(p)
#define testAssertNotEqual(first, second) XASSERT_NotEqual(first, second)
#define testAssertNull(p) XASSERT_Null(p)