Improve CHECK_{STR,UNS,INT}_EQ macros

This commit is contained in:
Joel Rosdahl 2010-07-16 14:37:48 +02:00
parent 032636e5a3
commit 0fa05736bb
2 changed files with 93 additions and 54 deletions

View File

@ -16,7 +16,7 @@
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "ccache.h"
#include "framework.h"
#include <stdio.h>
#if defined(HAVE_TERMIOS_H) && defined(HAVE_SYS_IOCTL_H)
@ -142,3 +142,75 @@ cct_check_failed(const char *file, int line, const char *what,
}
fprintf(stderr, "\n");
}
int
cct_check_int_eq(const char *file, int line, const char *expression,
int expected, int actual)
{
if (expected == actual) {
cct_check_passed();
return 1;
} else {
char *exp_str, *act_str;
x_asprintf(&exp_str, "%i", expected);
x_asprintf(&act_str, "%i", actual);
cct_check_failed(file, line, expression, exp_str, act_str);
free(exp_str);
free(act_str);
return 0;
}
}
int
cct_check_uns_eq(const char *file, int line, const char *expression,
unsigned expected, unsigned actual)
{
if (expected == actual) {
cct_check_passed();
return 1;
} else {
char *exp_str, *act_str;
x_asprintf(&exp_str, "%i", expected);
x_asprintf(&act_str, "%i", actual);
cct_check_failed(file, line, expression, exp_str, act_str);
free(exp_str);
free(act_str);
return 0;
}
}
int
cct_check_str_eq(const char *file, int line, const char *expression,
char *expected, char *actual, int free1, int free2)
{
int result;
if (expected && actual && strcmp(actual, expected) == 0) {
cct_check_passed();
result = 1;
} else {
char *exp_str, *act_str;
if (expected) {
x_asprintf(&exp_str, "\"%s\"", expected);
} else {
exp_str = x_strdup("(null)");
}
if (actual) {
x_asprintf(&act_str, "\"%s\"", actual);
} else {
act_str = x_strdup("(null)");
}
cct_check_failed(file, line, expression, exp_str, act_str);
free(exp_str);
free(act_str);
result = 0;
}
if (free1) {
free(expected);
}
if (free2) {
free(actual);
}
return result;
}

View File

@ -59,82 +59,43 @@
} \
} while (0)
#define CHECK_UNS_EQ(expected, actual) \
#define CHECK_POINTER_EQ_BASE(t, e, a, f1, f2) \
do { \
unsigned exp = (expected); \
unsigned act = (actual); \
if (exp == act) { \
cct_check_passed(); \
} else { \
char *exp_str, *act_str; \
x_asprintf(&exp_str, "%u", exp); \
x_asprintf(&act_str, "%u", act); \
cct_check_failed(__FILE__, __LINE__, #actual, exp_str, act_str); \
free(exp_str); \
free(act_str); \
if (cct_check_##t##_eq(__FILE__, __LINE__, #a, (e), (a), (f1), (f2))) { \
return _test_counter; \
} \
} while (0)
/*****************************************************************************/
#define CHECK_INT_EQ(expected, actual) \
do { \
int exp = (expected); \
int act = (actual); \
if (exp == act) { \
cct_check_passed(); \
} else { \
char *exp_str, *act_str; \
x_asprintf(&exp_str, "%u", exp); \
x_asprintf(&act_str, "%u", act); \
cct_check_failed(__FILE__, __LINE__, #actual, exp_str, act_str); \
free(exp_str); \
free(act_str); \
if (cct_check_int_eq(__FILE__, __LINE__, #actual, (expected), (actual))) { \
return _test_counter; \
} \
} while (0)
#define CHECK_STR_EQ_BASE(expected, actual, free1, free2) \
#define CHECK_UNS_EQ(expected, actual) \
do { \
char *exp = (expected); \
char *act = (actual); \
if (exp && act && strcmp(act, exp) == 0) { \
cct_check_passed(); \
if (free1) { \
free(exp); \
} \
if (free2) { \
free(act); \
} \
} else { \
char *exp_str, *act_str; \
if (exp) { \
x_asprintf(&exp_str, "\"%s\"", exp); \
} else { \
exp_str = x_strdup("(null)"); \
} \
if (act) { \
x_asprintf(&act_str, "\"%s\"", act); \
} else { \
act_str = x_strdup("(null)"); \
} \
cct_check_failed(__FILE__, __LINE__, #actual, exp_str, act_str); \
free(exp_str); \
free(act_str); \
if (cct_check_int_eq(__FILE__, __LINE__, #actual, (expected), (actual))) { \
return _test_counter; \
} \
} while (0)
/*****************************************************************************/
#define CHECK_STR_EQ(expected, actual) \
CHECK_STR_EQ_BASE(expected, actual, 0, 0)
CHECK_POINTER_EQ_BASE(str, expected, actual, 0, 0)
#define CHECK_STR_EQ_FREE1(expected, actual) \
CHECK_STR_EQ_BASE(expected, actual, 1, 0)
CHECK_POINTER_EQ_BASE(str, expected, actual, 1, 0)
#define CHECK_STR_EQ_FREE2(expected, actual) \
CHECK_STR_EQ_BASE(expected, actual, 0, 1)
CHECK_POINTER_EQ_BASE(str, expected, actual, 0, 1)
#define CHECK_STR_EQ_FREE12(expected, actual) \
CHECK_STR_EQ_BASE(expected, actual, 1, 1)
CHECK_POINTER_EQ_BASE(str, expected, actual, 1, 1)
/*****************************************************************************/
@ -148,5 +109,11 @@ void cct_test_end();
void cct_check_passed(void);
void cct_check_failed(const char *file, int line, const char *assertion,
const char *expected, const char *actual);
int cct_check_int_eq(const char *file, int line, const char *expression,
int expected, int actual);
int cct_check_uns_eq(const char *file, int line, const char *expression,
unsigned expected, unsigned actual);
int cct_check_str_eq(const char *file, int line, const char *expression,
char *expected, char *actual, int free1, int free2);
#endif