Implemented BinaryTree_Remove() and wrote a test for it
This commit is contained in:
parent
34201c1165
commit
60c3f81884
4 changed files with 286 additions and 44 deletions
|
@ -1,5 +1,6 @@
|
|||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "../src/BinaryTree/BinaryTree.h"
|
||||
|
||||
|
@ -35,6 +36,15 @@ void testLifetime(void)
|
|||
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;
|
||||
|
@ -54,6 +64,76 @@ void testInsert(void)
|
|||
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));
|
||||
|
||||
|
@ -63,19 +143,14 @@ void testInsert(void)
|
|||
value = 4;
|
||||
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
|
||||
|
||||
value = 5;
|
||||
assert(EXIT_FAILURE == 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));
|
||||
|
||||
assert(EXIT_SUCCESS == BinaryTree_Remove(&tree, &value));
|
||||
assert(EXIT_SUCCESS == BinaryTree_Insert(&tree, &value));
|
||||
|
||||
BinaryTree_Destroy(&tree);
|
||||
assert(allocator.reserved == 0);
|
||||
assert(allocator.reserved == 0);
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -117,10 +192,36 @@ void testEqual(void)
|
|||
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;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
testLifetime();
|
||||
testInsert();
|
||||
testEqual();
|
||||
testMany();
|
||||
testRemoval();
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue