1
0
Fork 0

Fixed serialization code for double linked lists and updated tests.

This commit is contained in:
wael 2022-01-03 22:50:13 +02:00
parent 106b1817b2
commit b8278330db
No known key found for this signature in database
GPG Key ID: C0A5FBF4558963D4
3 changed files with 36 additions and 32 deletions

View File

@ -749,33 +749,25 @@ int clear_list_data_double_linked_list(double_linked_list_t **list, void (*free_
/* This function attempts to serialize a doubly-linked list into an array. */
/* The function will allocate and return a pointer to said array on success, NULL on failure. */
/* The length of the array is sent through the length pointer, and set to -1 on failure. */
void **serialize_double_linked_list(double_linked_list_t *list, int *length) {
/* Note that the array has to be allocated by the caller. */
/* The array setting function is used to set the values at an index, takes the pointer to the array, index and value to put. */
void *serialize_double_linked_list(double_linked_list_t *list, void (*array_setting_function)(void*, size_t, void*), void *array) {
/* Local variables. */
void **array;
double_node_t *current;
int i;
size_t i = 0L;
/* Sanity check. */
if (list == NULL || list->head == NULL || length == NULL || list->length <= 0)
if (!list || !(list->head) || list->length <= 0 || !array || !array_setting_function)
return NULL;
/* Set the length. */
*length = list->length;
/* Attempt to allocate the array. */
array = (void**) (malloc(list->length * sizeof(void*)));
if (array == NULL) {
*length = -1;
return NULL;
}
/* Loop and add the elements. */
current = list->head;
for (i = 0; i < *length; i++) {
array[i] = current->data;
for (current = list->head; current; i++) {
/* Set the ith value. */
array_setting_function(array, i, current->data);
/* Advance the pointer. */
current = current->next;
}
/* Return the filled array. */
/* Return the result. */
return array;
}

View File

@ -89,7 +89,6 @@ int remove_node_tail_data_double_linked_list(double_linked_list_t *list, void (*
int clear_list_double_linked_list(double_linked_list_t **list);
/* This function deletes all the nodes and data stored within of the given list and the list itself. */
int clear_list_data_double_linked_list(double_linked_list_t **list, void (*free_function)(const void*));
/* This function serializes the list. */
void **serialize_double_linked_list(double_linked_list_t *list, int *length);
/* This function serializes the list, it relies on an array setting function and an allocated array/buffer. */
static inline void *serialize_double_linked_list(double_linked_list_t *list, void (*array_setting_function)(void*, size_t, void*), void *array);
#endif /* DOUBLE_LINKED_LIST_H */

View File

@ -18,6 +18,7 @@
/* Define functions. */
double_node_t* generate_random_node();
double_linked_list_t* generate_long_random_list(int length);
static inline void set_array_value_index(void *array, size_t index, void *value);
int list_count_length(double_linked_list_t *list);
int test_serialization();
int test_linked_lists();
@ -105,6 +106,15 @@ double_linked_list_t* generate_long_random_list(int length) {
return list;
}
/* This function is used to set a value at an index in an array. */
static inline void set_array_value_index(void *array, size_t index, void *value) {
/* Cast */
int *arr = (int*) array;
int *val = (int*) value;
/* Set the value. */
arr[index] = *val;
}
/* This function checks the actual length of a list. */
int list_count_length(double_linked_list_t *list) {
if (list == NULL || list->head == NULL)
@ -123,16 +133,21 @@ int list_count_length(double_linked_list_t *list) {
/* This function attempts to allocate a list, fill it, then serialize it. */
int test_serialization() {
/* Local variables. */
int i, length;
int i;
size_t length;
/* Used to allocate and store a value. */
int *value;
/* Used to store the array. */
void **array;
int *value, *array;
/* Allocations. */
/* Initialize the list. */
double_linked_list_t *list = initialize_double_linked_list();
if (list == NULL) {
if (!list)
return 1;
/* Allocate the array. */
array = (int*) malloc(sizeof(int) * SERIALIZATION_TEST_LIST_LENGTH);
if (!array) {
free(list);
return 1;
}
@ -140,7 +155,7 @@ int test_serialization() {
for (i = 0; i < SERIALIZATION_TEST_LIST_LENGTH; i++) {
/* Allocate the value store. */
value = (int*) malloc(sizeof(int));
if (value == NULL) {
if (!value) {
/* Clear the list. */
clear_list_data_double_linked_list(&list, NULL);
/* Free the array. */
@ -154,10 +169,8 @@ int test_serialization() {
append_node_data_double_linked_list(list, value);
}
/* Serialize the list. */
array = serialize_double_linked_list(list, &length);
/* Check if the serialization failed. */
if (length == -1 || length != list->length) {
/* Serialize the list and check if the serialization failed. */
if (!serialize_double_linked_list(list, set_array_value_index, array)) {
/* Clear the list. */
clear_list_data_double_linked_list(&list, NULL);
/* Free the array. */
@ -168,7 +181,7 @@ int test_serialization() {
/* Check the values. */
for (i = 0; i < SERIALIZATION_TEST_LIST_LENGTH; i++) {
if (*((int *)(array[i])) != i) {
if (array[i] != i) {
/* Clear the list. */
clear_list_data_double_linked_list(&list, NULL);
/* Free the array. */