forked from ebiggers/avl_tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
274 lines (232 loc) · 5.92 KB
/
test.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* This is a test program for avl_tree.h and avl_tree.c. Compile with:
*
* $ gcc test.c avl_tree.c -o test -std=c99 -Wall -O2
*
* The test strategy isn't very sophisticated; it just relies on repeated random
* operations to cover as many cases as possible. Feel free to improve it.
*
* -----------------------------------------------------------------------------
*
* Written in 2014-2016 by Eric Biggers <ebiggers3@gmail.com>
*
* To the extent possible under law, the author(s) have dedicated all copyright
* and related and neighboring rights to this software to the public domain
* worldwide via the Creative Commons Zero 1.0 Universal Public Domain
* Dedication (the "CC0").
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the CC0 for more details.
*
* You should have received a copy of the CC0 along with this software; if not
* see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include "avl_tree.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
/* Change this to 0, and this turns into a benchmark program. */
#define VERIFY 1
struct test_node {
#if VERIFY
int height;
int reached;
#endif
int n;
struct avl_tree_node node;
};
static struct avl_tree_node *root = NULL;
static struct test_node *nodes;
static int node_idx;
static inline int
avl_get_balance_factor(const struct avl_tree_node *node)
{
return (int)(node->parent_balance & 3) - 1;
}
#define TEST_NODE(__node) avl_tree_entry(__node, struct test_node, node)
#define INT_VALUE(node) TEST_NODE(node)->n
#define HEIGHT(node) ((node) ? TEST_NODE(node)->height : 0)
#define max(a, b) ((a) > (b) ? (a) : (b))
static int
cmp_int_nodes(const struct avl_tree_node *node1,
const struct avl_tree_node *node2)
{
return INT_VALUE(node1) - INT_VALUE(node2);
}
static void
insert(int n)
{
struct test_node *i = &nodes[node_idx++];
i->n = n;
assert(NULL == avl_tree_insert(&root, &i->node, cmp_int_nodes));
}
static struct test_node *
lookup(int n)
{
struct test_node query;
struct avl_tree_node *result;
query.n = n;
result = avl_tree_lookup_node(root, &query.node, cmp_int_nodes);
return result ? TEST_NODE(result) : NULL;
}
static void
deletenode(struct test_node *node)
{
avl_tree_remove(&root, &node->node);
}
static void
delete(int n)
{
struct test_node *node;
node = lookup(n);
assert(node);
deletenode(node);
}
#if VERIFY
static void
__setheights(struct avl_tree_node *node)
{
if (node) {
assert(node->left != node);
assert(node->right != node);
__setheights(node->left);
__setheights(node->right);
TEST_NODE(node)->height = max(HEIGHT(node->left), HEIGHT(node->right)) + 1;
}
}
static void
__checktree(struct avl_tree_node *node)
{
int f = avl_get_balance_factor(node);
assert(f >= -1 && f <= 1);
assert(f == HEIGHT(node->right) - HEIGHT(node->left));
if (node->left) {
assert(INT_VALUE(node->left) < INT_VALUE(node));
__checktree(node->left);
}
if (node->right) {
assert(INT_VALUE(node->right) > INT_VALUE(node));
__checktree(node->right);
}
}
static void
setheights(void)
{
__setheights(root);
}
static void
checktree(void)
{
if (root)
__checktree(root);
}
static int
cmp_ints(const void *p1, const void *p2)
{
return *(const int *)p1 - *(const int *)p2;
}
static void
verify(const int *data, int count)
{
const struct avl_tree_node *cur;
int x;
int data_sorted[count];
memcpy(data_sorted, data, count * sizeof(data[0]));
qsort(data_sorted, count, sizeof(data[0]), cmp_ints);
/* Check in-order traversal. */
for (cur = avl_tree_first_in_order(root), x = 0;
cur;
cur = avl_tree_next_in_order(cur), x++)
{
assert(INT_VALUE(cur) == data_sorted[x]);
TEST_NODE(cur)->reached = 0;
}
assert(x == count);
/* Check reverse in-order traversal. */
for (cur = avl_tree_last_in_order(root), x = count - 1;
cur;
cur = avl_tree_prev_in_order(cur), x--)
{
assert(INT_VALUE(cur) == data_sorted[x]);
TEST_NODE(cur)->reached = 0;
}
assert(x == -1);
/* Check postorder traversal. */
for (cur = avl_tree_first_in_postorder(root), x = 0;
cur;
cur = avl_tree_next_in_postorder(cur, avl_get_parent(cur)), x++)
{
assert(!(TEST_NODE(cur)->reached));
TEST_NODE(cur)->reached = 1;
assert(!avl_get_parent(cur) ||
!TEST_NODE(avl_get_parent(cur))->reached);
assert(!cur->left || TEST_NODE(cur->left)->reached);
assert(!cur->right || TEST_NODE(cur->right)->reached);
}
assert(x == count);
}
#endif
static void
shuffle(int data[], int count)
{
for (int i = 0; i < count; i++) {
const int x = rand() % count;
const int v0 = data[0];
const int vx = data[x];
data[0] = vx;
data[x] = v0;
}
}
static void
test(int data[], int count)
{
shuffle(data, count);
node_idx = 0;
/* Insert the data, checking the AVL tree invariants after each step. */
for (int i = 0; i < count; i++) {
insert(data[i]);
#if VERIFY
setheights();
checktree();
verify(data, i + 1);
#endif
}
/* Delete the data in random order, checking the AVL tree invariants
* after each step. */
shuffle(data, count);
for (int i = 0; i < count; i++) {
delete(data[i]);
#if VERIFY
setheights();
checktree();
verify(data + (i + 1), count - (i + 1));
#endif
}
}
int
main(void)
{
const int num_iterations = 100000;
const int max_node_count = 50;
int data[max_node_count];
for (int i = 0; i < max_node_count; i++)
data[i] = i;
nodes = malloc(max_node_count * sizeof(nodes[0]));
printf("Using max_node_count=%d\n", max_node_count);
for (int i = 0; i < num_iterations; i++) {
if (i % 1024 == 0)
printf("Iteration %d/%d\n", i, num_iterations);
/* Reset the tree. */
root = NULL;
/* Do the test with a random number of nodes, up to the
* 'max_node_count'. */
test(data, rand() % max_node_count);
/* Shuffle the array. */
shuffle(data, max_node_count);
}
printf("Done.\n");
free(nodes);
return 0;
}