Basic assert function, basic tests

This commit is contained in:
Imbus 2024-03-27 12:11:08 +01:00
parent ee38dceea2
commit 404f821ef3

View file

@ -3,6 +3,20 @@
#include "tree.h" #include "tree.h"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_RESET "\x1b[0m"
int assert(bool condition, const char *message) {
if (!condition) {
printf(ANSI_COLOR_RED "Assertion failed: %s\n" ANSI_COLOR_RESET, message);
exit(1);
} else {
printf(ANSI_COLOR_GREEN "Assertion passed: %s\n" ANSI_COLOR_RESET, message);
}
return 0;
}
int main(void) { int main(void) {
Tree tree = {NULL}; Tree tree = {NULL};
@ -10,23 +24,28 @@ int main(void) {
tree_insert(&tree, 1); tree_insert(&tree, 1);
tree_insert(&tree, 2); tree_insert(&tree, 2);
tree_insert(&tree, 3); tree_insert(&tree, 3);
// Check size and print inorder
printf("Tree size: %d\n", tree_size(tree.root));
tree_inorder(tree.root); tree_inorder(tree.root);
printf("\n");
printf("Tree contains 2: %d\n", tree_contains(tree.root, 2)); // Basic sanity checks
assert(tree.root->data == 1, "Root should be 1");
assert(tree.root->right->data == 2, "Right should be 2");
assert(tree.root->right->right->data == 3, "Right right should be 3");
assert(tree_size(tree.root) == 3, "Tree size should be 3");
// Test removal
assert(tree_contains(tree.root, 2), "Tree should contain 2");
tree_remove(&tree.root, 2);
assert(!tree_contains(tree.root, 2), "Tree should not contain 2");
// Clear the tree // Clear the tree
tree_clear(&tree); tree_clear(&tree);
// Check if the tree is really empty // Check if the tree is empty
if (tree.root == NULL) assert(tree.root == NULL, "Tree should be empty");
printf("Tree is empty\n"); assert(tree_size(tree.root) == 0, "Tree size should be 0");
else
printf("Tree is not empty: %d\n", tree.root->data);
// Check size and print inorder // Print size and inorder traversal
printf("Tree size: %d\n", tree_size(tree.root)); printf("Tree size: %d\n", tree_size(tree.root));
tree_inorder(tree.root); tree_inorder(tree.root);