import java.io.*;

/**
 * A testing class for SortedLists.
 *
 * @author Zach Tomaszewski, for ICS211-1
 * @version 19 Oct 2003
 */
public class SortedListTest {


  /**
   * Testing and prints to the screen.
   */
  public static void main (String[] args){

    //IF YOUR CLASS IS NOT CALLED OrderedList, CHANGE IT HERE.
    SortedList list = new OrderedList();
    
    try{
      BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    
      try{
        System.out.println("\nEmpty list is empty [true]: " + list.isEmpty());
        System.out.print("Adding a element (2): ");
        list.add(new Integer(2));
        System.out.println("passed.");
        System.out.println("List is empty now [false]: " + list.isEmpty());
        System.out.println("List size is now [1]: " + list.size());
        
        System.out.print("\nGetting an element: ");
        list.get(1);  //get position one
        System.out.println("passed.");

        System.out.print("\nAdding more elements: ");
        System.out.print("(4 ");
        list.add(new Integer(4));
        System.out.print("1 ");
        list.add(new Integer(1));
        System.out.print("7 ");
        list.add(new Integer(7));
        System.out.print("3 ");
        list.add(new Integer(3));
        System.out.print("5): ");
        list.add(new Integer(5));
        System.out.println("passed.");

        System.out.println("List size is now [6]: " + list.size());
        System.out.println("Printing list:");
        printList(list);
        System.out.println("The list should be sorted (1 2 3 4 5 7).");
        System.out.println("Hit enter to continue.");
        stdin.readLine();  //pause
        
        System.out.print("\nRemoving item at position 3: ");
        list.remove(3);
        System.out.println("passed.");
        System.out.println("List size is now [5]: " + list.size());
        System.out.print("Removing item at position 1: ");
        list.remove(1);
        System.out.println("passed.");
        System.out.println("Printing list [2 4 5 7]:");
        printList(list);
        
        System.out.print("Removing item '2': ");
        list.remove(new Integer(2));
        System.out.println("passed.");
        System.out.print("Removing item '5': ");
        list.remove(new Integer(5));
        System.out.println("passed.");

        System.out.println("Printing list [4 7]:");
        printList(list);
        
        System.out.print("Removing all: ");
        list.removeAll();
        System.out.println("passed.");
        System.out.println("List size is now [0]: " + list.size());
        System.out.println("Hit enter to continue.");
        stdin.readLine();  //pause
                
      }catch (SortedListException sle){
        System.out.println("failed.");
        System.out.println("Please fix your code and try again.");
        System.exit(0);
      }

      
      System.out.println("\nNow checking a few sample error cases.");
      System.out.println("For each case, you should see a \"caught\" status.");
      try{
        System.out.print("\nGetting an item on an empty list: ");
        list.get(3);
        System.out.println("failed.");
      }catch (SortedListException sle){
        System.out.println("caught.");
      }

      try{
        System.out.print("Removing an item from an empty list: ");
        list.remove(3);
        System.out.println("failed.");
      }catch (SortedListException sle){
        System.out.println("caught.");
      }

      try {
        list.add(new Integer(7));
        System.out.print("Removing a non-existent item: ");
        list.remove(new Integer(3));
        System.out.println("failed.");
      }catch (SortedListException sle){
              System.out.println("caught.");
      }
  
      try {
        System.out.print("Getting a position out of bounds: ");
        list.get(2);
        System.out.println("failed.");
      }catch (SortedListException sle){
              System.out.println("caught.");
      }

      try {
        System.out.print("Checking if you support duplicates (your choice): ");
        list.add(new Integer(7));
        if (list.size() == 2) {
          System.out.println("yes");
        }else if (list.size() == 1) {
          System.out.println("no");
        }else {
          System.out.println("strange error.");
        }
        System.out.println("List [7] or [7 7]:");
        printList(list);
      }catch (SortedListException sle){
              System.out.println("throws exception");
      }

      System.out.println("\nDone testing your SortedList implementation.");

    }catch (IOException ioe){
      System.out.println("I/O Error!");
    }
  }
  
  public static void printList (SortedList list){
    try {
      for (int i = 1; i <= list.size(); i++){
        System.out.println(list.get(i));
      }
    }catch (SortedListException sle) {
      System.out.println("Oops!  Tried to get an ungetable object while printing.");
    }
  }

}//end class
