8.7 Testing |
The testing code harbors no surprises.
332. <tavl-test.c 332> = <Program License 2> #include <assert.h> #include <limits.h> #include <stdio.h> #include "tavl.h" #include "test.h" <TBST print function; tbst => tavl 293> <BST traverser check function; bst => tavl 105> <Compare two TAVL trees for structure and content 333> <Recursively verify TAVL tree structure 334> <AVL tree verify function; avl => tavl 192> <BST test function; bst => tavl 101> <BST overflow test function; bst => tavl 123>
333. <Compare two TAVL trees for structure and content 333> = static int
compare_trees (struct tavl_node *a, struct tavl_node *b)
{ int okay; if (a == NULL || b == NULL)
{ if (a != NULL || b != NULL)
{ printf (" a=%d b=%d\n", a ? *(int *) a->tavl_data : -1,
b ? *(int *) b->tavl_data : -1); assert (0); } return 1; } assert (a != b); if (*(int *) a->tavl_data != *(int *) b->tavl_data || a->tavl_tag[0] != b->tavl_tag[0]
|| a->tavl_tag[1] != b->tavl_tag[1] || a->tavl_balance != b->tavl_balance)
{ printf (" Copied nodes differ: a=%d (bal=%d) b=%d (bal=%d) a:", *(int *) a->tavl_data, a->tavl_balance, *(int *) b->tavl_data, b->tavl_balance); if (a->tavl_tag[0] == TAVL_CHILD)
printf ("l"); if (a->tavl_tag[1] == TAVL_CHILD)
printf ("r"); printf (" b:"); if (b->tavl_tag[0] == TAVL_CHILD)
printf ("l"); if (b->tavl_tag[1] == TAVL_CHILD)
printf ("r"); printf ("\n"); return 0; } if (a->tavl_tag[0] == TAVL_THREAD) assert ((a->tavl_link[0] == NULL) != (a->tavl_link[0] != b->tavl_link[0])); if (a->tavl_tag[1] == TAVL_THREAD) assert ((a->tavl_link[1] == NULL) != (a->tavl_link[1] != b->tavl_link[1])); okay = 1; if (a->tavl_tag[0] == TAVL_CHILD) okay &= compare_trees (a->tavl_link[0], b->tavl_link[0]); if (a->tavl_tag[1] == TAVL_CHILD) okay &= compare_trees (a->tavl_link[1], b->tavl_link[1]); return okay; }
This code is included in 332.
334. <Recursively verify TAVL tree structure 334> = static void
recurse_verify_tree (struct tavl_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. */ if (node == NULL)
{ *count = 0; *height = 0; return; } d = *(int *) node->tavl_data; <Verify binary search tree ordering 115> subcount[0] = subcount[1] = 0; subheight[0] = subheight[1] = 0; if (node->tavl_tag[0] == TAVL_CHILD) recurse_verify_tree (node->tavl_link[0], okay, &subcount[0], min, d - 1, &subheight[0]); if (node->tavl_tag[1] == TAVL_CHILD) recurse_verify_tree (node->tavl_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]); <Verify AVL node balance factor; avl => tavl 191> }
This code is included in 332.
8.6 Copying | 8 Threaded AVL Trees | 9 Threaded Red-Black Trees |