5#ifndef RUNTIME_PLATFORM_ASSERT_H_
6#define RUNTIME_PLATFORM_ASSERT_H_
11#if !defined(DEBUG) && !defined(NDEBUG)
12#error neither DEBUG nor NDEBUG defined
13#elif defined(DEBUG) && defined(NDEBUG)
14#error both DEBUG and NDEBUG defined
20#if defined(DEBUG) || defined(TESTING)
35 bool will_abort =
false)
const;
60 template <
typename E,
typename A>
61 void Equals(
const E& expected,
const A& actual);
63 template <
typename E,
typename A>
64 void NotEquals(
const E& not_expected,
const A& actual);
66 template <
typename E,
typename A,
typename T>
67 void FloatEquals(
const E& expected,
const A& actual,
const T& tol);
69 void StringEquals(
const char* expected,
const char* actual);
71 void IsSubstring(
const char* needle,
const char* haystack);
73 void IsNotSubstring(
const char* needle,
const char* haystack);
75 template <
typename E,
typename A>
76 void LessThan(
const E& left,
const A& right);
78 template <
typename E,
typename A>
79 void LessEqual(
const E& left,
const A& right);
81 template <
typename E,
typename A>
82 void GreaterThan(
const E& left,
const A& right);
84 template <
typename E,
typename A>
85 void GreaterEqual(
const E& left,
const A& right);
94 static bool failed() {
return failed_; }
102 if (
p !=
nullptr)
return p;
103 Fail(
"expected: not nullptr, found nullptr");
110template <
typename E,
typename A>
112 if (actual == expected)
return;
113 std::ostringstream ess, ass;
116 std::string es = ess.str(), as = ass.str();
117 Fail(
"expected: <%s> but was: <%s>", es.c_str(), as.c_str());
120template <
typename E,
typename A>
122 if (actual != not_expected)
return;
123 std::ostringstream ness;
124 ness << not_expected;
125 std::string nes = ness.str();
126 Fail(
"did not expect: <%s>", nes.c_str());
129template <
typename E,
typename A,
typename T>
130void Expect::FloatEquals(
const E& expected,
const A& actual,
const T& tol) {
131 if (((expected - tol) <= actual) && (actual <= (expected + tol))) {
134 std::ostringstream ess, ass, tolss;
138 std::string es = ess.str(), as = ass.str(), tols = tolss.str();
139 Fail(
"expected: <%s> but was: <%s> (tolerance: <%s>)", es.c_str(), as.c_str(),
143static void Escape(std::string&
dst,
const char*
src) {
145 while ((c = *
src++) !=
'\0') {
148 }
else if (c ==
'\'') {
150 }
else if (c ==
'\"') {
152 }
else if (c ==
'\\') {
160inline void Expect::StringEquals(
const char* expected,
const char* actual) {
161 if (strcmp(expected, actual) == 0)
return;
162 if (actual ==
nullptr) {
163 Fail(
"expected:\n<\"%s\">\nbut was nullptr", expected);
165 if (strcmp(expected, actual) == 0)
return;
167 Escape(es, expected);
169 Fail(
"expected:\n<\"%s\">\nbut was:\n<\"%s\">", es.c_str(), as.c_str());
173inline void Expect::IsSubstring(
const char* needle,
const char* haystack) {
174 if (strstr(haystack, needle) !=
nullptr)
return;
175 Fail(
"expected <\"%s\"> to be a substring of <\"%s\">", needle, haystack);
178inline void Expect::IsNotSubstring(
const char* needle,
const char* haystack) {
179 if (strstr(haystack, needle) ==
nullptr)
return;
180 Fail(
"expected <\"%s\"> to not be a substring of <\"%s\">", needle, haystack);
183template <
typename E,
typename A>
184void Expect::LessThan(
const E& left,
const A& right) {
185 if (left < right)
return;
186 std::ostringstream ess, ass;
189 std::string es = ess.str(), as = ass.str();
190 Fail(
"expected: %s < %s", es.c_str(), as.c_str());
193template <
typename E,
typename A>
194void Expect::LessEqual(
const E& left,
const A& right) {
195 if (left <= right)
return;
196 std::ostringstream ess, ass;
199 std::string es = ess.str(), as = ass.str();
200 Fail(
"expected: %s <= %s", es.c_str(), as.c_str());
203template <
typename E,
typename A>
204void Expect::GreaterThan(
const E& left,
const A& right) {
205 if (left > right)
return;
206 std::ostringstream ess, ass;
209 std::string es = ess.str(), as = ass.str();
210 Fail(
"expected: %s > %s", es.c_str(), as.c_str());
213template <
typename E,
typename A>
214void Expect::GreaterEqual(
const E& left,
const A& right) {
215 if (left >= right)
return;
216 std::ostringstream ess, ass;
219 std::string es = ess.str(), as = ass.str();
220 Fail(
"expected: %s >= %s", es.c_str(), as.c_str());
224void Expect::NotNull(
const T p) {
225 if (
p !=
nullptr)
return;
226 Fail(
"expected: not nullptr, found nullptr");
231 if (
p ==
nullptr)
return;
232 Fail(
"expected: nullptr, found not null pointer");
239#define FATAL(format, ...) \
240 dart::Assert(__FILE__, __LINE__).Fail(format, __VA_ARGS__);
242#define FATAL(format, ...) \
243 dart::Assert(__FILE__, __LINE__).Fail(format, ##__VA_ARGS__);
246#define UNIMPLEMENTED() FATAL("unimplemented code")
248#define UNREACHABLE() FATAL("unreachable code")
250#define OUT_OF_MEMORY() FATAL("Out of memory.")
258#define ASSERT(cond) \
260 if (!(cond)) dart::Assert(__FILE__, __LINE__).Fail("expected: %s", #cond); \
263#define ASSERT_EQUAL(actual, expected) \
265 if ((expected) != (actual)) { \
266 const std::string actual_str = std::to_string(actual); \
267 const std::string expected_str = std::to_string(expected); \
268 dart::Assert(__FILE__, __LINE__) \
269 .Fail("expected \"%s\" = %s, actual \"%s\" = %s", #expected, \
270 expected_str.c_str(), #actual, actual_str.c_str()); \
274#define ASSERT_LESS_OR_EQUAL(actual, expected) \
276 if ((actual) > (expected)) { \
277 const std::string actual_str = std::to_string(actual); \
278 const std::string expected_str = std::to_string(expected); \
279 dart::Assert(__FILE__, __LINE__) \
280 .Fail("expected \"%s\" = %s >= actual \"%s\" = %s", #expected, \
281 expected_str.c_str(), #actual, actual_str.c_str()); \
285#define ASSERT_IMPLIES(antecedent, consequent) \
288 ASSERT(consequent); \
294#define DEBUG_ASSERT(cond) ASSERT(cond)
298#define ASSERT_NOTNULL(ptr) dart::Assert(__FILE__, __LINE__).NotNull((ptr))
305#define ASSERT(condition) \
307 } while (false && (condition))
309#define ASSERT_EQUAL(expected, actual) \
311 } while (false && ((expected) != (actual)))
313#define ASSERT_LESS_OR_EQUAL(expected, actual) \
315 } while (false && ((actual) > (expected)))
317#define ASSERT_IMPLIES(antecedent, consequent) \
319 } while (false && (!(antecedent) || (consequent)))
321#define DEBUG_ASSERT(cond)
323#define ASSERT_NOTNULL(ptr) (ptr)
327#define RELEASE_ASSERT(cond) \
329 if (!(cond)) dart::Assert(__FILE__, __LINE__).Fail("expected: %s", #cond); \
332#define RELEASE_ASSERT_WITH_MSG(cond, msg) \
335 dart::Assert(__FILE__, __LINE__).Fail("%s: expected: %s", msg, #cond); \
339#define COMPILE_ASSERT(expr) static_assert(expr, "")
348#define EXPECT(condition) \
349 if (!(condition)) { \
350 dart::Expect(__FILE__, __LINE__).Fail("expected: %s", #condition); \
353#define EXPECT_EQ(expected, actual) \
354 dart::Expect(__FILE__, __LINE__).Equals((expected), (actual))
356#define EXPECT_NE(not_expected, actual) \
357 dart::Expect(__FILE__, __LINE__).NotEquals((not_expected), (actual))
359#define EXPECT_FLOAT_EQ(expected, actual, tol) \
360 dart::Expect(__FILE__, __LINE__).FloatEquals((expected), (actual), (tol))
362#define EXPECT_STREQ(expected, actual) \
363 dart::Expect(__FILE__, __LINE__).StringEquals((expected), (actual))
365#define EXPECT_SUBSTRING(needle, haystack) \
366 dart::Expect(__FILE__, __LINE__).IsSubstring((needle), (haystack))
368#define EXPECT_NOTSUBSTRING(needle, haystack) \
369 dart::Expect(__FILE__, __LINE__).IsNotSubstring((needle), (haystack))
371#define EXPECT_LT(left, right) \
372 dart::Expect(__FILE__, __LINE__).LessThan((left), (right))
374#define EXPECT_LE(left, right) \
375 dart::Expect(__FILE__, __LINE__).LessEqual((left), (right))
377#define EXPECT_GT(left, right) \
378 dart::Expect(__FILE__, __LINE__).GreaterThan((left), (right))
380#define EXPECT_GE(left, right) \
381 dart::Expect(__FILE__, __LINE__).GreaterEqual((left), (right))
383#define EXPECT_NOTNULL(ptr) dart::Expect(__FILE__, __LINE__).NotNull((ptr))
385#define EXPECT_NULLPTR(ptr) dart::Expect(__FILE__, __LINE__).Null((ptr))
388#define FAIL(format, ...) \
389 dart::Expect(__FILE__, __LINE__).Fail(format, __VA_ARGS__);
391#define FAIL(format, ...) \
392 dart::Expect(__FILE__, __LINE__).Fail(format, ##__VA_ARGS__);
Assert(const char *file, int line)
DART_NORETURN void Fail(const char *format,...) const PRINTF_ATTRIBUTE(2
DART_NORETURN void T NotNull(const T p)
DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicAssertionHelper)
void Print(const char *format, va_list arguments, bool will_abort=false) const
DynamicAssertionHelper(const char *file, int line)
Expect(const char *file, int line)
void Fail(const char *format,...) const PRINTF_ATTRIBUTE(2
void static bool failed()
uint32_t uint32_t * format
SK_API sk_sp< SkSurface > Null(int width, int height)
static bool Equals(const Object &expected, const Object &actual)
bool Equals(const T *a, const T *b)
bool NotEquals(const T *a, const T *b)