From: Joel Rosdahl Date: Fri, 16 Jul 2010 12:37:48 +0000 (+0200) Subject: Improve CHECK_{STR,UNS,INT}_EQ macros X-Git-Tag: v3.1~180^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0fa05736bbb6d3523964a0649c67297f2a3e1e15;p=thirdparty%2Fccache.git Improve CHECK_{STR,UNS,INT}_EQ macros --- diff --git a/test/framework.c b/test/framework.c index fa7cbd2a8..709a8413f 100644 --- a/test/framework.c +++ b/test/framework.c @@ -16,7 +16,7 @@ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "config.h" +#include "ccache.h" #include "framework.h" #include #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; +} diff --git a/test/framework.h b/test/framework.h index 5334d9c8b..2bf0de4c2 100644 --- a/test/framework.h +++ b/test/framework.h @@ -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