2011-05-23 17:18:52 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2019-05-17 10:48:29 +10:00
|
|
|
* Copyright 2010-2019 Danny Robson <danny@nerdcruft.net>
|
2011-05-23 17:18:52 +10:00
|
|
|
*/
|
|
|
|
|
2018-05-10 12:44:03 +10:00
|
|
|
#pragma once
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2021-04-12 16:03:22 +10:00
|
|
|
#include "./common.hpp"
|
2021-01-20 15:18:25 +11:00
|
|
|
#include "./panic.hpp"
|
2021-04-12 16:03:22 +10:00
|
|
|
#include "./debugger.hpp"
|
|
|
|
|
2019-05-17 12:26:08 +10:00
|
|
|
#include "../platform.hpp"
|
2021-01-20 15:18:25 +11:00
|
|
|
#include "../maths.hpp"
|
2019-05-04 11:32:59 +10:00
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2020-09-10 15:08:10 +10:00
|
|
|
#include <utility>
|
|
|
|
|
2016-02-03 12:04:47 +11:00
|
|
|
|
2015-01-28 14:49:34 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2019-05-04 11:32:59 +10:00
|
|
|
#ifdef COMPILER_GCC
|
|
|
|
#define CHECK(C) do { \
|
|
|
|
_Pragma("GCC diagnostic push") \
|
|
|
|
_Pragma("GCC diagnostic ignored \"-Wnonnull-compare\"") \
|
|
|
|
DEBUG_ONLY ( \
|
|
|
|
if (!(C)) \
|
|
|
|
panic (#C); \
|
|
|
|
); \
|
|
|
|
_Pragma("GCC diagnostic pop") \
|
|
|
|
} while (0)
|
2019-05-17 12:26:08 +10:00
|
|
|
#elif defined(COMPILER_CLANG)
|
2019-05-04 11:32:59 +10:00
|
|
|
#define CHECK(C) do { \
|
|
|
|
DEBUG_ONLY ( \
|
|
|
|
if (!(C)) \
|
|
|
|
panic (#C); \
|
|
|
|
); \
|
2016-02-03 12:04:47 +11:00
|
|
|
} while (0)
|
2019-05-17 12:26:08 +10:00
|
|
|
#else
|
|
|
|
#error Unhandled compiler
|
2019-05-04 11:32:59 +10:00
|
|
|
#endif
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2016-06-24 15:30:41 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-04-24 15:12:09 +10:00
|
|
|
#define SCOPED_SANITY(A) \
|
2018-08-05 14:42:02 +10:00
|
|
|
::cruft::debug::scoped_sanity PASTE(__scoped_sanity_checker,__LINE__) ((A)); \
|
2018-04-18 21:43:58 +10:00
|
|
|
(void)PASTE(__scoped_sanity_checker,__LINE__);
|
2016-06-24 15:30:41 +10:00
|
|
|
|
|
|
|
|
2018-04-24 15:12:09 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-08-05 14:42:02 +10:00
|
|
|
#define CHECK_SANITY(A,...) CHECK(::cruft::debug::is_valid ((A) __VA_OPT__(,) __VA_ARGS__))
|
2018-04-24 15:12:09 +10:00
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define CHECK_EQ(A,B) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2016-08-03 18:11:29 +10:00
|
|
|
const auto &__a = (A); \
|
|
|
|
const auto &__b = (B); \
|
2016-02-03 12:04:47 +11:00
|
|
|
\
|
2018-08-05 14:42:02 +10:00
|
|
|
if (!::cruft::almost_equal (__a, __b)) { \
|
2018-05-10 12:44:03 +10:00
|
|
|
std::cerr << "expected equality\n" \
|
|
|
|
"__a: " #A " is " << __a << "\n" \
|
|
|
|
"__b: " #B " is " << __b << "\n"; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
2016-02-03 12:04:47 +11:00
|
|
|
} \
|
2015-01-28 15:01:17 +11:00
|
|
|
); \
|
2011-05-23 17:18:52 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-05-10 12:44:03 +10:00
|
|
|
#define CHECK_LT(A,B) do { \
|
|
|
|
DEBUG_ONLY ( \
|
|
|
|
const auto &__a = (A); \
|
|
|
|
const auto &__b = (B); \
|
|
|
|
\
|
|
|
|
if (__a >= __b) { \
|
|
|
|
std::cerr << "expected less than\n" \
|
|
|
|
"__a: " << #A << " is " << __a << "\n" \
|
|
|
|
"__b: " << #B << " is " << __b << "\n"; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
2018-05-10 12:44:03 +10:00
|
|
|
}; \
|
|
|
|
); \
|
2012-11-09 15:11:18 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-05-10 12:44:03 +10:00
|
|
|
#define CHECK_LE(A,B) do { \
|
|
|
|
DEBUG_ONLY ( \
|
|
|
|
const auto &__a = (A); \
|
|
|
|
const auto &__b = (B); \
|
|
|
|
\
|
2019-02-03 17:28:53 +11:00
|
|
|
if (!(__a <= __b)) { \
|
2018-05-10 12:44:03 +10:00
|
|
|
std::cerr << "expected less than or equal\n" \
|
|
|
|
"__a: " << #A << " is " << __a << "\n" \
|
|
|
|
"__b: " << #B << " is " << __b << "\n"; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
2018-05-10 12:44:03 +10:00
|
|
|
} \
|
|
|
|
); \
|
2014-02-12 17:05:37 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-05-10 12:44:03 +10:00
|
|
|
#define CHECK_GT(A,B) do { \
|
|
|
|
DEBUG_ONLY ( \
|
|
|
|
const auto &__a = (A); \
|
|
|
|
const auto &__b = (B); \
|
|
|
|
\
|
|
|
|
if (__a <= __b) { \
|
|
|
|
std::cerr << "expected greater than\n" \
|
|
|
|
"__a: " << #A << " is " << __a << "\n" \
|
|
|
|
"__b: " << #B << " is " << __b << "\n"; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
2018-05-10 12:44:03 +10:00
|
|
|
} \
|
|
|
|
); \
|
2012-11-09 15:11:18 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-05-10 12:44:03 +10:00
|
|
|
#define CHECK_GE(A,B) do { \
|
|
|
|
DEBUG_ONLY ( \
|
|
|
|
const auto &__a = (A); \
|
|
|
|
const auto &__b = (B); \
|
|
|
|
\
|
|
|
|
if (__a < __b) { \
|
|
|
|
std::cerr << "expected greater or equal\n" \
|
|
|
|
"__a: " << #A << " is " << __a << "\n" \
|
|
|
|
"__b: " << #B << " is " << __b << "\n"; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
2018-05-10 12:44:03 +10:00
|
|
|
}; \
|
|
|
|
); \
|
2014-02-12 17:05:37 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2015-05-29 15:51:25 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2020-09-24 08:03:41 +10:00
|
|
|
#define CHECK_INCLUSIVE(V,L,H) do { \
|
2018-05-10 12:44:03 +10:00
|
|
|
DEBUG_ONLY ( \
|
|
|
|
const auto &__v = (V); \
|
|
|
|
const auto &__l = (L); \
|
|
|
|
const auto &__h = (H); \
|
|
|
|
\
|
|
|
|
if (__v < __l || __v > __h) { \
|
2020-09-24 08:03:41 +10:00
|
|
|
std::cerr << "expected inclusive\n" \
|
2018-05-10 12:44:03 +10:00
|
|
|
"__l: " << #L << " is " << +__l << "\n" \
|
|
|
|
"__h: " << #H << " is " << +__h << "\n" \
|
|
|
|
"__v: " << #V << " is " << +__v << "\n"; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
2018-05-10 12:44:03 +10:00
|
|
|
}; \
|
|
|
|
); \
|
2015-05-29 15:51:25 +10:00
|
|
|
} while (0)
|
|
|
|
|
2020-09-24 08:03:41 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define CHECK_INDEX(V,H) do { \
|
|
|
|
DEBUG_ONLY ( \
|
|
|
|
const auto &__v = (V); \
|
|
|
|
const auto &__h = (H); \
|
|
|
|
\
|
|
|
|
_Pragma("GCC diagnostic push") \
|
|
|
|
_Pragma("GCC diagnostic ignored \"-Wtype-limits\"") \
|
|
|
|
\
|
|
|
|
if (intmax_t (__v) < 0 || __v >= __h) { \
|
|
|
|
std::cerr << "expected index\n" \
|
|
|
|
"__h: " << #H << " is " << +__h << "\n" \
|
|
|
|
"__v: " << #V << " is " << +__v << "\n"; \
|
|
|
|
breakpoint (); \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
_Pragma("GCC diagnostic pop") \
|
|
|
|
); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-05-10 12:44:03 +10:00
|
|
|
#define CHECK_NEQ(A,B) do { \
|
|
|
|
DEBUG_ONLY( \
|
|
|
|
const auto &__a = (A); \
|
|
|
|
const auto &__b = (B); \
|
|
|
|
\
|
2018-08-05 14:42:02 +10:00
|
|
|
if (::cruft::almost_equal (__a, __b)) { \
|
2018-05-10 12:44:03 +10:00
|
|
|
std::cerr << "expected inequality\n" \
|
|
|
|
"__a: " << #A << " is " << __a << "\n" \
|
|
|
|
"__b: " << #B << " is " << __b << "\n"; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
2018-05-10 12:44:03 +10:00
|
|
|
}; \
|
|
|
|
); \
|
2011-05-23 17:18:52 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2015-02-13 18:02:09 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-05-10 12:44:03 +10:00
|
|
|
#define CHECK_ZERO(A) do { \
|
|
|
|
DEBUG_ONLY ( \
|
|
|
|
const auto &__a = (A); \
|
|
|
|
\
|
2018-08-05 14:42:02 +10:00
|
|
|
if (!::cruft::almost_zero (__a)) { \
|
2018-05-10 12:44:03 +10:00
|
|
|
std::cerr << "expected zero\n" \
|
|
|
|
"__a: " << #A << " is " << __a << "\n"; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
2018-05-10 12:44:03 +10:00
|
|
|
}; \
|
|
|
|
); \
|
2015-02-13 18:02:09 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2015-02-05 20:35:11 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-05-10 12:44:03 +10:00
|
|
|
#define CHECK_NEZ(A) do { \
|
|
|
|
DEBUG_ONLY ( \
|
|
|
|
const auto &__a = (A); \
|
|
|
|
\
|
2018-08-05 14:42:02 +10:00
|
|
|
if (::cruft::exactly_zero (__a)) { \
|
2018-05-10 12:44:03 +10:00
|
|
|
std::cerr << "expected non-zero\n" \
|
|
|
|
"__a: " << #A << " is " << __a << '\n'; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
|
|
|
} \
|
2018-05-10 12:44:03 +10:00
|
|
|
); \
|
2015-02-05 20:35:11 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2016-08-03 18:12:05 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-05-10 12:44:03 +10:00
|
|
|
#define CHECK_MOD(V,M) do { \
|
|
|
|
DEBUG_ONLY ( \
|
|
|
|
const auto &__check_mod_v = (V); \
|
|
|
|
const auto &__check_mod_m = (M); \
|
|
|
|
\
|
2018-08-05 14:42:02 +10:00
|
|
|
if (!::cruft::exactly_zero (__check_mod_v % __check_mod_m)) { \
|
2018-05-10 12:44:03 +10:00
|
|
|
std::cerr << "expected zero modulus\n" \
|
|
|
|
"__v: " << #V << " is " << __check_mod_v << "\n" \
|
|
|
|
"__m: " << #M << " is " << __check_mod_m << "\n"; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
|
|
|
} \
|
2018-05-10 12:44:03 +10:00
|
|
|
); \
|
2016-08-03 18:12:05 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2016-02-05 14:27:58 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#if defined(ENABLE_DEBUGGING)
|
2018-05-10 12:44:03 +10:00
|
|
|
#define CHECK_ENUM(C, ...) do { \
|
|
|
|
const auto &__c = (C); \
|
|
|
|
const auto &__e = { __VA_ARGS__ }; \
|
|
|
|
\
|
|
|
|
if (std::find (std::cbegin (__e), \
|
|
|
|
std::cend (__e), \
|
|
|
|
__c) == std::end (__e)) { \
|
|
|
|
std::cerr << "expect enum\n" \
|
|
|
|
"__c: " << #C << " is " << __c << '\n'; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
2018-05-10 12:44:03 +10:00
|
|
|
} \
|
2016-02-05 14:27:58 +11:00
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define CHECK_ENUM(C,...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2018-04-13 18:49:36 +10:00
|
|
|
#if !defined(NDEBUG)
|
2019-05-17 12:26:08 +10:00
|
|
|
#define CHECK_FINITE(V) do { \
|
2018-05-10 12:44:03 +10:00
|
|
|
const auto &__v = (V); \
|
|
|
|
if (!std::isfinite (__v)) { \
|
|
|
|
std::cerr << "expected finite value\n" \
|
|
|
|
"__v: " << #V << " is " << __v << '\n'; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
2018-05-10 12:44:03 +10:00
|
|
|
} \
|
2018-04-13 18:49:36 +10:00
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define CHECK_FINITE(V,...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-05 14:27:58 +11:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-05-10 12:44:03 +10:00
|
|
|
#define CHECK_THROWS(E,C) do { \
|
|
|
|
DEBUG_ONLY ( \
|
|
|
|
bool caught = false; \
|
|
|
|
\
|
|
|
|
try \
|
|
|
|
{ C; } \
|
|
|
|
catch (E const&) \
|
|
|
|
{ caught = true; } \
|
|
|
|
\
|
2018-05-10 14:53:43 +10:00
|
|
|
if (!caught) { \
|
2018-05-10 12:44:03 +10:00
|
|
|
std::cerr << "expected exception: " << #E << '\n' \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
|
|
|
} \
|
2018-05-10 12:44:03 +10:00
|
|
|
); \
|
2011-05-23 17:18:52 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-05-10 12:44:03 +10:00
|
|
|
#define CHECK_NOTHROW(C) do { \
|
|
|
|
DEBUG_ONLY ( \
|
|
|
|
try { \
|
|
|
|
C; \
|
|
|
|
} catch (const std::exception &e) { \
|
|
|
|
std::cerr << "unexpected exception: " << e.what () << '\n'; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
2018-05-10 12:44:03 +10:00
|
|
|
} catch (...) { \
|
|
|
|
std::cerr << "unexpected exception: unknown\n"; \
|
2018-05-10 14:53:43 +10:00
|
|
|
breakpoint (); \
|
2018-05-10 12:44:03 +10:00
|
|
|
} \
|
|
|
|
); \
|
2012-06-20 16:47:48 +10:00
|
|
|
} while (0)
|