]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
src/testing.h: Support NULL pointers in EXPECT_EQ_STR().
authorFlorian Forster <octo@google.com>
Thu, 16 Jul 2020 16:20:27 +0000 (18:20 +0200)
committerFlorian Forster <octo@google.com>
Tue, 21 Jul 2020 15:30:10 +0000 (17:30 +0200)
Changes in "format_graphite" fix the following error:

./src/testing.h:77:56: error: the address of ‘want’ will always evaluate as ‘true’ [-Werror=address]
   77 |              #actual, got__ ? got__ : "(null)", expect ? expect : "(null)");   \

src/testing.h
src/utils/format_graphite/format_graphite_test.c

index c1badcc8e3666f452dee242b94b8ac08af1f4574..c846652dc98f3ff4c8b1348de9e1adb438df6621 100644 (file)
@@ -72,12 +72,18 @@ static int check_count__;
   do {                                                                         \
     /* Evaluate 'actual' only once. */                                         \
     const char *got__ = actual;                                                \
-    if (strcmp(expect, got__) != 0) {                                          \
+    if ((expect == NULL) != (got__ == NULL)) {                                 \
+      printf("not ok %i - %s = \"%s\", want \"%s\"\n", ++check_count__,        \
+             #actual, got__ ? got__ : "(null)", expect ? expect : "(null)");   \
+      return -1;                                                               \
+    }                                                                          \
+    if ((expect != NULL) && (strcmp(expect, got__) != 0)) {                    \
       printf("not ok %i - %s = \"%s\", want \"%s\"\n", ++check_count__,        \
              #actual, got__, expect);                                          \
       return -1;                                                               \
     }                                                                          \
-    printf("ok %i - %s = \"%s\"\n", ++check_count__, #actual, got__);          \
+    printf("ok %i - %s = \"%s\"\n", ++check_count__, #actual,                  \
+           got__ ? got__ : "(null)");                                          \
   } while (0)
 
 #define EXPECT_EQ_INT(expect, actual)                                          \
index ac421db3e1eaa976d14c0619b595b02ec6e29b45..5e4439bc327f37de2f1bd89a4d383bbac92a7890 100644 (file)
@@ -159,8 +159,11 @@ DEF_TEST(metric_name) {
         .type = "single",
     };
 
-    char want[1024];
-    ssnprintf(want, sizeof(want), "%s 42 1480063672\r\n", cases[i].want_name);
+    strbuf_t want = STRBUF_CREATE;
+    if (cases[i].want_name != NULL) {
+      strbuf_print(&want, cases[i].want_name);
+      strbuf_print(&want, " 42 1480063672\r\n");
+    }
 
     if (cases[i].plugin_instance != NULL)
       sstrncpy(vl.plugin_instance, cases[i].plugin_instance,
@@ -173,7 +176,8 @@ DEF_TEST(metric_name) {
     EXPECT_EQ_INT(0, format_graphite(got, sizeof(got), &ds_single, &vl,
                                      cases[i].prefix, cases[i].suffix, '@',
                                      cases[i].flags));
-    EXPECT_EQ_STR(want, got);
+    EXPECT_EQ_STR(want.ptr, got);
+    STRBUF_DESTROY(want);
   }
 
   return 0;