From 7846c1d30f3c98a33a4d98651a96bd5afa86a310 Mon Sep 17 00:00:00 2001 From: Neil Henning Date: Wed, 31 Jan 2024 20:32:49 +0000 Subject: [PATCH] Try run on apple silicon. --- .github/workflows/cmake.yml | 23 +- hashmap.h | 27 +- test/test.inc | 6 + test/utest.h | 735 +++++++++++++++++++++++------------- 4 files changed, 528 insertions(+), 263 deletions(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index 7c772cc..408cb74 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -10,15 +10,20 @@ jobs: build: strategy: matrix: - os: [ubuntu-latest, windows-latest, macOS-latest] + os: [ubuntu-latest, windows-latest, macos-11, macos-14] type: [Debug, RelWithDebInfo, MinSizeRel, Release] - compiler: [default, clang, gcc] + compiler: [default, clang, gcc, tcc] exclude: - - {os: "macOS-latest", compiler: "clang"} - - {os: "windows-latest", compiler: "gcc"} - - {os: "macOS-latest", compiler: "gcc"} + - {os: "macos-11", compiler: "clang"} + - {os: "macos-11", compiler: "gcc"} + - {os: "macos-11", compiler: "tcc"} + - {os: "macos-14", compiler: "clang"} + - {os: "macos-14", compiler: "gcc"} + - {os: "macos-14", compiler: "tcc"} - {os: "ubuntu-latest", compiler: "default"} - {os: "ubuntu-latest", compiler: "default"} + - {os: "windows-latest", compiler: "gcc"} + - {os: "windows-latest", compiler: "tcc"} runs-on: ${{ matrix.os }} steps: @@ -29,7 +34,7 @@ jobs: - name: Setup dependencies if: startsWith(matrix.os, 'ubuntu') - run: sudo apt-get install -y gcc-10 g++-10 clang + run: sudo apt-get install -y gcc-10 g++-10 clang tcc - name: Configure CMake shell: bash @@ -43,6 +48,12 @@ jobs: working-directory: ${{github.workspace}}/build run: cmake $GITHUB_WORKSPACE/test -DCMAKE_BUILD_TYPE=${{ matrix.type }} -DCMAKE_C_COMPILER=gcc-10 -DCMAKE_CXX_COMPILER=g++-10 + - name: Configure CMake with TCC (Ubuntu) + shell: bash + if: matrix.compiler == 'tcc' && startsWith(matrix.os, 'ubuntu') + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE/test -DCMAKE_BUILD_TYPE=${{ matrix.type }} -DCMAKE_C_COMPILER=tcc -DCMAKE_CXX_COMPILER=g++-10 + - name: Configure CMake with Clang (Ubuntu) shell: bash if: (matrix.compiler == 'clang') && startsWith(matrix.os, 'ubuntu') diff --git a/hashmap.h b/hashmap.h index b4aaf57..ee3f526 100644 --- a/hashmap.h +++ b/hashmap.h @@ -81,14 +81,26 @@ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-function" #pragma clang diagnostic ignored "-Wstatic-in-inline" + +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + +#if defined(__TINYC__) +#define HASHMAP_ATTRIBUTE(a) __attribute((a)) +#else +#define HASHMAP_ATTRIBUTE(a) __attribute__((a)) #endif #if defined(_MSC_VER) #define HASHMAP_WEAK __inline -#elif defined(__clang__) || defined(__GNUC__) -#define HASHMAP_WEAK __attribute__((weak)) +#elif defined(__MINGW32__) || defined(__MINGW64__) +#define HASHMAP_WEAK static HASHMAP_ATTRIBUTE(used) +#elif defined(__clang__) || defined(__GNUC__) || defined(__TINYC__) +#define HASHMAP_WEAK HASHMAP_ATTRIBUTE(weak) #else -#error Non clang, non gcc, non MSVC compiler found! +#error Non clang, non gcc, non MSVC, non tcc compiler found! #endif #if defined(_MSC_VER) @@ -701,7 +713,14 @@ HASHMAP_ALWAYS_INLINE hashmap_uint32_t hashmap_clz(const hashmap_uint32_t x) { #if defined(_MSC_VER) unsigned long result; _BitScanReverse(&result, x); - return 31 - HASHMAP_CAST(hashmap_uint32_t, result); + return 31u - HASHMAP_CAST(hashmap_uint32_t, result); +#elif defined(__TINYC__) + union { + double d; + hashmap_uint64_t u; + } u; + u.d = HASHMAP_CAST(double, x) + 0.5; + return 1054u - HASHMAP_CAST(hashmap_uint32_t, u.u >> 52); #else return HASHMAP_CAST(hashmap_uint32_t, __builtin_clz(x)); #endif diff --git a/test/test.inc b/test/test.inc index 46f195a..270e24a 100644 --- a/test/test.inc +++ b/test/test.inc @@ -14,6 +14,12 @@ #define NOEXCEPT #endif +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + #include MY_TEST_WRAPPER(create) { diff --git a/test/utest.h b/test/utest.h index 021f366..073041a 100644 --- a/test/utest.h +++ b/test/utest.h @@ -64,6 +64,12 @@ Disable warning about pointer or reference to potentially throwing function. */ #pragma warning(disable : 5039) + +/* + Disable warning about macro expansion producing 'defined' has undefined + behavior. +*/ +#pragma warning(disable : 5105) #endif #if _MSC_VER > 1930 @@ -94,6 +100,16 @@ typedef uint32_t utest_uint32_t; #include #if defined(__cplusplus) +#if defined(_MSC_VER) && !defined(_CPPUNWIND) +/* We're on MSVC and the compiler is compiling without exception support! */ +#elif !defined(_MSC_VER) && !defined(__EXCEPTIONS) +/* We're on a GCC/Clang compiler that doesn't have exception support! */ +#else +#define UTEST_HAS_EXCEPTIONS 1 +#endif +#endif + +#if defined(UTEST_HAS_EXCEPTIONS) #include #endif @@ -111,6 +127,12 @@ typedef uint32_t utest_uint32_t; #define UTEST_TEST_FAILURE (1) #define UTEST_TEST_SKIPPED (2) +#if defined(__TINYC__) +#define UTEST_ATTRIBUTE(a) __attribute((a)) +#else +#define UTEST_ATTRIBUTE(a) __attribute__((a)) +#endif + #if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__) #if defined(__MINGW64__) || defined(__MINGW32__) @@ -119,6 +141,9 @@ typedef uint32_t utest_uint32_t; #pragma GCC diagnostic ignored "-Wunknown-pragmas" #endif +#if defined(_WINDOWS_) || defined(_WINDOWS_H) +typedef LARGE_INTEGER utest_large_integer; +#else // use old QueryPerformanceCounter definitions (not sure is this needed in some // edge cases or not) on Win7 with VS2015 these extern declaration cause "second // C linkage of overloaded function not allowed" error @@ -142,6 +167,7 @@ UTEST_C_FUNC __declspec(dllimport) int __stdcall QueryPerformanceFrequency( #if defined(__MINGW64__) || defined(__MINGW32__) #pragma GCC diagnostic pop #endif +#endif #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \ defined(__NetBSD__) || defined(__DragonFly__) || defined(__sun__) || \ @@ -170,7 +196,7 @@ UTEST_C_FUNC __declspec(dllimport) int __stdcall QueryPerformanceFrequency( #endif #elif defined(__APPLE__) -#include +#include #endif #if defined(_MSC_VER) && (_MSC_VER < 1920) @@ -254,7 +280,7 @@ UTEST_C_FUNC __declspec(dllimport) int __stdcall QueryPerformanceFrequency( #define UTEST_INLINE inline #define UTEST_INITIALIZER(f) \ - static void f(void) __attribute__((constructor)); \ + static void f(void) UTEST_ATTRIBUTE(constructor); \ static void f(void) #endif @@ -328,7 +354,7 @@ static UTEST_INLINE utest_int64_t utest_ns(void) { #endif return UTEST_CAST(utest_int64_t, ts.tv_sec) * 1000 * 1000 * 1000 + ts.tv_nsec; #elif __APPLE__ - return UTEST_CAST(utest_int64_t, mach_absolute_time()); + return UTEST_CAST(utest_int64_t, clock_gettime_nsec_np(CLOCK_UPTIME_RAW)); #elif __EMSCRIPTEN__ return emscripten_performance_now() * 1000000.0; #else @@ -356,17 +382,17 @@ UTEST_EXTERN struct utest_state_s utest_state; #if defined(_MSC_VER) #define UTEST_WEAK __forceinline #elif defined(__MINGW32__) || defined(__MINGW64__) -#define UTEST_WEAK static __attribute__((used)) -#elif defined(__clang__) || defined(__GNUC__) -#define UTEST_WEAK __attribute__((weak)) +#define UTEST_WEAK static UTEST_ATTRIBUTE(used) +#elif defined(__clang__) || defined(__GNUC__) || defined(__TINYC__) +#define UTEST_WEAK UTEST_ATTRIBUTE(weak) #else -#error Non clang, non gcc, non MSVC compiler found! +#error Non clang, non gcc, non MSVC, non tcc compiler found! #endif #if defined(_MSC_VER) #define UTEST_UNUSED #else -#define UTEST_UNUSED __attribute__((unused)) +#define UTEST_UNUSED UTEST_ATTRIBUTE(unused) #endif #ifdef __clang__ @@ -404,10 +430,125 @@ UTEST_EXTERN struct utest_state_s utest_state; #define UTEST_OVERLOADABLE #elif defined(__clang__) /* otherwise, if we are using clang with c - use the overloadable attribute */ -#define UTEST_OVERLOADABLE __attribute__((overloadable)) +#define UTEST_OVERLOADABLE UTEST_ATTRIBUTE(overloadable) #endif -#if defined(UTEST_OVERLOADABLE) +#if defined(__cplusplus) && (__cplusplus >= 201103L) + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif + +#include + +template ::value> +struct utest_type_deducer final { + static void _(const T t); +}; + +template <> struct utest_type_deducer { + static void _(const signed char c) { + UTEST_PRINTF("%d", static_cast(c)); + } +}; + +template <> struct utest_type_deducer { + static void _(const unsigned char c) { + UTEST_PRINTF("%u", static_cast(c)); + } +}; + +template <> struct utest_type_deducer { + static void _(const short s) { UTEST_PRINTF("%d", static_cast(s)); } +}; + +template <> struct utest_type_deducer { + static void _(const unsigned short s) { + UTEST_PRINTF("%u", static_cast(s)); + } +}; + +template <> struct utest_type_deducer { + static void _(const float f) { UTEST_PRINTF("%f", static_cast(f)); } +}; + +template <> struct utest_type_deducer { + static void _(const double d) { UTEST_PRINTF("%f", d); } +}; + +template <> struct utest_type_deducer { + static void _(const long double d) { +#if defined(__MINGW32__) || defined(__MINGW64__) + /* MINGW is weird - doesn't like LF at all?! */ + UTEST_PRINTF("%f", (double)d); +#else + UTEST_PRINTF("%Lf", d); +#endif + } +}; + +template <> struct utest_type_deducer { + static void _(const int i) { UTEST_PRINTF("%d", i); } +}; + +template <> struct utest_type_deducer { + static void _(const unsigned int i) { UTEST_PRINTF("%u", i); } +}; + +template <> struct utest_type_deducer { + static void _(const long i) { UTEST_PRINTF("%ld", i); } +}; + +template <> struct utest_type_deducer { + static void _(const unsigned long i) { UTEST_PRINTF("%lu", i); } +}; + +template <> struct utest_type_deducer { + static void _(const long long i) { UTEST_PRINTF("%lld", i); } +}; + +template <> struct utest_type_deducer { + static void _(const unsigned long long i) { UTEST_PRINTF("%llu", i); } +}; + +template struct utest_type_deducer { + static void _(const T *t) { + UTEST_PRINTF("%p", static_cast(const_cast(t))); + } +}; + +template struct utest_type_deducer { + static void _(T *t) { UTEST_PRINTF("%p", static_cast(t)); } +}; + +template struct utest_type_deducer { + static void _(const T t) { + UTEST_PRINTF("%llu", static_cast(t)); + } +}; + +template +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(const T t) { + utest_type_deducer::_(t); +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#elif defined(UTEST_OVERLOADABLE) + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(signed char c); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(signed char c) { + UTEST_PRINTF("%d", UTEST_CAST(int, c)); +} + +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned char c); +UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(unsigned char c) { + UTEST_PRINTF("%u", UTEST_CAST(unsigned int, c)); +} + UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f); UTEST_WEAK UTEST_OVERLOADABLE void utest_type_printer(float f) { UTEST_PRINTF("%f", UTEST_CAST(double, f)); @@ -482,7 +623,8 @@ utest_type_printer(long long unsigned int i) { #endif #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \ - !(defined(__MINGW32__) || defined(__MINGW64__)) + !(defined(__MINGW32__) || defined(__MINGW64__)) || \ + defined(__TINYC__) #define utest_type_printer(val) \ UTEST_PRINTF(_Generic((val), signed char \ : "%d", unsigned char \ @@ -510,7 +652,7 @@ utest_type_printer(long long unsigned int i) { #define utest_type_printer(...) UTEST_PRINTF("undef") #endif -#ifdef _MSC_VER +#if defined(_MSC_VER) #define UTEST_SURPRESS_WARNING_BEGIN \ __pragma(warning(push)) __pragma(warning(disable : 4127)) \ __pragma(warning(disable : 4571)) __pragma(warning(disable : 4130)) @@ -549,6 +691,22 @@ utest_type_printer(long long unsigned int i) { #define UTEST_STRNCMP(x, y, size) strncmp(x, y, size) #endif +#if defined(_MSC_VER) +#define UTEST_STRNCPY(x, y, size) strcpy_s(x, size, y) +#elif !defined(__clang__) && defined(__GNUC__) +static UTEST_INLINE char * +utest_strncpy_gcc(char *const dst, const char *const src, const size_t size) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wstringop-overflow" + return strncpy(dst, src, size); +#pragma GCC diagnostic pop +} + +#define UTEST_STRNCPY(x, y, size) utest_strncpy_gcc(x, y, size) +#else +#define UTEST_STRNCPY(x, y, size) strncpy(x, y, size) +#endif + #define UTEST_SKIP(msg) \ do { \ UTEST_PRINTF(" Skipped : '%s'\n", (msg)); \ @@ -557,7 +715,7 @@ utest_type_printer(long long unsigned int i) { } while (0) #if defined(__clang__) -#define UTEST_EXPECT(x, y, cond) \ +#define UTEST_COND(x, y, cond, msg, is_assert) \ UTEST_SURPRESS_WARNING_BEGIN do { \ _Pragma("clang diagnostic push") \ _Pragma("clang diagnostic ignored \"-Wlanguage-extension-token\"") \ @@ -567,7 +725,7 @@ utest_type_printer(long long unsigned int i) { UTEST_AUTO(y) yEval = (y); \ if (!((xEval)cond(yEval))) { \ _Pragma("clang diagnostic pop") \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : ("); \ UTEST_PRINTF(#x ") " #cond " (" #y); \ UTEST_PRINTF(")\n"); \ @@ -576,18 +734,24 @@ utest_type_printer(long long unsigned int i) { UTEST_PRINTF(" vs "); \ utest_type_printer(yEval); \ UTEST_PRINTF("\n"); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ } \ } \ while (0) \ UTEST_SURPRESS_WARNING_END -#elif defined(__GNUC__) -#define UTEST_EXPECT(x, y, cond) \ +#elif defined(__GNUC__) || defined(__TINYC__) +#define UTEST_COND(x, y, cond, msg, is_assert) \ UTEST_SURPRESS_WARNING_BEGIN do { \ UTEST_AUTO(x) xEval = (x); \ UTEST_AUTO(y) yEval = (y); \ if (!((xEval)cond(yEval))) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : ("); \ UTEST_PRINTF(#x ") " #cond " (" #y); \ UTEST_PRINTF(")\n"); \ @@ -596,119 +760,248 @@ utest_type_printer(long long unsigned int i) { UTEST_PRINTF(" vs "); \ utest_type_printer(yEval); \ UTEST_PRINTF("\n"); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ } \ } \ while (0) \ UTEST_SURPRESS_WARNING_END #else -#define UTEST_EXPECT(x, y, cond) \ +#define UTEST_COND(x, y, cond, msg, is_assert) \ UTEST_SURPRESS_WARNING_BEGIN do { \ if (!((x)cond(y))) { \ - UTEST_PRINTF("%s:%u: Failure (Expected " #cond " Actual)\n", __FILE__, \ + UTEST_PRINTF("%s:%i: Failure (Expected " #cond " Actual)", __FILE__, \ __LINE__); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s", msg); \ + } \ + UTEST_PRINTF("\n"); \ *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ } \ } \ while (0) \ UTEST_SURPRESS_WARNING_END #endif -#define EXPECT_TRUE(x) \ +#define EXPECT_EQ(x, y) UTEST_COND(x, y, ==, "", 0) +#define EXPECT_EQ_MSG(x, y, msg) UTEST_COND(x, y, ==, msg, 0) +#define ASSERT_EQ(x, y) UTEST_COND(x, y, ==, "", 1) +#define ASSERT_EQ_MSG(x, y, msg) UTEST_COND(x, y, ==, msg, 1) + +#define EXPECT_NE(x, y) UTEST_COND(x, y, !=, "", 0) +#define EXPECT_NE_MSG(x, y, msg) UTEST_COND(x, y, !=, msg, 0) +#define ASSERT_NE(x, y) UTEST_COND(x, y, !=, "", 1) +#define ASSERT_NE_MSG(x, y, msg) UTEST_COND(x, y, !=, msg, 1) + +#define EXPECT_LT(x, y) UTEST_COND(x, y, <, "", 0) +#define EXPECT_LT_MSG(x, y, msg) UTEST_COND(x, y, <, msg, 0) +#define ASSERT_LT(x, y) UTEST_COND(x, y, <, "", 1) +#define ASSERT_LT_MSG(x, y, msg) UTEST_COND(x, y, <, msg, 1) + +#define EXPECT_LE(x, y) UTEST_COND(x, y, <=, "", 0) +#define EXPECT_LE_MSG(x, y, msg) UTEST_COND(x, y, <=, msg, 0) +#define ASSERT_LE(x, y) UTEST_COND(x, y, <=, "", 1) +#define ASSERT_LE_MSG(x, y, msg) UTEST_COND(x, y, <=, msg, 1) + +#define EXPECT_GT(x, y) UTEST_COND(x, y, >, "", 0) +#define EXPECT_GT_MSG(x, y, msg) UTEST_COND(x, y, >, msg, 0) +#define ASSERT_GT(x, y) UTEST_COND(x, y, >, "", 1) +#define ASSERT_GT_MSG(x, y, msg) UTEST_COND(x, y, >, msg, 1) + +#define EXPECT_GE(x, y) UTEST_COND(x, y, >=, "", 0) +#define EXPECT_GE_MSG(x, y, msg) UTEST_COND(x, y, >=, msg, 0) +#define ASSERT_GE(x, y) UTEST_COND(x, y, >=, "", 1) +#define ASSERT_GE_MSG(x, y, msg) UTEST_COND(x, y, >=, msg, 1) + +#define UTEST_TRUE(x, msg, is_assert) \ UTEST_SURPRESS_WARNING_BEGIN do { \ - if (!(x)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + const int xEval = !!(x); \ + if (!(xEval)) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : true\n"); \ - UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ + UTEST_PRINTF(" Actual : %s\n", (xEval) ? "true" : "false"); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ } \ } \ while (0) \ UTEST_SURPRESS_WARNING_END -#define EXPECT_FALSE(x) \ +#define EXPECT_TRUE(x) UTEST_TRUE(x, "", 0) +#define EXPECT_TRUE_MSG(x, msg) UTEST_TRUE(x, msg, 0) +#define ASSERT_TRUE(x) UTEST_TRUE(x, "", 1) +#define ASSERT_TRUE_MSG(x, msg) UTEST_TRUE(x, msg, 1) + +#define UTEST_FALSE(x, msg, is_assert) \ UTEST_SURPRESS_WARNING_BEGIN do { \ - if (x) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + const int xEval = !!(x); \ + if (xEval) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : false\n"); \ - UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ + UTEST_PRINTF(" Actual : %s\n", (xEval) ? "true" : "false"); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ } \ } \ while (0) \ UTEST_SURPRESS_WARNING_END -#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_FALSE(x) UTEST_FALSE(x, "", 0) +#define EXPECT_FALSE_MSG(x, msg) UTEST_FALSE(x, msg, 0) +#define ASSERT_FALSE(x) UTEST_FALSE(x, "", 1) +#define ASSERT_FALSE_MSG(x, msg) UTEST_FALSE(x, msg, 1) -#define EXPECT_STREQ(x, y) \ +#define UTEST_STREQ(x, y, msg, is_assert) \ UTEST_SURPRESS_WARNING_BEGIN do { \ - if (UTEST_NULL == x || UTEST_NULL == y || 0 != strcmp(x, y)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF(" Actual : \"%s\"\n", y); \ + const char *xEval = (x); \ + const char *yEval = (y); \ + if (UTEST_NULL == xEval || UTEST_NULL == yEval || \ + 0 != strcmp(xEval, yEval)) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : \"%s\"\n", xEval); \ + UTEST_PRINTF(" Actual : \"%s\"\n", yEval); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ } \ } \ while (0) \ UTEST_SURPRESS_WARNING_END -#define EXPECT_STRNE(x, y) \ +#define EXPECT_STREQ(x, y) UTEST_STREQ(x, y, "", 0) +#define EXPECT_STREQ_MSG(x, y, msg) UTEST_STREQ(x, y, msg, 0) +#define ASSERT_STREQ(x, y) UTEST_STREQ(x, y, "", 1) +#define ASSERT_STREQ_MSG(x, y, msg) UTEST_STREQ(x, y, msg, 1) + +#define UTEST_STRNE(x, y, msg, is_assert) \ UTEST_SURPRESS_WARNING_BEGIN do { \ - if (UTEST_NULL == x || UTEST_NULL == y || 0 == strcmp(x, y)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%s\"\n", x); \ - UTEST_PRINTF(" Actual : \"%s\"\n", y); \ + const char *xEval = (x); \ + const char *yEval = (y); \ + if (UTEST_NULL == xEval || UTEST_NULL == yEval || \ + 0 == strcmp(xEval, yEval)) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : \"%s\"\n", xEval); \ + UTEST_PRINTF(" Actual : \"%s\"\n", yEval); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ } \ } \ while (0) \ UTEST_SURPRESS_WARNING_END -#define EXPECT_STRNEQ(x, y, n) \ +#define EXPECT_STRNE(x, y) UTEST_STRNE(x, y, "", 0) +#define EXPECT_STRNE_MSG(x, y, msg) UTEST_STRNE(x, y, msg, 0) +#define ASSERT_STRNE(x, y) UTEST_STRNE(x, y, "", 1) +#define ASSERT_STRNE_MSG(x, y, msg) UTEST_STRNE(x, y, msg, 1) + +#define UTEST_STRNEQ(x, y, n, msg, is_assert) \ UTEST_SURPRESS_WARNING_BEGIN do { \ - if (UTEST_NULL == x || UTEST_NULL == y || 0 != UTEST_STRNCMP(x, y, n)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%.*s\"\n", UTEST_CAST(int, n), x); \ - UTEST_PRINTF(" Actual : \"%.*s\"\n", UTEST_CAST(int, n), y); \ + const char *xEval = (x); \ + const char *yEval = (y); \ + const size_t nEval = UTEST_CAST(size_t, n); \ + if (UTEST_NULL == xEval || UTEST_NULL == yEval || \ + 0 != UTEST_STRNCMP(xEval, yEval, nEval)) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : \"%.*s\"\n", UTEST_CAST(int, nEval), xEval); \ + UTEST_PRINTF(" Actual : \"%.*s\"\n", UTEST_CAST(int, nEval), yEval); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ } \ } \ while (0) \ UTEST_SURPRESS_WARNING_END -#define EXPECT_STRNNE(x, y, n) \ +#define EXPECT_STRNEQ(x, y, n) UTEST_STRNEQ(x, y, n, "", 0) +#define EXPECT_STRNEQ_MSG(x, y, n, msg) UTEST_STRNEQ(x, y, n, msg, 0) +#define ASSERT_STRNEQ(x, y, n) UTEST_STRNEQ(x, y, n, "", 1) +#define ASSERT_STRNEQ_MSG(x, y, n, msg) UTEST_STRNEQ(x, y, n, msg, 1) + +#define UTEST_STRNNE(x, y, n, msg, is_assert) \ UTEST_SURPRESS_WARNING_BEGIN do { \ - if (UTEST_NULL == x || UTEST_NULL == y || 0 == UTEST_STRNCMP(x, y, n)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%.*s\"\n", UTEST_CAST(int, n), x); \ - UTEST_PRINTF(" Actual : \"%.*s\"\n", UTEST_CAST(int, n), y); \ + const char *xEval = (x); \ + const char *yEval = (y); \ + const size_t nEval = UTEST_CAST(size_t, n); \ + if (UTEST_NULL == xEval || UTEST_NULL == yEval || \ + 0 == UTEST_STRNCMP(xEval, yEval, nEval)) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : \"%.*s\"\n", UTEST_CAST(int, nEval), xEval); \ + UTEST_PRINTF(" Actual : \"%.*s\"\n", UTEST_CAST(int, nEval), yEval); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ } \ } \ while (0) \ UTEST_SURPRESS_WARNING_END -#define EXPECT_NEAR(x, y, epsilon) \ +#define EXPECT_STRNNE(x, y, n) UTEST_STRNNE(x, y, n, "", 0) +#define EXPECT_STRNNE_MSG(x, y, n, msg) UTEST_STRNNE(x, y, n, msg, 0) +#define ASSERT_STRNNE(x, y, n) UTEST_STRNNE(x, y, n, "", 1) +#define ASSERT_STRNNE_MSG(x, y, n, msg) UTEST_STRNNE(x, y, n, msg, 1) + +#define UTEST_NEAR(x, y, epsilon, msg, is_assert) \ UTEST_SURPRESS_WARNING_BEGIN do { \ const double diff = \ utest_fabs(UTEST_CAST(double, x) - UTEST_CAST(double, y)); \ if (diff > UTEST_CAST(double, epsilon) || utest_isnan(diff)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : %f\n", UTEST_CAST(double, x)); \ UTEST_PRINTF(" Actual : %f\n", UTEST_CAST(double, y)); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ *utest_result = UTEST_TEST_FAILURE; \ + if (is_assert) { \ + return; \ + } \ } \ } \ while (0) \ UTEST_SURPRESS_WARNING_END -#if defined(__cplusplus) -#define EXPECT_EXCEPTION(x, exception_type) \ +#define EXPECT_NEAR(x, y, epsilon) UTEST_NEAR(x, y, epsilon, "", 0) +#define EXPECT_NEAR_MSG(x, y, epsilon, msg) UTEST_NEAR(x, y, epsilon, msg, 0) +#define ASSERT_NEAR(x, y, epsilon) UTEST_NEAR(x, y, epsilon, "", 1) +#define ASSERT_NEAR_MSG(x, y, epsilon, msg) UTEST_NEAR(x, y, epsilon, msg, 1) + +#if defined(UTEST_HAS_EXCEPTIONS) +#define UTEST_EXCEPTION(x, exception_type, msg, is_assert) \ UTEST_SURPRESS_WARNING_BEGIN do { \ int exception_caught = 0; \ try { \ @@ -718,206 +1011,117 @@ utest_type_printer(long long unsigned int i) { } catch (...) { \ exception_caught = 2; \ } \ - if (exception_caught != 1) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + if (1 != exception_caught) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : %s exception\n", #exception_type); \ - UTEST_PRINTF(" Actual : %s\n", (exception_caught == 2) \ + UTEST_PRINTF(" Actual : %s\n", (2 == exception_caught) \ ? "Unexpected exception" \ : "No exception"); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ *utest_result = UTEST_TEST_FAILURE; \ - } \ - } \ - while (0) \ - UTEST_SURPRESS_WARNING_END -#endif - -#if defined(__clang__) -#define UTEST_ASSERT(x, y, cond) \ - UTEST_SURPRESS_WARNING_BEGIN do { \ - _Pragma("clang diagnostic push") \ - _Pragma("clang diagnostic ignored \"-Wlanguage-extension-token\"") \ - _Pragma("clang diagnostic ignored \"-Wc++98-compat-pedantic\"") \ - _Pragma("clang diagnostic ignored \"-Wfloat-equal\"") \ - UTEST_AUTO(x) xEval = (x); \ - UTEST_AUTO(y) yEval = (y); \ - if (!((xEval)cond(yEval))) { \ - _Pragma("clang diagnostic pop") \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : ("); \ - UTEST_PRINTF(#x ") " #cond " (" #y); \ - UTEST_PRINTF(")\n"); \ - UTEST_PRINTF(" Actual : "); \ - utest_type_printer(xEval); \ - UTEST_PRINTF(" vs "); \ - utest_type_printer(yEval); \ - UTEST_PRINTF("\n"); \ - *utest_result = UTEST_TEST_FAILURE; \ - return; \ - } \ - } \ - while (0) \ - UTEST_SURPRESS_WARNING_END -#elif defined(__GNUC__) -#define UTEST_ASSERT(x, y, cond) \ - UTEST_SURPRESS_WARNING_BEGIN do { \ - UTEST_AUTO(x) xEval = (x); \ - UTEST_AUTO(y) yEval = (y); \ - if (!((xEval)cond(yEval))) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : ("); \ - UTEST_PRINTF(#x ") " #cond " (" #y); \ - UTEST_PRINTF(")\n"); \ - UTEST_PRINTF(" Actual : "); \ - utest_type_printer(xEval); \ - UTEST_PRINTF(" vs "); \ - utest_type_printer(yEval); \ - UTEST_PRINTF("\n"); \ - *utest_result = UTEST_TEST_FAILURE; \ - return; \ - } \ - } \ - while (0) \ - UTEST_SURPRESS_WARNING_END -#else -#define UTEST_ASSERT(x, y, cond) \ - UTEST_SURPRESS_WARNING_BEGIN do { \ - if (!((x)cond(y))) { \ - UTEST_PRINTF("%s:%u: Failure (Expected " #cond " Actual)\n", __FILE__, \ - __LINE__); \ - *utest_result = UTEST_TEST_FAILURE; \ - return; \ - } \ - } \ - while (0) \ - UTEST_SURPRESS_WARNING_END -#endif - -#define ASSERT_TRUE(x) \ - UTEST_SURPRESS_WARNING_BEGIN do { \ - if (!(x)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : true\n"); \ - UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ - *utest_result = UTEST_TEST_FAILURE; \ - return; \ + if (is_assert) { \ + return; \ + } \ } \ } \ while (0) \ UTEST_SURPRESS_WARNING_END -#define ASSERT_FALSE(x) \ - UTEST_SURPRESS_WARNING_BEGIN do { \ - if (x) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : false\n"); \ - UTEST_PRINTF(" Actual : %s\n", (x) ? "true" : "false"); \ - *utest_result = UTEST_TEST_FAILURE; \ - return; \ - } \ - } \ - while (0) \ - UTEST_SURPRESS_WARNING_END - -#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) \ - UTEST_SURPRESS_WARNING_BEGIN do { \ - if (UTEST_NULL == x || UTEST_NULL == y || 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_result = UTEST_TEST_FAILURE; \ - return; \ - } \ - } \ - while (0) \ - UTEST_SURPRESS_WARNING_END - -#define ASSERT_STRNE(x, y) \ - UTEST_SURPRESS_WARNING_BEGIN do { \ - if (UTEST_NULL == x || UTEST_NULL == y || 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_result = UTEST_TEST_FAILURE; \ - return; \ - } \ - } \ - while (0) \ - UTEST_SURPRESS_WARNING_END - -#define ASSERT_STRNEQ(x, y, n) \ - UTEST_SURPRESS_WARNING_BEGIN do { \ - if (UTEST_NULL == x || UTEST_NULL == y || 0 != UTEST_STRNCMP(x, y, n)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%.*s\"\n", UTEST_CAST(int, n), x); \ - UTEST_PRINTF(" Actual : \"%.*s\"\n", UTEST_CAST(int, n), y); \ - *utest_result = UTEST_TEST_FAILURE; \ - return; \ - } \ - } \ - while (0) \ - UTEST_SURPRESS_WARNING_END - -#define ASSERT_STRNNE(x, y, n) \ - UTEST_SURPRESS_WARNING_BEGIN do { \ - if (UTEST_NULL == x || UTEST_NULL == y || 0 == UTEST_STRNCMP(x, y, n)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : \"%.*s\"\n", UTEST_CAST(int, n), x); \ - UTEST_PRINTF(" Actual : \"%.*s\"\n", UTEST_CAST(int, n), y); \ - *utest_result = UTEST_TEST_FAILURE; \ - return; \ - } \ - } \ - while (0) \ - UTEST_SURPRESS_WARNING_END - -#define ASSERT_NEAR(x, y, epsilon) \ - UTEST_SURPRESS_WARNING_BEGIN do { \ - const double diff = \ - utest_fabs(UTEST_CAST(double, x) - UTEST_CAST(double, y)); \ - if (diff > UTEST_CAST(double, epsilon) || utest_isnan(diff)) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ - UTEST_PRINTF(" Expected : %f\n", UTEST_CAST(double, x)); \ - UTEST_PRINTF(" Actual : %f\n", UTEST_CAST(double, y)); \ - *utest_result = UTEST_TEST_FAILURE; \ - return; \ - } \ - } \ - while (0) \ - UTEST_SURPRESS_WARNING_END - -#if defined(__cplusplus) +#define EXPECT_EXCEPTION(x, exception_type) \ + UTEST_EXCEPTION(x, exception_type, "", 0) +#define EXPECT_EXCEPTION_MSG(x, exception_type, msg) \ + UTEST_EXCEPTION(x, exception_type, msg, 0) #define ASSERT_EXCEPTION(x, exception_type) \ + UTEST_EXCEPTION(x, exception_type, "", 1) +#define ASSERT_EXCEPTION_MSG(x, exception_type, msg) \ + UTEST_EXCEPTION(x, exception_type, msg, 1) + +#define UTEST_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message, \ + msg, is_assert) \ UTEST_SURPRESS_WARNING_BEGIN do { \ int exception_caught = 0; \ + char *message_caught = UTEST_NULL; \ try { \ x; \ - } catch (const exception_type &) { \ + } catch (const exception_type &e) { \ + const char *const what = e.what(); \ exception_caught = 1; \ + if (0 != \ + UTEST_STRNCMP(what, exception_message, strlen(exception_message))) { \ + const size_t message_size = strlen(what) + 1; \ + message_caught = UTEST_PTR_CAST(char *, malloc(message_size)); \ + UTEST_STRNCPY(message_caught, what, message_size); \ + } \ } catch (...) { \ exception_caught = 2; \ } \ - if (exception_caught != 1) { \ - UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__); \ + if (1 != exception_caught) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ UTEST_PRINTF(" Expected : %s exception\n", #exception_type); \ - UTEST_PRINTF(" Actual : %s\n", (exception_caught == 2) \ + UTEST_PRINTF(" Actual : %s\n", (2 == exception_caught) \ ? "Unexpected exception" \ : "No exception"); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ *utest_result = UTEST_TEST_FAILURE; \ - return; \ + if (is_assert) { \ + return; \ + } \ + } else if (UTEST_NULL != message_caught) { \ + UTEST_PRINTF("%s:%i: Failure\n", __FILE__, __LINE__); \ + UTEST_PRINTF(" Expected : %s exception with message %s\n", \ + #exception_type, exception_message); \ + UTEST_PRINTF(" Actual message : %s\n", message_caught); \ + if (strlen(msg) > 0) { \ + UTEST_PRINTF(" Message : %s\n", msg); \ + } \ + *utest_result = UTEST_TEST_FAILURE; \ + free(message_caught); \ + if (is_assert) { \ + return; \ + } \ } \ } \ while (0) \ UTEST_SURPRESS_WARNING_END + +#define EXPECT_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message) \ + UTEST_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message, "", 0) +#define EXPECT_EXCEPTION_WITH_MESSAGE_MSG(x, exception_type, \ + exception_message, msg) \ + UTEST_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message, msg, 0) +#define ASSERT_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message) \ + UTEST_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message, "", 1) +#define ASSERT_EXCEPTION_WITH_MESSAGE_MSG(x, exception_type, \ + exception_message, msg) \ + UTEST_EXCEPTION_WITH_MESSAGE(x, exception_type, exception_message, msg, 1) +#endif + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#define UTEST_SURPRESS_WARNINGS_BEGIN \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wunsafe-buffer-usage\"") +#define UTEST_SURPRESS_WARNINGS_END _Pragma("clang diagnostic pop") +#else +#define UTEST_SURPRESS_WARNINGS_BEGIN +#define UTEST_SURPRESS_WARNINGS_END +#endif +#elif defined(__GNUC__) && __GNUC__ >= 8 && defined(__cplusplus) +#define UTEST_SURPRESS_WARNINGS_BEGIN \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wclass-memaccess\"") +#define UTEST_SURPRESS_WARNINGS_END _Pragma("GCC diagnostic pop") +#else +#define UTEST_SURPRESS_WARNINGS_BEGIN +#define UTEST_SURPRESS_WARNINGS_END #endif #define UTEST(SET, NAME) \ + UTEST_SURPRESS_WARNINGS_BEGIN \ 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) { \ @@ -926,7 +1130,7 @@ utest_type_printer(long long unsigned int i) { } \ UTEST_INITIALIZER(utest_register_##SET##_##NAME) { \ const size_t index = utest_state.tests_length++; \ - const char *name_part = #SET "." #NAME; \ + const char name_part[] = #SET "." #NAME; \ const size_t name_size = strlen(name_part) + 1; \ char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ utest_state.tests = UTEST_PTR_CAST( \ @@ -938,9 +1142,12 @@ utest_type_printer(long long unsigned int i) { utest_state.tests[index].func = &utest_##SET##_##NAME; \ utest_state.tests[index].name = name; \ utest_state.tests[index].index = 0; \ + UTEST_SNPRINTF(name, name_size, "%s", name_part); \ + } else if (name) { \ + free(name); \ } \ - UTEST_SNPRINTF(name, name_size, "%s", name_part); \ } \ + UTEST_SURPRESS_WARNINGS_END \ void utest_run_##SET##_##NAME(int *utest_result) #define UTEST_F_SETUP(FIXTURE) \ @@ -951,18 +1158,8 @@ utest_type_printer(long long unsigned int i) { static void utest_f_teardown_##FIXTURE(int *utest_result, \ struct FIXTURE *utest_fixture) -#if defined(__GNUC__) && __GNUC__ >= 8 && defined(__cplusplus) -#define UTEST_FIXTURE_SURPRESS_WARNINGS_BEGIN \ - _Pragma("GCC diagnostic push") \ - _Pragma("GCC diagnostic ignored \"-Wclass-memaccess\"") -#define UTEST_FIXTURE_SURPRESS_WARNINGS_END _Pragma("GCC diagnostic pop") -#else -#define UTEST_FIXTURE_SURPRESS_WARNINGS_BEGIN -#define UTEST_FIXTURE_SURPRESS_WARNINGS_END -#endif - #define UTEST_F(FIXTURE, NAME) \ - UTEST_FIXTURE_SURPRESS_WARNINGS_BEGIN \ + UTEST_SURPRESS_WARNINGS_BEGIN \ UTEST_EXTERN struct utest_state_s utest_state; \ static void utest_f_setup_##FIXTURE(int *, struct FIXTURE *); \ static void utest_f_teardown_##FIXTURE(int *, struct FIXTURE *); \ @@ -981,7 +1178,7 @@ utest_type_printer(long long unsigned int i) { } \ UTEST_INITIALIZER(utest_register_##FIXTURE##_##NAME) { \ const size_t index = utest_state.tests_length++; \ - const char *name_part = #FIXTURE "." #NAME; \ + const char name_part[] = #FIXTURE "." #NAME; \ const size_t name_size = strlen(name_part) + 1; \ char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ utest_state.tests = UTEST_PTR_CAST( \ @@ -989,11 +1186,15 @@ utest_type_printer(long long unsigned int i) { utest_realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ sizeof(struct utest_test_state_s) * \ utest_state.tests_length)); \ - utest_state.tests[index].func = &utest_f_##FIXTURE##_##NAME; \ - utest_state.tests[index].name = name; \ - UTEST_SNPRINTF(name, name_size, "%s", name_part); \ + if (utest_state.tests) { \ + utest_state.tests[index].func = &utest_f_##FIXTURE##_##NAME; \ + utest_state.tests[index].name = name; \ + UTEST_SNPRINTF(name, name_size, "%s", name_part); \ + } else if (name) { \ + free(name); \ + } \ } \ - UTEST_FIXTURE_SURPRESS_WARNINGS_END \ + UTEST_SURPRESS_WARNINGS_END \ void utest_run_##FIXTURE##_##NAME(int *utest_result, \ struct FIXTURE *utest_fixture) @@ -1006,6 +1207,7 @@ utest_type_printer(long long unsigned int i) { int *utest_result, struct FIXTURE *utest_fixture, size_t utest_index) #define UTEST_I(FIXTURE, NAME, INDEX) \ + UTEST_SURPRESS_WARNINGS_BEGIN \ UTEST_EXTERN struct utest_state_s utest_state; \ static void utest_run_##FIXTURE##_##NAME##_##INDEX(int *, struct FIXTURE *); \ static void utest_i_##FIXTURE##_##NAME##_##INDEX(int *utest_result, \ @@ -1024,7 +1226,7 @@ utest_type_printer(long long unsigned int i) { utest_uint64_t iUp; \ for (i = 0; i < (INDEX); i++) { \ const size_t index = utest_state.tests_length++; \ - const char *name_part = #FIXTURE "." #NAME; \ + const char name_part[] = #FIXTURE "." #NAME; \ const size_t name_size = strlen(name_part) + 32; \ char *name = UTEST_PTR_CAST(char *, malloc(name_size)); \ utest_state.tests = UTEST_PTR_CAST( \ @@ -1032,16 +1234,26 @@ utest_type_printer(long long unsigned int i) { utest_realloc(UTEST_PTR_CAST(void *, utest_state.tests), \ sizeof(struct utest_test_state_s) * \ utest_state.tests_length)); \ - utest_state.tests[index].func = &utest_i_##FIXTURE##_##NAME##_##INDEX; \ - utest_state.tests[index].index = i; \ - utest_state.tests[index].name = name; \ - iUp = UTEST_CAST(utest_uint64_t, i); \ - UTEST_SNPRINTF(name, name_size, "%s/%" UTEST_PRIu64, name_part, iUp); \ + if (utest_state.tests) { \ + utest_state.tests[index].func = &utest_i_##FIXTURE##_##NAME##_##INDEX; \ + utest_state.tests[index].index = i; \ + utest_state.tests[index].name = name; \ + iUp = UTEST_CAST(utest_uint64_t, i); \ + UTEST_SNPRINTF(name, name_size, "%s/%" UTEST_PRIu64, name_part, iUp); \ + } else if (name) { \ + free(name); \ + } \ } \ } \ + UTEST_SURPRESS_WARNINGS_END \ void utest_run_##FIXTURE##_##NAME##_##INDEX(int *utest_result, \ struct FIXTURE *utest_fixture) +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif + UTEST_WEAK double utest_fabs(double d); UTEST_WEAK @@ -1068,6 +1280,17 @@ int utest_isnan(double d) { return both.u > 0x7ff0000000000000u; } +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" +#endif +#endif + UTEST_WEAK int utest_should_filter_test(const char *filter, const char *testcase); UTEST_WEAK int utest_should_filter_test(const char *filter, @@ -1299,7 +1522,7 @@ int utest_main(int argc, const char *const argv[]) { ns = utest_ns(); errno = 0; -#if defined(__cplusplus) +#if defined(UTEST_HAS_EXCEPTIONS) UTEST_SURPRESS_WARNING_BEGIN try { utest_state.tests[index].func(&result, utest_state.tests[index].index); @@ -1415,6 +1638,12 @@ int utest_main(int argc, const char *const argv[]) { return UTEST_CAST(int, failed); } +#if defined(__clang__) +#if __has_warning("-Wunsafe-buffer-usage") +#pragma clang diagnostic pop +#endif +#endif + /* we need, in exactly one source file, define the global struct that will hold the data we need to run utest. This macro allows the user to declare the