12.5 Testing |
484. <rtrb-test.c 484> = <Program License 2> #include <assert.h> #include <limits.h> #include <stdio.h> #include "rtrb.h" #include "test.h" <RTBST print function; rtbst => rtrb 414> <BST traverser check function; bst => rtrb 105> <Compare two RTRB trees for structure and content 485> <Recursively verify RTRB tree structure 486> <RB tree verify function; rb => rtrb 246> <BST test function; bst => rtrb 101> <BST overflow test function; bst => rtrb 123>
485. <Compare two RTRB trees for structure and content 485> = static int
compare_trees (struct rtrb_node *a, struct rtrb_node *b)
{ int okay; if (a == NULL || b == NULL)
{ if (a != NULL || b != NULL)
{ printf (" a=%d b=%d\n", a ? *(int *) a->rtrb_data : -1,
b ? *(int *) b->rtrb_data : -1); assert (0); } return 1; } assert (a != b); if (*(int *) a->rtrb_data != *(int *) b->rtrb_data || a->rtrb_rtag != b->rtrb_rtag || a->rtrb_color != b->rtrb_color)
{ printf (" Copied nodes differ: a=%d%c b=%d%c a:", *(int *) a->rtrb_data, a->rtrb_color == RTRB_RED ? 'r' : 'b', *(int *) b->rtrb_data, b->rtrb_color == RTRB_RED ? 'r' : 'b'); if (a->rtrb_rtag == RTRB_CHILD)
printf ("r"); printf (" b:"); if (b->rtrb_rtag == RTRB_CHILD)
printf ("r"); printf ("\n"); return 0; } if (a->rtrb_rtag == RTRB_THREAD) assert ((a->rtrb_link[1] == NULL) != (a->rtrb_link[1] != b->rtrb_link[1])); okay = compare_trees (a->rtrb_link[0], b->rtrb_link[0]); if (a->rtrb_rtag == RTRB_CHILD) okay &= compare_trees (a->rtrb_link[1], b->rtrb_link[1]); return okay; }
This code is included in 484.
486. <Recursively verify RTRB tree structure 486> = static void
recurse_verify_tree (struct rtrb_node *node, int *okay, size_t *count, int min, int max, int *bh)
{ int d; /* Value of this node's data. */ size_t subcount[2]; /* Number of nodes in subtrees. */ int subbh[2]; /* Black-heights of subtrees. */ if (node == NULL)
{ *count = 0; *bh = 0; return; } d = *(int *) node->rtrb_data; <Verify binary search tree ordering 115> subcount[0] = subcount[1] = 0; subbh[0] = subbh[1] = 0; recurse_verify_tree (node->rtrb_link[0], okay, &subcount[0], min, d - 1, &subbh[0]); if (node->rtrb_rtag == RTRB_CHILD) recurse_verify_tree (node->rtrb_link[1], okay, &subcount[1], d + 1, max, &subbh[1]); *count = 1 + subcount[0] + subcount[1]; *bh = (node->rtrb_color == RTRB_BLACK) + subbh[0]; <Verify RB node color; rb => rtrb 243> <Verify RTRB node rule 1 compliance 487> <Verify RB node rule 2 compliance; rb => rtrb 245> }
This code is included in 484.
487. <Verify RTRB node rule 1 compliance 487> = /* Verify compliance with rule 1. */ if (node->rtrb_color == RTRB_RED)
{ if (node->rtrb_link[0] != NULL
&& node->rtrb_link[0]->rtrb_color == RTRB_RED)
{ printf (" Red node %d has red left child %d\n", d, *(int *) node->rtrb_link[0]->rtrb_data); *okay = 0; } if (node->rtrb_rtag == RTRB_CHILD
&& node->rtrb_link[1]->rtrb_color == RTRB_RED)
{ printf (" Red node %d has red right child %d\n", d, *(int *) node->rtrb_link[1]->rtrb_data); *okay = 0; } }
This code is included in 486.
12.4.3 Step 4: Finish Up | 12 Right-Threaded Red-Black Trees | 13 BSTs with Parent Pointers |