lib

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

commit 507e5d30142a9a8dc604fa27abf89dfd3b26d21f
parent dfc005fc9e7bda36134af393226c318cac7494e5
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 23 Jan 2017 14:59:17 +0100

Add(Tests): Now testing on mlist

Diffstat:
Mtests/main.c | 1+
Mtests/test.h | 1+
Atests/test_list.c | 194+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 196 insertions(+), 0 deletions(-)

diff --git a/tests/main.c b/tests/main.c @@ -5,6 +5,7 @@ int main(void) { register_infos_tests(); register_args_tests(); + register_list_tests(); m_info("Beginning tests...\n"); ret = test_all(); diff --git a/tests/test.h b/tests/test.h @@ -21,6 +21,7 @@ void register_infos_tests(void); void register_args_tests(void); +void register_list_tests(void); void callback_q(const char *s); void callback_w(const char *s); void callback_e(const char *s); diff --git a/tests/test_list.c b/tests/test_list.c @@ -0,0 +1,194 @@ +#include "test.h" + +int list_str_free(void *member) { + char *str = member; + if (str != NULL) + free(str); + return 1; +} + +TEST(list_add_null) { + mlist_t *ptr = NULL; + void *null = NULL; + + list_add(ptr, null, 0); + TEST_ASSERT((ptr == NULL), "Returned pointer is not null."); + return TEST_SUCCESS; +} + +TEST(list_add_member) { + mlist_t *ptr = NULL; + char test[] = "Hello !\n"; + + list_add(ptr, test, sizeof(test)); + TEST_ASSERT(!strcmp(ptr->member, "Hello !\n"), "Member have an unexpected value."); + TEST_ASSERT((ptr->size == sizeof(test)), "Size is wrong"); + list_free(ptr, NULL); + return TEST_SUCCESS; +} + +TEST(list_add_member_head_list) { + mlist_t *ptr = NULL; + char test[] = "Hello !\n"; + char test2[] = "Hello2 !\n"; + + list_add(ptr, test, sizeof(test)); + list_add(ptr, test2, sizeof(test2)); + TEST_ASSERT(!strcmp(ptr->member, "Hello !\n"), "Head of the list is a wrong pointer."); + TEST_ASSERT(!ptr->prev, "Head of the list prev pointer is not null"); + list_free(ptr, NULL); + return TEST_SUCCESS; +} + +TEST(list_add_member_test_multiples) { + mlist_t *ptr = NULL; + char test[] = "Hello !\n"; + char test2[] = "Hello2 !\n"; + + list_add(ptr, test, sizeof(test)); + list_add(ptr, test2, sizeof(test2)); + TEST_ASSERT(!strcmp(ptr->next->member, "Hello2 !\n"), "Member have an unexpected value."); + TEST_ASSERT(!strcmp(ptr->next->prev->member, "Hello !\n"), "Member have an unexpected value."); + TEST_ASSERT((ptr->next->head == ptr), "Head pointer is not right"); + list_free(ptr, NULL); + return TEST_SUCCESS; +} + +TEST(list_for_each) { + mlist_t *ptr = NULL, *tmp; + char test[] = "Hello !\n"; + char test2[] = "Hello2 !\n"; + char test3[] = "Hello3 !\n"; + char *s_tmp; + size_t i = 0; + + list_add(ptr, test, sizeof(test)); + list_add(ptr, test2, sizeof(test2)); + list_add(ptr, test3, sizeof(test3)); + list_for_each(ptr, tmp, s_tmp) { + switch (i) { + case 0: TEST_ASSERT(s_tmp, "Hello !\n"); break; + case 1: TEST_ASSERT(s_tmp, "Hello2 !\n"); break; + case 2: TEST_ASSERT(s_tmp, "Hello3 !\n"); break; + case 3: TEST_ASSERT(0, "list_for_each too many iterations."); + } + } + list_free(ptr, NULL); + return TEST_SUCCESS; +} + +TEST(list_tail) { + mlist_t *ptr = NULL, *tmp; + char test[] = "Hello !\n"; + char test2[] = "Hello2 !\n"; + char test3[] = "Hello3 !\n"; + + tmp = list_tail(ptr); + TEST_ASSERT(!tmp, "Pointer is not null."); + list_add(ptr, test, sizeof(test)); + list_add(ptr, test2, sizeof(test2)); + list_add(ptr, test3, sizeof(test3)); + tmp = list_tail(ptr); + TEST_ASSERT(!strcmp(tmp->member, "Hello3 !\n"), "Tail is wrong."); + list_free(ptr, NULL); + return TEST_SUCCESS; +} + +TEST(list_add_after) { + mlist_t *ptr = NULL, *tmp; + char test[] = "Hello !\n"; + char test2[] = "Hello2 !\n"; + char test3[] = "Hello3 !\n"; + char test4[] = "Hello2.5 !\n"; + char test5[] = "Hello4 !\n"; + + list_add(ptr, test, sizeof(test)); + list_add(ptr, test2, sizeof(test2)); + list_add(ptr, test3, sizeof(test3)); + list_add_after(ptr, ptr->next, test4, sizeof(test4)); + TEST_ASSERT(!strcmp(ptr->member, "Hello !\n"), "Head is wrong."); + TEST_ASSERT(!strcmp(ptr->next->member, "Hello2 !\n"), "Order is wrong."); + TEST_ASSERT(!strcmp(ptr->next->next->member, "Hello2.5 !\n"), "Order is wrong."); + TEST_ASSERT(!strcmp(ptr->next->next->next->member, "Hello3 !\n"), "Order is wrong."); + tmp = list_tail(ptr); + list_add_after(ptr, tmp, test5, sizeof(test5)); + tmp = list_tail(ptr); + TEST_ASSERT(!strcmp(tmp->member, "Hello4 !\n"), "Order is wrong."); + TEST_ASSERT(!tmp->next, "Next pointer is wrong."); + list_free(ptr, NULL); + return TEST_SUCCESS; +} + +TEST(list_add_before) { + mlist_t *ptr = NULL, *tmp; + char test[] = "Hello !\n"; + char test2[] = "Hello2 !\n"; + char test3[] = "Hello3 !\n"; + char test4[] = "Hello2.5 !\n"; + char test5[] = "Hello4 !\n"; + + list_add(ptr, test, sizeof(test)); + list_add(ptr, test2, sizeof(test2)); + list_add(ptr, test3, sizeof(test3)); + list_add_before(ptr, ptr->next, test4, sizeof(test4)); + TEST_ASSERT(!strcmp(ptr->member, "Hello !\n"), "Head is wrong."); + TEST_ASSERT(!strcmp(ptr->next->member, "Hello2.5 !\n"), "Order is wrong."); + TEST_ASSERT(!strcmp(ptr->next->next->member, "Hello2 !\n"), "Order is wrong."); + TEST_ASSERT(!strcmp(ptr->next->next->next->member, "Hello3 !\n"), "Order is wrong."); + list_add_before(ptr, ptr, test5, sizeof(test5)); + tmp = ptr->head; + TEST_ASSERT(!strcmp(tmp->member, "Hello4 !\n"), "Order is wrong."); + TEST_ASSERT(!tmp->prev, "Next pointer is wrong."); + list_free(ptr, NULL); + return TEST_SUCCESS; +} + +TEST(list_for_each_rev) { + mlist_t *ptr = NULL, *tmp; + char test[] = "Hello !\n"; + char test2[] = "Hello2 !\n"; + char test3[] = "Hello3 !\n"; + char *s_tmp; + size_t i = 0; + + list_add(ptr, test, sizeof(test)); + list_add(ptr, test2, sizeof(test2)); + list_add(ptr, test3, sizeof(test3)); + list_for_each_rev(ptr, tmp, s_tmp) { + switch (i) { + case 0: TEST_ASSERT(s_tmp, "Hello3 !\n"); break; + case 1: TEST_ASSERT(s_tmp, "Hello2 !\n"); break; + case 2: TEST_ASSERT(s_tmp, "Hello !\n"); break; + case 3: TEST_ASSERT(0, "list_for_each too many iterations."); + } + } + list_free(ptr, NULL); + return TEST_SUCCESS; +} + +TEST(list_size) { + mlist_t *ptr = NULL; + char test[] = "Hello !\n"; + char test2[] = "Hello2 !\n"; + char test3[] = "Hello3 !\n"; + + list_add(ptr, test, sizeof(test)); + list_add(ptr, test2, sizeof(test2)); + list_add(ptr, test3, sizeof(test3)); + TEST_ASSERT(list_size(ptr) == 3, "Size is wrong."); + list_free(ptr, NULL); + return TEST_SUCCESS; +} + +void register_list_tests(void) { + reg_test("mlist", list_add_null); + reg_test("mlist", list_add_member); + reg_test("mlist", list_add_member_head_list); + reg_test("mlist", list_add_member_test_multiples); + reg_test("mlist", list_for_each); + reg_test("mlist", list_tail); + reg_test("mlist", list_add_after); + reg_test("mlist", list_add_before); + reg_test("mlist", list_for_each_rev); + reg_test("mlist", list_size); +}