Simplify installation procedure
[cgds.git] / test / t.Tree.c
1 #include <stdlib.h>
2 #include "cgds/Tree.h"
3 #include "helpers.h"
4 #include "lut.h"
5
6 void t_tree_clear()
7 {
8 Tree* t = tree_new(int);
9
10 tree_set_root(t, 0);
11 tree_add_child(t, t->root, 1);
12 tree_add_child(t, t->root, 2);
13 tree_add_child(t, t->root, 3);
14 tree_add_child(t, t->root->firstChild, 1);
15 tree_add_child(t, t->root->firstChild, 2);
16 tree_add_child(t, t->root->firstChild->next, 1);
17 tree_add_child(t, t->root->firstChild->next, 2);
18 tree_add_child(t, t->root->firstChild->next, 3);
19 tree_add_child(t, t->root->firstChild->next, 4);
20
21 tree_clear(t);
22 lu_assert(tree_empty(t));
23
24 tree_destroy(t);
25 }
26
27 void t_tree_size()
28 {
29 Tree* t = tree_new(int);
30
31 tree_set_root(t, 0);
32 tree_add_child(t, t->root, 1);
33 tree_add_child(t, t->root, 2);
34 tree_add_child(t, t->root, 3);
35 lu_assert_int_eq(tree_size(t), 4);
36
37 tree_add_child(t, t->root->firstChild, 1);
38 tree_add_child(t, t->root->firstChild, 2);
39 lu_assert_int_eq(tree_size(t), 6);
40
41 tree_add_child(t, t->root->firstChild->next, 1);
42 tree_add_child(t, t->root->firstChild->next, 2);
43 tree_add_child(t, t->root->firstChild->next, 3);
44 tree_add_child(t, t->root->firstChild->next, 4);
45 lu_assert_int_eq(tree_size(t), 10);
46
47 tree_destroy(t);
48 }
49
50 void t_tree_add_remove()
51 {
52 Tree* t = tree_new(int);
53
54 tree_set_root(t, 0);
55 tree_add_child(t, t->root, 1);
56 tree_add_child(t, t->root, 2);
57 tree_add_child(t, t->root, 3);
58 tree_add_child(t, t->root->firstChild, 1);
59 tree_add_child(t, t->root->firstChild, 2);
60 tree_add_child(t, t->root->firstChild->next, 1);
61 tree_add_child(t, t->root->firstChild->next, 2);
62 tree_add_child(t, t->root->firstChild->next, 3);
63 tree_add_child(t, t->root->firstChild->next, 4);
64 tree_add_child(t, t->root->lastChild, 1);
65 tree_add_child(t, t->root->lastChild, 2);
66 tree_add_child(t, t->root->lastChild, 3);
67 lu_assert_int_eq(tree_size(t), 13);
68
69 tree_remove(t, t->root->lastChild);
70 lu_assert_int_eq(tree_size(t), 9);
71 tree_rm_childs(t, t->root->firstChild);
72 lu_assert_int_eq(tree_size(t), 7);
73
74 tree_destroy(t);
75 }
76
77 void t_tree_iterate()
78 {
79 Tree* t = tree_new(int);
80
81 tree_set_root(t, 0);
82 tree_add_child(t, t->root, 1);
83 tree_add_child(t, t->root, 2);
84 tree_add_child(t, t->root, 3);
85 tree_add_child(t, t->root->firstChild, 4);
86 tree_add_child(t, t->root->firstChild, 5);
87 tree_add_child(t, t->root->firstChild->next, 6);
88 tree_add_child(t, t->root->firstChild->next, 7);
89 tree_add_child(t, t->root->firstChild->next, 8);
90 tree_add_child(t, t->root->firstChild->next, 9);
91
92 TreeIterator* ti = tree_get_iterator(t, IN_BREADTH);
93 int a;
94 for (int i = 0; i < 10; i++)
95 {
96 lu_assert(treeI_has_data(ti));
97 treeI_get(ti, a);
98 lu_assert_int_eq(a, i);
99 treeI_move_next(ti);
100 }
101
102 treeI_destroy(ti);
103 tree_destroy(t);
104 }
105
106 void t_tree_copy()
107 {
108 Tree* t = tree_new(int);
109
110 tree_set_root(t, 0);
111 tree_add_child(t, t->root, 1);
112 tree_add_child(t, t->root, 2);
113 tree_add_child(t, t->root, 3);
114 tree_add_child(t, t->root->firstChild, 1);
115 tree_add_child(t, t->root->firstChild, 2);
116 tree_add_child(t, t->root->firstChild->next, 1);
117 tree_add_child(t, t->root->firstChild->next, 2);
118 tree_add_child(t, t->root->firstChild->next, 3);
119 tree_add_child(t, t->root->firstChild->next, 4);
120
121 Tree* tc = tree_copy(t);
122 lu_assert_int_eq(t->size, tc->size);
123
124 TreeIterator* ti = tree_get_iterator(t, IN_DEPTH);
125 TreeIterator* tci = tree_get_iterator(tc, IN_DEPTH);
126 int a, b;
127 while (treeI_has_data(ti))
128 {
129 treeI_get(ti, a);
130 treeI_get(tci, b);
131 lu_assert_int_eq(a, b);
132 treeI_move_next(ti);
133 treeI_move_next(tci);
134 }
135 treeI_destroy(ti);
136 treeI_destroy(tci);
137
138 tree_destroy(t);
139 tree_destroy(tc);
140 }