From: Florian Forster Date: Thu, 16 Jul 2020 16:20:27 +0000 (+0200) Subject: src/testing.h: Support NULL pointers in EXPECT_EQ_STR(). X-Git-Tag: 6.0.0-rc0~145^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1e5a0feeeeeb1c5c5a3457ea7f4edae545750af2;p=thirdparty%2Fcollectd.git src/testing.h: Support NULL pointers in EXPECT_EQ_STR(). 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)"); \ --- diff --git a/src/testing.h b/src/testing.h index c1badcc8e..c846652dc 100644 --- a/src/testing.h +++ b/src/testing.h @@ -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) \ diff --git a/src/utils/format_graphite/format_graphite_test.c b/src/utils/format_graphite/format_graphite_test.c index ac421db3e..5e4439bc3 100644 --- a/src/utils/format_graphite/format_graphite_test.c +++ b/src/utils/format_graphite/format_graphite_test.c @@ -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;