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 <stdexcept>
#include <iomanip>
using cruft::parray;

View File

@ -6,4 +6,8 @@
* Copyright 2010-2019 Danny Robson <danny@nerdcruft.net>
*/
#include "./assert.hpp"
#include "./assert.hpp"
#include <fmt/ostream.h>
#include <iostream>

View File

@ -9,18 +9,19 @@
#pragma once
#include "./common.hpp"
#include "./panic.hpp"
#include "./debugger.hpp"
#include "../platform.hpp"
#include "../maths.hpp"
#include <utility>
#ifndef NDEBUG
#include <iostream>
#endif
#include <cassert>
///////////////////////////////////////////////////////////////////////////////
#define assert_message(MSG) __assert_fail(MSG, __FILE__, __LINE__, __PRETTY_FUNCTION__);
///////////////////////////////////////////////////////////////////////////////
#ifdef COMPILER_GCC
@ -29,7 +30,7 @@
_Pragma("GCC diagnostic ignored \"-Wnonnull-compare\"") \
DEBUG_ONLY ( \
if (!(C)) \
panic (#C); \
assert_message (#C); \
); \
_Pragma("GCC diagnostic pop") \
} while (0)
@ -37,7 +38,7 @@
#define CHECK(C) do { \
DEBUG_ONLY ( \
if (!(C)) \
panic (#C); \
assert_message (#C); \
); \
} while (0)
#else
@ -61,10 +62,8 @@
const auto &__a = (A); \
const auto &__b = (B); \
\
if (!::cruft::almost_equal (__a, __b)) { \
std::cerr << "expected equality\n" \
"__a: " #A " is " << __a << "\n" \
"__b: " #B " is " << __b << "\n"; \
if (!::cruft::almost_equal (__a, __b)) { \
assert_message ("expected: " #A " == " #B); \
breakpoint (); \
} \
); \
@ -78,9 +77,7 @@
const auto &__b = (B); \
\
if (__a >= __b) { \
std::cerr << "expected less than\n" \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
assert_message ("expected: " #A " < " #B); \
breakpoint (); \
}; \
); \
@ -94,9 +91,7 @@
const auto &__b = (B); \
\
if (!(__a <= __b)) { \
std::cerr << "expected less than or equal\n" \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
assert_message ("expected: " #A " <= " #B); \
breakpoint (); \
} \
); \
@ -110,9 +105,7 @@
const auto &__b = (B); \
\
if (__a <= __b) { \
std::cerr << "expected greater than\n" \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
assert_message ("expected: " #A " > " #B); \
breakpoint (); \
} \
); \
@ -126,9 +119,7 @@
const auto &__b = (B); \
\
if (__a < __b) { \
std::cerr << "expected greater or equal\n" \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
assert_message ("expected: " #A " >= " #B); \
breakpoint (); \
}; \
); \
@ -143,10 +134,7 @@
const auto &__h = (H); \
\
if (__v < __l || __v > __h) { \
std::cerr << "expected inclusive\n" \
"__l: " << #L << " is " << +__l << "\n" \
"__h: " << #H << " is " << +__h << "\n" \
"__v: " << #V << " is " << +__v << "\n"; \
assert_message ("expected: " #L " <= " #V " <= " #H); \
breakpoint (); \
}; \
); \
@ -163,9 +151,7 @@
_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"; \
assert_message ("expected: 0 <= " #V " <= " #H); \
breakpoint (); \
}; \
\
@ -180,10 +166,8 @@
const auto &__a = (A); \
const auto &__b = (B); \
\
if (::cruft::almost_equal (__a, __b)) { \
std::cerr << "expected inequality\n" \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
if (::cruft::almost_equal (__a, __b)) { \
assert_message ("expected: " #A " != " #B); \
breakpoint (); \
}; \
); \
@ -195,9 +179,8 @@
DEBUG_ONLY ( \
const auto &__a = (A); \
\
if (!::cruft::almost_zero (__a)) { \
std::cerr << "expected zero\n" \
"__a: " << #A << " is " << __a << "\n"; \
if (!::cruft::almost_zero (__a)) { \
assert_message ("expected: " #A " == 0"); \
breakpoint (); \
}; \
); \
@ -209,9 +192,8 @@
DEBUG_ONLY ( \
const auto &__a = (A); \
\
if (::cruft::exactly_zero (__a)) { \
std::cerr << "expected non-zero\n" \
"__a: " << #A << " is " << __a << '\n'; \
if (::cruft::exactly_zero (__a)) { \
assert_message ("expected: " #A " != 0"); \
breakpoint (); \
} \
); \
@ -224,10 +206,8 @@
const auto &__check_mod_v = (V); \
const auto &__check_mod_m = (M); \
\
if (!::cruft::exactly_zero (__check_mod_v % __check_mod_m)) { \
std::cerr << "expected zero modulus\n" \
"__v: " << #V << " is " << __check_mod_v << "\n" \
"__m: " << #M << " is " << __check_mod_m << "\n"; \
if (!::cruft::exactly_zero (__check_mod_v % __check_mod_m)) { \
assert_message ("expected: " #V " % " #M " == 0"); \
breakpoint (); \
} \
); \
@ -243,8 +223,7 @@
if (std::find (std::cbegin (__e), \
std::cend (__e), \
__c) == std::end (__e)) { \
std::cerr << "expect enum\n" \
"__c: " << #C << " is " << __c << '\n'; \
assert_message ("expected: " #C " in " #__VA_ARGS__); \
breakpoint (); \
} \
} while (0)
@ -253,51 +232,15 @@
#endif
//-----------------------------------------------------------------------------
#if !defined(NDEBUG)
#define CHECK_FINITE(V) do { \
const auto &__v = (V); \
if (!std::isfinite (__v)) { \
std::cerr << "expected finite value\n" \
"__v: " << #V << " is " << __v << '\n'; \
breakpoint (); \
} \
const auto &__v = (V); \
if (!std::isfinite (__v)) { \
assert_message ("expected: " #V " is finite"); \
breakpoint (); \
} \
} while (0)
#else
#define CHECK_FINITE(V,...)
#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 "../../debug/panic.hpp"
using cruft::hash::murmur2;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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