1
0
Fork 0

Slight test code improvements for single linked list.

This commit is contained in:
wael 2022-01-03 22:49:28 +02:00
parent 14a4162fc3
commit 106b1817b2
No known key found for this signature in database
GPG Key ID: C0A5FBF4558963D4
2 changed files with 17 additions and 17 deletions

View File

@ -17,7 +17,6 @@
/* This header defines a single-linked-list and related functions. */
/* Use the single nodes. */
#include <stdlib.h> /* Used in allocations and deallocations. */
#include <string.h> /* For using memcpy. */
#ifndef SINGLE_LINKED_LIST_H
#define SINGLE_LINKED_LIST_H
#include "../nodes/single_node.h"

View File

@ -17,7 +17,7 @@
/* Define functions. */
node_t* generate_random_node();
linked_list_t* generate_long_random_list(int length);
void set_array_value_index(void *array, size_t index, void *value);
static inline void set_array_value_index(void *array, size_t index, void *value);
int list_count_length(linked_list_t *list);
int test_serialization();
int test_linked_lists();
@ -55,15 +55,6 @@ node_t* generate_random_node() {
return result;
}
/* This function is used to set a value at an index in the array. */
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 generates a long list with random values in the nodes, returns NULL on failures. */
/* Length denotes the length of the list. */
linked_list_t* generate_long_random_list(int length) {
@ -76,7 +67,7 @@ linked_list_t* generate_long_random_list(int length) {
int i;
/* Allocate the list. */
list = initialize_single_linked_list(sizeof(int));
list = initialize_single_linked_list();
if (list == NULL)
return NULL;
@ -111,6 +102,15 @@ linked_list_t* generate_long_random_list(int length) {
return list;
}
/* 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 list. */
int list_count_length(linked_list_t *list) {
if (list == NULL || list->head == NULL)
@ -136,12 +136,13 @@ int test_serialization() {
/* Allocations. */
/* Initialize the list. */
linked_list_t *list = initialize_single_linked_list(sizeof(int));
if (list == NULL)
linked_list_t *list = initialize_single_linked_list();
if (!list)
return 1;
/* Allocate the array. */
array = (int*) malloc(sizeof(int) * SERIALIZATION_TEST_LIST_LENGTH);
if (array == NULL) {
if (!array) {
free(list);
return 1;
}
@ -150,7 +151,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_single_linked_list(&list, NULL);
/* Free the array. */
@ -203,7 +204,7 @@ int test_linked_lists() {
int error;
/* Initialize an empty list. */
list = initialize_single_linked_list(sizeof(int));
list = initialize_single_linked_list();
/* Check that it was correctly created. */
assert(list != NULL);
assert(list->length == INITIAL_LIST_LENGTH);