lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit 70b5da33c4f1585a188463a39e920d8dc28b844c
parent f6f560be7451ccf439376071a393596ed24f212a
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 23 May 2017 18:17:56 +0200

Fix(Mockups): Beginning of a better designs for mockups

Diffstat:
Minc/fail_test.h | 31+++++++++++++++++++------------
Msrc/test.c | 79++++++++++++-------------------------------------------------------------------
2 files changed, 31 insertions(+), 79 deletions(-)

diff --git a/inc/fail_test.h b/inc/fail_test.h @@ -20,7 +20,7 @@ # undef strdup -# define malloc(x) fl_malloc(x) +# define malloc(size) fl_malloc(size) # define write(fd, ptr, len) fl_write(fd, ptr, len) # define read(fd, ptr, len) fl_read(fd, ptr, len) # define close(fd) fl_close(fd) @@ -34,6 +34,13 @@ #include <errno.h> +#define MOCK_SET_DECL(fn_name) void set_##fn_name##_fail(int val); +#define MOCK_SET_IMP(fn_name) static int g_##fn_name##_fail = -1; \ + void set_##fn_name##_fail(int val) { \ + if (g_##fn_name##_fail == -1) \ + g_##fn_name##_fail = val; \ + } + void *fl_malloc(size_t alloc); ssize_t fl_write(int fd, const void *ptr, size_t len); ssize_t fl_read(int fd, void *ptr, size_t len); @@ -46,17 +53,17 @@ char *fl_strcat(char *dst, const char *src); int fl_mkdir(const char *pathname, mode_t mode); pid_t fl_fork(void); -void set_malloc_fail(int val); -void set_write_fail(int val); -void set_read_fail(int val); -void set_close_fail(int val); -void set_strdup_fail(int val); -void set_fstat_fail(int val); -void set_calloc_fail(int val); -void set_strcpy_fail(int val); -void set_strcat_fail(int val); -void set_mkdir_fail(int val); -void set_fork_fail(int val); +MOCK_SET_DECL(malloc); +MOCK_SET_DECL(write); +MOCK_SET_DECL(read); +MOCK_SET_DECL(close); +MOCK_SET_DECL(strdup); +MOCK_SET_DECL(fstat); +MOCK_SET_DECL(calloc); +MOCK_SET_DECL(strcpy); +MOCK_SET_DECL(strcat); +MOCK_SET_DECL(mkdir); +MOCK_SET_DECL(fork); # endif /* M_FAIL_TEST_H */ #endif /* COMPILE_WITH_TEST */ diff --git a/src/test.c b/src/test.c @@ -22,6 +22,7 @@ # include <fcntl.h> # include <unistd.h> + /* Real functions */ static void *(*real_malloc)(size_t) = &malloc; static ssize_t (*real_write)(int, const void *, size_t) = &write; @@ -37,17 +38,17 @@ static pid_t (*real_fork)(void) = &fork; # include <fail_test.h> -static int g_malloc_fail = -1; -static int g_write_fail = -1; -static int g_read_fail = -1; -static int g_close_fail = -1; -static int g_strdup_fail = -1; -static int g_fstat_fail = -1; -static int g_calloc_fail = -1; -static int g_strcpy_fail = -1; -static int g_strcat_fail = -1; -static int g_mkdir_fail = -1; -static int g_fork_fail = -1; +MOCK_SET_IMP(malloc); +MOCK_SET_IMP(write); +MOCK_SET_IMP(read); +MOCK_SET_IMP(close); +MOCK_SET_IMP(strdup); +MOCK_SET_IMP(fstat); +MOCK_SET_IMP(calloc); +MOCK_SET_IMP(strcpy); +MOCK_SET_IMP(strcat); +MOCK_SET_IMP(mkdir); +MOCK_SET_IMP(fork); void *fl_malloc(size_t alloc) { if (g_malloc_fail == -1) @@ -178,60 +179,4 @@ pid_t fl_fork(void) { return real_fork(); } -void set_malloc_fail(int val) { - if (g_malloc_fail == -1) - g_malloc_fail = val; -} - -void set_calloc_fail(int val) { - if (g_calloc_fail == -1) - g_calloc_fail = val; -} - -void set_write_fail(int val) { - if (g_write_fail == -1) - g_write_fail = val; -} - -void set_read_fail(int val) { - if (g_read_fail == -1) - g_read_fail = val; -} - -void set_close_fail(int val) { - if (g_close_fail == -1) - g_close_fail = val; -} - -void set_strdup_fail(int val) { - if (g_strdup_fail == -1) - g_strdup_fail = val; -} - -void set_fstat_fail(int val) { - if (g_fstat_fail == -1) - g_fstat_fail = val; -} - -void set_strcpy_fail(int val) { - if (g_strcpy_fail == -1) - g_strcpy_fail = val; -} - -void set_strcat_fail(int val) { - if (g_strcat_fail == -1) - g_strcat_fail = val; -} - -void set_mkdir_fail(int val) { - if (g_mkdir_fail == -1) - g_mkdir_fail = val; -} - -void set_fork_fail(int val) { - if (g_fork_fail == -1) { - g_fork_fail = val; - } -} - #endif /* COMPILE_WITH_TEST */