import java.io.*;

/**
 * A class to test both a DictionaryEntry class and a BST implementation.
 *
 * @author Zach Tomaszewski
 * @version 19 November 2003
 */
public class TestBST {
  
  /**
   * Runs a number to tests, calling each method of BinarySearchTree
   * and a couple methods from DictionaryEntry.  
   * Prints the tests being run, expected results,
   * and received results to the screen (System.out).
   */
  public static void main(String[] args){
  
    try{
      BufferedReader stdin = new BufferedReader(
                                 new InputStreamReader(System.in));

      System.out.println("Testing DictionaryEntry's compareTo first...");
      DictionaryEntry dog = new DictionaryEntry("dog", "bark");
      DictionaryEntry cat = new DictionaryEntry("cat", "meow");
      DictionaryEntry capitalDog = new DictionaryEntry("Dog", "");

      System.out.print("'cat' compareTo 'dog' [<0]: ");
      System.out.println(cat.compareTo(dog));
      System.out.print("'dog' compareTo 'cat' [>0]: ");
      System.out.println(dog.compareTo(cat));
      System.out.print("'cat' compareTo 'cat' [==0]: ");
      System.out.println(cat.compareTo(cat));
      System.out.print("'Dog' compareTo 'dog' [==0]: ");
      System.out.println(capitalDog.compareTo(dog));
      System.out.println();
      System.out.println("a DictionaryEntry toString: ");
      System.out.println(dog.toString());

      System.out.print("\n(Pausing... Hit enter to continue.)");
      stdin.readLine();
      System.out.println();

      System.out.println("Now testing BST with numbers..");
      System.out.println("Creating new BinarySearchTree.");
      BinarySearchTree bst = new BinarySearchTree();
      System.out.print("Size [0]: "); 
      System.out.println(bst.size());
      System.out.print("isEmpty [true]: "); 
      System.out.println(bst.isEmpty());
      System.out.print("\nAdding Integers... ");
      try{
        bst.insert(new Integer(100));
        bst.insert(new Integer(50));
        bst.insert(new Integer(70));
        bst.insert(new Integer(130));
        bst.insert(new Integer(115));
        bst.insert(new Integer(150));
        bst.insert(new Integer(180));
        bst.insert(new Integer(40));

      }catch (BSTException bste){
        System.out.println("failed (BSTException).");
        System.out.println("Please fix your code and try again.");
        System.exit(0);
      }
      System.out.println("done.");
      
      System.out.print("Size [8]: "); 
      System.out.println(bst.size());
      System.out.print("isEmpty [false]: "); 
      System.out.println(bst.isEmpty());

      try{
        System.out.print("Adding a duplicate object [exception]: ");
        bst.insert(new Integer(150));
        System.out.println("failed -- no exception thrown.");
      }catch (BSTException bste){
        System.out.println("exception (correct)");
      }


      System.out.println();
      System.out.println("toStringing the BST "+
                         "(numbers should be in sorted order):");
      System.out.println(bst.toString());
      System.out.println();

      try{
        System.out.print("Retrieving [180]: ");
        System.out.println(bst.retrieve(new Integer(180)));
        System.out.print("Retrieving [50]: ");
        System.out.println(bst.retrieve(new Integer(50)));
        System.out.print("Retrieving [115]: ");
        System.out.println(bst.retrieve(new Integer(115)));
        System.out.print("Retrieving [150]: ");
        System.out.println(bst.retrieve(new Integer(150)));
        System.out.print("Retrieving [100]: ");
        System.out.println(bst.retrieve(new Integer(100)));
      }catch (BSTException bste){
        System.out.println("failed (BSTException " + bste.getMessage() + ").");
        System.out.println("Please fix your code and try again.");
        System.exit(0);
      }

      try{
        System.out.print("Retrieving an item not in tree [exception]: ");
        bst.retrieve(new Integer(300));
        System.out.println("failed -- no exception thrown.");
      }catch (BSTException bste){
        System.out.println("exception (correct)");
      }

      System.out.print("\nPausing... (Hit enter to continue.)");
      stdin.readLine();
      System.out.println();

      try{
        System.out.print("Removing [70]: ");
        System.out.println(bst.remove(new Integer(70)));
        System.out.print("Removing [50]: ");
        System.out.println(bst.remove(new Integer(50)));
        System.out.print("Removing [150]: ");
        System.out.println(bst.remove(new Integer(150)));
        System.out.print("Removing [130]: ");
        System.out.println(bst.remove(new Integer(130)));
        System.out.print("Removing [100]: ");
        System.out.println(bst.remove(new Integer(100)));
        System.out.println("printing the BST [40 115 180]:");
      System.out.println(bst.toString());

      }catch (BSTException bste){
        System.out.println("failed (BSTException " + bste.getMessage() + ").");
        System.out.println("Please fix your code and try again.");
        System.exit(0);
      }
                  
      System.out.print("Size [3]: "); 
      System.out.println(bst.size());

      try{
        System.out.print("Removing an item not in tree [exception]: ");
        bst.remove(new Integer(666));
        System.out.println("failed -- no exception thrown.");
      }catch (BSTException bste){
        System.out.println("exception (correct)");
      }
      
      System.out.println();
      System.out.println("Removing all.");
      bst.removeAll();
      System.out.print("Size [0]: "); 
      System.out.println(bst.size());
      System.out.print("isEmpty [true]: "); 
      System.out.println(bst.isEmpty());
      
      System.out.println("\nTesting DONE.");
      
    }catch (IOException ioe){
      System.out.println("IO Exception occurred.  Unable to proceed.");
    }
  }

}//end class

