X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Ftesting.h;h=2e4bf054b3855ea9717a6e08d8e3d1aa08b2991c;hb=72a76347831a848e6722d9147297a1e03c4c01d8;hp=5df1b83a3e0f076b3a35bff53df09b021b9a0ea5;hpb=2bda2a5648c87a2c5b8304238cd80ff17984c5cd;p=collectd.git diff --git a/src/testing.h b/src/testing.h index 5df1b83a..2e4bf054 100644 --- a/src/testing.h +++ b/src/testing.h @@ -1,6 +1,6 @@ /** * collectd - src/tests/macros.h - * Copyright (C) 2013 Florian octo Forster + * Copyright (C) 2013-2015 Florian octo Forster * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -24,10 +24,19 @@ * Florian octo Forster */ +#ifndef TESTING_H +#define TESTING_H 1 + +#include + static int fail_count__ = 0; static int check_count__ = 0; -#define DEF_TEST(func) static int test_##func () +#ifndef DBL_PRECISION +# define DBL_PRECISION 1e-12 +#endif + +#define DEF_TEST(func) static int test_##func (void) #define RUN_TEST(func) do { \ int status; \ @@ -42,16 +51,54 @@ 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) -#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_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 = %d\n", ++check_count__, #actual, got__); \ +} while (0) + +#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); \ + } \ + 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 = %.15g\n", ++check_count__, #actual, got__); \ } while (0) #define CHECK_NOT_NULL(expr) do { \ @@ -65,3 +112,5 @@ static int check_count__ = 0; status_ = (long) (expr); \ OK1(status_ == 0L, #expr); \ } while (0) + +#endif /* TESTING_H */