]> granicus.if.org Git - transmission/commitdiff
steal some ideas about unit test error logging from Google Test
authorJordan Lee <jordan@transmissionbt.com>
Sun, 14 Oct 2012 17:59:24 +0000 (17:59 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Sun, 14 Oct 2012 17:59:24 +0000 (17:59 +0000)
libtransmission/libtransmission-test.h

index 7a4021c7d2565f05d7caf21df3a0b0de193cd271..9661952edfcb7762efde657efdb9d7ebe7aa373c 100644 (file)
@@ -6,33 +6,96 @@
 #include <stdio.h>
 #include <string.h>
 
-static int test = 0;
+#include "transmission.h"
+#include "utils.h" /* tr_strcmp0() */
 
-#define REPORT_TEST(test, res) \
-    fprintf( stderr, "%s test #%d (%s, %d)\n", res, test, __FILE__, __LINE__ )
+static int current_test = 0;
+
+#define REPORT_TEST(test, result) \
+    fprintf( stderr, "%s %s:%d\n", result, __FILE__, __LINE__ )
+
+static inline bool
+should_print (bool pass)
+{
+  if (!pass)
+    return true;
 
 #ifdef VERBOSE
-  #define check( A )                                           \
-    do {                                                       \
-        ++test;                                                        \
-        if( A )                                                        \
-           REPORT_TEST(test, "PASS");                          \
-        else {                                                 \
-           REPORT_TEST(test, "FAIL");                          \
-            return test;                                       \
-        }                                                      \
-    } while(0)
+  return true;
 #else
-  #define check( A )                                           \
-    do {                                                       \
-        ++test;                                                        \
-        if( !( A ) ){                                          \
-           REPORT_TEST(test, "FAIL");                          \
-            return test;                                       \
-        }                                                      \
-    } while(0)
+  return false;
 #endif
+}
+
+static inline bool
+check_condition_impl (const char * file, int line, bool condition)
+{
+  const bool pass = condition;
+
+  if (should_print (pass))
+    fprintf (stderr, "%s %s:%d\n", pass?"PASS":"FAIL", file, line);
+
+  return pass;
+}
+
+static inline bool
+check_streq_impl (const char * file, int line, const char * expected, const char * actual)
+{
+  const bool pass = !tr_strcmp0 (expected, actual);
+
+  if (should_print (pass)) {
+    if (pass)
+      fprintf (stderr, "PASS %s:%d\n", file, line);
+    else
+      fprintf (stderr, "FAIL %s:%d, expected \"%s\", got \"%s\"\n", file, line, expected?expected:"(null)", actual?actual:"(null)");
+  }
+
+  return pass;
+}
+
+static inline bool
+check_eq_impl (const char * file, int line, int64_t expected, int64_t actual)
+{
+  const bool pass = expected == actual;
+
+  if (should_print (pass)) {
+    if (pass)
+      fprintf (stderr, "PASS %s:%d\n", file, line);
+    else
+      fprintf (stderr, "FAIL %s:%d, expected \"%"PRId64"\", got \"%"PRId64"\"\n", file, line, expected, actual);
+  }
+
+  return pass;
+}
+
+/***
+****
+***/
+
+#define check(condition) \
+  do { \
+    ++current_test; \
+    if (!check_condition_impl (__FILE__, __LINE__, (condition))) \
+      return current_test; \
+  } while(0)
+
+#define check_streq(expected, actual) \
+  do { \
+    ++current_test; \
+    if (!check_streq_impl (__FILE__, __LINE__, (expected), (actual))) \
+      return current_test; \
+  } while(0)
+
+#define check_int_eq(expected, actual) \
+  do { \
+    ++current_test; \
+    if (!check_eq_impl (__FILE__, __LINE__, (expected), (actual))) \
+      return current_test; \
+  } while (0)
 
+/***
+****
+***/
 
 typedef int (*testFunc)( void );
 #define NUM_TESTS(tarray) ((int) (sizeof(tarray)/sizeof(tarray[0])))
@@ -42,7 +105,7 @@ runTests( const testFunc * const tests, int numTests )
 {
     int ret, i;
 
-    (void) test; /* Use test even if we don't have any tests to run */
+    (void) current_test; /* Use test even if we don't have any tests to run */
 
     for( i = 0; i < numTests; i++ )
        if( (ret = (*tests[i])()) )
@@ -51,12 +114,10 @@ runTests( const testFunc * const tests, int numTests )
     return 0;  /* All tests passed */
 }
 
-#define MAIN_SINGLE_TEST(test)                                 \
-int main( void )                                                       \
-{                                                              \
-    const testFunc tests[] = { test };                         \
-                                                               \
-    return runTests(tests, 1);                                 \
+#define MAIN_SINGLE_TEST(test) \
+int main( void ) { \
+    const testFunc tests[] = { test }; \
+    return runTests(tests, 1); \
 }
 
 #endif /* !LIBTRANSMISSION_TEST_H */