test: add errno check macros

This commit is contained in:
Peter Hutterer 2021-06-04 15:23:51 +10:00
parent 2073269f47
commit 50180532a4
3 changed files with 57 additions and 0 deletions

View file

@ -54,6 +54,36 @@ _pwtest_fail_comparison_bool(const char *file, int line, const char *func,
const char *operator, bool a, bool b,
const char *astr, const char *bstr);
void
_pwtest_fail_errno(const char *file, int line, const char *func,
int expected, int err_no);
#define pwtest_errno_check(r_, errno_) \
do { \
int _r = r_; \
int _e = errno_; \
if (_e == 0) { \
if (_r == -1) \
_pwtest_fail_errno(__FILE__, __LINE__, __func__, _e, errno); \
} else { \
if (_r != -1 || errno != _e) \
_pwtest_fail_errno(__FILE__, __LINE__, __func__, _e, errno); \
} \
} while(0)
#define pwtest_neg_errno_check(r_, errno_) \
do { \
int _r = r_; \
int _e = errno_; \
if (_e == 0) { \
if (_r < 0) \
_pwtest_fail_errno(__FILE__, __LINE__, __func__, _e, -_r); \
} else { \
if (_r >= 0 || _r != _e) \
_pwtest_fail_errno(__FILE__, __LINE__, __func__, -_e, _r >= 0 ? 0 : -_r); \
} \
} while(0)
#define pwtest_comparison_bool_(a_, op_, b_) \
do { \
bool _a = !!(a_); \

View file

@ -226,6 +226,17 @@ void _pwtest_fail_comparison_bool(const char *file, int line, const char *func,
exit(PWTEST_FAIL);
}
SPA_NORETURN
void _pwtest_fail_errno(const char *file, int line, const char *func,
int expected, int err_no)
{
pwtest_log("FAILED ERRNO CHECK: expected %d (%s), got %d (%s)\n",
expected, strerror(expected), err_no, strerror(err_no));
pwtest_log("in %s() (%s:%d)\n", func, file, line);
pwtest_backtrace(0);
exit(PWTEST_FAIL);
}
SPA_NORETURN
void _pwtest_fail_comparison_int(const char *file, int line, const char *func,

View file

@ -202,6 +202,22 @@ struct pwtest_context *pwtest_get_context(struct pwtest_test *t);
_pwtest_fail_condition(PWTEST_SYSTEM_ERROR, __FILE__, __LINE__, __func__, \
"error", __VA_ARGS__)
/** Assert r is not -1 and if it is, print the errno */
#define pwtest_errno_ok(r_) \
pwtest_errno_check(r_, 0);
/** Assert r is -1 and the errno is the given one */
#define pwtest_errno(r_, errno_) \
pwtest_errno_check(r_, errno_);
/** Assert r is not < 0 and if it is assume it's a negative errno */
#define pwtest_neg_errno_ok(r_) \
pwtest_neg_errno_check(r_, 0);
/** Assert r is < 0 and the given negative errno */
#define pwtest_neg_errno(r_, errno_) \
pwtest_neg_errno_check(r_, errno_);
/** Assert boolean (a == b) */
#define pwtest_bool_eq(a_, b_) \
pwtest_comparison_bool_(a_, ==, b_)