1
0
C_lib/tests/double_linked_list_tests.c

178 lines
4.9 KiB
C

/* Include statements. */
#include <stdio.h>
#include <assert.h>
#include "../linked_lists/double_linked_list.c"
/* Define constants. */
#define PRINT_INFO_TESTING_START "Starting tests.\n"
#define PRINT_INFO_TESTING_END "Done testing.\n"
#define ERROR_TEXT_RANDOM_LIST_FAIL_ALLOCATION "Long list test failed because of allocation errors!\n"
#define SOME_TEST_FAILED "Recheck the tests, one of them failed!\n"
#define FUNCTION_ERROR "Some function encountered a data error in testing.\n"
#define INITIAL_LIST_LENGTH 0
#define LIST_LENGTH_ONE 1
#define LIST_LENGTH_LONG_TEST 4096
/* Define functions. */
double_node_t* generate_random_node();
double_linked_list_t* generate_long_random_list(int length);
int list_count_length(double_linked_list_t *list);
int test_linked_lists();
/* Starting point. */
int main() {
printf(PRINT_INFO_TESTING_START);
if (test_linked_lists()) {
printf(SOME_TEST_FAILED);
}
printf(PRINT_INFO_TESTING_END);
}
/* This function generates a random number and puts it in a newly allocated node. */
/* Caller has to free memory afterwards, returns NULL on failure. */
double_node_t* generate_random_node() {
/* The node to return. */
double_node_t* result;
int *value;
/* Attempt to allocate the node. */
result = (double_node_t*) malloc(sizeof(double_node_t));
if (result == NULL)
return NULL;
value = (int*) malloc(sizeof(int));
if (value == NULL) {
free(result);
return NULL;
}
*value = random();
/* Store and return. */
result->next = NULL;
result->previous = NULL;
result->data = value;
return result;
}
/* This function generates a long list with random values in the nodes, returns NULL on failures. */
/* Length denotes the length of the list. */
double_linked_list_t* generate_long_random_list(int length) {
/* Sanity check. */
if (length <= 0)
return NULL;
/* Variables. */
double_linked_list_t *list;
double_node_t *current;
int i;
/* Allocate the list. */
list = initialize_double_linked_list();
if (list == NULL)
return NULL;
/* Fill the list with random nodes. */
/* First node. */
current = generate_random_node();
if (current == NULL) {
free(list);
return NULL;
}
/* Attempt to append. */
if (!append_node_double_linked_list(list, current)) {
free(current->data);
free(current);
free(list);
return NULL;
}
/* Loop and add the rest. */
for (i = 1; i < length; i++) {
/* Generate and attempt to append. */
current = generate_random_node();
if (!append_node_double_linked_list(list, current)) {
/* Abort. */
free(current->data);
free(current);
clear_list_data_double_linked_list(&list, NULL);
return NULL;
}
}
/* Update the list's length. */
list->length = length;
/* Return the result. */
return list;
}
/* This function checks the actual length of a list. */
int list_count_length(double_linked_list_t *list) {
if (list == NULL || list->head == NULL)
return 0;
/* Loop and count. */
double_node_t *current = list->head;
int count = 0;
while (current != NULL) {
count++;
current = current->next;
}
/* Return the result. */
return count;
}
/* This function does the testing. */
int test_linked_lists() {
/* Local variables. */
double_linked_list_t *list;
double_node_t *node, *tail;
int error;
/* Initialize an empty list. */
list = initialize_double_linked_list();
/* Check that it was correctly created. */
assert(list != NULL);
assert(list->length == INITIAL_LIST_LENGTH);
assert(list->head == NULL);
assert(list->tail == NULL);
/* Generate and store the node. */
node = generate_random_node();
/* Attempt to add it to the list. */
append_node_double_linked_list(list, node);
/* Check that the list grew and that both the head and tail got set. */
assert(list->length == LIST_LENGTH_ONE);
assert(list->head != NULL && list->head == node);
assert(list->tail != NULL && list->tail == node);
assert(get_node_i_double_linked_list(list, 0, &error) == node);
if (!error)
printf(FUNCTION_ERROR);
assert(get_node_index_pointer_double_linked_list(list, node) == 0);
/* Attempt to free the nodes and then the list. */
remove_node_i_data_double_linked_list(list, 0, NULL);
assert(list->length == INITIAL_LIST_LENGTH);
assert(list->head == NULL);
node = NULL;
free(list);
list = NULL;
/* Attempt to allocate a long list. */
list = generate_long_random_list(LIST_LENGTH_LONG_TEST);
if (list == NULL) {
printf(ERROR_TEXT_RANDOM_LIST_FAIL_ALLOCATION);
return 1;
}
assert(list->length == LIST_LENGTH_LONG_TEST);
assert(list->length == list_count_length(list));
/* Test the get head/tail functions. */
assert(get_node_head_double_linked_list(list) == list->head);
tail = get_node_tail_double_linked_list(list);
assert(tail != NULL && tail->next == NULL);
/* Test the indexer functions. */
assert(get_node_index_pointer_double_linked_list(list, tail) == (list->length - 1));
/* Test the freeing functions. */
clear_list_data_double_linked_list(&list, NULL);
assert(list == NULL);
/* Ran successfully, return 0. */
return 0;
}