Binary Tree with working insertions, no duplicates

This commit is contained in:
VegOwOtenks 2024-07-01 16:31:45 +02:00
parent 30f288e5d7
commit fe63781996
6 changed files with 464 additions and 0 deletions

126
tests/BinaryTree.test.c Normal file
View file

@ -0,0 +1,126 @@
#include <assert.h>
#include <stdlib.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;
}
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));
value = 2;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
value = 4;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
value = 7;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
value = 6;
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
value = 8;
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;
}
int main()
{
testLifetime();
testInsert();
testEqual();
return 0;
}

View file

@ -36,3 +36,11 @@ target_link_libraries(regex-test
StringView
utf8
)
add_executable(BinaryTree-test BinaryTree.test.c)
target_link_libraries(BinaryTree-test
BinaryTree
dynamicarray
pointers
allocator-interface
)