From 35b3c082200ed6571e47245c5424306a78016f92 Mon Sep 17 00:00:00 2001 From: Marc Ransome Date: Sat, 30 Nov 2024 11:56:10 +0000 Subject: [PATCH] Consolidate unit test macros and external declarations --- src/common.c | 18 +----------------- src/config.c | 18 +----------------- src/flog.c | 18 +----------------- src/testing.h | 19 +++++++++++++++++++ 4 files changed, 22 insertions(+), 51 deletions(-) create mode 100644 src/testing.h diff --git a/src/common.c b/src/common.c index db2dab2..63c2955 100644 --- a/src/common.c +++ b/src/common.c @@ -24,23 +24,7 @@ #include #ifdef UNIT_TESTING -extern void mock_assert(const int result, const char * const expression, - const char * const file, const int line); - -#undef assert -#define assert(expression) \ - mock_assert((int)(expression), #expression, __FILE__, __LINE__); - -extern void * _test_malloc(const size_t size, const char *file, const int line); -extern void * _test_realloc(void *ptr, const size_t size, const char *file, const int line); -extern void * _test_calloc(const size_t number_of_elements, const size_t size, - const char *file, const int line); -extern void _test_free(void * const ptr, const char *file, const int line); - -#define malloc(size) _test_malloc(size, __FILE__, __LINE__) -#define realloc(ptr, size, file, line) _test_realloc(ptr, size, __FILE__, __LINE__) -#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) -#define free(ptr) _test_free(ptr, __FILE__, __LINE__) +#include "testing.h" #endif static const char * diff --git a/src/config.c b/src/config.c index aeaa437..ac80d20 100644 --- a/src/config.c +++ b/src/config.c @@ -32,23 +32,7 @@ #include #ifdef UNIT_TESTING -extern void mock_assert(const int result, const char * const expression, - const char * const file, const int line); - -#undef assert -#define assert(expression) \ - mock_assert((int)(expression), #expression, __FILE__, __LINE__); - -extern void * _test_malloc(const size_t size, const char *file, const int line); -extern void * _test_realloc(void *ptr, const size_t size, const char *file, const int line); -extern void * _test_calloc(const size_t number_of_elements, const size_t size, - const char *file, const int line); -extern void _test_free(void * const ptr, const char *file, const int line); - -#define malloc(size) _test_malloc(size, __FILE__, __LINE__) -#define realloc(ptr, size, file, line) _test_realloc(ptr, size, __FILE__, __LINE__) -#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) -#define free(ptr) _test_free(ptr, __FILE__, __LINE__) +#include "testing.h" #endif const int subsystem_len = 257; diff --git a/src/flog.c b/src/flog.c index 02ca266..bee5914 100644 --- a/src/flog.c +++ b/src/flog.c @@ -29,23 +29,7 @@ #include "config.h" #ifdef UNIT_TESTING -extern void mock_assert(const int result, const char * const expression, - const char * const file, const int line); - -#undef assert -#define assert(expression) \ - mock_assert((int)(expression), #expression, __FILE__, __LINE__); - -extern void * _test_malloc(const size_t size, const char *file, const int line); -extern void * _test_realloc(void *ptr, const size_t size, const char *file, const int line); -extern void * _test_calloc(const size_t number_of_elements, const size_t size, - const char *file, const int line); -extern void _test_free(void * const ptr, const char *file, const int line); - -#define malloc(size) _test_malloc(size, __FILE__, __LINE__) -#define realloc(ptr, size, file, line) _test_realloc(ptr, size, __FILE__, __LINE__) -#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) -#define free(ptr) _test_free(ptr, __FILE__, __LINE__) +#include "testing.h" #endif #define OS_LOG_FORMAT_PUBLIC "%{public}s" diff --git a/src/testing.h b/src/testing.h new file mode 100644 index 0000000..06b43ac --- /dev/null +++ b/src/testing.h @@ -0,0 +1,19 @@ +#include + +extern void mock_assert(const int result, const char * const expression, + const char * const file, const int line); + +#undef assert +#define assert(expression) \ + mock_assert((int)(expression), #expression, __FILE__, __LINE__); + +extern void * _test_malloc(const size_t size, const char *file, const int line); +extern void * _test_realloc(void *ptr, const size_t size, const char *file, const int line); +extern void * _test_calloc(const size_t number_of_elements, const size_t size, + const char *file, const int line); +extern void _test_free(void * const ptr, const char *file, const int line); + +#define malloc(size) _test_malloc(size, __FILE__, __LINE__) +#define realloc(ptr, size, file, line) _test_realloc(ptr, size, __FILE__, __LINE__) +#define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) +#define free(ptr) _test_free(ptr, __FILE__, __LINE__)