126 lines
2.3 KiB
C
126 lines
2.3 KiB
C
#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;
|
|
}
|