diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a82cbd6 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Unity"] + path = Unity + url = https://github.com/ThrowTheSwitch/Unity diff --git a/Clib.c b/Clib.c index 3200179..cf46852 100644 --- a/Clib.c +++ b/Clib.c @@ -291,6 +291,22 @@ void list_print(LinkedList *list, void (*print_func)(void *)) { } +// TODO comment out FREE_TO_NULL not working +void list_destroy(LinkedList *list){ + ListNode *current = list->head->prev; + ListNode *previous; + for (int i = 0; i < list->size; i++) { + previous = current; + current = current->prev; + /*free(previous->content);*/ + free(previous); + } + + free(list->head); + free(list->tail); + free(list); +} + //=======================================================================================// // // // Hash Map API // diff --git a/Clib.h b/Clib.h index b84cd50..663b03d 100644 --- a/Clib.h +++ b/Clib.h @@ -41,6 +41,10 @@ #define CLIB_ERROR (1) #define E_ELEMENT_ALREADY_EXIST (2) +#define FREE_TO_NULL(ptr) do{ \ + free((ptr)); \ + (ptr) = NULL; \ + } while(0) @@ -277,6 +281,8 @@ void list_print(LinkedList *list, void (*print_func)(void *)); +void list_destroy(LinkedList *list); + //=======================================================================================// // // // Hash Map API // diff --git a/Makefile b/Makefile index 676f994..4a91d4e 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,9 @@ Clib.o: Clib.h Clib.c gcc -c Clib.c test: Clib.a - gcc -g unit-tests.c $< -o $@ -# ./$@ + gcc -g $< unit-tests.c -I Unity/src Unity/libunity.a -o $@ + ./$@ clean: rm *.o *.a + diff --git a/Unity b/Unity new file mode 160000 index 0000000..386c540 --- /dev/null +++ b/Unity @@ -0,0 +1 @@ +Subproject commit 386c540510929c0cf6f7b8d04cb4af40754bb822 diff --git a/unit-tests.c b/unit-tests.c index 1d3b531..240a08e 100644 --- a/unit-tests.c +++ b/unit-tests.c @@ -22,59 +22,150 @@ #include "Clib.h" #include +#include "unity.h" void printInt (void *node){ int num = *(int*) node; printf("%d ", num); } +LinkedList *list; +// **************************************************************************************** +// test_sendReset_transmissionError +// **************************************************************************************** +/** + * Check creation of linked list + * + * Function under testing: + * #create_linked_list + * + * Checked: + * - List return pointer not null + * - List is empty + * - Head and tail pointers are joint and are NULL terminated on the other link + */ +// **************************************************************************************** +void test_create_linked_list(void){ -int main (){ LinkedList * list = create_linked_list(); - /*list_print(list, printInt);*/ - int nums[] = { 0, 1, 2, 3, 4 }; - list_push_back(list, &nums[0]); - list_push_back(list, &nums[1]); - list_push_back(list, &nums[2]); - list_push_back(list, &nums[3]); - list_push_back(list, &nums[4]); + TEST_ASSERT_NOT_NULL(list); + TEST_ASSERT_EQUAL_INT(0, list->size); + TEST_ASSERT_NOT_NULL(list->head); + TEST_ASSERT_NOT_NULL(list->tail); + TEST_ASSERT_NULL(list->head->next); + TEST_ASSERT_NULL(list->tail->prev); + TEST_ASSERT_EQUAL_PTR(list->head->prev, list->tail); + TEST_ASSERT_EQUAL_PTR(list->tail->next, list->head); - list_print(list, printInt); + list_destroy(list); +} - int first = *(int*) list_pop_back(list); - if (first == 4){ - printf("Ok\n"); - } - else { - printf("Error on pop back\n"); - } +// TODO cant assign pointer to null +void test_destroy_linked_list(void){ + LinkedList *list = create_linked_list(); + ListNode *head = list->head; + ListNode *tail = list->tail; + + list_destroy(list); + + /*list = NULL;*/ + /*printf("%p\n", list);*/ + /*TEST_ASSERT_NULL(list);*/ + /*TEST_ASSERT_NULL(head);*/ + /*TEST_ASSERT_NULL(tail);*/ + +} - list_pop_back(list); - list_pop_back(list); - first = *(int*) list_pop_back(list); - if (first == 1){ - printf("Ok\n"); +// **************************************************************************************** +// test_list_push_back +// **************************************************************************************** +/** + * Check push back function on sucessive calls + * + * Function under testing: + * #list_push_back + * + * Checked: + * - + * - + */ +// **************************************************************************************** +void test_list_push_back(void){ + int test_nums[] = { 0, 1, 2, 3, 4 }; + + for (int i = 0; i < sizeof(test_nums)/sizeof(int); ++i){ + + list_push_back(list, &test_nums[i]); + + // Check size is increasing on every push + TEST_ASSERT_EQUAL(i + 1, list->size); + // Check last element on the list is the element already inserted + TEST_ASSERT_EQUAL_INT(test_nums[i], *(int*)list->tail->next->content); + } - else { - printf("Error on pop back\n"); - } - list_pop_back(list); - - // Empty list - list_push_front(list, &nums[0]); - list_push_front(list, &nums[1]); - list_push_front(list, &nums[2]); - list_push_front(list, &nums[3]); - list_push_front(list, &nums[4]); - - first = *(int*) list_pop_front(list); - if (first == 4){ - printf("Ok\n"); + + // Second iteration + ListNode *node = list->head->prev; + int num = 0; + + // Check if node is tail node + while (node->prev){ // Same as node != list->tail + // Check all nodes remain on the pushed order + TEST_ASSERT_EQUAL_INT(test_nums[num], *(int*)node->content); + node = node->prev; + ++num; } - else { - printf("Error on pop front\n"); - } + // Check list current size corresponds with the actual size + TEST_ASSERT_EQUAL(num, list->size); + +} + + +// TBD +void test_list_push_front(void){ + +} + +void test_list_pop_back(void){ + +} + +void test_list_pop_front(void){ + +} + +void test_list_get_first(void){ + +} +void test_list_get_last(void){ + +} + +void test_list_print(void){ + +} + + +// Needed by Unity test framework. This functions will be executed before and after each test. +void setUp(void){ + list = create_linked_list(); +} + +void tearDown(void){ + list_destroy(list); +} + + +int main (){ + + UNITY_BEGIN(); + RUN_TEST(test_create_linked_list); + RUN_TEST(test_destroy_linked_list); + RUN_TEST(test_list_push_back); + RUN_TEST(test_list_pop_back); + return UNITY_END(); + }