-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_red_black_tree.c
208 lines (152 loc) · 4.5 KB
/
test_red_black_tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <core/structures/ordered/red_black_tree.h>
#include <core/structures/vector.h>
#include <core/system/memory_pool.h>
#include "test.h"
#include <stdlib.h>
/*
*/
#define TEST_DELETE
int main(int argc, char **argv)
{
BEGIN_TESTS();
int i;
int lowest;
int count;
int key;
struct core_red_black_tree tree;
struct core_memory_pool memory_pool;
struct core_vector keys;
int *result;
int size;
unsigned int seed;
/*
* This is a test to see how to print an integer with leading 0s.
*/
/*
printf("%02d\n", 3);
*/
lowest = 0;
core_memory_pool_init(&memory_pool, 1024*1024, -1);
core_vector_init(&keys, sizeof(int));
#if 0
#endif
count = 100000;
#if 0
count = 1;
#endif
seed = 88;
core_red_black_tree_init(&tree, sizeof(int), sizeof(int), &memory_pool);
#if 0
core_red_black_tree_set_memory_pool(&tree, &memory_pool);
#endif
#if 0
core_red_black_tree_print(&tree);
#endif
for (i = 0; i < count; ++i) {
key = rand_r(&seed) % (2 * count);
core_vector_push_back(&keys, &key);
if (i == 0)
lowest = key;
if (key < lowest)
lowest = key;
#if 0
printf("add node %d (tree before)\n", key);
core_red_black_tree_print(&tree);
#endif
TEST_INT_EQUALS(core_red_black_tree_has_ignored_rules(&tree), 0);
core_red_black_tree_add(&tree, &key);
TEST_INT_EQUALS(core_red_black_tree_has_ignored_rules(&tree), 0);
TEST_INT_EQUALS(core_red_black_tree_size(&tree), i + 1);
TEST_POINTER_NOT_EQUALS(core_red_black_tree_get(&tree, &key), NULL);
result = core_red_black_tree_get_lowest_key(&tree);
TEST_POINTER_NOT_EQUALS(result, NULL);
#if 0
core_red_black_tree_run_assertions(&tree);
#endif
#if 0
core_red_black_tree_print(&tree);
TEST_INT_EQUALS(*result, lowest);
#endif
#if 0
core_red_black_tree_print(&tree);
#endif
}
#if 0
core_red_black_tree_print(&tree);
#endif
#if 0
core_memory_pool_examine(&memory_pool);
#endif
core_red_black_tree_run_assertions(&tree);
count = 0;
size = count;
for (i = 0; i < count; ++i) {
key = core_vector_at_as_int(&keys, i);
result = core_red_black_tree_get(&tree, &key);
TEST_POINTER_NOT_EQUALS(result, NULL);
#ifdef TEST_DELETE
core_red_black_tree_delete(&tree, &key);
--size;
#endif
TEST_INT_EQUALS(core_red_black_tree_size(&tree), size);
}
core_red_black_tree_destroy(&tree);
core_vector_destroy(&keys);
TEST_INT_EQUALS(core_memory_pool_profile_balance_count(&memory_pool), 0);
core_memory_pool_destroy(&memory_pool);
/*
* Small deletions
*/
{
struct core_red_black_tree tree;
struct core_memory_pool memory_pool;
struct core_vector keys;
core_vector_init(&keys, sizeof(int));
core_memory_pool_init(&memory_pool, 1024*1024, -1);
core_red_black_tree_init(&tree, sizeof(int), sizeof(int), &memory_pool);
i = 0;
size = 100000;
while (i < size) {
key = rand_r(&seed) % (2 * size);
/*
* For this test, we don't want duplicates.
*/
while (core_red_black_tree_get(&tree, &key) != NULL) {
key = rand_r(&seed) % (2 * size);
}
#if 0
printf("Add %d\n", key);
#endif
core_red_black_tree_add(&tree, &key);
#if 0
core_red_black_tree_print(&tree);
#endif
TEST_POINTER_NOT_EQUALS(core_red_black_tree_get(&tree, &key), NULL);
TEST_POINTER_NOT_EQUALS(core_red_black_tree_get_lowest_key(&tree), NULL);
result = core_red_black_tree_get(&tree, &key);
*result = i;
core_vector_push_back(&keys, &key);
++i;
}
i = 0;
#if 0
core_red_black_tree_print(&tree);
#endif
while (i < size) {
key = core_vector_at_as_int(&keys, i);
/*
printf("Delete %d\n", key);
*/
TEST_POINTER_NOT_EQUALS(core_red_black_tree_get(&tree, &key), NULL);
core_red_black_tree_delete(&tree, &key);
TEST_POINTER_EQUALS(core_red_black_tree_get(&tree, &key), NULL);
++i;
}
core_red_black_tree_destroy(&tree);
core_vector_destroy(&keys);
TEST_INT_EQUALS(core_memory_pool_profile_balance_count(&memory_pool), 0);
core_memory_pool_destroy(&memory_pool);
}
END_TESTS();
return 0;
}