2011-05-23 17:18:52 +10:00
|
|
|
/*
|
2011-06-21 20:26:32 +10:00
|
|
|
* This file is part of libgim.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
|
|
|
* Waif is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* Waif is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2011-06-21 20:26:32 +10:00
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
|
|
|
* Copyright 2010 Danny Robson <danny@blubinc.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __DEBUG_HPP
|
|
|
|
#define __DEBUG_HPP
|
|
|
|
|
|
|
|
|
|
|
|
#include "annotations.hpp"
|
2011-06-16 20:54:34 +10:00
|
|
|
#include "maths.hpp"
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
2011-07-16 14:47:10 +10:00
|
|
|
#define trace { \
|
|
|
|
std::cerr << __FILE__ << ":" << __func__ << ":" << __LINE__ << std::endl; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#define verify_soft(C, COND) ({ \
|
2011-08-29 14:32:30 +10:00
|
|
|
const auto __DEBUG_value = (C); \
|
|
|
|
check_soft(__DEBUG_value COND); \
|
|
|
|
__DEBUG_value; \
|
2011-05-23 17:18:52 +10:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2011-08-29 14:32:30 +10:00
|
|
|
#define verify_hard(C, COND) ({ \
|
|
|
|
const auto __DEBUG_value = (C); \
|
|
|
|
check_hard(__DEBUG_value COND); \
|
|
|
|
__DEBUG_value; \
|
2011-05-23 17:18:52 +10:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
#define _check_meta(C, SUCCESS, FAILURE) do { \
|
2011-08-29 14:32:30 +10:00
|
|
|
const auto __DEBUG_value = (C); \
|
|
|
|
if (unlikely (!__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)
|
|
|
|
|
|
|
|
|
|
|
|
#define check_hard(C) _check_meta((C), { ; }, { panic (); })
|
2011-09-24 13:18:57 +10:00
|
|
|
#define check_soft(C) _check_meta((C), { ; }, { breakpoint (); })
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2011-06-21 23:39:24 +10:00
|
|
|
#define check_eq(A,B) do { \
|
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
|
|
|
_check_meta (almost_equal (__a, __b), \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
|
|
|
std::ostringstream os; \
|
|
|
|
os << "expected equality.\n" \
|
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n != \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
|
|
|
panic (os.str ()); \
|
|
|
|
}); \
|
2011-05-23 17:18:52 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2011-06-21 23:39:24 +10:00
|
|
|
#define check_neq(A,B) do { \
|
|
|
|
const auto __a = (A); \
|
|
|
|
const auto __b = (B); \
|
|
|
|
_check_meta (!almost_equal (__a, __b), \
|
|
|
|
{ ; }, \
|
|
|
|
{ \
|
|
|
|
std::ostringstream os; \
|
|
|
|
os << "unexpected equality.\n" \
|
|
|
|
<< "__a: " << #A << " is " << __a << ")" \
|
|
|
|
<< "\n == \n" \
|
|
|
|
<< "__b: " << #B << " is " << __b << ")"; \
|
|
|
|
panic (os.str ()); \
|
|
|
|
}); \
|
2011-05-23 17:18:52 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
#define check_throws(E,C) do { \
|
|
|
|
bool caught = false; \
|
|
|
|
\
|
|
|
|
try \
|
|
|
|
{ C; } \
|
|
|
|
catch (E) \
|
|
|
|
{ caught = true; } \
|
|
|
|
\
|
|
|
|
if (unlikely (!caught)) \
|
|
|
|
panic ("expected exception: " #E); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define check(C) check_hard(C)
|
|
|
|
|
|
|
|
|
|
|
|
class panic_error {
|
|
|
|
protected:
|
|
|
|
std::string m_what;
|
|
|
|
|
|
|
|
public:
|
|
|
|
panic_error (const std::string &_what):
|
|
|
|
m_what (_what)
|
|
|
|
{ ; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void panic (const std::string&) terminal;
|
|
|
|
void panic (void) terminal;
|
|
|
|
|
|
|
|
|
|
|
|
void not_implemented (void) terminal;
|
|
|
|
void unreachable (void) terminal;
|
|
|
|
void unreachable (const std::string&) terminal;
|
|
|
|
|
|
|
|
|
|
|
|
void breakpoint (void);
|
|
|
|
|
|
|
|
#endif // __DEBUG_HPP
|