2
* ivykis, an event handling library
3
* Copyright (C) 2002, 2003 Lennert Buytenhek
4
* Dedicated to Marija Kulikova.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License version
8
* 2.1 as published by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Lesser General Public License version 2.1 for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License version 2.1 along with this program; if not, write to the
17
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18
* Boston, MA 02110-1301, USA.
26
#include <sys/types.h>
31
tree_node_print(int depth, struct iv_avl_node *an,
32
void (*print)(struct iv_avl_node *an))
36
for (i = 0; i < depth; i++)
38
printf("%p (parent=%p): ", an, an->parent);
42
tree_node_print(depth + 1, an->left, print);
43
if (an->right != NULL)
44
tree_node_print(depth + 1, an->right, print);
48
tree_print(char *msg, struct iv_avl_tree *this,
49
void (*print)(struct iv_avl_node *an))
52
if (this->root != NULL)
53
tree_node_print(0, this->root, print);
59
struct iv_avl_node an;
63
static void printit(struct iv_avl_node *an)
65
struct foo *f = container_of(an, struct foo, an);
67
printf("%d (height %d)\n", f->num, f->an.height);
71
tree_node_verify(struct iv_avl_tree *this, struct iv_avl_node *an,
72
struct iv_avl_node *parent,
73
struct iv_avl_node *min, struct iv_avl_node *max, int *cnt)
79
if (an->parent != parent) {
80
printf("parent mismatch: %p, %p versus %p\n",
81
an, an->parent, parent);
87
if (min != NULL || max != NULL) {
91
if (min != NULL && this->compare(min, an) >= 0)
93
if (max != NULL && this->compare(an, max) >= 0)
97
printf("violated %p < %p < %p\n", min, an, max);
101
if (an->left != NULL)
102
hl = tree_node_verify(this, an->left, an, min, an, cnt);
105
if (an->right != NULL)
106
hr = tree_node_verify(this, an->right, an, an, max, cnt);
108
if (abs(hl - hr) > 1)
109
printf("balance mismatch!\n");
111
my = 1 + ((hl > hr) ? hl : hr);
112
if (an->height != my) {
113
printf("height mismatch: %d vs %d/%d\n", an->height, hl, hr);
120
static void tree_check(struct iv_avl_tree *this, int expected_count)
125
if (this->root != NULL)
126
tree_node_verify(this, this->root, NULL, NULL, NULL, &count);
128
if (expected_count >= 0 && expected_count != count) {
129
printf("count mismatch: %d versus %d\n", count, expected_count);
135
static int docomp(struct iv_avl_node *_a, struct iv_avl_node *_b)
137
struct foo *a = container_of(_a, struct foo, an);
138
struct foo *b = container_of(_b, struct foo, an);
151
static struct iv_avl_tree x;
152
static struct foo *f[NUM];
156
struct iv_avl_node *an;
159
srandom(time(NULL) ^ getpid());
161
INIT_IV_AVL_TREE(&x, docomp);
163
for (i = 0; i < NUM; i++) {
167
printf("inserting #%d\n", i);
169
f[i] = malloc(sizeof(struct foo));
172
f[i]->num = random();
173
ret = iv_avl_tree_insert(&x, &f[i]->an);
174
} while (ret == -EEXIST);
177
fprintf(stderr, "error %d\n", ret);
181
tree_check(&x, i + 1);
186
iv_avl_tree_for_each (an, &x)
189
for (i = 0; i < NUM; i++) {
191
printf("deleting #%d\n", i);
195
iv_avl_tree_delete(&x, an);
197
tree_check(&x, NUM - i - 1);
200
tree_print("deletions done", &x, printit);