debug: make assertions debug-only

This commit is contained in:
Danny Robson 2015-01-28 15:01:17 +11:00
parent cee9951f82
commit d7db3d8d29

View File

@ -28,8 +28,9 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#ifdef ENABLE_DEBUGGING #ifdef ENABLE_DEBUGGING
#define DEBUG_ONLY(X) \ #define DEBUG_ONLY(X) do { \
X; X \
} while (0)
#else #else
#define DEBUG_ONLY(X) #define DEBUG_ONLY(X)
#endif #endif
@ -43,14 +44,18 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define TRACE { \ #define TRACE { \
DEBUG_ONLY( \
std::cerr << __FILE__ << ":" << __func__ << ":" << __LINE__ << std::endl; \ std::cerr << __FILE__ << ":" << __func__ << ":" << __LINE__ << std::endl; \
); \
} }
#define WARN(C) do { \ #define WARN(C) do { \
DEBUG_ONLY( \
if (C) { \ if (C) { \
std::cerr << __FILE__ << ":" << __func__ << ":" << __LINE__ << ", " << #C << std::endl; \ std::cerr << __FILE__ << ":" << __func__ << ":" << __LINE__ << ", " << #C << std::endl; \
} \ } \
); \
} while (0) } while (0)
@ -77,6 +82,7 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define CHECK_EQ(A,B) do { \ #define CHECK_EQ(A,B) do { \
DEBUG_ONLY( \
const auto __a = (A); \ const auto __a = (A); \
const auto __b = (B); \ const auto __b = (B); \
_CHECK_META (almost_equal (__a, __b), \ _CHECK_META (almost_equal (__a, __b), \
@ -91,11 +97,13 @@
<< "__b: " << #B << " is " << __b << ")"; \ << "__b: " << #B << " is " << __b << ")"; \
panic (__debug_os.str ()); \ panic (__debug_os.str ()); \
}); \ }); \
); \
} while (0) } while (0)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define CHECK_LT(A,B) do { \ #define CHECK_LT(A,B) do { \
DEBUG_ONLY( \
const auto __a = (A); \ const auto __a = (A); \
const auto __b = (B); \ const auto __b = (B); \
_CHECK_META (__a < __b, \ _CHECK_META (__a < __b, \
@ -109,11 +117,13 @@
<< "__b: " << #B << " is " << __b << ")"; \ << "__b: " << #B << " is " << __b << ")"; \
panic (__debug_check_lt_os.str ()); \ panic (__debug_check_lt_os.str ()); \
}); \ }); \
); \
} while (0) } while (0)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define CHECK_LE(A,B) do { \ #define CHECK_LE(A,B) do { \
DEBUG_ONLY( \
const auto __a = (A); \ const auto __a = (A); \
const auto __b = (B); \ const auto __b = (B); \
_CHECK_META (__a <= __b, \ _CHECK_META (__a <= __b, \
@ -127,11 +137,13 @@
<< "__b: " << #B << " is " << __b << ")"; \ << "__b: " << #B << " is " << __b << ")"; \
panic (__debug_check_lt_os.str ()); \ panic (__debug_check_lt_os.str ()); \
}); \ }); \
); \
} while (0) } while (0)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define CHECK_GT(A,B) do { \ #define CHECK_GT(A,B) do { \
DEBUG_ONLY( \
const auto __a = (A); \ const auto __a = (A); \
const auto __b = (B); \ const auto __b = (B); \
_CHECK_META (__a > __b, \ _CHECK_META (__a > __b, \
@ -145,11 +157,13 @@
<< "__b: " << #B << " is " << __b << ")"; \ << "__b: " << #B << " is " << __b << ")"; \
panic (__debug_check_gt_os.str ()); \ panic (__debug_check_gt_os.str ()); \
}); \ }); \
); \
} while (0) } while (0)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define CHECK_GE(A,B) do { \ #define CHECK_GE(A,B) do { \
DEBUG_ONLY( \
const auto __a = (A); \ const auto __a = (A); \
const auto __b = (B); \ const auto __b = (B); \
_CHECK_META (__a >= __b, \ _CHECK_META (__a >= __b, \
@ -163,11 +177,13 @@
<< "__b: " << #B << " is " << __b << ")"; \ << "__b: " << #B << " is " << __b << ")"; \
panic (__debug_check_gt_os.str ()); \ panic (__debug_check_gt_os.str ()); \
}); \ }); \
); \
} while (0) } while (0)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define CHECK_NEQ(A,B) do { \ #define CHECK_NEQ(A,B) do { \
DEBUG_ONLY( \
const auto __a = (A); \ const auto __a = (A); \
const auto __b = (B); \ const auto __b = (B); \
_CHECK_META (!almost_equal (__a, __b), \ _CHECK_META (!almost_equal (__a, __b), \
@ -180,11 +196,13 @@
<< "__b: " << #B << " is " << __b << ")"; \ << "__b: " << #B << " is " << __b << ")"; \
panic (__debug_neq_os.str ()); \ panic (__debug_neq_os.str ()); \
}); \ }); \
); \
} while (0) } while (0)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define CHECK_THROWS(E,C) do { \ #define CHECK_THROWS(E,C) do { \
DEBUG_ONLY( \
bool caught = false; \ bool caught = false; \
\ \
try \ try \
@ -194,16 +212,19 @@
\ \
if (!caught) \ if (!caught) \
panic ("expected exception: " #E); \ panic ("expected exception: " #E); \
); \
} while (0) } while (0)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define CHECK_NOTHROW(C) do { \ #define CHECK_NOTHROW(C) do { \
DEBUG_ONLY( \
try { \ try { \
C; \ C; \
} catch (...) { \ } catch (...) { \
panic ("unexpected exception"); \ panic ("unexpected exception"); \
} \ } \
); \
} while (0) } while (0)