debug/assert: don't use iostream

This commit is contained in:
Danny Robson 2021-04-12 16:56:06 +10:00
parent 08566fde6c
commit 0977fb89b3
11 changed files with 50 additions and 91 deletions

View File

@ -12,6 +12,7 @@
#include <iterator> #include <iterator>
#include <stdexcept> #include <stdexcept>
#include <iomanip>
using cruft::parray; using cruft::parray;

View File

@ -7,3 +7,7 @@
*/ */
#include "./assert.hpp" #include "./assert.hpp"
#include <fmt/ostream.h>
#include <iostream>

View File

@ -9,18 +9,19 @@
#pragma once #pragma once
#include "./common.hpp" #include "./common.hpp"
#include "./panic.hpp"
#include "./debugger.hpp" #include "./debugger.hpp"
#include "../platform.hpp" #include "../platform.hpp"
#include "../maths.hpp" #include "../maths.hpp"
#include <utility> #include <utility>
#ifndef NDEBUG #include <cassert>
#include <iostream>
#endif
///////////////////////////////////////////////////////////////////////////////
#define assert_message(MSG) __assert_fail(MSG, __FILE__, __LINE__, __PRETTY_FUNCTION__);
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#ifdef COMPILER_GCC #ifdef COMPILER_GCC
@ -29,7 +30,7 @@
_Pragma("GCC diagnostic ignored \"-Wnonnull-compare\"") \ _Pragma("GCC diagnostic ignored \"-Wnonnull-compare\"") \
DEBUG_ONLY ( \ DEBUG_ONLY ( \
if (!(C)) \ if (!(C)) \
panic (#C); \ assert_message (#C); \
); \ ); \
_Pragma("GCC diagnostic pop") \ _Pragma("GCC diagnostic pop") \
} while (0) } while (0)
@ -37,7 +38,7 @@
#define CHECK(C) do { \ #define CHECK(C) do { \
DEBUG_ONLY ( \ DEBUG_ONLY ( \
if (!(C)) \ if (!(C)) \
panic (#C); \ assert_message (#C); \
); \ ); \
} while (0) } while (0)
#else #else
@ -62,9 +63,7 @@
const auto &__b = (B); \ const auto &__b = (B); \
\ \
if (!::cruft::almost_equal (__a, __b)) { \ if (!::cruft::almost_equal (__a, __b)) { \
std::cerr << "expected equality\n" \ assert_message ("expected: " #A " == " #B); \
"__a: " #A " is " << __a << "\n" \
"__b: " #B " is " << __b << "\n"; \
breakpoint (); \ breakpoint (); \
} \ } \
); \ ); \
@ -78,9 +77,7 @@
const auto &__b = (B); \ const auto &__b = (B); \
\ \
if (__a >= __b) { \ if (__a >= __b) { \
std::cerr << "expected less than\n" \ assert_message ("expected: " #A " < " #B); \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
breakpoint (); \ breakpoint (); \
}; \ }; \
); \ ); \
@ -94,9 +91,7 @@
const auto &__b = (B); \ const auto &__b = (B); \
\ \
if (!(__a <= __b)) { \ if (!(__a <= __b)) { \
std::cerr << "expected less than or equal\n" \ assert_message ("expected: " #A " <= " #B); \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
breakpoint (); \ breakpoint (); \
} \ } \
); \ ); \
@ -110,9 +105,7 @@
const auto &__b = (B); \ const auto &__b = (B); \
\ \
if (__a <= __b) { \ if (__a <= __b) { \
std::cerr << "expected greater than\n" \ assert_message ("expected: " #A " > " #B); \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
breakpoint (); \ breakpoint (); \
} \ } \
); \ ); \
@ -126,9 +119,7 @@
const auto &__b = (B); \ const auto &__b = (B); \
\ \
if (__a < __b) { \ if (__a < __b) { \
std::cerr << "expected greater or equal\n" \ assert_message ("expected: " #A " >= " #B); \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
breakpoint (); \ breakpoint (); \
}; \ }; \
); \ ); \
@ -143,10 +134,7 @@
const auto &__h = (H); \ const auto &__h = (H); \
\ \
if (__v < __l || __v > __h) { \ if (__v < __l || __v > __h) { \
std::cerr << "expected inclusive\n" \ assert_message ("expected: " #L " <= " #V " <= " #H); \
"__l: " << #L << " is " << +__l << "\n" \
"__h: " << #H << " is " << +__h << "\n" \
"__v: " << #V << " is " << +__v << "\n"; \
breakpoint (); \ breakpoint (); \
}; \ }; \
); \ ); \
@ -163,9 +151,7 @@
_Pragma("GCC diagnostic ignored \"-Wtype-limits\"") \ _Pragma("GCC diagnostic ignored \"-Wtype-limits\"") \
\ \
if (intmax_t (__v) < 0 || __v >= __h) { \ if (intmax_t (__v) < 0 || __v >= __h) { \
std::cerr << "expected index\n" \ assert_message ("expected: 0 <= " #V " <= " #H); \
"__h: " << #H << " is " << +__h << "\n" \
"__v: " << #V << " is " << +__v << "\n"; \
breakpoint (); \ breakpoint (); \
}; \ }; \
\ \
@ -181,9 +167,7 @@
const auto &__b = (B); \ const auto &__b = (B); \
\ \
if (::cruft::almost_equal (__a, __b)) { \ if (::cruft::almost_equal (__a, __b)) { \
std::cerr << "expected inequality\n" \ assert_message ("expected: " #A " != " #B); \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
breakpoint (); \ breakpoint (); \
}; \ }; \
); \ ); \
@ -196,8 +180,7 @@
const auto &__a = (A); \ const auto &__a = (A); \
\ \
if (!::cruft::almost_zero (__a)) { \ if (!::cruft::almost_zero (__a)) { \
std::cerr << "expected zero\n" \ assert_message ("expected: " #A " == 0"); \
"__a: " << #A << " is " << __a << "\n"; \
breakpoint (); \ breakpoint (); \
}; \ }; \
); \ ); \
@ -210,8 +193,7 @@
const auto &__a = (A); \ const auto &__a = (A); \
\ \
if (::cruft::exactly_zero (__a)) { \ if (::cruft::exactly_zero (__a)) { \
std::cerr << "expected non-zero\n" \ assert_message ("expected: " #A " != 0"); \
"__a: " << #A << " is " << __a << '\n'; \
breakpoint (); \ breakpoint (); \
} \ } \
); \ ); \
@ -225,9 +207,7 @@
const auto &__check_mod_m = (M); \ const auto &__check_mod_m = (M); \
\ \
if (!::cruft::exactly_zero (__check_mod_v % __check_mod_m)) { \ if (!::cruft::exactly_zero (__check_mod_v % __check_mod_m)) { \
std::cerr << "expected zero modulus\n" \ assert_message ("expected: " #V " % " #M " == 0"); \
"__v: " << #V << " is " << __check_mod_v << "\n" \
"__m: " << #M << " is " << __check_mod_m << "\n"; \
breakpoint (); \ breakpoint (); \
} \ } \
); \ ); \
@ -243,8 +223,7 @@
if (std::find (std::cbegin (__e), \ if (std::find (std::cbegin (__e), \
std::cend (__e), \ std::cend (__e), \
__c) == std::end (__e)) { \ __c) == std::end (__e)) { \
std::cerr << "expect enum\n" \ assert_message ("expected: " #C " in " #__VA_ARGS__); \
"__c: " << #C << " is " << __c << '\n'; \
breakpoint (); \ breakpoint (); \
} \ } \
} while (0) } while (0)
@ -253,51 +232,15 @@
#endif #endif
//-----------------------------------------------------------------------------
#if !defined(NDEBUG) #if !defined(NDEBUG)
#define CHECK_FINITE(V) do { \ #define CHECK_FINITE(V) do { \
const auto &__v = (V); \ const auto &__v = (V); \
if (!std::isfinite (__v)) { \ if (!std::isfinite (__v)) { \
std::cerr << "expected finite value\n" \ assert_message ("expected: " #V " is finite"); \
"__v: " << #V << " is " << __v << '\n'; \
breakpoint (); \ breakpoint (); \
} \ } \
} while (0) } while (0)
#else #else
#define CHECK_FINITE(V,...) #define CHECK_FINITE(V,...)
#endif #endif
///////////////////////////////////////////////////////////////////////////////
#define CHECK_THROWS(E,C) do { \
DEBUG_ONLY ( \
bool caught = false; \
\
try \
{ C; } \
catch (E const&) \
{ caught = true; } \
\
if (!caught) { \
std::cerr << "expected exception: " << #E << '\n' \
breakpoint (); \
} \
); \
} while (0)
///////////////////////////////////////////////////////////////////////////////
#define CHECK_NOTHROW(C) do { \
DEBUG_ONLY ( \
try { \
C; \
} catch (const std::exception &e) { \
std::cerr << "unexpected exception: " << e.what () << '\n'; \
breakpoint (); \
} catch (...) { \
std::cerr << "unexpected exception: unknown\n"; \
breakpoint (); \
} \
); \
} while (0)

View File

@ -11,6 +11,8 @@
#include "common.hpp" #include "common.hpp"
#include "../../debug/panic.hpp"
using cruft::hash::murmur2; using cruft::hash::murmur2;

View File

@ -15,8 +15,8 @@
#include "../thread/monitor.hpp" #include "../thread/monitor.hpp"
#include "../thread/semaphore.hpp" #include "../thread/semaphore.hpp"
#include "../thread/ticketlock.hpp" #include "../thread/ticketlock.hpp"
#include "../parallel/queue.hpp" #include "../parallel/queue.hpp"
#include "../debug/panic.hpp"
#include <atomic> #include <atomic>
#include <cstddef> #include <cstddef>

View File

@ -10,6 +10,8 @@
#include "base.hpp" #include "base.hpp"
#include "../../debug/panic.hpp"
namespace cruft::log::sink { namespace cruft::log::sink {
class console : public crtp<console> { class console : public crtp<console> {
public: public:

View File

@ -10,10 +10,11 @@
#include "fwd.hpp" #include "fwd.hpp"
#include "../view.hpp" #include "../debug/panic.hpp"
#include "../introspection/name.hpp" #include "../introspection/name.hpp"
#include "../log.hpp" #include "../log.hpp"
#include "../typeidx.hpp" #include "../typeidx.hpp"
#include "../view.hpp"
#include <map> #include <map>

View File

@ -10,6 +10,7 @@
#include "cast.hpp" #include "cast.hpp"
#include "debug/assert.hpp" #include "debug/assert.hpp"
#include "debug/panic.hpp"
#include "parallel/stack.hpp" #include "parallel/stack.hpp"
#include "view.hpp" #include "view.hpp"

View File

@ -11,6 +11,8 @@
#include "maths.hpp" #include "maths.hpp"
#include "debug/assert.hpp" #include "debug/assert.hpp"
#include <ostream>
#include <cstdint> #include <cstdint>
using cruft::rational; using cruft::rational;

View File

@ -8,6 +8,8 @@
#include "uri.hpp" #include "uri.hpp"
#include "debug/panic.hpp"
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>

View File

@ -8,6 +8,7 @@
#include "version.hpp" #include "version.hpp"
#include "maths.hpp" #include "maths.hpp"
#include "debug/panic.hpp"
#include <cstring> #include <cstring>
#include <stdexcept> #include <stdexcept>