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


GNU libavl 2.0.1

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

8.12 Testing

There's little new in the testing code. We do add an test for tbst_balance(), because none of the existing tests exercise it. This test doesn't check that tbst_balance() actually balances the tree, it just verifies that afterwards the tree contains the items it should, so to be certain that balancing is correct, turn up the verbosity and look at the trees printed.

Function print_tree_structure() prints thread node numbers preceded by `>', with null threads indicated by `>>'. This notation is compatible with the plain text output format of the texitree program used to draw the binary trees in this book. (It will cause errors for PostScript output because it omits node names.)

 
&#60;@xref{\NODE\, , License.&#62;,1}
#include &#60;assert.h&#62;
#include &#60;limits.h&#62;
#include &#60;stdio.h&#62;
#include "tbst.h"
#include "test.h"
&#60;@xref{\NODE\, , TBST print function.&#62;,291}
&#60;@xref{\NODE\, , BST traverser check function; bst =>.&#62; tbst,104}
&#60;@xref{\NODE\, , Compare two TBSTs for structure and content.&#62;,292}
&#60;@xref{\NODE\, , Recursively verify TBST structure.&#62;,293}
&#60;@xref{\NODE\, , TBST verify function.&#62;,294}
&#60;@xref{\NODE\, , TBST test function.&#62;,295}
&#60;@xref{\NODE\, , BST overflow test function; bst =>.&#62; tbst,122}

 
void 
print_tree_structure (struct tbst_node *node, int level)
{ int i; if (level > 16)
{ printf ("[...]"); return; } if (node == NULL)
{ printf ("<nil>"); return; } printf ("%d(", node-&#62;tbst_data ? *(int *) node-&#62;tbst_data : -1); for (i = 0; i <= 1; i++)
{ if (node-&#62;tbst_tag[i] == TBST_CHILD)
{ if (node-&#62;tbst_link[i] == node)
printf ("loop"); else
print_tree_structure (node-&#62;tbst_link[i], level + 1); } else if (node-&#62;tbst_link[i] != NULL) printf (">%d",
(node-&#62;tbst_link[i]-&#62;tbst_data ? *(int *) node-&#62;tbst_link[i]-&#62;tbst_data : -1)); else
printf (">>"); if (i == 0)
fputs (",", stdout); } putchar (')'); } void
print_whole_tree (const struct tbst_table *tree, const char *title)
{ printf ("%s:", title); print_tree_structure (tree-&#62;tbst_root, 0); putchar ('\n'); }
This code is included in @refalso{290

 
static int 
compare_trees (struct tbst_node *a, struct tbst_node *b)
{ int okay; if (a == NULL || b == NULL)
{ if (a != NULL || b != NULL)
{ printf ("a=%db=%d\n", a ? *(int *) a-&#62;tbst_data : -1,
b ? *(int *) b-&#62;tbst_data : -1); assert (0); } return 1; } assert (a != b); if (*(int *) a-&#62;tbst_data != *(int *) b-&#62;tbst_data || a-&#62;tbst_tag[0] != b-&#62;tbst_tag[0]
|| a-&#62;tbst_tag[1] != b-&#62;tbst_tag[1])
{ printf ("Copiednodesdiffer:a=%db=%da:", *(int *) a-&#62;tbst_data, *(int *) b-&#62;tbst_data); if (a-&#62;tbst_tag[0] == TBST_CHILD)
printf ("l"); if (a-&#62;tbst_tag[1] == TBST_CHILD)
printf ("r"); printf ("b:"); if (b-&#62;tbst_tag[0] == TBST_CHILD)
printf ("l"); if (b-&#62;tbst_tag[1] == TBST_CHILD)
printf ("r"); printf ("\n"); return 0; } if (a-&#62;tbst_tag[0] == TBST_THREAD) assert ((a-&#62;tbst_link[0] == NULL) != (a-&#62;tbst_link[0] != b-&#62;tbst_link[0])); if (a-&#62;tbst_tag[1] == TBST_THREAD) assert ((a-&#62;tbst_link[1] == NULL) != (a-&#62;tbst_link[1] != b-&#62;tbst_link[1])); okay = 1; if (a-&#62;tbst_tag[0] == TBST_CHILD) okay &= compare_trees (a-&#62;tbst_link[0], b-&#62;tbst_link[0]); if (a-&#62;tbst_tag[1] == TBST_CHILD) okay &= compare_trees (a-&#62;tbst_link[1], b-&#62;tbst_link[1]); return okay; }
This code is included in @refalso{290

 
static void 
recurse_verify_tree (struct tbst_node *node, int *okay, size_t *count, int min, int max)
{ int d; /* Value of this node's data. */ size_t subcount[2]; /* Number of nodes in subtrees. */ if (node == NULL)
{ *count = 0; return; } d = *(int *) node-&#62;tbst_data; &#60;@xref{\NODE\, , Verify binary search tree ordering.&#62;,114} subcount[0] = subcount[1] = 0; if (node-&#62;tbst_tag[0] == TBST_CHILD) recurse_verify_tree (node-&#62;tbst_link[0], okay, &subcount[0], min, d - 1); if (node-&#62;tbst_tag[1] == TBST_CHILD) recurse_verify_tree (node-&#62;tbst_link[1], okay, &subcount[1], d + 1, max); *count = 1 + subcount[0] + subcount[1]; }
This code is included in @refalso{290

 
static int 
verify_tree (struct tbst_table *tree, int array[], size_t n)
{ int okay = 1; &#60;@xref{\NODE\, , Check tree.&#62;-&#62;bst_count is correct; bst => tbst,110} if (okay)
{
&#60;@xref{\NODE\, , Check BST structure; bst =>.&#62; tbst,111}
} if (okay)
{
&#60;@xref{\NODE\, , Check that the tree contains all the elements it should; bst =>.&#62; tbst,115}
} if (okay)
{
&#60;@xref{\NODE\, , Check that forward traversal works; bst =>.&#62; tbst,116}
} if (okay)
{
&#60;@xref{\NODE\, , Check that backward traversal works; bst =>.&#62; tbst,117}
} if (okay)
{
&#60;@xref{\NODE\, , Check that traversal from the null element works; bst =>.&#62; tbst,118}
} return okay; }
This code is included in @refalso{290

 
int 
test_correctness (struct libavl_allocator *allocator, int insert[], int delete[], int n, int verbosity)
{ struct tbst_table *tree; int okay = 1; int i; &#60;@xref{\NODE\, , Test creating a BST and inserting into it; bst =>.&#62; tbst,102} &#60;@xref{\NODE\, , Test BST traversal during modifications; bst =>.&#62; tbst,103} &#60;@xref{\NODE\, , Test deleting nodes from the BST and making copies of it; bst =>.&#62; tbst,105} &#60;@xref{\NODE\, , Test destroying the tree; bst =>.&#62; tbst,108} &#60;@xref{\NODE\, , Test TBST balancing.&#62;,296} return okay; }
This code is included in @refalso{290

 
/* Test tbst_balance(). */
if (verbosity >= 2) 
printf ("Testingbalancing...\n"); tree = tbst_create (compare_ints, NULL, allocator); if (tree == NULL)
{ if (verbosity >= 0)
printf ("Outofmemorycreatingtree.\n"); return 1; } for (i = 0; i < n; i++)
{ void **p = tbst_probe (tree, &insert[i]); if (p == NULL)
{ if (verbosity >= 0)
printf ("Outofmemoryininsertion.\n"); tbst_destroy (tree, NULL); return 1; } if (*p != &insert[i])
printf ("Duplicateitemintree!\n"); } if (verbosity >= 4)
print_whole_tree (tree, "Pre-balance"); tbst_balance (tree); if (verbosity >= 4)
print_whole_tree (tree, "Post-balance"); if (!verify_tree (tree, insert, n)) return 0; tbst_destroy (tree, NULL);
This code is included in @refalso{295


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

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