]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Improve CHECK_{STR,UNS,INT}_EQ macros
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 16 Jul 2010 12:37:48 +0000 (14:37 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 16 Jul 2010 12:37:48 +0000 (14:37 +0200)
test/framework.c
test/framework.h

index fa7cbd2a8abb5741fc40494b75e7e8dc2b6b9030..709a8413f50c91f3f71c2f88bfb9c20c7d25cd1c 100644 (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;
+}
index 5334d9c8be4735b9792de635df0dba78317c14bb..2bf0de4c28fa82f456580d8d2fd3284dcff9b760 100644 (file)
                } \
        } 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