diff --git a/utest.h b/utest.h index 2cdb874..5ee26e0 100644 --- a/utest.h +++ b/utest.h @@ -75,8 +75,8 @@ #endif #pragma warning(push, 1) -#include #include +#include #pragma warning(pop) #elif defined(__linux__) @@ -299,89 +299,167 @@ #define utest_type_printer(...) UTEST_PRINTF("undef") #endif +#if defined(__clang__) #define UTEST_EXPECT(x, y, cond) \ - if (!((x)cond(y))) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - *utest_result = 1; \ + { \ + _Pragma("clang diagnostic push") _Pragma( \ + "clang diagnostic ignored \"-Wlanguage-extension-token\"") typeof(y) \ + xEval = (x); \ + typeof(y) yEval = (y); \ + _Pragma("clang diagnostic pop") if (!((xEval)cond(yEval))) { \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : "); \ + utest_type_printer(xEval); \ + UTEST_PRINTF("\n"); \ + UTEST_PRINTF(" Actual : "); \ + utest_type_printer(yEval); \ + UTEST_PRINTF("\n"); \ + *utest_result = 1; \ + } \ } +#elif defined(__GNUC__) +#define UTEST_EXPECT(x, y, cond) \ + { \ + typeof(y) xEval = (x); \ + typeof(y) yEval = (y); \ + if (!((xEval)cond(yEval))) { \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : "); \ + utest_type_printer(xEval); \ + UTEST_PRINTF("\n"); \ + UTEST_PRINTF(" Actual : "); \ + utest_type_printer(yEval); \ + UTEST_PRINTF("\n"); \ + *utest_result = 1; \ + } \ + } +#else +#define UTEST_EXPECT(x, y, cond) \ + { \ + if (!((x)cond(y))) { \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + *utest_result = 1; \ + } \ + } +#endif #define EXPECT_TRUE(x) \ if (!(x)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : true\n"); \ - UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : true\n"); \ + UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ *utest_result = 1; \ } #define EXPECT_FALSE(x) \ if (x) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : false\n"); \ - UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : false\n"); \ + UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ *utest_result = 1; \ } -#define EXPECT_EQ(x, y) UTEST_EXPECT(x, y, == ) -#define EXPECT_NE(x, y) UTEST_EXPECT(x, y, != ) -#define EXPECT_LT(x, y) UTEST_EXPECT(x, y, < ) -#define EXPECT_LE(x, y) UTEST_EXPECT(x, y, <= ) -#define EXPECT_GT(x, y) UTEST_EXPECT(x, y, > ) -#define EXPECT_GE(x, y) UTEST_EXPECT(x, y, >= ) +#define EXPECT_EQ(x, y) UTEST_EXPECT(x, y, ==) +#define EXPECT_NE(x, y) UTEST_EXPECT(x, y, !=) +#define EXPECT_LT(x, y) UTEST_EXPECT(x, y, <) +#define EXPECT_LE(x, y) UTEST_EXPECT(x, y, <=) +#define EXPECT_GT(x, y) UTEST_EXPECT(x, y, >) +#define EXPECT_GE(x, y) UTEST_EXPECT(x, y, >=) #define EXPECT_STREQ(x, y) \ if (0 != strcmp(x, y)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF(" Actual : \"%s\"\n", y); \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : \"%s\"\n", x); \ + UTEST_PRINTF(" Actual : \"%s\"\n", y); \ *utest_result = 1; \ } #define EXPECT_STRNE(x, y) \ if (0 == strcmp(x, y)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF(" Actual : \"%s\"\n", y); \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : \"%s\"\n", x); \ + UTEST_PRINTF(" Actual : \"%s\"\n", y); \ *utest_result = 1; \ } +#if defined(__clang__) #define UTEST_ASSERT(x, y, cond) \ - if (!((x)cond(y))) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - *utest_result = 1; \ - return; \ + { \ + _Pragma("clang diagnostic push") _Pragma( \ + "clang diagnostic ignored \"-Wlanguage-extension-token\"") typeof(y) \ + xEval = (x); \ + typeof(y) yEval = (y); \ + _Pragma("clang diagnostic pop") if (!((xEval)cond(yEval))) { \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : "); \ + utest_type_printer(xEval); \ + UTEST_PRINTF("\n"); \ + UTEST_PRINTF(" Actual : "); \ + utest_type_printer(yEval); \ + UTEST_PRINTF("\n"); \ + *utest_result = 1; \ + return; \ + } \ } +#elif defined(__GNUC__) +#define UTEST_ASSERT(x, y, cond) \ + { \ + typeof(y) xEval = (x); \ + typeof(y) yEval = (y); \ + if (!((xEval)cond(yEval))) { \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : "); \ + utest_type_printer(xEval); \ + UTEST_PRINTF("\n"); \ + UTEST_PRINTF(" Actual : "); \ + utest_type_printer(yEval); \ + UTEST_PRINTF("\n"); \ + *utest_result = 1; \ + return; \ + } \ + } +#else +#define UTEST_ASSERT(x, y, cond) \ + { \ + if (!((x)cond(y))) { \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + *utest_result = 1; \ + return; \ + } \ + } +#endif #define ASSERT_TRUE(x) \ if (!(x)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : true\n"); \ - UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : true\n"); \ + UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ *utest_result = 1; \ return; \ } #define ASSERT_FALSE(x) \ if (x) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : false\n"); \ - UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : false\n"); \ + UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ *utest_result = 1; \ return; \ } -#define ASSERT_EQ(x, y) UTEST_ASSERT(x, y, == ) -#define ASSERT_NE(x, y) UTEST_ASSERT(x, y, != ) -#define ASSERT_LT(x, y) UTEST_ASSERT(x, y, < ) -#define ASSERT_LE(x, y) UTEST_ASSERT(x, y, <= ) -#define ASSERT_GT(x, y) UTEST_ASSERT(x, y, > ) -#define ASSERT_GE(x, y) UTEST_ASSERT(x, y, >= ) +#define ASSERT_EQ(x, y) UTEST_ASSERT(x, y, ==) +#define ASSERT_NE(x, y) UTEST_ASSERT(x, y, !=) +#define ASSERT_LT(x, y) UTEST_ASSERT(x, y, <) +#define ASSERT_LE(x, y) UTEST_ASSERT(x, y, <=) +#define ASSERT_GT(x, y) UTEST_ASSERT(x, y, >) +#define ASSERT_GE(x, y) UTEST_ASSERT(x, y, >=) #define ASSERT_STREQ(x, y) \ EXPECT_STREQ(x, y); \ if (0 != strcmp(x, y)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF(" Actual : \"%s\"\n", y); \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : \"%s\"\n", x); \ + UTEST_PRINTF(" Actual : \"%s\"\n", y); \ *utest_result = 1; \ return; \ } @@ -389,9 +467,9 @@ #define ASSERT_STRNE(x, y) \ EXPECT_STRNE(x, y); \ if (0 == strcmp(x, y)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF(" Actual : \"%s\"\n", y); \ + UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : \"%s\"\n", x); \ + UTEST_PRINTF(" Actual : \"%s\"\n", y); \ *utest_result = 1; \ return; \ } @@ -400,7 +478,7 @@ UTEST_EXTERN struct utest_state_s utest_state; \ static void utest_run_##SET##_##NAME(int *utest_result); \ static void utest_##SET##_##NAME(int *utest_result, size_t utest_index) { \ - (void) utest_index; \ + (void)utest_index; \ utest_run_##SET##_##NAME(utest_result); \ } \ UTEST_INITIALIZER(utest_register_##SET##_##NAME) { \ @@ -435,7 +513,7 @@ static void utest_f_##FIXTURE##_##NAME(int *utest_result, \ size_t utest_index) { \ struct FIXTURE fixture; \ - (void) utest_index; \ + (void)utest_index; \ memset(&fixture, 0, sizeof(fixture)); \ utest_f_setup_##FIXTURE(utest_result, &fixture); \ if (0 != *utest_result) { \