www.delorie.com/gnu/docs/avl/libavl_258.html   search  
 
Buy GNU books!


GNU libavl 2.0.1

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

15.8 Testing

The testing code harbors no surprises.

 
&#60;@xref{\NODE\, , License.&#62;,1}
#include &#60;assert.h&#62;
#include &#60;limits.h&#62;
#include &#60;stdio.h&#62;
#include "pavl.h"
#include "test.h"
&#60;@xref{\NODE\, , BST print function; bst =>.&#62; pavl,119}
&#60;@xref{\NODE\, , BST traverser check function; bst =>.&#62; pavl,104}
&#60;@xref{\NODE\, , Compare two PAVL trees for structure and content.&#62;,549}
&#60;@xref{\NODE\, , Recursively verify PAVL tree structure.&#62;,550}
&#60;@xref{\NODE\, , AVL tree verify function; avl =>.&#62; pavl,190}
&#60;@xref{\NODE\, , BST test function; bst =>.&#62; pavl,100}
&#60;@xref{\NODE\, , BST overflow test function; bst =>.&#62; pavl,122}

 
/* Compares binary trees rooted at a and b,
   making sure that they are identical. */
static int 
compare_trees (struct pavl_node *a, struct pavl_node *b)
{ int okay; if (a == NULL || b == NULL)
{ assert (a == NULL && b == NULL); return 1; } if (*(int *) a-&#62;pavl_data != *(int *) b-&#62;pavl_data || ((a-&#62;pavl_link[0] != NULL) != (b-&#62;pavl_link[0] != NULL)) || ((a-&#62;pavl_link[1] != NULL) != (b-&#62;pavl_link[1] != NULL)) || ((a-&#62;pavl_parent != NULL) != (b-&#62;pavl_parent != NULL)) || (a-&#62;pavl_parent != NULL && b-&#62;pavl_parent != NULL && a-&#62;pavl_parent-&#62;pavl_data != b-&#62;pavl_parent-&#62;pavl_data) || a-&#62;pavl_balance != b-&#62;pavl_balance)
{ printf ("Copiednodesdiffer:\n" "a:%d,bal%+d,parent%d,%sleftchild,%srightchild\n" "b:%d,bal%+d,parent%d,%sleftchild,%srightchild\n", *(int *) a-&#62;pavl_data, a-&#62;pavl_balance, a-&#62;pavl_parent != NULL ? *(int *) a-&#62;pavl_parent : -1, a-&#62;pavl_link[0] != NULL ? "has" : "no", a-&#62;pavl_link[1] != NULL ? "has" : "no", *(int *) b-&#62;pavl_data, b-&#62;pavl_balance, b-&#62;pavl_parent != NULL ? *(int *) b-&#62;pavl_parent : -1, b-&#62;pavl_link[0] != NULL ? "has" : "no", b-&#62;pavl_link[1] != NULL ? "has" : "no"); return 0; } okay = 1; if (a-&#62;pavl_link[0] != NULL) okay &= compare_trees (a-&#62;pavl_link[0], b-&#62;pavl_link[0]); if (a-&#62;pavl_link[1] != NULL) okay &= compare_trees (a-&#62;pavl_link[1], b-&#62;pavl_link[1]); return okay; }
This code is included in @refalso{548

 
static void 
recurse_verify_tree (struct pavl_node *node, int *okay, size_t *count, int min, int max, int *height)
{ int d; /* Value of this node's data. */ size_t subcount[2]; /* Number of nodes in subtrees. */ int subheight[2]; /* Heights of subtrees. */ int i; if (node == NULL)
{ *count = 0; *height = 0; return; } d = *(int *) node-&#62;pavl_data; &#60;@xref{\NODE\, , Verify binary search tree ordering.&#62;,114} recurse_verify_tree (node-&#62;pavl_link[0], okay, &subcount[0], min, d - 1, &subheight[0]); recurse_verify_tree (node-&#62;pavl_link[1], okay, &subcount[1], d + 1, max, &subheight[1]); *count = 1 + subcount[0] + subcount[1]; *height = 1 + (subheight[0] > subheight[1] ? subheight[0] : subheight[1]); &#60;@xref{\NODE\, , Verify AVL node balance factor; avl =>.&#62; pavl,189} &#60;@xref{\NODE\, , Verify PBST node parent pointers; pbst =>.&#62; pavl,518} }
This code is included in @refalso{548


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003