utilitiec/tests/BinaryTree.test.c

256 lines
5.7 KiB
C

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "../src/BinaryTree/BinaryTree.h"
int doublecomparator(double* this, double* other, void* xdata)
{
assert(xdata == (void*) 0xDEADBEEF);
if (*this > *other) {
return 1;
} else if (*this < *other) {
return -1;
}
return 0;
}
void testLifetime(void)
{
allocator_t allocator;
Allocator_CreateSystemAllocator(&allocator);
BinaryTree tree;
assert(EXIT_SUCCESS == BinaryTree_Create(
&tree,
(BinaryTreeComparator) doublecomparator,
(void*) 0xDEADBEEF,
sizeof(double),
&allocator)
);
BinaryTree_Destroy(&tree);
assert(allocator.reserved == 0);
return;
}
int _PrintDoubleNode(void* context, BinaryTreeNode* node)
{
(void) context;
for (size_t i = 1; i != node->depth; i++) putchar('\t');
printf("%.0f\n", *(double*) node->value);
return EXIT_SUCCESS;
}
void testInsert(void)
{
allocator_t allocator;
Allocator_CreateSystemAllocator(&allocator);
BinaryTree tree;
assert(EXIT_SUCCESS == BinaryTree_Create(
&tree,
(BinaryTreeComparator) doublecomparator,
(void*) 0xDEADBEEF,
sizeof(double),
&allocator)
);
double value = 5;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
assert(tree.root != NULL);
value = 3;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
assert(*(double*) tree.root->left->value == value);
value = 2;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
assert(*(double*) tree.root->value == 3);
assert(*(double*) tree.root->left->value == 2);
assert(*(double*) tree.root->right->value == 5);
value = 4;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
assert(*(double*) tree.root->value == 3);
assert(*(double*) tree.root->left->value == 2);
assert(*(double*) tree.root->right->value == 5);
assert(*(double*) tree.root->right->left->value == 4);
value = 7;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
assert(*(double*) tree.root->value == 3);
assert(*(double*) tree.root->left->value == 2);
assert(*(double*) tree.root->right->value == 5);
assert(*(double*) tree.root->right->left->value == 4);
assert(*(double*) tree.root->right->right->value == 7);
value = 6;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
assert(*(double*) tree.root->value == 5);
assert(*(double*) tree.root->left->value == 3);
assert(*(double*) tree.root->right->value == 7);
assert(*(double*) tree.root->right->left->value == 6);
assert(*(double*) tree.root->left->left->value == 2);
assert(*(double*) tree.root->left->right->value == 4);
value = 8;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
assert(*(double*) tree.root->value == 5);
assert(*(double*) tree.root->left->value == 3);
assert(*(double*) tree.root->right->value == 7);
assert(*(double*) tree.root->right->left->value == 6);
assert(*(double*) tree.root->right->right->value == 8);
assert(*(double*) tree.root->left->left->value == 2);
assert(*(double*) tree.root->left->right->value == 4);
BinaryTree_Destroy(&tree);
assert(allocator.reserved == 0);
return;
}
void testRemoval(void)
{
allocator_t allocator;
Allocator_CreateSystemAllocator(&allocator);
BinaryTree tree;
assert(EXIT_SUCCESS == BinaryTree_Create(
&tree,
(BinaryTreeComparator) doublecomparator,
(void*) 0xDEADBEEF,
sizeof(double),
&allocator)
);
double value = 5;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
assert(tree.root != NULL);
value = 3;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
value = 2;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
value = 4;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
value = 5;
assert(EXIT_FAILURE == BinaryTree_Insert(&tree, &value));
assert(EXIT_SUCCESS == BinaryTree_Remove(&tree, &value));
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
BinaryTree_Destroy(&tree);
assert(allocator.reserved == 0);
return;
}
void testEqual(void)
{
allocator_t allocator;
Allocator_CreateSystemAllocator(&allocator);
BinaryTree tree;
assert(EXIT_SUCCESS == BinaryTree_Create(
&tree,
(BinaryTreeComparator) doublecomparator,
(void*) 0xDEADBEEF,
sizeof(double),
&allocator)
);
double value = 5;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
assert(tree.root != NULL);
value = 3;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
value = 2;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
value = 4;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
value = 5;
assert(EXIT_FAILURE == BinaryTree_Insert(&tree, &value));
BinaryTree_Destroy(&tree);
assert(allocator.reserved == 0);
return;
}
void testMany(void)
{
allocator_t allocator;
Allocator_CreateSystemAllocator(&allocator);
BinaryTree tree;
assert(EXIT_SUCCESS == BinaryTree_Create(
&tree,
(BinaryTreeComparator) doublecomparator,
(void*) 0xDEADBEEF,
sizeof(double),
&allocator)
);
for (double value = 0; value < 300; value++) {
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
}
BinaryTree_Destroy(&tree);
assert(allocator.reserved == 0);
return;
}
void testRanges(void)
{
allocator_t allocator;
Allocator_CreateSystemAllocator(&allocator);
BinaryTree tree;
assert(EXIT_SUCCESS == BinaryTree_Create(
&tree,
(BinaryTreeComparator) doublecomparator,
(void*) 0xDEADBEEF,
sizeof(double),
&allocator)
);
for (double value = 0; value < 300; value++) {
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
}
for (double value = 150; value <= 200; value++) {
assert(EXIT_SUCCESS == BinaryTree_Remove(&tree, &value));
}
BinaryTree_Destroy(&tree);
assert(allocator.reserved == 0);
return;
}
int main()
{
testLifetime();
testInsert();
testEqual();
testMany();
testRemoval();
testRanges();
return 0;
}