X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Ftesting.h;h=1bcc276c1a8666be6b30113addc572af02689ef5;hb=f8d0e8e1ab1bf901f43398b8549c302b85979b40;hp=84a1242953284309fe015df0ee6c43d95e6ba434;hpb=71478cd6550a4e930a1e9b8a906a66579e4e387a;p=collectd.git diff --git a/src/testing.h b/src/testing.h index 84a12429..1bcc276c 100644 --- a/src/testing.h +++ b/src/testing.h @@ -24,6 +24,11 @@ * Florian octo Forster */ +#ifndef TESTING_H +#define TESTING_H 1 + +#include + static int fail_count__ = 0; static int check_count__ = 0; @@ -31,7 +36,7 @@ static int check_count__ = 0; # define DBL_PRECISION 1e-12 #endif -#define DEF_TEST(func) static int test_##func () +#define DEF_TEST(func) static int test_##func (void) #define RUN_TEST(func) do { \ int status; \ @@ -50,36 +55,52 @@ static int check_count__ = 0; } while (0) #define OK(cond) OK1(cond, #cond) -#define STREQ(expect, actual) do { \ - if (strcmp (expect, actual) != 0) { \ - printf ("not ok %i - %s incorrect: expected \"%s\", got \"%s\"\n", \ - ++check_count__, #actual, expect, actual); \ +#define EXPECT_EQ_STR(expect, actual) do { \ + /* Evaluate 'actual' only once. */ \ + const char *got__ = actual; \ + if (strcmp (expect, got__) != 0) { \ + printf ("not ok %i - %s = \"%s\", want \"%s\"\n", \ + ++check_count__, #actual, got__, expect); \ return (-1); \ } \ - printf ("ok %i - %s evaluates to \"%s\"\n", ++check_count__, #actual, expect); \ + printf ("ok %i - %s = \"%s\"\n", ++check_count__, #actual, got__); \ } 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 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_UINT64(expect, actual) do { \ + uint64_t want__ = (uint64_t) expect; \ + uint64_t got__ = (uint64_t) actual; \ + if (got__ != want__) { \ + printf ("not ok %i - %s = %"PRIu64", want %"PRIu64"\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); \ + } \ + printf ("ok %i - %s = %"PRIu64"\n", ++check_count__, #actual, got__); \ +} while (0) + +#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 (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 { \ @@ -93,3 +114,5 @@ static int check_count__ = 0; status_ = (long) (expr); \ OK1(status_ == 0L, #expr); \ } while (0) + +#endif /* TESTING_H */