lib

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

commit 81cb8709314629e7d63b2349f44009439ef032c3
parent 8b89a6094f728a94e8bbcb71d0aa18f1e408b1e4
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Thu, 27 Apr 2017 19:23:16 +0200

Add(Mockup): Add calloc to existing mockups:

With tests !

Diffstat:
Minc/fail_test.h | 3+++
Msrc/test.c | 19+++++++++++++++++++
Mtests/test_tests.c | 16++++++++++++++++
3 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/inc/fail_test.h b/inc/fail_test.h @@ -26,6 +26,7 @@ # define close(fd) fl_close(fd) # define strdup(str) fl_strdup(str) # define fstat(fd, buf) fl_fstat(fd, buf) +# define calloc(memb, size) fl_calloc(memb, size) void *fl_malloc(size_t alloc); ssize_t fl_write(int fd, const void *ptr, size_t len); @@ -33,6 +34,7 @@ ssize_t fl_read(int fd, void *ptr, size_t len); int fl_close(int fd); char *fl_strdup(const char *str); int fl_fstat(int fd, struct stat *buf); +void *fl_calloc(size_t nmemb, size_t size); void set_malloc_fail(int val); void set_write_fail(int val); @@ -40,6 +42,7 @@ 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); # endif /* M_FAIL_TEST_H */ diff --git a/src/test.c b/src/test.c @@ -29,6 +29,7 @@ static ssize_t (*real_read)(int, void *, size_t) = &read; static int (*real_close)(int) = &close; static char *(*real_strdup)(const char *) = &strdup; static int (*real_fstat)(int, struct stat *) = &fstat; +static void *(*real_calloc)(size_t, size_t) = &calloc; # include <fail_test.h> @@ -38,6 +39,7 @@ 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; void *fl_malloc(size_t alloc) { if (g_malloc_fail == -1) @@ -50,6 +52,18 @@ void *fl_malloc(size_t alloc) { return real_malloc(alloc); } +void *fl_calloc(size_t nmemb, size_t size) { + if (g_calloc_fail == -1) + return real_calloc(nmemb, size); + if (g_calloc_fail == 0) + { + g_calloc_fail = -1; + return NULL; + } + g_calloc_fail--; + return real_calloc(nmemb, size); +} + ssize_t fl_write(int fd, const void *ptr, size_t len) { if (g_write_fail == -1) return real_write(fd, ptr, len); @@ -111,6 +125,11 @@ void set_malloc_fail(int val) { 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; diff --git a/tests/test_tests.c b/tests/test_tests.c @@ -84,6 +84,21 @@ TEST(test_fail_malloc) { return TEST_SUCCESS; } +TEST(test_fail_calloc) { + + char *ret = calloc(1, 10); + TEST_ASSERT(ret != NULL, "calloc should have succeed (Or we're very unlucky)"); + free(ret); + + set_calloc_fail(1); + ret = calloc(1, 10); + TEST_ASSERT(ret != NULL, "calloc should have succeed (Or we're very unlucky)"); + free(ret); + ret = calloc(1, 10); + TEST_ASSERT(ret == NULL, "calloc should have failed") + return TEST_SUCCESS; +} + # define TMP_FD_FN "/tmp/test_fake_functions" TEST(test_fail_write) { @@ -166,6 +181,7 @@ void register_tests_tests(void) { test_test_result(); test_test_group_fail(); reg_test("fake_functions", test_fail_malloc); + reg_test("fake_functions", test_fail_calloc); reg_test("fake_functions", test_fail_write); reg_test("fake_functions", test_fail_read); reg_test("fake_functions", test_fail_close);