1
0
Fork 0

Got queue tests to compile and run, still needs debugging.

This commit is contained in:
wael 2022-01-03 23:16:15 +02:00
parent d6658187fb
commit 4db662556a
No known key found for this signature in database
GPG Key ID: C0A5FBF4558963D4
6 changed files with 47 additions and 38 deletions

View File

@ -29,7 +29,7 @@
/* This function initializes a new empty queue. */
/* Will return a pointer to the queue on success, otherwise will return NULL. */
queue_t *initialize_queue() {
static inline queue_t *initialize_queue() {
/* Variables. */
queue_t *result;
@ -61,7 +61,7 @@ queue_t *initialize_queue() {
/* This function enqueues a node. */
/* This function attempts to enqueue a given node into the given queue, on success will return SUCCESS_CODE_QUEUE_C, otherwise will return FAILURE_CODE_QUEUE_C. */
/* Note that if either the queue or node pointer are NULL, the function will automatically fail. */
int enqueue(queue_t *queue, node_t *node) {
static inline int enqueue(queue_t *queue, node_t *node) {
/* Input sanity check. */
if (!queue || !(queue->list) || !node || ((!(queue->list->head) && queue->tail) || (queue->list->head && !(queue->tail))))
return FAILURE_CODE_QUEUE_C;
@ -85,7 +85,7 @@ int enqueue(queue_t *queue, node_t *node) {
/* This function allocates and enqueues a node. */
/* This function attempts to create a node then enqueue it into the given queue, on success will return SUCCESS_CODE_QUEUE_C, otherwise will return FAILURE_CODE_QUEUE_C. */
/* Note that if either the queue pointer is NULL or node allocation fails, the function will automatically fail. */
int enqueue_data(queue_t *queue, void *data) {
static inline int enqueue_data(queue_t *queue, void *data) {
/* Variables. */
node_t *node;
@ -117,7 +117,7 @@ int enqueue_data(queue_t *queue, void *data) {
/* This function dequeues a node. */
/* This function attempts to dequeue a node from the given queue, on success will return a pointer to the dequeued node - otherwise returns NULL. */
/* Note that if either the queue or node pointer are NULL, the function will automatically fail. */
node_t *dequeue(queue_t *queue) {
static inline node_t *dequeue(queue_t *queue) {
/* Variables. */
node_t *result, *replacement, *tmp;
int i;
@ -156,7 +156,7 @@ node_t *dequeue(queue_t *queue) {
/* This function frees all the nodes of the given queue and the queue itself (setting it to NULL). */
/* Note that it doesn't touch the data stored within the queue's nodes, returns 1 on success and 0 on failure - in the latter case might exhibit undefined behavior. */
int clear_queue(queue_t **queue) {
static inline int clear_queue(queue_t **queue) {
/* Sanity check. */
if (!queue || !(*queue))
return FAILURE_CODE_QUEUE_C;
@ -174,7 +174,7 @@ int clear_queue(queue_t **queue) {
/* This function frees all the nodes of the given queue and the queue itself (setting it to NULL).*/
/* Note that it does free the stored data, returns 1 on success and 0 on failure - in the latter case might exhibit undefined behavior. */
/* If free_function is NULL, then it the standard library's free() call is used. */
int clear_queue_data(queue_t **queue, void (*free_func)()) {
static inline int clear_queue_data(queue_t **queue, void (*free_func)()) {
/* Sanity check. */
if (!queue || !(*queue))
return FAILURE_CODE_QUEUE_C;
@ -190,14 +190,10 @@ int clear_queue_data(queue_t **queue, void (*free_func)()) {
}
/* This function serializes the queue. */
/* Note that it'll return NULL on allocation failure or invalid input - in the former case it'll set the correct length in the specified pointer, in the second it will set -1. */
void *serialize_queue(queue_t *queue, int *length) {
/* Sanity check. */
if (!queue) {
*length = -1;
return NULL;
}
/* Note that it'll return NULL on allocation failure or invalid input. */
/* 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. */
static inline void *serialize_queue(queue_t *queue, void (*array_setting_function)(void*, size_t, void*), void *array) {
/* Delegate. */
return serialize_linked_list(queue->list, length);
return (!queue) ? NULL : serialize_linked_list(queue->list, array_setting_function, array);
}

View File

@ -43,18 +43,18 @@ typedef struct queue {
} queue_t;
/* This function initializes a new queue. */
queue_t *initialize_queue();
static inline queue_t *initialize_queue();
/* This function enqueues a node. */
int enqueue(queue_t *queue, node_t *node);
static inline int enqueue(queue_t *queue, node_t *node);
/* This function creates and enqueues a node. */
int enqueue_data(queue_t *queue, void *data);
static inline int enqueue_data(queue_t *queue, void *data);
/* This function dequeues the last node. */
node_t *dequeue(queue_t *queue);
static inline node_t *dequeue(queue_t *queue);
/* This function deletes all the nodes of the given queue. */
int clear_queue(queue_t **queue);
static inline int clear_queue(queue_t **queue);
/* This function deletes all the nodes of the given queue. */
int clear_queue_data(queue_t **queue, void (*free_function)());
/* This function serializes the queue. */
void *serialize_queue(queue_t *queue, int *length);
static inline int clear_queue_data(queue_t **queue, void (*free_function)());
/* This function serializes the queue into an array, it relies on an array setting function and an allocated buffer. */
static inline void *serialize_queue(queue_t *queue, void (*array_setting_function)(void*, size_t, void*), void *array);
#endif /* QUEUE_H */

View File

@ -449,10 +449,9 @@ int clear_list_data_single_linked_list(linked_list_t **list, void (*free_functio
}
/* This function attetmps to serialize a linked list into an array. */
/* The function will allocate and return a pointer to said array on success, NULL on failure. */
/* 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. */
static inline void *serialize_linked_list(linked_list_t *list, void (*array_setting_function)(void*, size_t, void*), void *array) {
extern inline void *serialize_linked_list(linked_list_t *list, void (*array_setting_function)(void*, size_t, void*), void *array) {
/* Local variables. */
node_t *current;
size_t i = 0L;

View File

@ -90,5 +90,5 @@ int clear_list_single_linked_list(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_single_linked_list(linked_list_t **list, void (*free_function)(void*));
/* This function attempts to serialize a linked list into an array, it relies on an array setting function and an allocated buffer. */
static inline void *serialize_linked_list(linked_list_t *list, void (*array_setting_function)(void*, size_t, void*), void *array);
extern inline void *serialize_linked_list(linked_list_t *list, void (*array_setting_function)(void*, size_t, void*), void *array);
#endif /* SINGLE_LINKED_LIST_H */

View File

@ -23,7 +23,7 @@ queue_tests.o: queue_tests.c
gcc -g -c -fanalyzer -fsanitize=address -fsanitize=leak -fsanitize=undefined queue_tests.c -o queue_tests.o
queue_tests.out: queue_tests.o single_linked_list.o
gcc -g -fsanitize=address -fsanitize=leak -fsanitize=undefined single_linked_list.o queue_tests.o -o queue_tests.out
gcc -g -fsanitize=address -fsanitize=leak -fsanitize=undefined queue_tests.o single_linked_list.o -o queue_tests.out
quick_sort_tests.out: array_print.o quick_sort_tests.o
gcc -g -fanalyzer -Wanalyzer-too-complex -fanalyzer-call-summaries -fsanitize=address -fsanitize=leak -fsanitize=undefined array_print.o quick_sort_tests.o -o quick_sort_tests.out

View File

@ -17,6 +17,7 @@
/* Define functions. */
node_t* generate_random_node();
queue_t* generate_long_random_queue(int length);
static inline void set_array_value_index(void *array, size_t index, void *value);
int queue_count_length(queue_t *queue);
int test_serialization();
int test_queues();
@ -102,6 +103,15 @@ queue_t* generate_long_random_queue(int length) {
return queue;
}
/* This function is used to set a value at an index in the 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 queue. */
int queue_count_length(queue_t *queue) {
if (!queue || !queue->list || !queue->list->head)
@ -120,23 +130,29 @@ int queue_count_length(queue_t *queue) {
/* This function attempts to allocate a queue, 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 queue. */
queue_t *queue = initialize_queue();
if (queue == NULL)
if (!queue)
return 1;
/* Allocate the array. */
array = (int*) malloc(sizeof(int) * SERIALIZATION_TEST_QUEUE_LENGTH);
if (!array) {
free(queue);
return 1;
}
/* Fill the queue. */
for (i = 0; i < SERIALIZATION_TEST_QUEUE_LENGTH; i++) {
/* Allocate the value store. */
value = (int*) malloc(sizeof(int));
if (value == NULL) {
if (!value) {
/* Clear the queue. */
clear_queue_data(&queue, NULL);
/* Free the array. */
@ -150,10 +166,8 @@ int test_serialization() {
enqueue_data(queue, value);
}
/* Serialize the queue. */
array = serialize_queue(queue, &length);
/* Check if the serialization failed. */
if (length == -1 || length != queue->list->length) {
/* Serialize the queue and check if the serialization failed. */
if (!serialize_queue(queue, set_array_value_index, array)) {
/* Clear the queue. */
clear_queue_data(&queue, NULL);
/* Free the array. */
@ -164,7 +178,7 @@ int test_serialization() {
/* Check the values. */
for (i = 0; i < SERIALIZATION_TEST_QUEUE_LENGTH; i++) {
if (*((int *)(array[i])) != i) {
if (array[i] != i) {
/* Clear the queue. */
clear_queue_data(&queue, NULL);
/* Free the array. */