1
0
Fork 0

Updated tests for heaps and BSTs.

This commit is contained in:
wael 2022-01-29 20:58:37 +02:00
parent d25af752db
commit f3c3735e47
No known key found for this signature in database
GPG Key ID: C0A5FBF4558963D4
2 changed files with 162 additions and 1 deletions

View File

@ -66,7 +66,6 @@ int test_binary_search() {
/* Attempt to allocate the function run time struct. */
runtime = (function_run_time*) malloc(sizeof(function_run_time));
if (runtime == NULL) {
/* TODO: Check why the sanitizer complains of a leak. */
printf(TESTING_ALLOCATION_ERROR);
exit(ALLOCATION_ERROR_CODE);
}

162
tests/max_heap_tests.c Normal file
View File

@ -0,0 +1,162 @@
/* Include statements. */
#include <stdio.h>
#include <assert.h>
#include "../trees/binary_tree/heaps/max_heap/max_heap.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 TEST_INITIALIZE_HEAP_NODE_SUCCESS "initialize_tree_node() passed tests!\n"
#define TEST_INITIALIZE_HEAP_NODE_STORE_SUCCESS "initialized_tree_node_store() passed tests!\n"
#define TEST_HEAP_NODE_LINK_SUCCESS "Heap node linking functions (left and right) passed tests!\n"
#define TEST_FREE_HEAP_SUCCESS "free_max_heap() passed tests!\n"
#define ARRAY_START_POINT 0
#define ARRAY_LENGTH_HEAP_NODES 30
/* Define functions. */
heap_node_t* generate_random_heap_node();
int comparison_function_int(const void *first, const void *second);
max_heap_t* insert_sorted_array_into_heap(int *array, int start, int end);
int test_max_heap();
/* Starting point. */
int main() {
printf(PRINT_INFO_TESTING_START);
printf(test_max_heap() ? SOME_TEST_FAILED : 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. */
heap_node_t* generate_random_heap_node() {
/* The node to return. */
heap_node_t* result;
int *value;
/* Attempt to allocate the node. */
result = (heap_node_t*) malloc(sizeof(heap_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->right = NULL;
result->left = NULL;
result->data = value;
return result;
}
/* This function is used to compare two integers (given their pointers), exhibits undefined behaviour on NULL pointers. */
int comparison_function_int(const void *first, const void *second) {
return *((int*)first) == *((int*)second) ? HEAP_COMPARISON_EQUAL_TO : ((*((int*)first) < *((int*)second)) ? HEAP_COMPARISON_SMALLER_THAN : HEAP_COMPARISON_LARGER_THAN);
}
/* This function will generate a max heap from the given sorted array, returns the root. */
max_heap_t* insert_sorted_array_into_heao(int *array, int start, int end) {
/* Allocate the heap. */
max_heap_t *heap = (max_heap_t*) malloc(sizeof(max_heap_t));
if (!heap)
return NULL;
/* Delegate. */
initialize_max_heap_recursive(array, start, end, heap);
return heap;
}
/* This function does the testing. */
int test_max_heap() {
/* Local variables. */
heap_node_t *root, *right, *left;
int *first_value, *second_value, *array;
/* Allocate the root. */
root = (heap_node_t*) malloc(sizeof(max_heap_t));
if (!root)
return 1;
/* Set all to NULL. */
root->right = NULL;
root->left = NULL;
root->data = NULL;
/* Print result. */
printf(TEST_INITIALIZE_TREE_NODE_SUCCESS);
/* Allocate two nodes with random values. */
first_value = (int*) malloc(sizeof(int));
second_value = (int*) malloc(sizeof(int));
if (!first_value || !second_value) {
free(first_value);
free(second_value);
free(root);
return 1;
}
/* Generate right and left nodes. */
right = (heap_node_t*) malloc(sizeof(heap_node_t));
left = (heap_node_t*) malloc(sizeof(heap_node_t));
if (!right || !left) {
free(first_value);
free(second_value);
free(right);
free(left);
free(root);
return 1;
}
right->data = first_value;
left->data = second_value;
assert(*((int*)right->data) == *first_value && *((int*)left->data) == *second_value);
/* Print result. */
printf(TEST_INITIALIZE_HEAP_NODE_STORE_SUCCESS);
/* Link the left and right nodes. */
assert((root, right) == 0);
assert(link_tree_node_left(root, left) == 0);
assert(root->right == right);
assert(root->left == left);
/* Print result. */
printf(TEST_TREE_NODE_LINK_SUCCESS);
/* Free tree, but not data. */
free_tree(root);
assert(first_value != NULL && second_value != NULL);
/* Print the result. */
printf(TEST_FREE_TREE_SUCCESS);
/* Free the data. */
free(first_value);
first_value = NULL;
/* Free the nodes. */
right = NULL;
left = NULL;
/* Build the bigger tree. */
root = build_tree(&array);
if (root == NULL) {
free(second_value);
return 1;
}
/* Verify that it is indeed a BST. */
//assert(check_tree_bst(root, comparison_function_int) == 1);
/* Attempt to insert an extra node, check where it ends up. */
*second_value = ARRAY_LENGTH_TREE_NODES;
right = initialize_tree_node_store(second_value);
if (right == NULL) {
free_tree_data(root, NULL);
free(second_value);
return 1;
}
/* Get the max node. */
left = root;
while (left->right != NULL)
left = left->right;
/* Attach the node. */
assert(insert_node_into_tree(root, right, comparison_function_int) == TREE_SUCCESS_CODE);
assert(left->right == right);
/* Cleanup. */
free_tree(root);
free(array);
free(second_value);
/* Ran successfully so far, return last test. */
return check_tree_build_data_free();
}