12.5 Testing [ToC] [Index]     [Skip Back]     [Prev] [Up] [Next]

482. <rtrb-test.c 482> =
<License 1>
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include "rtrb.h"
#include "test.h"

<RTBST print function; rtbst => rtrb 412>
<BST traverser check function; bst => rtrb 104>
<Compare two RTRB trees for structure and content 483>
<Recursively verify RTRB tree structure 484>
<RB tree verify function; rb => rtrb 244>
<BST test function; bst => rtrb 100>
<BST overflow test function; bst => rtrb 122>

483. <Compare two RTRB trees for structure and content 483> =
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 482.

484. <Recursively verify RTRB tree structure 484> =
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 114> 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 241> <Verify RTRB node rule 1 compliance 485> <Verify RB node rule 2 compliance; rb => rtrb 243> }

This code is included in 482.

485. <Verify RTRB node rule 1 compliance 485> =
/* 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 484.

Prev: 12.4.3 Step 4: Finish Up Up: 12 Right-Threaded Red-Black Trees 13 BSTs with Parent Pointers Next