src/daemon/utils_time.h: Improve precision of conversion macros.
[collectd.git] / src / testing.h
index 805449f..1bbe800 100644 (file)
  *   Florian octo Forster <octo at collectd.org>
  */
 
+#include <inttypes.h>
+
 static int fail_count__ = 0;
 static int check_count__ = 0;
 
+#ifndef DBL_PRECISION
+# define DBL_PRECISION 1e-12
+#endif
+
 #define DEF_TEST(func) static int test_##func ()
 
 #define RUN_TEST(func) do { \
@@ -55,6 +61,15 @@ static int check_count__ = 0;
   printf ("ok %i - %s evaluates to \"%s\"\n", ++check_count__, #actual, expect); \
 } while (0)
 
+#define EXPECT_EQ(expect, actual, format) do { \
+  if ((expect) != (actual)) {\
+    printf ("not ok %i - %s incorrect: expected " format ", got " format "\n", \
+        ++check_count__, #actual, expect, actual); \
+    return (-1); \
+  } \
+  printf ("ok %i - %s evaluates to " format "\n", ++check_count__, #actual, expect); \
+} while (0)
+
 #define EXPECT_INTEQ(expect, actual) do { \
   if ((expect) != (actual)) {\
     printf ("not ok %i - %s incorrect: expected %d, got %d\n", \
@@ -64,13 +79,15 @@ static int check_count__ = 0;
   printf ("ok %i - %s evaluates to %d\n", ++check_count__, #actual, expect); \
 } while (0)
 
+#define EXPECT_EQ_UINT64(expect, actual) EXPECT_EQ((expect), (actual), "%"PRIu64)
+
 #define DBLEQ(expect, actual) do { \
   double e = (expect); double a = (actual); \
   if (isnan (e) && !isnan (a)) { \
     printf ("not ok %i - %s incorrect: expected %.15g, got %.15g\n", \
         ++check_count__, #actual, e, a); \
     return (-1); \
-  } else if (!isnan (e) && (((e-a) < -1e-12) || ((e-a) > 1e-12))) { \
+  } else if (!isnan (e) && (((e-a) < -DBL_PRECISION) || ((e-a) > DBL_PRECISION))) { \
     printf ("not ok %i - %s incorrect: expected %.15g, got %.15g\n", \
         ++check_count__, #actual, e, a); \
     return (-1); \