diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..89deb07 --- /dev/null +++ b/.clang-format @@ -0,0 +1,2 @@ +BasedOnStyle: LLVM +SortIncludes: false \ No newline at end of file diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..89deb07 --- /dev/null +++ b/.clang-format @@ -0,0 +1,2 @@ +BasedOnStyle: LLVM +SortIncludes: false \ No newline at end of file diff --git a/utest.h b/utest.h index 229d06e..1125551 100644 --- a/utest.h +++ b/utest.h @@ -205,23 +205,18 @@ #define UTEST_UNUSED __attribute__((unused)) #endif -#define UTEST_PRINTF0(FORMAT) \ +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wvariadic-macros" +#endif +#define UTEST_PRINTF(...) \ if (utest_state.output) { \ - fprintf(utest_state.output, FORMAT); \ + fprintf(utest_state.output, __VA_ARGS__); \ } \ - printf(FORMAT) - -#define UTEST_PRINTF1(FORMAT, P0) \ - if (utest_state.output) { \ - fprintf(utest_state.output, FORMAT, P0); \ - } \ - printf(FORMAT, P0) - -#define UTEST_PRINTF2(FORMAT, P0, P1) \ - if (utest_state.output) { \ - fprintf(utest_state.output, FORMAT, P0, P1); \ - } \ - printf(FORMAT, P0, P1) + printf(__VA_ARGS__) +#ifdef __clang__ +#pragma clang diagnostic pop +#endif #ifdef _MSC_VER #define UTEST_SNPRINTF(BUFFER, N, ...) _snprintf_s(BUFFER, N, N, __VA_ARGS__) @@ -247,37 +242,37 @@ #if defined(UTEST_OVERLOADABLE) UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f) { - UTEST_PRINTF1("%f", UTEST_CAST(double, f)); + UTEST_PRINTF("%f", UTEST_CAST(double, f)); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(double d); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(double d) { - UTEST_PRINTF1("%f", d); + UTEST_PRINTF("%f", d); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long double d); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long double d) { - UTEST_PRINTF1("%Lf", d); + UTEST_PRINTF("%Lf", d); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(int i) { - UTEST_PRINTF1("%d", i); + UTEST_PRINTF("%d", i); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned int i) { - UTEST_PRINTF1("%u", i); + UTEST_PRINTF("%u", i); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long int i) { - UTEST_PRINTF1("%ld", i); + UTEST_PRINTF("%ld", i); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long unsigned int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long unsigned int i) { - UTEST_PRINTF1("%lu", i); + UTEST_PRINTF("%lu", i); } /* @@ -287,13 +282,13 @@ #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long int i) { - UTEST_PRINTF1("%lld", i); + UTEST_PRINTF("%lld", i); } UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long unsigned int i); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(long long unsigned int i) { - UTEST_PRINTF1("%llu", i); + UTEST_PRINTF("%llu", i); } #endif #else @@ -301,92 +296,170 @@ we don't have the ability to print the values we got, so we create a macro to tell our users we can't do anything fancy */ -#define utest_type_printer(...) UTEST_PRINTF0("undef") +#define utest_type_printer(...) UTEST_PRINTF("undef") #endif +#if defined(__clang__) #define UTEST_EXPECT(x, y, cond) \ - if (!((x)cond(y))) { \ - UTEST_PRINTF2("%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_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF0(" Expected : true\n"); \ - UTEST_PRINTF1(" 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_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF0(" Expected : false\n"); \ - UTEST_PRINTF1(" 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_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF1(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF1(" 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_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF1(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF1(" 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_PRINTF2("%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_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF0(" Expected : true\n"); \ - UTEST_PRINTF1(" 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_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF0(" Expected : false\n"); \ - UTEST_PRINTF1(" 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_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF1(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF1(" 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; \ } @@ -394,9 +467,9 @@ #define ASSERT_STRNE(x, y) \ EXPECT_STRNE(x, y); \ if (0 == strcmp(x, y)) { \ - UTEST_PRINTF2("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF1(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF1(" 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; \ } @@ -405,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) { \ @@ -440,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) { \