|
| 1 | +/* ************************************************************************** */ |
| 2 | +/* */ |
| 3 | +/* ::: :::::::: */ |
| 4 | +/* test_main.c :+: :+: :+: */ |
| 5 | +/* +:+ +:+ +:+ */ |
| 6 | +/* By: jko <[email protected]> +#+ +:+ +#+ */ |
| 7 | +/* +#+#+#+#+#+ +#+ */ |
| 8 | +/* Created: 2020/04/05 17:50:41 by jko #+# #+# */ |
| 9 | +/* Updated: 2020/04/05 19:12:55 by jko ### ########.fr */ |
| 10 | +/* */ |
| 11 | +/* ************************************************************************** */ |
| 12 | + |
| 13 | +#include "tree.h" |
| 14 | +#include <stdio.h> |
| 15 | + |
| 16 | +int cmp(void *a, void *b) |
| 17 | +{ |
| 18 | + return (*(int *)a - *(int *)b); |
| 19 | +} |
| 20 | + |
| 21 | +void print_tree(int index, t_node *node) |
| 22 | +{ |
| 23 | + if (!node) |
| 24 | + return ; |
| 25 | + printf("#%d node : %d\n", index, *(int *)node->data); |
| 26 | + print_tree(index * 2, node->left); |
| 27 | + print_tree(index * 2 + 1, node->right); |
| 28 | +} |
| 29 | + |
| 30 | +void free_data(void *a) |
| 31 | +{ |
| 32 | + printf("free data %d\n", *(int *)a); |
| 33 | +} |
| 34 | + |
| 35 | +int main(void) |
| 36 | +{ |
| 37 | + t_node *temp; |
| 38 | + int nums[101]; |
| 39 | + for (int i = 0; i < 101; ++i) { |
| 40 | + nums[i] = i; |
| 41 | + } |
| 42 | + |
| 43 | + t_tree *tree = tree_init(cmp); |
| 44 | + printf("size = %d\n", tree_size(tree)); |
| 45 | + print_tree(1, tree->root); |
| 46 | + |
| 47 | + tree_insert(tree, &nums[50]); |
| 48 | + printf("size = %d\n", tree_size(tree)); |
| 49 | + print_tree(1, tree->root); |
| 50 | + |
| 51 | + tree_insert(tree, &nums[1]); |
| 52 | + printf("size = %d\n", tree_size(tree)); |
| 53 | + print_tree(1, tree->root); |
| 54 | + |
| 55 | + tree_insert(tree, &nums[32]); |
| 56 | + printf("size = %d\n", tree_size(tree)); |
| 57 | + print_tree(1, tree->root); |
| 58 | + |
| 59 | + tree_insert(tree, &nums[63]); |
| 60 | + printf("size = %d\n", tree_size(tree)); |
| 61 | + print_tree(1, tree->root); |
| 62 | + |
| 63 | + tree_insert(tree, &nums[50]); |
| 64 | + printf("size = %d\n", tree_size(tree)); |
| 65 | + print_tree(1, tree->root); |
| 66 | + |
| 67 | + tree_insert(tree, &nums[1]); |
| 68 | + printf("size = %d\n", tree_size(tree)); |
| 69 | + print_tree(1, tree->root); |
| 70 | + |
| 71 | + tree_insert(tree, &nums[32]); |
| 72 | + printf("size = %d\n", tree_size(tree)); |
| 73 | + print_tree(1, tree->root); |
| 74 | + |
| 75 | + tree_insert(tree, &nums[63]); |
| 76 | + printf("size = %d\n", tree_size(tree)); |
| 77 | + print_tree(1, tree->root); |
| 78 | + |
| 79 | + |
| 80 | + if ((temp = tree_find(tree, &nums[63]))) |
| 81 | + printf("find %d\n", *(int *)temp->data); |
| 82 | + else |
| 83 | + printf("not found\n"); |
| 84 | + |
| 85 | + tree_delete(tree, &nums[63], free_data); |
| 86 | + printf("size = %d\n", tree_size(tree)); |
| 87 | + print_tree(1, tree->root); |
| 88 | + |
| 89 | + tree = 0; |
| 90 | + system("leaks a.out > leaks_result && cat leaks_result | grep leaked"); |
| 91 | + |
| 92 | + return (0); |
| 93 | +} |
0 commit comments