]> git.lizzy.rs Git - dragonstd.git/blob - test/test_tree.c
refactoring + documentation + testing + added Map and Refcount
[dragonstd.git] / test / test_tree.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <time.h>
5 #include "../tree.h"
6
7 #define NUM_ELEMENTS 1e5
8
9 int cmp_int(const void *ia, const void *ib)
10 {
11         return *(const int *) ia - *(const int *) ib;
12 }
13
14 void clear_callback(void *ia, void *ib)
15 {
16         assert(*(int *) ia > *(int *) ib);
17         free(ia);
18 }
19
20 int main()
21 {
22         srand(time(NULL));
23
24         printf("------------\n");
25         printf("Testing Tree\n");
26         printf("------------\n");
27
28         Tree tree;
29
30         printf("testing ini\n");
31         tree_ini(&tree);
32
33         int a = 0;
34         int b = 1;
35         int c = 2;
36         int d = 2;
37         int e = 3;
38
39         printf("testing add\n");
40         assert(tree_add(&tree, &a, &cmp_int, NULL) == &a);
41         assert(tree_add(&tree, &b, &cmp_int, NULL) == &b);
42         assert(tree_add(&tree, &c, &cmp_int, NULL) == &c);
43         assert(tree_add(&tree, &d, &cmp_int, NULL) == &c);
44
45         printf("testing get\n");
46         assert(tree_get(&tree, &a, &cmp_int, NULL) == &a);
47         assert(tree_get(&tree, &b, &cmp_int, NULL) == &b);
48         assert(tree_get(&tree, &c, &cmp_int, NULL) == &c);
49         assert(tree_get(&tree, &d, &cmp_int, NULL) == &c);
50         assert(tree_get(&tree, &e, &cmp_int, NULL) == NULL);
51
52         printf("testing del\n");
53         assert(tree_del(&tree, &a, &cmp_int, NULL) == &a);
54         assert(tree_get(&tree, &a, &cmp_int, NULL) == NULL);
55
56         printf("testing clr\n");
57         tree_clr(&tree, NULL, NULL, 0);
58         assert(tree_get(&tree, &b, &cmp_int, NULL) == NULL);
59
60         printf("testing order and speed with %d elements\n", (int) NUM_ELEMENTS);
61         for (int i = 0; i < NUM_ELEMENTS; i++) {
62                 int *n = malloc(sizeof *n);
63                 *n = rand();
64
65                 if (tree_add(&tree, n, &cmp_int, NULL) != n)
66                         free(n);
67         }
68
69         int last = -1;
70         tree_clr(&tree, &clear_callback, &last, TRAVERSION_INORDER);
71 }