Add basic Set implementation - TODO: add iterators for Set and HashTable
[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 lu_assert(!treeI_has_data(ti));
102
103 treeI_destroy(ti);
104 tree_destroy(t);
105 }
106
107 void t_tree_copy()
108 {
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);
141 }