diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1bfc1dc..b911fba 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -37,11 +37,11 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") set_source_files_properties(main.c test.c PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(main.c test.c PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(main.c test.c PROPERTIES diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1bfc1dc..b911fba 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -37,11 +37,11 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") set_source_files_properties(main.c test.c PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(main.c test.c PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(main.c test.c PROPERTIES diff --git a/test/test.c b/test/test.c index 74c2327..4766e74 100644 --- a/test/test.c +++ b/test/test.c @@ -1,32 +1,34 @@ -// This is free and unencumbered software released into the public domain. -// -// Anyone is free to copy, modify, publish, use, compile, sell, or -// distribute this software, either in source code form or as a compiled -// binary, for any purpose, commercial or non-commercial, and by any -// means. -// -// In jurisdictions that recognize copyright laws, the author or authors -// of this software dedicate any and all copyright interest in the -// software to the public domain. We make this dedication for the benefit -// of the public at large and to the detriment of our heirs and -// successors. We intend this dedication to be an overt act of -// relinquishment in perpetuity of all present and future rights to this -// software under copyright law. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// -// For more information, please refer to +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to +*/ #include "utest.h" #ifdef _MSC_VER -// disable 'conditional expression is constant' - our examples below use this! +/* disable 'conditional expression is constant' - our examples below use this! */ #pragma warning(disable : 4127) #pragma #endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1bfc1dc..b911fba 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -37,11 +37,11 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") set_source_files_properties(main.c test.c PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Werror" + COMPILE_FLAGS "-Wall -Wextra -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") set_source_files_properties(main.c test.c PROPERTIES - COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror" + COMPILE_FLAGS "-Wall -Wextra -Weverything -Werror -std=gnu89" ) elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") set_source_files_properties(main.c test.c PROPERTIES diff --git a/test/test.c b/test/test.c index 74c2327..4766e74 100644 --- a/test/test.c +++ b/test/test.c @@ -1,32 +1,34 @@ -// This is free and unencumbered software released into the public domain. -// -// Anyone is free to copy, modify, publish, use, compile, sell, or -// distribute this software, either in source code form or as a compiled -// binary, for any purpose, commercial or non-commercial, and by any -// means. -// -// In jurisdictions that recognize copyright laws, the author or authors -// of this software dedicate any and all copyright interest in the -// software to the public domain. We make this dedication for the benefit -// of the public at large and to the detriment of our heirs and -// successors. We intend this dedication to be an overt act of -// relinquishment in perpetuity of all present and future rights to this -// software under copyright law. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// -// For more information, please refer to +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to +*/ #include "utest.h" #ifdef _MSC_VER -// disable 'conditional expression is constant' - our examples below use this! +/* disable 'conditional expression is constant' - our examples below use this! */ #pragma warning(disable : 4127) #pragma #endif diff --git a/utest.h b/utest.h index 17b9315..bfc1b0e 100644 --- a/utest.h +++ b/utest.h @@ -1,42 +1,50 @@ -// The latest version of this library is available on GitHub; -// https://github.com/sheredom/utest.h +/* + The latest version of this library is available on GitHub; + https://github.com/sheredom/utest.h +*/ -// This is free and unencumbered software released into the public domain. -// -// Anyone is free to copy, modify, publish, use, compile, sell, or -// distribute this software, either in source code form or as a compiled -// binary, for any purpose, commercial or non-commercial, and by any -// means. -// -// In jurisdictions that recognize copyright laws, the author or authors -// of this software dedicate any and all copyright interest in the -// software to the public domain. We make this dedication for the benefit -// of the public at large and to the detriment of our heirs and -// successors. We intend this dedication to be an overt act of -// relinquishment in perpetuity of all present and future rights to this -// software under copyright law. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// -// For more information, please refer to +/* + This is free and unencumbered software released into the public domain. + + Anyone is free to copy, modify, publish, use, compile, sell, or + distribute this software, either in source code form or as a compiled + binary, for any purpose, commercial or non-commercial, and by any + means. + + In jurisdictions that recognize copyright laws, the author or authors + of this software dedicate any and all copyright interest in the + software to the public domain. We make this dedication for the benefit + of the public at large and to the detriment of our heirs and + successors. We intend this dedication to be an overt act of + relinquishment in perpetuity of all present and future rights to this + software under copyright law. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + For more information, please refer to +*/ #ifndef SHEREDOM_UTEST_H_INCLUDED #define SHEREDOM_UTEST_H_INCLUDED #ifdef _MSC_VER -// Disable warning about not inlining 'inline' functions. -// TODO: We'll fix this later by not using fprintf within our macros, and -// instead use snprintf to a realloc'ed buffer. +/* + Disable warning about not inlining 'inline' functions. + TODO: We'll fix this later by not using fprintf within our macros, and + instead use snprintf to a realloc'ed buffer. +*/ #pragma warning(disable : 4710) -// Disable warning about inlining functions that are not marked 'inline'. -// TODO: add a UTEST_NOINLINE onto the macro generated functions to fix this. +/* + Disable warning about inlining functions that are not marked 'inline'. + TODO: add a UTEST_NOINLINE onto the macro generated functions to fix this. +*/ #pragma warning(disable : 4711) #pragma warning(push, 1) #endif @@ -61,29 +69,31 @@ #endif #pragma warning(push, 1) -#include #include +#include #pragma warning(pop) #elif defined(__linux__) -// slightly obscure include here - we need to include glibc's features.h, but -// we don't want to just include a header that might not be defined for other -// c libraries like musl. Instead we include limits.h, which we know on all -// glibc distributions includes features.h +/* + slightly obscure include here - we need to include glibc's features.h, but + we don't want to just include a header that might not be defined for other + c libraries like musl. Instead we include limits.h, which we know on all + glibc distributions includes features.h +*/ #include #if defined(__GLIBC__) && defined(__GLIBC_MINOR__) #include #if ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__))) -// glibc is version 2.17 or above, so we can just use clock_gettime +/* glibc is version 2.17 or above, so we can just use clock_gettime */ #define UTEST_USE_CLOCKGETTIME -#else // ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__))) +#else #include #include -#endif // ((2 < __GLIBC__) || ((2 == __GLIBC__) && (17 <= __GLIBC_MINOR__))) -#endif // defined(__GLIBC__) && defined(__GLIBC_MINOR__) +#endif +#endif #elif defined(__APPLE__) #include @@ -174,7 +184,7 @@ FILE *output; }; -// extern to the global state utest needs to execute +/* extern to the global state utest needs to execute */ UTEST_EXTERN struct utest_state_s utest_state; #if defined(_MSC_VER) @@ -221,10 +231,10 @@ #endif #if defined(__cplusplus) -// if we are using c++ we can use overloaded methods (its in the language) +/* if we are using c++ we can use overloaded methods (its in the language) */ #define UTEST_OVERLOADABLE #elif defined(__clang__) -// otherwise, if we are using clang with c we can use the overloadable attribute +/* otherwise, if we are using clang with c - use the overloadable attribute */ #define UTEST_OVERLOADABLE __attribute__((overloadable)) #endif @@ -264,9 +274,11 @@ UTEST_PRINTF1("%lu", i); } -// long long is a c++11 extension -// TODO: grok for c++11 version here -#if !defined(__cplusplus) +/* + long long is a c++11 extension + TODO: grok for c++11 version here +*/ +#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); @@ -279,8 +291,10 @@ } #endif #else -// 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 +/* + 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") #endif @@ -306,12 +320,12 @@ *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)) { \ @@ -347,12 +361,12 @@ 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); \ @@ -370,7 +384,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) { \ @@ -385,7 +399,7 @@ utest_state.tests_length)); \ utest_state.tests[index].func = &utest_##SET##_##NAME; \ utest_state.tests[index].name = name; \ - UTEST_SNPRINTF(name, name_size, "%s", name_part); \ + UTEST_SNPRINTF(name, name_size, "%s", name_part); \ } \ void utest_run_##SET##_##NAME(int *utest_result) @@ -399,11 +413,13 @@ #define UTEST_F(FIXTURE, NAME) \ 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 *); \ static void utest_run_##FIXTURE##_##NAME(int *, struct FIXTURE *); \ static void utest_f_##FIXTURE##_##NAME(int *utest_result, \ size_t utest_index) { \ - (void) utest_index; \ struct FIXTURE fixture; \ + (void)utest_index; \ memset(&fixture, 0, sizeof(fixture)); \ utest_f_setup_##FIXTURE(utest_result, &fixture); \ if (0 != *utest_result) { \ @@ -424,7 +440,7 @@ 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); \ + UTEST_SNPRINTF(name, name_size, "%s", name_part); \ } \ void utest_run_##FIXTURE##_##NAME(int *utest_result, \ struct FIXTURE *utest_fixture) @@ -466,8 +482,8 @@ utest_state.tests[index].func = &utest_i_##FIXTURE##_##NAME##_##INDEX; \ utest_state.tests[index].index = i; \ utest_state.tests[index].name = name; \ - UTEST_SNPRINTF(name, name_size, "%s/%" UTEST_PRIu64, name_part, \ - UTEST_CAST(uint64_t, i)); \ + UTEST_SNPRINTF(name, name_size, "%s/%" UTEST_PRIu64, name_part, \ + UTEST_CAST(uint64_t, i)); \ } \ } \ void utest_run_##FIXTURE##_##NAME##_##INDEX(int *utest_result, \ @@ -484,27 +500,29 @@ while (('\0' != *filter_cur) && ('\0' != *testcase_cur)) { if ('*' == *filter_cur) { - // store the position of the wildcard + /* store the position of the wildcard */ filter_wildcard = filter_cur; - // skip the wildcard character + /* skip the wildcard character */ filter_cur++; while (('\0' != *filter_cur) && ('\0' != *testcase_cur)) { if ('*' == *filter_cur) { - // we found another wildcard (filter is something like *foo*) so we - // exit the current loop, and return to the parent loop to handle - // the wildcard case + /* + we found another wildcard (filter is something like *foo*) so we + exit the current loop, and return to the parent loop to handle + the wildcard case + */ break; } else if (*filter_cur != *testcase_cur) { - // otherwise our filter didn't match, so reset it + /* otherwise our filter didn't match, so reset it */ filter_cur = filter_wildcard; } - // move testcase along + /* move testcase along */ testcase_cur++; - // move filter along + /* move filter along */ filter_cur++; } @@ -512,16 +530,16 @@ return 0; } - // if the testcase has been exhausted, we don't have a match! + /* if the testcase has been exhausted, we don't have a match! */ if ('\0' == *testcase_cur) { return 1; } } else { if (*testcase_cur != *filter_cur) { - // test case doesn't match filter + /* test case doesn't match filter */ return 1; } else { - // move our filter and testcase forward + /* move our filter and testcase forward */ testcase_cur++; filter_cur++; } @@ -531,7 +549,7 @@ if (('\0' != *filter_cur) || (('\0' != *testcase_cur) && ((filter == filter_cur) || ('*' != filter_cur[-1])))) { - // we have a mismatch! + /* we have a mismatch! */ return 1; } } @@ -540,7 +558,7 @@ } static UTEST_INLINE int utest_strncmp(const char *a, const char *b, size_t n) { - // strncmp breaks on Wall / Werror on gcc/clang, so we avoid using it + /* strncmp breaks on Wall / Werror on gcc/clang, so we avoid using it */ unsigned i; for (i = 0; i < n; i++) { @@ -576,7 +594,7 @@ const char *filter = 0; size_t ran_tests = 0; - // loop through all arguments looking for our options + /* loop through all arguments looking for our options */ for (index = 1; index < UTEST_CAST(size_t, argc); index++) { const char help_str[] = "--help"; const char filter_str[] = "--filter="; @@ -593,7 +611,7 @@ goto cleanup; } else if (0 == utest_strncmp(argv[index], filter_str, strlen(filter_str))) { - // user wants to filter what test cases run! + /* user wants to filter what test cases run! */ filter = argv[index] + strlen(filter_str); } else if (0 == utest_strncmp(argv[index], output_str, strlen(output_str))) { @@ -692,17 +710,21 @@ return UTEST_CAST(int, failed); } -// 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 -// data without having to use the UTEST_MAIN macro, thus allowing them to write -// their own main() function. +/* + 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 + data without having to use the UTEST_MAIN macro, thus allowing them to write + their own main() function. +*/ #define UTEST_STATE() struct utest_state_s utest_state = {0, 0, 0} -// define a main() function to call into utest.h and start executing tests! A -// user can optionally not use this macro, and instead define their own main() -// function and manually call utest_main. The user must, in exactly one source -// file, use the UTEST_STATE macro to declare a global struct variable that -// utest requires. +/* + define a main() function to call into utest.h and start executing tests! A + user can optionally not use this macro, and instead define their own main() + function and manually call utest_main. The user must, in exactly one source + file, use the UTEST_STATE macro to declare a global struct variable that + utest requires. +*/ #define UTEST_MAIN() \ UTEST_STATE(); \ int main(int argc, const char *const argv[]) { \