Some fixes + improvements (Vector) + code reformatting
[cgds.git] / test / t.Tree.c
CommitLineData
a7868768
BA
1#include <stdlib.h>
2#include "cgds/Tree.h"
aef8a996
BA
3#include "helpers.h"
4#include "lut.h"
a7868768 5
4029c991 6void t_tree_clear()
a7868768 7{
e45132ac
BA
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);
a7868768
BA
25}
26
4029c991 27void t_tree_size()
a7868768 28{
e45132ac 29 Tree* t = tree_new(int);
a7868768 30
e45132ac
BA
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);
a7868768 36
e45132ac
BA
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);
a7868768 40
e45132ac
BA
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);
a7868768 46
e45132ac 47 tree_destroy(t);
a7868768
BA
48}
49
4029c991 50void t_tree_add_remove()
a7868768 51{
e45132ac
BA
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);
a7868768
BA
75}
76
4029c991 77void t_tree_iterate()
a7868768 78{
e45132ac
BA
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 }
7820a2aa 101 lu_assert(!treeI_has_data(ti));
e45132ac
BA
102
103 treeI_destroy(ti);
104 tree_destroy(t);
a7868768
BA
105}
106
4029c991 107void t_tree_copy()
a7868768 108{
e45132ac
BA
109 Tree* t = tree_new(int);
110
111 tree_set_root(t, 0);
112 tree_add_child(t, t->root, 1);
113 tree_add_child(t, t->root, 2);
114 tree_add_child(t, t->root, 3);
115 tree_add_child(t, t->root->firstChild, 1);
116 tree_add_child(t, t->root->firstChild, 2);
117 tree_add_child(t, t->root->firstChild->next, 1);
118 tree_add_child(t, t->root->firstChild->next, 2);
119 tree_add_child(t, t->root->firstChild->next, 3);
120 tree_add_child(t, t->root->firstChild->next, 4);
121
122 Tree* tc = tree_copy(t);
123 lu_assert_int_eq(t->size, tc->size);
124
125 TreeIterator* ti = tree_get_iterator(t, IN_DEPTH);
126 TreeIterator* tci = tree_get_iterator(tc, IN_DEPTH);
127 int a, b;
128 while (treeI_has_data(ti))
129 {
130 treeI_get(ti, a);
131 treeI_get(tci, b);
132 lu_assert_int_eq(a, b);
133 treeI_move_next(ti);
134 treeI_move_next(tci);
135 }
136 treeI_destroy(ti);
137 treeI_destroy(tci);
138
139 tree_destroy(t);
140 tree_destroy(tc);
a7868768 141}