Skip to content

Commit

Permalink
Added unit test and Unity submodule (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
perseoGI committed Nov 19, 2020
1 parent d813ec5 commit a9e88a9
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "Unity"]
path = Unity
url = https://github.com/ThrowTheSwitch/Unity
16 changes: 16 additions & 0 deletions Clib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
Expand Down
6 changes: 6 additions & 0 deletions Clib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)



Expand Down Expand Up @@ -277,6 +281,8 @@ void list_print(LinkedList *list, void (*print_func)(void *));



void list_destroy(LinkedList *list);

//=======================================================================================//
// //
// Hash Map API //
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

1 change: 1 addition & 0 deletions Unity
Submodule Unity added at 386c54
169 changes: 130 additions & 39 deletions unit-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,150 @@

#include "Clib.h"
#include <stdio.h>
#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();

}

0 comments on commit a9e88a9

Please sign in to comment.