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
|
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include "btree.h"
char *program_name = "btree-test";
int output_debug;
// #define DEBUG
#ifdef DEBUG
# ifndef debug
# define debug(level, fmt, ...) \
if (level <= output_debug) \
{ \
printf (fmt "\n", ##__VA_ARGS__); \
fflush (stddout); \
}
# else
# define debug(level, fmt, ...) do { } while (0)
# endif
#endif
struct region
{
uintptr_t start;
uintptr_t length;
};
/* Compare two regions. Two regions are considered equal if there is
any overlap at all. */
static int
region_compare (const struct region *a, const struct region *b)
{
uintptr_t a_end = a->start + (a->length - 1);
uintptr_t b_end = b->start + (b->length - 1);
int ret = 0;
if (a_end < b->start)
ret = -1;
if (a->start > b_end)
ret = 1;
debug (5, "%d+%d %s %d+%d\n",
a->start, a_end,
ret == 0 ? "==" : ret == -1 ? "<" : ">",
b->start, b_end);
return ret;
}
struct region_node
{
hurd_btree_node_t node;
struct region region;
};
BTREE_CLASS(region_node, struct region_node, struct region, region, node,
region_compare, true)
void
print_node (struct region_node *a)
{
printf ("%d+%d%s(%d)",
a->region.start, a->region.start + a->region.length - 1,
BTREE_NODE_RED_P (&a->node) ? "r" : "b",
BTREE_NP (a->node.parent)
? ((struct region_node *) BTREE_NP (a->node.parent))->region.start
: -1);
}
bool
do_print_nodes (struct region_node *a, int depth)
{
bool saw_one = false;
if (depth > 0)
{
if (a)
{
struct region_node *l, *r;
l = (struct region_node *) BTREE_NP_CHILD (a->node.left);
r = (struct region_node *) BTREE_NP_CHILD (a->node.right);
saw_one = do_print_nodes (l, depth - 1);
saw_one |= do_print_nodes (r, depth - 1);
}
else
{
do_print_nodes (NULL, depth - 1);
do_print_nodes (NULL, depth - 1);
}
}
else
{
if (a)
{
print_node (a);
saw_one = true;
}
else
printf ("NULL");
printf (" ");
}
return saw_one;
}
void
print_nodes (struct region_node *a, int depth)
{
int i;
bool saw_one = true;
for (i = 0; i < depth && saw_one; i ++)
{
saw_one = do_print_nodes (a, i);
printf ("\n");
}
}
int
main (int argc, char *argv[])
{
hurd_btree_region_node_t root;
hurd_btree_region_node_tree_init (&root);
/* 0-5, 2-7, 4-9, ... */
#define COUNT 100
#define LENGTH 5
int i;
for (i = 0; i < 100; i += 2)
{
struct region_node *node = calloc (sizeof (struct region_node), 1);
assert (node);
node->region.start = i;
node->region.length = 5;
struct region_node *conflict
= hurd_btree_region_node_insert (&root, node);
assert (! conflict);
int j;
for (j = 0; j <= i + 10; j ++)
{
struct region region;
region.start = j;
region.length = 20;
node = hurd_btree_region_node_find_first (&root, ®ion);
if (node)
debug (5, "Searching for %d-%d and got %d-%d",
region.start, region.start + region.length - 1,
node->region.start,
node->region.start + node->region.length - 1);
else
debug (5, "Searching for %d-%d and got NULL",
region.start, region.start + region.length - 1);
if (j >= 0 && j <= i + LENGTH - 1)
{
int expect = j - LENGTH + 1;
if (expect < 0)
expect = 0;
if (expect % 2 == 1)
expect ++;
debug (5, "Expected %d-%d", expect, expect + LENGTH - 1);
if (! node || node->region.start != expect)
{
print_nodes (BTREE_NP (root.btree.root), 6);
printf ("\n");
fflush (stdout);
}
assert (node);
assert (node->region.start == expect);
}
else
{
debug (5, "Expected NULL");
assert (! node);
}
}
}
return 0;
}
|