Testing: Return failure from OK1() and other fixes.
authorFlorian Forster <octo@collectd.org>
Fri, 10 Jul 2015 11:14:43 +0000 (13:14 +0200)
committerFlorian Forster <octo@collectd.org>
Fri, 10 Jul 2015 11:14:43 +0000 (13:14 +0200)
* The tests for common and meta_data logged "not ok" but didn't signal
  failure because OK1() didn't include a return(-1) line. Adding this line
  caused some restructuring of the utils_vl_lookup test, because it used
  that macro in non-int functions.
* Fix DBLEQ() to work correctly with an expected NaN. Previously, the
  if condition would fall through to the "expect != actual" part, which
  is true for "NaN != NaN".
* Let the mock cdtime() return a non-zero value, as the (invalid) zero
  value is used in parse_values() to detect whether the time has been
  parsed already. This lead to the "N:..." tests failing.
* Correct the expected behavior of meta_data_add_*() when keys already
  exist: they're replaced rather than causing an error.

src/daemon/common_test.c
src/daemon/meta_data_test.c
src/daemon/utils_time_mock.c
src/testing.h
src/utils_vl_lookup_test.c

index 21602d5..0ee4e7e 100644 (file)
@@ -328,11 +328,11 @@ DEF_TEST(parse_values)
     };
 
     int status = parse_values (cases[i].buffer, &vl, &ds);
-    OK(status == cases[i].status);
+    EXPECT_INTEQ (cases[i].status, status);
     if (status != 0)
       continue;
 
-    OK(cases[i].value == vl.values[0].gauge);
+    DBLEQ (cases[i].value, vl.values[0].gauge);
   }
 
   return (0);
index 0ce8324..6d61107 100644 (file)
@@ -87,19 +87,23 @@ DEF_TEST(base)
   OK1 (b, "b evaluates to true");
 
   /* retrieving the wrong type always fails */
-  OK (meta_data_get_boolean (m, "string", &b) != 0);
-  OK (meta_data_get_string (m, "signed_int", &s) != 0);
-  OK (meta_data_get_string (m, "unsigned_int", &s) != 0);
-  OK (meta_data_get_string (m, "double", &s) != 0);
-  OK (meta_data_get_string (m, "boolean", &s) != 0);
+  EXPECT_INTEQ (-2, meta_data_get_boolean (m, "string", &b));
+  EXPECT_INTEQ (-2, meta_data_get_string (m, "signed_int", &s));
+  EXPECT_INTEQ (-2, meta_data_get_string (m, "unsigned_int", &s));
+  EXPECT_INTEQ (-2, meta_data_get_string (m, "double", &s));
+  EXPECT_INTEQ (-2, meta_data_get_string (m, "boolean", &s));
 
-  /* adding existing keys fails */
-  OK (meta_data_add_signed_int (m, "string", 666) != 0);
-  OK (meta_data_add_signed_int (m, "signed_int", 666) != 0);
+  /* replace existing keys */
+  CHECK_ZERO (meta_data_add_signed_int (m, "string", 666));
+  OK(meta_data_type (m, "string") == MD_TYPE_SIGNED_INT);
 
-  /* deleting, then adding a key works */
+  CHECK_ZERO (meta_data_add_signed_int (m, "signed_int", 666));
+  CHECK_ZERO (meta_data_get_signed_int (m, "signed_int", &si));
+  EXPECT_INTEQ (666, (int) si);
+
+  /* deleting keys */
   CHECK_ZERO (meta_data_delete (m, "signed_int"));
-  CHECK_ZERO (meta_data_add_signed_int (m, "signed_int", 42));
+  EXPECT_INTEQ (-2, meta_data_delete (m, "doesnt exist"));
 
   meta_data_destroy (m);
   return 0;
index 5edfe6f..2bde4d3 100644 (file)
@@ -28,6 +28,6 @@
 
 cdtime_t cdtime (void)
 {
-  return (0);
+  return (1542455354518929408);
 }
 
index d22af4c..805449f 100644 (file)
@@ -42,6 +42,7 @@ static int check_count__ = 0;
 #define OK1(cond, text) do { \
   _Bool result = (cond); \
   printf ("%s %i - %s\n", result ? "ok" : "not ok", ++check_count__, text); \
+  if (!result) { return -1; } \
 } while (0)
 #define OK(cond) OK1(cond, #cond)
 
@@ -64,12 +65,17 @@ static int check_count__ = 0;
 } while (0)
 
 #define DBLEQ(expect, actual) do { \
-  if ((isnan (expect) && !isnan (actual)) || ((expect) != (actual))) {\
+  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, expect, actual); \
+        ++check_count__, #actual, e, a); \
+    return (-1); \
+  } else if (!isnan (e) && (((e-a) < -1e-12) || ((e-a) > 1e-12))) { \
+    printf ("not ok %i - %s incorrect: expected %.15g, got %.15g\n", \
+        ++check_count__, #actual, e, a); \
     return (-1); \
   } \
-  printf ("ok %i - %s evaluates to %.15g\n", ++check_count__, #actual, expect); \
+  printf ("ok %i - %s evaluates to %.15g\n", ++check_count__, #actual, e); \
 } while (0)
 
 #define CHECK_NOT_NULL(expr) do { \
index 6a2676a..41cc0a4 100644 (file)
@@ -65,7 +65,7 @@ static void *lookup_class_callback (data_set_t const *ds,
   identifier_t *class = user_class;
   identifier_t *obj;
 
-  OK(expect_new_obj);
+  assert (expect_new_obj);
 
   memcpy (&last_class_ident, class, sizeof (last_class_ident));
   
@@ -81,7 +81,7 @@ static void *lookup_class_callback (data_set_t const *ds,
   return ((void *) obj);
 }
 
-static void checked_lookup_add (lookup_t *obj, /* {{{ */
+static int checked_lookup_add (lookup_t *obj, /* {{{ */
     char const *host,
     char const *plugin, char const *plugin_instance,
     char const *type, char const *type_instance,
@@ -101,7 +101,8 @@ static void checked_lookup_add (lookup_t *obj, /* {{{ */
   memmove (user_class, &ident, sizeof (ident));
 
   OK(lookup_add (obj, &ident, group_by, user_class) == 0);
-} /* }}} void test_add */
+  return 0;
+} /* }}} int checked_lookup_add */
 
 static int checked_lookup_search (lookup_t *obj,
     char const *host,
@@ -129,20 +130,11 @@ static int checked_lookup_search (lookup_t *obj,
   return (status);
 }
 
-static lookup_t *checked_lookup_create (void)
-{
-  lookup_t *obj = lookup_create (
-      lookup_class_callback,
-      lookup_obj_callback,
-      (void *) free,
-      (void *) free);
-  OK(obj != NULL);
-  return (obj);
-}
-
 DEF_TEST(group_by_specific_host)
 {
-  lookup_t *obj = checked_lookup_create ();
+  lookup_t *obj;
+  CHECK_NOT_NULL (obj = lookup_create (
+        lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
 
   checked_lookup_add (obj, "/.*/", "test", "", "test", "/.*/", LU_GROUP_BY_HOST);
   checked_lookup_search (obj, "host0", "test", "", "test", "0",
@@ -160,7 +152,9 @@ DEF_TEST(group_by_specific_host)
 
 DEF_TEST(group_by_any_host)
 {
-  lookup_t *obj = checked_lookup_create ();
+  lookup_t *obj;
+  CHECK_NOT_NULL (obj = lookup_create (
+        lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
 
   checked_lookup_add (obj, "/.*/", "/.*/", "/.*/", "test", "/.*/", LU_GROUP_BY_HOST);
   checked_lookup_search (obj, "host0", "plugin0", "", "test", "0",
@@ -186,9 +180,12 @@ DEF_TEST(group_by_any_host)
 
 DEF_TEST(multiple_lookups)
 {
-  lookup_t *obj = checked_lookup_create ();
+  lookup_t *obj;
   int status;
 
+  CHECK_NOT_NULL (obj = lookup_create (
+        lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
+
   checked_lookup_add (obj, "/.*/", "plugin0", "", "test", "/.*/", LU_GROUP_BY_HOST);
   checked_lookup_add (obj, "/.*/", "/.*/", "", "test", "ti0", LU_GROUP_BY_HOST);
 
@@ -211,7 +208,9 @@ DEF_TEST(multiple_lookups)
 
 DEF_TEST(regex)
 {
-  lookup_t *obj = checked_lookup_create ();
+  lookup_t *obj;
+  CHECK_NOT_NULL (obj = lookup_create (
+        lookup_class_callback, lookup_obj_callback, (void *) free, (void *) free));
 
   checked_lookup_add (obj, "/^db[0-9]\\./", "cpu", "/.*/", "cpu", "/.*/",
       LU_GROUP_BY_TYPE_INSTANCE);