From 1b4c6cf763ac832965fd25d7900deb1fcc538a7b Mon Sep 17 00:00:00 2001 From: Sofi Date: Sun, 22 Oct 2023 01:46:01 +0200 Subject: [PATCH 1/3] fix: do not use named variadic macros for TEST_CASE Named variadic macros are explicitly a GNU extension and not part of ANSI-C. This was found using clang's `-Wvariadic-macros` warning. --- include/mtest.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/mtest.h b/include/mtest.h index 92f240e..8b1237c 100644 --- a/include/mtest.h +++ b/include/mtest.h @@ -63,10 +63,10 @@ #define REQUIRE_NE_STRING(expected, actual) REQUIRE_(strcmp((expected), (actual)), "REQUIRE_NE_STRING(%s,%s) failed (\"%s\" == \"%s\")", #expected, #actual, expected, actual) // A test case is implemented as a function that returns 0 if the test passes and 1 if it fails. -#define TEST_CASE(test_name, command...) \ +#define TEST_CASE(test_name, ...) \ static int test_name(void) { \ int _mtest_result_ = 0; \ - { command } \ + { __VA_ARGS__ } \ return _mtest_result_; \ } From 337a79dbb4f044bab84262fd87bb8d1153ae3744 Mon Sep 17 00:00:00 2001 From: Sofi Date: Sun, 22 Oct 2023 01:48:31 +0200 Subject: [PATCH 2/3] fix: implicit comparison of differently signed ints Makes the conversion from unsigned long from doing division into an explicit cast to int. This was found using clang's `-Wsign-compare` warning. --- include/mtest.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/mtest.h b/include/mtest.h index 8b1237c..f9d3da6 100644 --- a/include/mtest.h +++ b/include/mtest.h @@ -75,7 +75,7 @@ static int test_name(void) { #define RUN_TESTS(...) do { \ int success = 0, failed = 0; \ int(*test_functions[])(void) = {__VA_ARGS__}; \ - for (int i = 0; i < sizeof(test_functions)/sizeof(int(*)(void)); ++i) \ + for (int i = 0; i < (int)(sizeof(test_functions)/sizeof(int(*)(void))); ++i) \ test_functions[i]() ? failed++ : success++; \ printf("Tests run: %d. Succeeded: %d. Failed: %d", success+failed, success, failed); \ if (failed) exit(EXIT_FAILURE); \ @@ -94,7 +94,7 @@ int main (int argc, char *argv[]) { char* names = (char *)malloc(strlen(names_static)+1); \ strcpy(names, names_static); \ char* token = strtok(names, " ,"); \ - for (int i = 0; token != NULL && i < sizeof(test_functions)/sizeof(int(*)(void)); ++i) { \ + for (int i = 0; token != NULL && i < (int)(sizeof(test_functions)/sizeof(int(*)(void))); ++i) { \ if (list_test_cases) { \ printf("%s;", token); \ } else if (strcmp(argv[1], token) == 0) { \ From 08dc4356d3fcd4b169d334a71ca331a951ad9f7b Mon Sep 17 00:00:00 2001 From: Sofi Date: Sun, 22 Oct 2023 02:44:21 +0200 Subject: [PATCH 3/3] feat: set strict compile options for test_mtest Ensure that the compiler does not build test_mtest with implicit defaults. --- test/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c43b3ed..f5ae776 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,7 @@ add_executable(test_mtest test_mtest.c) target_link_libraries(test_mtest mtest) +# Avoid relying on hidden compiler behavior when compiling test_mtest. +target_compile_options(test_mtest PRIVATE -Wall -Wextra -Wpedantic) # Automatically discover test cases in test_mtest and add them to CTest. discover_tests(test_mtest)