libdev.a

C Helper library
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/libdev.a

commit 848685fbb31a9396d3737eb47f813e3610815d94
parent ed415e60fd8790bd30c6e390a198427ece873a36
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 10 Oct 2016 18:31:15 +0200

Add(Basic tests):

Add basic linked_list unit test in the tests/ folder
Fix list_get helper

Diffstat:
Mlibdev.h | 3++-
Mlists.c | 3+++
Mtests.c | 2+-
Atests/Makefile | 21+++++++++++++++++++++
Atests/main.c | 123+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 150 insertions(+), 2 deletions(-)

diff --git a/libdev.h b/libdev.h @@ -18,7 +18,7 @@ for (temp = list_get_last(org_list), p_tr = temp->member;\ temp != temp->head && (p_tr = temp->member); temp = temp->prev) -# define list_tail(org_list, p_tr) ptr = list_get_last(org_list)->member; +# define list_tail(org_list) list_get_last(org_list); # define list_add_after(org_list, p_tr1, p_tr2, sizeZ) org_list = list_insert_after(org_list, p_tr1, p_tr2, sizeZ); @@ -109,5 +109,6 @@ void print_trace(void); // tests.c void register_test(char *group, char *(*test)(void), char *name); t_test_results test_group(char *group); +void test_all(void); #endif /* __LIBDEV__ */ diff --git a/lists.c b/lists.c @@ -3,6 +3,8 @@ t_list *list_add_member(t_list *list, void *member, size_t size) { t_list *n_member, *tmp; + if (!member || !size) + return 0x0; n_member = malloc(sizeof(t_list)); n_member->member = malloc(size); L_ASSERT(n_member && n_member->member); @@ -50,6 +52,7 @@ t_list *list_insert_after(t_list *org, t_list *ptr, void *member, size_t size) tmp2 = tmp->next; tmp->next = n_member; n_member->prev = tmp; + n_member->next = tmp2; tmp2->prev = n_member; } n_member->head = org; diff --git a/tests.c b/tests.c @@ -66,7 +66,7 @@ void test_all(void) { } } if (!total) - warning("No tests registered, skipping."); + warning("No tests registered, skipping.\n"); else { printf("\n"); info("============================= RESULTS =============================\n"); diff --git a/tests/Makefile b/tests/Makefile @@ -0,0 +1,21 @@ +NAME = libdev.a-test +CC = gcc +CFLAGS = -Wall -Wextra -Werror -I.. -L.. -ldev -g +SRCS = main.c +OBJS = $(SRCS:%.c=%.o) + +all: $(NAME) + +$(NAME): $(OBJS) + $(CC) $(OBJS) $(CFLAGS) -o $(NAME) + ./$(NAME) + +clean: + rm -f $(OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY: re clean fclean diff --git a/tests/main.c b/tests/main.c @@ -0,0 +1,123 @@ +#include <libdev.h> + +TEST(list_add_null) { + t_list *ptr = 0x0; + void *null = 0x0; + + list_add(ptr, null, 0); + T_ASSERT((ptr == 0x0), "Returned pointer is not null."); + return TEST_SUCCESS; +} + +TEST(list_add_member) { + t_list *ptr = 0x0; + char test[] = "Hello !\n"; + + list_add(ptr, test, 8); + T_ASSERT(!strcmp(ptr->member, "Hello !\n"), "Member have an unexpected value."); + T_ASSERT((ptr->size == 8), "Size is wrong"); + return TEST_SUCCESS; +} + +TEST(list_add_member_head_list) { + t_list *ptr = 0x0; + char test[] = "Hello !\n"; + char test2[] = "Hello2 !\n"; + + list_add(ptr, test, 8); + list_add(ptr, test2, 9); + T_ASSERT(!strcmp(ptr->member, "Hello !\n"), "Head of the list is a wrong pointer."); + T_ASSERT(!ptr->prev, "Head of the list prev pointer is not null"); + return TEST_SUCCESS; +} + +TEST(list_add_member_test_multiples) { + t_list *ptr = 0x0; + char test[] = "Hello !\n"; + char test2[] = "Hello2 !\n"; + + list_add(ptr, test, 8); + list_add(ptr, test2, 9); + T_ASSERT(!strcmp(ptr->next->member, "Hello2 !\n"), "Member have an unexpected value."); + T_ASSERT(!strcmp(ptr->next->prev->member, "Hello !\n"), "Member have an unexpected value."); + T_ASSERT((ptr->next->head == ptr), "Head pointer is not right"); + return TEST_SUCCESS; +} + +TEST(list_for_each) { + t_list *ptr = 0x0, *tmp; + char test[] = "Hello !\n"; + char test2[] = "Hello2 !\n"; + char test3[] = "Hello3 !\n"; + char *s_tmp; + size_t i = 0; + + list_add(ptr, test, 8); + list_add(ptr, test2, 9); + list_add(ptr, test3, 9); + list_for_each(ptr, tmp, s_tmp) { + switch (i) { + case 0: T_ASSERT(s_tmp, "Hello !\n"); break; + case 1: T_ASSERT(s_tmp, "Hello2 !\n"); break; + case 2: T_ASSERT(s_tmp, "Hello3 !\n"); break; + case 3: T_ASSERT(0, "list_for_each too many iterations."); + } + } + return TEST_SUCCESS; +} + +TEST(list_tail) { + t_list *ptr = 0x0, *tmp; + char test[] = "Hello !\n"; + char test2[] = "Hello2 !\n"; + char test3[] = "Hello3 !\n"; + + tmp = list_tail(ptr); + T_ASSERT(!tmp, "Pointer is not null."); + list_add(ptr, test, 8); + list_add(ptr, test2, 9); + list_add(ptr, test3, 9); + tmp = list_tail(ptr); + T_ASSERT(!strcmp(tmp->member, "Hello3 !\n"), "Tail is wrong."); + return TEST_SUCCESS; +} + +TEST(list_add_after) { + t_list *ptr = 0x0, *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, 8); + list_add(ptr, test2, 9); + list_add(ptr, test3, 9); + list_add_after(ptr, ptr->next, test4, 11); + T_ASSERT(!strcmp(ptr->member, "Hello !\n"), "Head is wrong."); + T_ASSERT(!strcmp(ptr->next->member, "Hello2 !\n"), "Order is wrong."); + T_ASSERT(!strcmp(ptr->next->next->member, "Hello2.5 !\n"), "Order is wrong."); + T_ASSERT(!strcmp(ptr->next->next->next->member, "Hello3 !\n"), "Order is wrong."); + tmp = list_tail(ptr); + list_add_after(ptr, tmp, test5, 9); + tmp = list_tail(ptr); + T_ASSERT(!strcmp(tmp->member, "Hello4 !\n"), "Order is wrong."); + T_ASSERT(!tmp->next, "Next pointer is wrong."); + return TEST_SUCCESS; +} + + + + +int main(void) { + reg_test("linked_list", list_add_null); + reg_test("linked_list", list_add_member); + reg_test("linked_list", list_add_member_head_list); + reg_test("linked_list", list_add_member_test_multiples); + reg_test("linked_list", list_for_each); + reg_test("linked_list", list_tail); + reg_test("linked_list", list_add_after); + + test_all(); + return 0; +}