2011-05-23 17:18:52 +10:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2015-10-29 10:43:41 +11:00
|
|
|
* Copyright 2010-2015 Danny Robson <danny@nerdcruft.net>
|
2011-05-23 17:18:52 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __DEBUG_HPP
|
|
|
|
#define __DEBUG_HPP
|
|
|
|
|
2015-02-05 20:35:25 +11:00
|
|
|
//#include "maths.hpp" // XXX: See notes at the end of file for maths.hpp inclusion
|
2011-05-23 17:18:52 +10:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2015-08-10 15:41:34 +10:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-03 18:11:17 +10:00
|
|
|
#ifdef ENABLE_DEBUGGING
|
2015-01-28 15:01:17 +11:00
|
|
|
#define DEBUG_ONLY(X) do { \
|
|
|
|
X \
|
|
|
|
} while (0)
|
2012-05-03 18:11:17 +10:00
|
|
|
#else
|
|
|
|
#define DEBUG_ONLY(X)
|
|
|
|
#endif
|
2012-04-12 14:06:59 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define EXIT_XSUCCESS 0
|
|
|
|
#define EXIT_XSKIP 77
|
|
|
|
#define EXIT_XHARD_ERROR 99
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define TRACE { \
|
|
|
|
DEBUG_ONLY( \
|
|
|
|
std::cerr << __FILE__ << ":" << __func__ << ":" << __LINE__ << std::endl; \
|
|
|
|
); \
|
2011-07-16 14:47:10 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-28 15:01:17 +11:00
|
|
|
#define WARN(C) do { \
|
|
|
|
DEBUG_ONLY( \
|
|
|
|
if (C) { \
|
|
|
|
std::cerr << __FILE__ << ":" << __func__ << ":" << __LINE__ << ", " << #C << std::endl; \
|
|
|
|
} \
|
|
|
|
); \
|
2011-11-04 16:50:41 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-11 12:34:21 +10:00
|
|
|
#define _CHECK_META(C, SUCCESS, FAILURE) do { \
|
2011-08-29 14:32:30 +10:00
|
|
|
const auto __DEBUG_value = (C); \
|
2014-10-17 19:24:53 +11:00
|
|
|
if (!__DEBUG_value) { \
|
2011-05-23 17:18:52 +10:00
|
|
|
std::cerr << PACKAGE << ": " \
|
|
|
|
<< __FILE__ << ":" \
|
|
|
|
<< __LINE__ << ": " \
|
|
|
|
<< __FUNCTION__ \
|
|
|
|
<< ". Assertion '" << #C \
|
2011-08-29 14:32:30 +10:00
|
|
|
<< "' failed: " << __DEBUG_value << std::endl; \
|
2011-05-23 17:18:52 +10:00
|
|
|
\
|
|
|
|
{ FAILURE } \
|
|
|
|
} else { \
|
|
|
|
{ SUCCESS } \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2015-01-28 14:49:34 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define CHECK(C) do { DEBUG_ONLY(_CHECK_META((C), { ; }, { panic (); });); } while (0)
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define CHECK_EQ(A,B) do { \
|
|
|
|
DEBUG_ONLY( \
|
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
|
|
|
_CHECK_META (almost_equal (__a, __b), \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
|
|
|
std::ostringstream __debug_os; \
|
|
|
|
__debug_os.precision (15); \
|
|
|
|
__debug_os \
|
|
|
|
<< "expected equality.\n" \
|
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n != \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
|
|
|
panic (__debug_os.str ()); \
|
|
|
|
}); \
|
|
|
|
); \
|
2011-05-23 17:18:52 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define CHECK_LT(A,B) do { \
|
|
|
|
DEBUG_ONLY( \
|
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
|
|
|
_CHECK_META (__a < __b, \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
|
|
|
std::ostringstream __debug_check_lt_os; \
|
|
|
|
__debug_check_lt_os \
|
|
|
|
<< "expected less than.\n" \
|
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n >= \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
|
|
|
panic (__debug_check_lt_os.str ()); \
|
|
|
|
}); \
|
|
|
|
); \
|
2012-11-09 15:11:18 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define CHECK_LE(A,B) do { \
|
|
|
|
DEBUG_ONLY( \
|
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
|
|
|
_CHECK_META (__a <= __b, \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
|
|
|
std::ostringstream __debug_check_lt_os; \
|
|
|
|
__debug_check_lt_os \
|
|
|
|
<< "expected less or equal to\n" \
|
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n > \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
|
|
|
panic (__debug_check_lt_os.str ()); \
|
|
|
|
}); \
|
|
|
|
); \
|
2014-02-12 17:05:37 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define CHECK_GT(A,B) do { \
|
|
|
|
DEBUG_ONLY( \
|
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
|
|
|
_CHECK_META (__a > __b, \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
|
|
|
std::ostringstream __debug_check_gt_os; \
|
|
|
|
__debug_check_gt_os \
|
|
|
|
<< "expected greater than.\n" \
|
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n <= \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
|
|
|
panic (__debug_check_gt_os.str ()); \
|
|
|
|
}); \
|
|
|
|
); \
|
2012-11-09 15:11:18 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define CHECK_GE(A,B) do { \
|
|
|
|
DEBUG_ONLY( \
|
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
|
|
|
_CHECK_META (__a >= __b, \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
|
|
|
std::ostringstream __debug_check_gt_os; \
|
|
|
|
__debug_check_gt_os \
|
|
|
|
<< "expected greater or equal to.\n" \
|
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n < \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
|
|
|
panic (__debug_check_gt_os.str ()); \
|
|
|
|
}); \
|
|
|
|
); \
|
2014-02-12 17:05:37 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2015-05-29 15:51:25 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define CHECK_LIMIT(VAL,LO,HI) do { \
|
|
|
|
DEBUG_ONLY( \
|
|
|
|
const auto __val = (VAL); \
|
|
|
|
const auto __hi = (HI); \
|
|
|
|
const auto __lo = (LO); \
|
|
|
|
\
|
|
|
|
_CHECK_META (__val >= __lo && __val <= __hi, \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
|
|
|
std::ostringstream __os; \
|
|
|
|
__os << "expected satisifies limit\n" \
|
|
|
|
<< "__val: " << #VAL << " = " << __val << '\n' \
|
|
|
|
<< "__lo: " << #LO << " = " << __lo << '\n' \
|
|
|
|
<< "__hi: " << #HI << " = " << __hi << '\n'; \
|
|
|
|
panic (__os.str ()); \
|
|
|
|
}); \
|
|
|
|
); \
|
|
|
|
} while (0)
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define CHECK_NEQ(A,B) do { \
|
|
|
|
DEBUG_ONLY( \
|
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
|
|
|
_CHECK_META (!almost_equal (__a, __b), \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
|
|
|
std::ostringstream __debug_neq_os; \
|
|
|
|
__debug_neq_os << "unexpected equality.\n" \
|
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n == \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
|
|
|
panic (__debug_neq_os.str ()); \
|
|
|
|
}); \
|
|
|
|
); \
|
2011-05-23 17:18:52 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2015-02-13 18:02:09 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define CHECK_ZERO(A) do { \
|
|
|
|
DEBUG_ONLY( \
|
|
|
|
const auto __a = (A); \
|
|
|
|
_CHECK_META (almost_zero (__a), \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
|
|
|
std::ostringstream __debug_nez_os; \
|
|
|
|
__debug_nez_os << "expected zero.\n" \
|
|
|
|
<< "__a: " << #A << " is " << __a << ")"; \
|
|
|
|
panic (__debug_nez_os.str ()); \
|
|
|
|
}); \
|
|
|
|
); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2015-02-05 20:35:11 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define CHECK_NEZ(A) do { \
|
|
|
|
DEBUG_ONLY( \
|
|
|
|
const auto __a = (A); \
|
|
|
|
_CHECK_META (!almost_zero (__a), \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
|
|
|
std::ostringstream __debug_nez_os; \
|
|
|
|
__debug_nez_os << "unexpected zero.\n" \
|
|
|
|
<< "__a: " << #A << " is " << __a << ")"; \
|
|
|
|
panic (__debug_nez_os.str ()); \
|
|
|
|
}); \
|
|
|
|
); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define CHECK_THROWS(E,C) do { \
|
|
|
|
DEBUG_ONLY( \
|
|
|
|
bool caught = false; \
|
|
|
|
\
|
|
|
|
try \
|
|
|
|
{ C; } \
|
|
|
|
catch (E) \
|
|
|
|
{ caught = true; } \
|
|
|
|
\
|
|
|
|
if (!caught) \
|
|
|
|
panic ("expected exception: " #E); \
|
|
|
|
); \
|
2011-05-23 17:18:52 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-06-20 16:47:48 +10:00
|
|
|
#define CHECK_NOTHROW(C) do { \
|
2015-01-28 15:01:17 +11:00
|
|
|
DEBUG_ONLY( \
|
|
|
|
try { \
|
|
|
|
C; \
|
|
|
|
} catch (...) { \
|
|
|
|
panic ("unexpected exception"); \
|
|
|
|
} \
|
|
|
|
); \
|
2012-06-20 16:47:48 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-05-23 17:18:52 +10:00
|
|
|
class panic_error {
|
|
|
|
protected:
|
|
|
|
std::string m_what;
|
|
|
|
|
|
|
|
public:
|
2015-06-04 22:18:43 +10:00
|
|
|
explicit panic_error (const std::string &_what):
|
2011-05-23 17:18:52 +10:00
|
|
|
m_what (_what)
|
|
|
|
{ ; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-10-29 10:43:41 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-10-17 19:24:53 +11:00
|
|
|
void panic [[noreturn]] (const std::string&);
|
2015-10-29 10:43:41 +11:00
|
|
|
constexpr void panic [[noreturn]] (const char*);
|
|
|
|
constexpr void panic [[noreturn]] (void);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2012-08-10 17:40:19 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-10-29 10:43:41 +11:00
|
|
|
constexpr void not_implemented [[noreturn]] (void);
|
|
|
|
constexpr void not_implemented [[noreturn]] (const char *msg);
|
2012-06-04 14:50:58 +10:00
|
|
|
|
2012-08-10 17:40:19 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-10-29 10:43:41 +11:00
|
|
|
constexpr void unreachable [[noreturn]] (void);
|
|
|
|
constexpr void unreachable [[noreturn]] (const char*);
|
|
|
|
|
2014-10-17 19:24:53 +11:00
|
|
|
void unusual (void);
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-05-23 17:18:52 +10:00
|
|
|
void breakpoint (void);
|
|
|
|
|
2012-05-01 12:14:25 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-01 12:14:25 +10:00
|
|
|
void enable_fpe (void);
|
|
|
|
void disable_fpe (void);
|
|
|
|
|
2012-08-10 17:40:19 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-10-29 10:43:41 +11:00
|
|
|
namespace util { namespace debug {
|
2012-08-10 17:40:19 +10:00
|
|
|
void init (void);
|
2015-02-13 17:30:19 +11:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool valid (const T&);
|
|
|
|
|
|
|
|
|
2015-03-03 04:13:29 +11:00
|
|
|
template <
|
|
|
|
template<size_t, typename...> class T,
|
|
|
|
size_t S,
|
|
|
|
typename ...Args
|
|
|
|
>
|
2015-02-13 17:30:19 +11:00
|
|
|
struct validator {
|
2015-03-03 04:13:29 +11:00
|
|
|
static bool is_valid (const T<S,Args...>&);
|
2015-02-13 17:30:19 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-03 04:13:29 +11:00
|
|
|
template <
|
|
|
|
template<size_t,typename...> class T,
|
|
|
|
size_t S,
|
|
|
|
typename ...Args
|
|
|
|
>
|
|
|
|
bool valid (const T<S,Args...> &v)
|
|
|
|
{ return validator<T,S,Args...>::is_valid (v); }
|
2015-02-13 17:30:19 +11:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void sanity (const T &t)
|
2015-04-30 17:31:19 +10:00
|
|
|
{
|
|
|
|
(void)t;
|
|
|
|
CHECK (valid (t));
|
|
|
|
}
|
2015-02-13 17:30:19 +11:00
|
|
|
|
|
|
|
|
2015-03-03 04:13:29 +11:00
|
|
|
template <
|
|
|
|
template<typename...> class T,
|
|
|
|
typename ...Args
|
|
|
|
>
|
2015-02-13 17:30:19 +11:00
|
|
|
void sanity (const T<Args...> &t)
|
2015-04-30 17:31:19 +10:00
|
|
|
{
|
|
|
|
(void)t;
|
|
|
|
CHECK (valid (t));
|
|
|
|
}
|
2015-10-29 10:43:41 +11:00
|
|
|
} }
|
2012-08-10 17:40:19 +10:00
|
|
|
|
2015-02-05 20:35:25 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// XXX: maths needs to be included so that CHECK_EQ/NEQ can call almost_equal,
|
|
|
|
// but maths.hpp might be using CHECK_ macros so we must include maths.hpp
|
|
|
|
// after we define the CHECK_ macros so the preprocessor can resolve them.
|
2015-10-29 10:43:41 +11:00
|
|
|
#include "./maths.hpp"
|
|
|
|
|
|
|
|
#include "./debug.ipp"
|
2015-02-05 20:35:25 +11:00
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#endif // __DEBUG_HPP
|