lib

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

commit 3226f42898495945398ff4776ace632a886a7b4d
parent 32703720d6ae5976f972b40bec6aa970dd0da225
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 14 Jun 2017 19:23:08 +0200

Add(Tests): Add tests for vector_string helpers

Diffstat:
Minc/m_list.h | 3++-
Msrc/m_list.c | 2+-
Mtests/test_list.c | 25+++++++++++++++++++++++++
3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/inc/m_list.h b/inc/m_list.h @@ -21,7 +21,6 @@ # include <stdlib.h> /* malloc */ # include <assert.h> /* assert */ # include <string.h> /* mem{cpy,move,cmp} */ -# include <m_types.h> /* Linked list */ typedef struct list_s { @@ -169,4 +168,6 @@ vector_string_t *vector_string_init(const char *str1, const char *str2); */ int vector_string_free(void *magic); +# include <morphux.h> + #endif diff --git a/src/m_list.c b/src/m_list.c @@ -255,7 +255,7 @@ vector_string_t *vector_string_init(const char *str1, const char *str2) { if (str2 != NULL) ret->str2 = strdup(str2); - if (str1 == NULL || str2 == NULL) + if (ret->str1 == NULL || ret->str2 == NULL) goto cleanup; return ret; diff --git a/tests/test_list.c b/tests/test_list.c @@ -249,6 +249,30 @@ TEST(list_remove) { return TEST_SUCCESS; } +TEST(vector_string_init) { + const char *s1 = "Hello"; + const char *s2 = "World !\n"; + vector_string_t *tmp = NULL; + + set_malloc_fail(0); + TEST_ASSERT(vector_string_init(s1, s2) == NULL, "Malloc failed did not raise an error"); + + set_strdup_fail(0); + TEST_ASSERT(vector_string_init(s1, s2) == NULL, "strdup failed did not raise an error"); + + set_strdup_fail(1); + TEST_ASSERT(vector_string_init(s1, s2) == NULL, "strdup failed did not raise an error"); + + + tmp = vector_string_init(s1, s2); + TEST_ASSERT(tmp != NULL, "An error hapenned"); + TEST_ASSERT(strcmp(tmp->str1, s1) == 0, "Strings are not the same"); + TEST_ASSERT(strcmp(tmp->str2, s2) == 0, "Strings are not the same"); + vector_string_free(tmp); + + return TEST_SUCCESS; +} + void register_list_tests(void) { reg_test("mlist", list_add_null); reg_test("mlist", list_add_member); @@ -262,4 +286,5 @@ void register_list_tests(void) { reg_test("mlist", list_size); reg_test("mlist", list_free); reg_test("mlist", list_remove); + reg_test("mlist", vector_string_init); }