| 1 |
/* |
| 2 |
* Tests for Miscellaneous Tree Operations |
| 3 |
* This test contains all of the minor tests that did not fit anywhere else. |
| 4 |
*/ |
| 5 |
|
| 6 |
#undef NDEBUG |
| 7 |
#include <assert.h> |
| 8 |
#include <c-stdaux.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
#include "c-rbtree.h" |
| 13 |
#include "c-rbtree-private.h" |
| 14 |
|
| 15 |
static void insert(CRBTree *t, CRBNode *n) { |
| 16 |
CRBNode **i, *p; |
| 17 |
|
| 18 |
c_assert(t); |
| 19 |
c_assert(n); |
| 20 |
c_assert(!c_rbnode_is_linked(n)); |
| 21 |
|
| 22 |
i = &t->root; |
| 23 |
p = NULL; |
| 24 |
while (*i) { |
| 25 |
p = *i; |
| 26 |
if (n < *i) { |
| 27 |
i = &(*i)->left; |
| 28 |
} else { |
| 29 |
c_assert(n > *i); |
| 30 |
i = &(*i)->right; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
c_rbtree_add(t, p, i, n); |
| 35 |
} |
| 36 |
|
| 37 |
static void test_move(void) { |
| 38 |
CRBTree t1 = C_RBTREE_INIT, t2 = C_RBTREE_INIT; |
| 39 |
CRBNode n[128]; |
| 40 |
unsigned int i; |
| 41 |
|
| 42 |
for (i = 0; i < sizeof(n) / sizeof(*n); ++i) { |
| 43 |
n[i] = (CRBNode)C_RBNODE_INIT(n[i]); |
| 44 |
insert(&t1, &n[i]); |
| 45 |
} |
| 46 |
|
| 47 |
c_assert(!c_rbtree_is_empty(&t1)); |
| 48 |
c_assert(c_rbtree_is_empty(&t2)); |
| 49 |
|
| 50 |
c_rbtree_move(&t2, &t1); |
| 51 |
|
| 52 |
c_assert(c_rbtree_is_empty(&t1)); |
| 53 |
c_assert(!c_rbtree_is_empty(&t2)); |
| 54 |
|
| 55 |
while (t2.root) |
| 56 |
c_rbnode_unlink(t2.root); |
| 57 |
|
| 58 |
c_assert(c_rbtree_is_empty(&t1)); |
| 59 |
c_assert(c_rbtree_is_empty(&t2)); |
| 60 |
} |
| 61 |
|
| 62 |
int main(int argc, char **argv) { |
| 63 |
test_move(); |
| 64 |
|
| 65 |
return 0; |
| 66 |
} |