Skip to content

Commit

Permalink
Consolidate unit test macros and external declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
marcransome committed Nov 30, 2024
1 parent 3bf7157 commit 35b3c08
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 51 deletions.
18 changes: 1 addition & 17 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,7 @@
#include <stdio.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

static const char *
Expand Down
18 changes: 1 addition & 17 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,7 @@
#include <unistd.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

const int subsystem_len = 257;
Expand Down
18 changes: 1 addition & 17 deletions src/flog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions src/testing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdlib.h>

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__)

0 comments on commit 35b3c08

Please sign in to comment.