Main Page | Compound List | File List | Compound Members

TreeTest Class Reference

List of all members.

Detailed Description

Unit test for the BinaryTree class.


Static Public Member Functions

void main (String argv[])

Private Member Functions

boolean equals (Object[] treeArray, OrderedVector ordVec)
void treeTest ()
 Test that values in the BinaryTree object are ordered properly and that object delete works properly.


Member Function Documentation

boolean TreeTest.equals Object[]  treeArray,
OrderedVector  ordVec
[private]
 

00082     {
00083         boolean rslt = true;
00084 
00085         if (ordVec.size() != treeArray.length) {
00086             rslt = false;
00087         }
00088         else {
00089             for (int i = 0; i < treeArray.length; i++) {
00090                 Integer i1 = (Integer)treeArray[i];
00091                 Integer i2 = (Integer)ordVec.elementAt(i);
00092                 if (i1.compareTo(i2) != 0) {
00093                     rslt = false;
00094                     break;
00095                 }
00096             }
00097         }
00098         return rslt;
00099     } // equals

void TreeTest.main String  argv[]  )  [static]
 

00160     {
00161         TreeTest test = new TreeTest();
00162         test.treeTest();
00163     }

void TreeTest.treeTest  )  [private]
 

Test that values in the BinaryTree object are ordered properly and that object delete works properly.

This test builds an ordered Vector which mirrors the ordered contents of the binary tree. Elements are removed from the tree and from the ordered vector. A check is then made that both the ordered vector and the contents of the tree are the same. This is a test, so no thought is given to efficiency.

00113     {
00114         Random rand = new Random( 127 );
00115         IntegerComparator compare = new IntegerComparator();
00116         BinaryTree tree = new BinaryTree( compare );
00117 
00118         final int INT_RANGE = 0xffff;
00119         final int NUM_VALS = 256;
00120 
00121         OrderedVector ordVec = new OrderedVector();
00122 
00123         for (int i = 0; i < NUM_VALS; i++) {
00124             int iRand = rand.nextInt( INT_RANGE );
00125             Integer I = new Integer( iRand );
00126             tree.insert( I );
00127             ordVec.add( I );
00128         } // for
00129 
00130         if (!equals(tree.toArray(), ordVec)) {
00131             System.out.println("Initially, tree array and ordered vector are not equal");
00132         }
00133         else {
00134             final int NUM_TO_REMOVE = NUM_VALS / 2;
00135             boolean testPassed = true;
00136             int count = 0;
00137             while (count < NUM_TO_REMOVE) {
00138                 int range = ordVec.size();
00139                 int index = rand.nextInt( range );
00140                 Integer intObj = (Integer)ordVec.elementAt( index );
00141                 ordVec.remove( index );
00142                 tree.delete( intObj );
00143                 if (!equals(tree.toArray(), ordVec)) {
00144                     testPassed = false;
00145                     break;
00146                 }
00147                 count++;
00148             } // while
00149             if (testPassed) {
00150                 System.out.println("Test passed");
00151             }
00152             else {
00153                 System.out.println("Test failed");
00154                 System.out.println("count = " + count );
00155             }
00156         }
00157     }


The documentation for this class was generated from the following file:
Generated on Mon Sep 1 20:49:25 2003 for BinaryTree by doxygen 1.3.3