From 67ea18d2b08da3c5442cb2bb2c7b2059e3da94a8 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 21 Aug 2015 13:55:42 +0200 Subject: [PATCH] src/testing.h: Rename the EXPECT_EQ_{DOUBLE,INT,STR} macros. Let all equality macros follow a common naming and logging schema. --- src/daemon/common_test.c | 68 +++++++++++++++++++++---------------------- src/daemon/meta_data_test.c | 22 +++++++------- src/daemon/utils_subst_test.c | 4 +-- src/daemon/utils_time_test.c | 2 +- src/testing.h | 48 +++++++++++++----------------- src/utils_latency_test.c | 24 +++++++-------- src/utils_mount_test.c | 12 ++++---- 7 files changed, 87 insertions(+), 93 deletions(-) diff --git a/src/daemon/common_test.c b/src/daemon/common_test.c index 0ee4e7e0..23c6f232 100644 --- a/src/daemon/common_test.c +++ b/src/daemon/common_test.c @@ -38,18 +38,18 @@ DEF_TEST(sstrncpy) ret = sstrncpy (ptr, "foobar", 8); OK(ret == ptr); - STREQ ("foobar", ptr); + EXPECT_EQ_STR ("foobar", ptr); OK(buffer[3] == buffer[12]); ret = sstrncpy (ptr, "abc", 8); OK(ret == ptr); - STREQ ("abc", ptr); + EXPECT_EQ_STR ("abc", ptr); OK(buffer[3] == buffer[12]); ret = sstrncpy (ptr, "collectd", 8); OK(ret == ptr); OK(ptr[7] == 0); - STREQ ("collect", ptr); + EXPECT_EQ_STR ("collect", ptr); OK(buffer[3] == buffer[12]); return (0); @@ -66,12 +66,12 @@ DEF_TEST(ssnprintf) status = ssnprintf (ptr, 8, "%i", 1337); OK(status == 4); - STREQ ("1337", ptr); + EXPECT_EQ_STR ("1337", ptr); status = ssnprintf (ptr, 8, "%s", "collectd"); OK(status == 8); OK(ptr[7] == 0); - STREQ ("collect", ptr); + EXPECT_EQ_STR ("collect", ptr); OK(buffer[3] == buffer[12]); return (0); @@ -83,7 +83,7 @@ DEF_TEST(sstrdup) ptr = sstrdup ("collectd"); OK(ptr != NULL); - STREQ ("collectd", ptr); + EXPECT_EQ_STR ("collectd", ptr); sfree(ptr); OK(ptr == NULL); @@ -103,40 +103,40 @@ DEF_TEST(strsplit) strncpy (buffer, "foo bar", sizeof (buffer)); status = strsplit (buffer, fields, 8); OK(status == 2); - STREQ ("foo", fields[0]); - STREQ ("bar", fields[1]); + EXPECT_EQ_STR ("foo", fields[0]); + EXPECT_EQ_STR ("bar", fields[1]); strncpy (buffer, "foo \t bar", sizeof (buffer)); status = strsplit (buffer, fields, 8); OK(status == 2); - STREQ ("foo", fields[0]); - STREQ ("bar", fields[1]); + EXPECT_EQ_STR ("foo", fields[0]); + EXPECT_EQ_STR ("bar", fields[1]); strncpy (buffer, "one two\tthree\rfour\nfive", sizeof (buffer)); status = strsplit (buffer, fields, 8); OK(status == 5); - STREQ ("one", fields[0]); - STREQ ("two", fields[1]); - STREQ ("three", fields[2]); - STREQ ("four", fields[3]); - STREQ ("five", fields[4]); + EXPECT_EQ_STR ("one", fields[0]); + EXPECT_EQ_STR ("two", fields[1]); + EXPECT_EQ_STR ("three", fields[2]); + EXPECT_EQ_STR ("four", fields[3]); + EXPECT_EQ_STR ("five", fields[4]); strncpy (buffer, "\twith trailing\n", sizeof (buffer)); status = strsplit (buffer, fields, 8); OK(status == 2); - STREQ ("with", fields[0]); - STREQ ("trailing", fields[1]); + EXPECT_EQ_STR ("with", fields[0]); + EXPECT_EQ_STR ("trailing", fields[1]); strncpy (buffer, "1 2 3 4 5 6 7 8 9 10 11 12 13", sizeof (buffer)); status = strsplit (buffer, fields, 8); OK(status == 8); - STREQ ("7", fields[6]); - STREQ ("8", fields[7]); + EXPECT_EQ_STR ("7", fields[6]); + EXPECT_EQ_STR ("8", fields[7]); strncpy (buffer, "single", sizeof (buffer)); status = strsplit (buffer, fields, 8); OK(status == 1); - STREQ ("single", fields[0]); + EXPECT_EQ_STR ("single", fields[0]); strncpy (buffer, "", sizeof (buffer)); status = strsplit (buffer, fields, 8); @@ -158,26 +158,26 @@ DEF_TEST(strjoin) status = strjoin (buffer, sizeof (buffer), fields, 2, "!"); OK(status == 7); - STREQ ("foo!bar", buffer); + EXPECT_EQ_STR ("foo!bar", buffer); status = strjoin (buffer, sizeof (buffer), fields, 1, "!"); OK(status == 3); - STREQ ("foo", buffer); + EXPECT_EQ_STR ("foo", buffer); status = strjoin (buffer, sizeof (buffer), fields, 0, "!"); OK(status < 0); status = strjoin (buffer, sizeof (buffer), fields, 2, "rcht"); OK(status == 10); - STREQ ("foorchtbar", buffer); + EXPECT_EQ_STR ("foorchtbar", buffer); status = strjoin (buffer, sizeof (buffer), fields, 4, ""); OK(status == 12); - STREQ ("foobarbazqux", buffer); + EXPECT_EQ_STR ("foobarbazqux", buffer); status = strjoin (buffer, sizeof (buffer), fields, 4, "!"); OK(status == 15); - STREQ ("foo!bar!baz!qux", buffer); + EXPECT_EQ_STR ("foo!bar!baz!qux", buffer); fields[0] = "0123"; fields[1] = "4567"; @@ -207,7 +207,7 @@ DEF_TEST(escape_slashes) strncpy (buffer, cases[i].str, sizeof (buffer)); OK(escape_slashes (buffer, sizeof (buffer)) == 0); - STREQ(cases[i].want, buffer); + EXPECT_EQ_STR(cases[i].want, buffer); } return 0; @@ -234,7 +234,7 @@ DEF_TEST(escape_string) strncpy (buffer, cases[i].str, sizeof (buffer)); OK(escape_string (buffer, sizeof (buffer)) == 0); - STREQ(cases[i].want, buffer); + EXPECT_EQ_STR(cases[i].want, buffer); } return 0; @@ -248,23 +248,23 @@ DEF_TEST(strunescape) strncpy (buffer, "foo\\tbar", sizeof (buffer)); status = strunescape (buffer, sizeof (buffer)); OK(status == 0); - STREQ ("foo\tbar", buffer); + EXPECT_EQ_STR ("foo\tbar", buffer); strncpy (buffer, "\\tfoo\\r\\n", sizeof (buffer)); status = strunescape (buffer, sizeof (buffer)); OK(status == 0); - STREQ ("\tfoo\r\n", buffer); + EXPECT_EQ_STR ("\tfoo\r\n", buffer); strncpy (buffer, "With \\\"quotes\\\"", sizeof (buffer)); status = strunescape (buffer, sizeof (buffer)); OK(status == 0); - STREQ ("With \"quotes\"", buffer); + EXPECT_EQ_STR ("With \"quotes\"", buffer); /* Backslash before null byte */ strncpy (buffer, "\\tbackslash end\\", sizeof (buffer)); status = strunescape (buffer, sizeof (buffer)); OK(status != 0); - STREQ ("\tbackslash end", buffer); + EXPECT_EQ_STR ("\tbackslash end", buffer); return (0); /* Backslash at buffer end */ @@ -328,11 +328,11 @@ DEF_TEST(parse_values) }; int status = parse_values (cases[i].buffer, &vl, &ds); - EXPECT_INTEQ (cases[i].status, status); + EXPECT_EQ_INT (cases[i].status, status); if (status != 0) continue; - DBLEQ (cases[i].value, vl.values[0].gauge); + EXPECT_EQ_DOUBLE (cases[i].value, vl.values[0].gauge); } return (0); @@ -370,7 +370,7 @@ DEF_TEST(value_to_rate) } OK(value_to_rate (&got, cases[i].v1, cases[i].ds_type, TIME_T_TO_CDTIME_T(cases[i].t1), &state) == 0); - DBLEQ(cases[i].want, got); + EXPECT_EQ_DOUBLE(cases[i].want, got); } return 0; diff --git a/src/daemon/meta_data_test.c b/src/daemon/meta_data_test.c index 6d61107d..b4c0e276 100644 --- a/src/daemon/meta_data_test.c +++ b/src/daemon/meta_data_test.c @@ -71,27 +71,27 @@ DEF_TEST(base) /* retrieve and check all values */ CHECK_ZERO (meta_data_get_string (m, "string", &s)); - STREQ ("foobar", s); + EXPECT_EQ_STR ("foobar", s); sfree (s); CHECK_ZERO (meta_data_get_signed_int (m, "signed_int", &si)); - EXPECT_INTEQ (-1, (int) si); + EXPECT_EQ_INT (-1, (int) si); CHECK_ZERO (meta_data_get_unsigned_int (m, "unsigned_int", &ui)); - EXPECT_INTEQ (1, (int) ui); + EXPECT_EQ_INT (1, (int) ui); CHECK_ZERO (meta_data_get_double (m, "double", &d)); - DBLEQ (47.11, d); + EXPECT_EQ_DOUBLE (47.11, d); CHECK_ZERO (meta_data_get_boolean (m, "boolean", &b)); OK1 (b, "b evaluates to true"); /* retrieving the wrong type always fails */ - 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)); + EXPECT_EQ_INT (-2, meta_data_get_boolean (m, "string", &b)); + EXPECT_EQ_INT (-2, meta_data_get_string (m, "signed_int", &s)); + EXPECT_EQ_INT (-2, meta_data_get_string (m, "unsigned_int", &s)); + EXPECT_EQ_INT (-2, meta_data_get_string (m, "double", &s)); + EXPECT_EQ_INT (-2, meta_data_get_string (m, "boolean", &s)); /* replace existing keys */ CHECK_ZERO (meta_data_add_signed_int (m, "string", 666)); @@ -99,11 +99,11 @@ DEF_TEST(base) 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); + EXPECT_EQ_INT (666, (int) si); /* deleting keys */ CHECK_ZERO (meta_data_delete (m, "signed_int")); - EXPECT_INTEQ (-2, meta_data_delete (m, "doesnt exist")); + EXPECT_EQ_INT (-2, meta_data_delete (m, "doesnt exist")); meta_data_destroy (m); return 0; diff --git a/src/daemon/utils_subst_test.c b/src/daemon/utils_subst_test.c index 75197663..2a708026 100644 --- a/src/daemon/utils_subst_test.c +++ b/src/daemon/utils_subst_test.c @@ -82,7 +82,7 @@ DEF_TEST(subst) } OK(subst (buffer, sizeof (buffer), cases[i].str, cases[i].off1, cases[i].off2, cases[i].rplmt) == &buffer[0]); - STREQ(cases[i].want, buffer); + EXPECT_EQ_STR(cases[i].want, buffer); } return 0; @@ -112,7 +112,7 @@ DEF_TEST(subst_string) } OK(subst_string (buffer, sizeof (buffer), cases[i].str, cases[i].srch, cases[i].rplmt) == buffer); - STREQ(cases[i].want, buffer); + EXPECT_EQ_STR(cases[i].want, buffer); } return 0; diff --git a/src/daemon/utils_time_test.c b/src/daemon/utils_time_test.c index 994b9bc0..71d2c653 100644 --- a/src/daemon/utils_time_test.c +++ b/src/daemon/utils_time_test.c @@ -85,7 +85,7 @@ DEF_TEST(conversion) EXPECT_EQ_UINT64 (cases[i].ts.tv_nsec, ts.tv_nsec); // cdtime -> double - DBLEQ (cases[i].d, CDTIME_T_TO_DOUBLE (cases[i].t)); + EXPECT_EQ_DOUBLE (cases[i].d, CDTIME_T_TO_DOUBLE (cases[i].t)); } return 0; diff --git a/src/testing.h b/src/testing.h index 5d4d61b5..0c415e48 100644 --- a/src/testing.h +++ b/src/testing.h @@ -52,31 +52,24 @@ static int check_count__ = 0; } while (0) #define OK(cond) OK1(cond, #cond) -#define STREQ(expect, actual) do { \ +#define EXPECT_EQ_STR(expect, actual) do { \ if (strcmp (expect, actual) != 0) { \ - printf ("not ok %i - %s incorrect: expected \"%s\", got \"%s\"\n", \ - ++check_count__, #actual, expect, actual); \ + printf ("not ok %i - %s = \"%s\", want \"%s\"\n", \ + ++check_count__, #actual, actual, expect); \ return (-1); \ } \ - printf ("ok %i - %s evaluates to \"%s\"\n", ++check_count__, #actual, expect); \ + printf ("ok %i - %s = \"%s\"\n", ++check_count__, #actual, actual); \ } 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", \ - ++check_count__, #actual, expect, actual); \ +#define EXPECT_EQ_INT(expect, actual) do { \ + int want__ = (int) expect; \ + int got__ = (int) actual; \ + if (got__ != want__) { \ + printf ("not ok %i - %s = %d, want %d\n", \ + ++check_count__, #actual, got__, want__); \ return (-1); \ } \ - printf ("ok %i - %s evaluates to %d\n", ++check_count__, #actual, expect); \ + printf ("ok %i - %s = %d\n", ++check_count__, #actual, got__); \ } while (0) #define EXPECT_EQ_UINT64(expect, actual) do { \ @@ -90,18 +83,19 @@ static int check_count__ = 0; printf ("ok %i - %s = %"PRIu64"\n", ++check_count__, #actual, got__); \ } while (0) -#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); \ +#define EXPECT_EQ_DOUBLE(expect, actual) do { \ + double want__ = (double) expect; \ + double got__ = (double) actual; \ + if (isnan (want__) && !isnan (got__)) { \ + printf ("not ok %i - %s = %.15g, want %.15g\n", \ + ++check_count__, #actual, got__, want__); \ return (-1); \ - } 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); \ + } else if (!isnan (want__) && (((want__-got__) < -DBL_PRECISION) || ((want__-got__) > DBL_PRECISION))) { \ + printf ("not ok %i - %s = %.15g, want %.15g\n", \ + ++check_count__, #actual, got__, want__); \ return (-1); \ } \ - printf ("ok %i - %s evaluates to %.15g\n", ++check_count__, #actual, e); \ + printf ("ok %i - %s = %.15g\n", ++check_count__, #actual, got__); \ } while (0) #define CHECK_NOT_NULL(expr) do { \ diff --git a/src/utils_latency_test.c b/src/utils_latency_test.c index b039c546..5769ec95 100644 --- a/src/utils_latency_test.c +++ b/src/utils_latency_test.c @@ -59,10 +59,10 @@ DEF_TEST(simple) i, cases[i].val, DOUBLE_TO_CDTIME_T (cases[i].val)); latency_counter_add (l, DOUBLE_TO_CDTIME_T (cases[i].val)); - DBLEQ (cases[i].min, CDTIME_T_TO_DOUBLE (latency_counter_get_min (l))); - DBLEQ (cases[i].max, CDTIME_T_TO_DOUBLE (latency_counter_get_max (l))); - DBLEQ (cases[i].sum, CDTIME_T_TO_DOUBLE (latency_counter_get_sum (l))); - DBLEQ (cases[i].avg, CDTIME_T_TO_DOUBLE (latency_counter_get_average (l))); + EXPECT_EQ_DOUBLE (cases[i].min, CDTIME_T_TO_DOUBLE (latency_counter_get_min (l))); + EXPECT_EQ_DOUBLE (cases[i].max, CDTIME_T_TO_DOUBLE (latency_counter_get_max (l))); + EXPECT_EQ_DOUBLE (cases[i].sum, CDTIME_T_TO_DOUBLE (latency_counter_get_sum (l))); + EXPECT_EQ_DOUBLE (cases[i].avg, CDTIME_T_TO_DOUBLE (latency_counter_get_average (l))); } latency_counter_destroy (l); @@ -80,15 +80,15 @@ DEF_TEST(percentile) latency_counter_add (l, TIME_T_TO_CDTIME_T (((time_t) i) + 1)); } - DBLEQ ( 1.0, CDTIME_T_TO_DOUBLE (latency_counter_get_min (l))); - DBLEQ (100.0, CDTIME_T_TO_DOUBLE (latency_counter_get_max (l))); - DBLEQ (100.0 * 101.0 / 2.0, CDTIME_T_TO_DOUBLE (latency_counter_get_sum (l))); - DBLEQ ( 50.5, CDTIME_T_TO_DOUBLE (latency_counter_get_average (l))); + EXPECT_EQ_DOUBLE ( 1.0, CDTIME_T_TO_DOUBLE (latency_counter_get_min (l))); + EXPECT_EQ_DOUBLE (100.0, CDTIME_T_TO_DOUBLE (latency_counter_get_max (l))); + EXPECT_EQ_DOUBLE (100.0 * 101.0 / 2.0, CDTIME_T_TO_DOUBLE (latency_counter_get_sum (l))); + EXPECT_EQ_DOUBLE ( 50.5, CDTIME_T_TO_DOUBLE (latency_counter_get_average (l))); - DBLEQ (50.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 50.0))); - DBLEQ (80.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 80.0))); - DBLEQ (95.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 95.0))); - DBLEQ (99.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 99.0))); + EXPECT_EQ_DOUBLE (50.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 50.0))); + EXPECT_EQ_DOUBLE (80.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 80.0))); + EXPECT_EQ_DOUBLE (95.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 95.0))); + EXPECT_EQ_DOUBLE (99.0, CDTIME_T_TO_DOUBLE (latency_counter_get_percentile (l, 99.0))); CHECK_ZERO (latency_counter_get_percentile (l, -1.0)); CHECK_ZERO (latency_counter_get_percentile (l, 101.0)); diff --git a/src/utils_mount_test.c b/src/utils_mount_test.c index c5ffbfbc..77a4205e 100644 --- a/src/utils_mount_test.c +++ b/src/utils_mount_test.c @@ -77,14 +77,14 @@ DEF_TEST(cu_mount_getoptionvalue) char line_opts[] = "foo=one,bar=two,qux=three"; char line_bool[] = "one,two,three"; - STREQ ("one", cu_mount_getoptionvalue (line_opts, "foo=")); - STREQ ("two", cu_mount_getoptionvalue (line_opts, "bar=")); - STREQ ("three", cu_mount_getoptionvalue (line_opts, "qux=")); + EXPECT_EQ_STR ("one", cu_mount_getoptionvalue (line_opts, "foo=")); + EXPECT_EQ_STR ("two", cu_mount_getoptionvalue (line_opts, "bar=")); + EXPECT_EQ_STR ("three", cu_mount_getoptionvalue (line_opts, "qux=")); OK (NULL == cu_mount_getoptionvalue (line_opts, "unknown=")); - STREQ ("", cu_mount_getoptionvalue (line_bool, "one")); - STREQ ("", cu_mount_getoptionvalue (line_bool, "two")); - STREQ ("", cu_mount_getoptionvalue (line_bool, "three")); + EXPECT_EQ_STR ("", cu_mount_getoptionvalue (line_bool, "one")); + EXPECT_EQ_STR ("", cu_mount_getoptionvalue (line_bool, "two")); + EXPECT_EQ_STR ("", cu_mount_getoptionvalue (line_bool, "three")); OK (NULL == cu_mount_getoptionvalue (line_bool, "four")); return (0); -- 2.11.0