更好的可用性是通过
/*!
* \file: assert_x.h
* \brief: Usability Improving Extensions to assert.h.
* \author: Per Nordlöw
*/
#pragma once
#include <errno.h>
#include <signal.h>
#include <assert.h>
#ifdef __cplusplus
extern "C" {
#endif
#if !defined(NDEBUG)
# define passert(expr) \
if (!(expr)) { \
fprintf(stderr, "%s:%d: %s: Assertion `%s' failed.", \
__FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(expr)); raise(SIGTRAP); \
}
# define passert_with(expr, sig) \
if (!(expr)) { \
fprintf(stderr, "%s:%d: %s: Assertion `%s' failed.", \
__FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(expr)); raise(sig); \
}
# define passert_eq(expected, actual) \
if (!(expected == actual)) { \
fprintf(stderr, "%s:%d: %s: Assertion `%s' == `%s' failed.", \
__FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(expected), __STRING(actual)); raise(SIGTRAP); \
}
# define passert_neq(expected, actual) \
if (!(expected != actual)) { \
fprintf(stderr, "%s:%d: %s: Assertion `%s' != `%s' failed.", \
__FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(expected), __STRING(actual)); raise(SIGTRAP); \
}
# define passert_lt(lhs, rhs) \
if (!(lhs < rhs)) { \
fprintf(stderr, "%s:%d: %s: Assertion `%s' < `%s' failed.", \
__FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(lhs), __STRING(rhs)); raise(SIGTRAP); \
}
# define passert_gt(lhs, rhs) \
if (!(lhs > rhs)) { \
fprintf(stderr, "%s:%d: %s: Assertion `%s' < `%s' failed.", \
__FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(lhs), __STRING(rhs)); raise(SIGTRAP); \
}
# define passert_lte(lhs, rhs) \
if (!(lhs <= rhs)) { \
fprintf(stderr, "%s:%d: %s: Assertion `%s' <= `%s' failed.", \
__FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(lhs), __STRING(rhs)); raise(SIGTRAP); \
}
# define passert_gte(lhs, rhs) \
if (!(lhs >= rhs)) { \
fprintf(stderr, "%s:%d: %s: Assertion `%s' >= `%s' failed.", \
__FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(lhs), __STRING(rhs)); raise(SIGTRAP); \
}
# define passert_zero(expr) \
if (!(expr == 0)) { \
fprintf(stderr, "%s:%d: %s: Assertion `%s' is zero failed.", \
__FILE__, __LINE__, __ASSERT_FUNCTION, __STRING(expr)); raise(SIGTRAP); \
}
#else
# define passert(expr)
# define passert_with(expr, sig)
# define passert_eq(expected, actual)
# define passert_lt(lhs, rhs)
# define passert_gt(lhs, rhs)
# define passert_lte(lhs, rhs)
# define passert_gte(lhs, rhs)
# define passert_zero(expr)
#endif
#ifdef __cplusplus
}
#endif