| 1 |
/* |
| 2 |
* Tests for Basic Tree Operations |
| 3 |
* This test does some basic tree operations and verifies their correctness. It |
| 4 |
* validates the RB-Tree invariants after each operation, to guarantee the |
| 5 |
* stability of the tree. |
| 6 |
* |
| 7 |
* For testing purposes, we use the memory address of a node as its key, and |
| 8 |
* order nodes in ascending order. |
| 9 |
*/ |
| 10 |
|
| 11 |
#undef NDEBUG |
| 12 |
#include <assert.h> |
| 13 |
#include <c-stdaux.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> |
| 17 |
#include <time.h> |
| 18 |
#include "c-rbtree.h" |
| 19 |
#include "c-rbtree-private.h" |
| 20 |
|
| 21 |
static size_t validate(CRBTree *t) { |
| 22 |
unsigned int i_black, n_black; |
| 23 |
CRBNode *n, *p, *o; |
| 24 |
size_t count = 0; |
| 25 |
|
| 26 |
c_assert(t); |
| 27 |
c_assert(!t->root || c_rbnode_is_black(t->root)); |
| 28 |
|
| 29 |
/* traverse to left-most child, count black nodes */ |
| 30 |
i_black = 0; |
| 31 |
n = t->root; |
| 32 |
while (n && n->left) { |
| 33 |
if (c_rbnode_is_black(n)) |
| 34 |
++i_black; |
| 35 |
n = n->left; |
| 36 |
} |
| 37 |
n_black = i_black; |
| 38 |
|
| 39 |
/* |
| 40 |
* Traverse tree and verify correctness: |
| 41 |
* 1) A node is either red or black |
| 42 |
* 2) The root is black |
| 43 |
* 3) All leaves are black |
| 44 |
* 4) Every red node must have two black child nodes |
| 45 |
* 5) Every path to a leaf contains the same number of black nodes |
| 46 |
* |
| 47 |
* Note that NULL nodes are considered black, which is why we don't |
| 48 |
* check for 3). |
| 49 |
*/ |
| 50 |
o = NULL; |
| 51 |
while (n) { |
| 52 |
++count; |
| 53 |
|
| 54 |
/* verify natural order */ |
| 55 |
c_assert(n > o); |
| 56 |
o = n; |
| 57 |
|
| 58 |
/* verify consistency */ |
| 59 |
c_assert(!n->right || c_rbnode_parent(n->right) == n); |
| 60 |
c_assert(!n->left || c_rbnode_parent(n->left) == n); |
| 61 |
|
| 62 |
/* verify 2) */ |
| 63 |
if (!c_rbnode_parent(n)) |
| 64 |
c_assert(c_rbnode_is_black(n)); |
| 65 |
|
| 66 |
if (c_rbnode_is_red(n)) { |
| 67 |
/* verify 4) */ |
| 68 |
c_assert(!n->left || c_rbnode_is_black(n->left)); |
| 69 |
c_assert(!n->right || c_rbnode_is_black(n->right)); |
| 70 |
} else { |
| 71 |
/* verify 1) */ |
| 72 |
c_assert(c_rbnode_is_black(n)); |
| 73 |
} |
| 74 |
|
| 75 |
/* verify 5) */ |
| 76 |
if (!n->left && !n->right) |
| 77 |
c_assert(i_black == n_black); |
| 78 |
|
| 79 |
/* get next node */ |
| 80 |
if (n->right) { |
| 81 |
n = n->right; |
| 82 |
if (c_rbnode_is_black(n)) |
| 83 |
++i_black; |
| 84 |
|
| 85 |
while (n->left) { |
| 86 |
n = n->left; |
| 87 |
if (c_rbnode_is_black(n)) |
| 88 |
++i_black; |
| 89 |
} |
| 90 |
} else { |
| 91 |
while ((p = c_rbnode_parent(n)) && n == p->right) { |
| 92 |
n = p; |
| 93 |
if (c_rbnode_is_black(p->right)) |
| 94 |
--i_black; |
| 95 |
} |
| 96 |
|
| 97 |
n = p; |
| 98 |
if (p && c_rbnode_is_black(p->left)) |
| 99 |
--i_black; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return count; |
| 104 |
} |
| 105 |
|
| 106 |
static void insert(CRBTree *t, CRBNode *n) { |
| 107 |
CRBNode **i, *p; |
| 108 |
|
| 109 |
c_assert(t); |
| 110 |
c_assert(n); |
| 111 |
c_assert(!c_rbnode_is_linked(n)); |
| 112 |
|
| 113 |
i = &t->root; |
| 114 |
p = NULL; |
| 115 |
while (*i) { |
| 116 |
p = *i; |
| 117 |
if (n < *i) { |
| 118 |
i = &(*i)->left; |
| 119 |
} else { |
| 120 |
c_assert(n > *i); |
| 121 |
i = &(*i)->right; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
c_rbtree_add(t, p, i, n); |
| 126 |
} |
| 127 |
|
| 128 |
static void shuffle(CRBNode **nodes, size_t n_memb) { |
| 129 |
unsigned int i, j; |
| 130 |
CRBNode *t; |
| 131 |
|
| 132 |
for (i = 0; i < n_memb; ++i) { |
| 133 |
j = rand() % n_memb; |
| 134 |
t = nodes[j]; |
| 135 |
nodes[j] = nodes[i]; |
| 136 |
nodes[i] = t; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
static void test_shuffle(void) { |
| 141 |
CRBNode *nodes[512]; |
| 142 |
CRBTree t = {}; |
| 143 |
unsigned int i, j; |
| 144 |
size_t n; |
| 145 |
|
| 146 |
/* allocate and initialize all nodes */ |
| 147 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) { |
| 148 |
nodes[i] = malloc(sizeof(*nodes[i])); |
| 149 |
c_assert(nodes[i]); |
| 150 |
c_rbnode_init(nodes[i]); |
| 151 |
} |
| 152 |
|
| 153 |
/* shuffle nodes and validate *empty* tree */ |
| 154 |
shuffle(nodes, sizeof(nodes) / sizeof(*nodes)); |
| 155 |
n = validate(&t); |
| 156 |
c_assert(n == 0); |
| 157 |
|
| 158 |
/* add all nodes and validate after each insertion */ |
| 159 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) { |
| 160 |
insert(&t, nodes[i]); |
| 161 |
n = validate(&t); |
| 162 |
c_assert(n == i + 1); |
| 163 |
} |
| 164 |
|
| 165 |
/* shuffle nodes again */ |
| 166 |
shuffle(nodes, sizeof(nodes) / sizeof(*nodes)); |
| 167 |
|
| 168 |
/* remove all nodes (in different order) and validate on each round */ |
| 169 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) { |
| 170 |
c_rbnode_unlink(nodes[i]); |
| 171 |
n = validate(&t); |
| 172 |
c_assert(n == sizeof(nodes) / sizeof(*nodes) - i - 1); |
| 173 |
} |
| 174 |
|
| 175 |
/* shuffle nodes and validate *empty* tree again */ |
| 176 |
shuffle(nodes, sizeof(nodes) / sizeof(*nodes)); |
| 177 |
n = validate(&t); |
| 178 |
c_assert(n == 0); |
| 179 |
|
| 180 |
/* add all nodes again */ |
| 181 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) { |
| 182 |
insert(&t, nodes[i]); |
| 183 |
n = validate(&t); |
| 184 |
c_assert(n == i + 1); |
| 185 |
} |
| 186 |
|
| 187 |
/* 4 times, remove half of the nodes and add them again */ |
| 188 |
for (j = 0; j < 4; ++j) { |
| 189 |
/* shuffle nodes again */ |
| 190 |
shuffle(nodes, sizeof(nodes) / sizeof(*nodes)); |
| 191 |
|
| 192 |
/* remove half of the nodes */ |
| 193 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes) / 2; ++i) { |
| 194 |
c_rbnode_unlink(nodes[i]); |
| 195 |
n = validate(&t); |
| 196 |
c_assert(n == sizeof(nodes) / sizeof(*nodes) - i - 1); |
| 197 |
} |
| 198 |
|
| 199 |
/* shuffle the removed half */ |
| 200 |
shuffle(nodes, sizeof(nodes) / sizeof(*nodes) / 2); |
| 201 |
|
| 202 |
/* add the removed half again */ |
| 203 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes) / 2; ++i) { |
| 204 |
insert(&t, nodes[i]); |
| 205 |
n = validate(&t); |
| 206 |
c_assert(n == sizeof(nodes) / sizeof(*nodes) / 2 + i + 1); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/* shuffle nodes again */ |
| 211 |
shuffle(nodes, sizeof(nodes) / sizeof(*nodes)); |
| 212 |
|
| 213 |
/* remove all */ |
| 214 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) { |
| 215 |
c_rbnode_unlink(nodes[i]); |
| 216 |
n = validate(&t); |
| 217 |
c_assert(n == sizeof(nodes) / sizeof(*nodes) - i - 1); |
| 218 |
} |
| 219 |
|
| 220 |
/* free nodes again */ |
| 221 |
for (i = 0; i < sizeof(nodes) / sizeof(*nodes); ++i) |
| 222 |
free(nodes[i]); |
| 223 |
} |
| 224 |
|
| 225 |
int main(int argc, char **argv) { |
| 226 |
unsigned int i; |
| 227 |
|
| 228 |
/* we want stable tests, so use fixed seed */ |
| 229 |
srand(0xdeadbeef); |
| 230 |
|
| 231 |
/* |
| 232 |
* The tests are pseudo random; run them multiple times, each run will |
| 233 |
* have different orders and thus different results. |
| 234 |
*/ |
| 235 |
for (i = 0; i < 4; ++i) |
| 236 |
test_shuffle(); |
| 237 |
|
| 238 |
return 0; |
| 239 |
} |