Tests
This commit is contained in:
parent
6d885040f3
commit
5a02d6537d
2 changed files with 81 additions and 8 deletions
66
test/test.c
Normal file
66
test/test.c
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
#include "tree.h"
|
||||
|
||||
// Function prototypes for helper functions
|
||||
void test_insert(Tree *tree);
|
||||
void test_remove(Tree *tree);
|
||||
void test_contains(Tree *tree);
|
||||
void test_inorder(Tree *tree);
|
||||
|
||||
int main() {
|
||||
Tree tree = { .root = NULL };
|
||||
|
||||
printf("\n-- Testing Insert --\n");
|
||||
test_insert(&tree);
|
||||
|
||||
printf("\n-- Testing Contains --\n");
|
||||
test_contains(&tree);
|
||||
|
||||
printf("\n-- Testing Inorder Traversal --\n");
|
||||
test_inorder(&tree);
|
||||
|
||||
printf("\n-- Testing Remove --\n");
|
||||
test_remove(&tree);
|
||||
|
||||
printf("\n-- Clearing Tree --\n");
|
||||
tree_clear(&tree);
|
||||
printf("Tree cleared.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void test_insert(Tree *tree) {
|
||||
int values[] = {10, 5, 15, 3, 7, 12, 18};
|
||||
for (int i = 0; i < 7; i++) {
|
||||
printf("Inserting %d\n", values[i]);
|
||||
tree_insert(tree, values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void test_remove(Tree *tree) {
|
||||
int values_to_remove[] = {15, 5, 10};
|
||||
for (int i = 0; i < 3; i++) {
|
||||
printf("Removing %d\n", values_to_remove[i]);
|
||||
if (tree_remove(&tree->root, values_to_remove[i]) == 0) {
|
||||
printf("Successfully removed %d\n", values_to_remove[i]);
|
||||
} else {
|
||||
printf("Failed to remove %d\n", values_to_remove[i]);
|
||||
}
|
||||
}
|
||||
printf("Remaining elements (in-order traversal):\n");
|
||||
tree_inorder(tree->root);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void test_contains(Tree *tree) {
|
||||
int values_to_check[] = {7, 15, 20};
|
||||
for (int i = 0; i < 3; i++) {
|
||||
printf("Checking if tree contains %d: %s\n", values_to_check[i],
|
||||
tree_contains(tree->root, values_to_check[i]) ? "Yes" : "No");
|
||||
}
|
||||
}
|
||||
|
||||
void test_inorder(Tree *tree) {
|
||||
printf("Tree elements (in-order traversal):\n");
|
||||
tree_inorder(tree->root);
|
||||
printf("\n");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue