Please send questions to st10@humboldt.edu .
/* Cs132ArrayListTest.java 1.0                       */
/* by Sharon Tuttle                                  */
/* last modified: 3-7-03                             */

public class Cs132ArrayListTest extends JPFalt 
{
    public static void main(String[] args) 
    { 
            new Cs132ArrayListTest(); 
    }
    
    /** Test suite for the class Cs132ArrayList */
        
    /*--------------------------------------------------------------------
     Define a Cs132ArrayList object and print the member data values,
     and test accessor and modifier methods 
    --------------------------------------------------------------------*/
    void TestCs132ArrayList()
    {
        println("\nDefine Cs132ArrayList object, print member data values");

        Cs132ArrayList myList = new Cs132ArrayList();
        println("new, empty list:");
        println(myList);
        println("");

        String stringToAdd = new String("First List Element");
        myList.addFirst(stringToAdd);
        println("list with 1 element:");
        println(myList);
        println("");
       
        Integer intToAdd = new Integer(13);
        myList.addFirst(intToAdd);
        println("list with 2 elements:");
        println(myList);  
        println("");    
    }
    
    /*---------------------------------------------------------------
     Test method for isEmpty()
    -----------------------------------------------------------------*/
    void isEmptyTest()
    {
        Cs132ArrayList myList = new Cs132ArrayList();

        testHeader("isEmpty");
        
        expected(true);
        actual(myList.isEmpty());
        
        String myEntry = new String("Hello");
        myList.addFirst(myEntry);

        expected(false);
        actual(myList.isEmpty());
    }
    
    /*---------------------------------------------------------------
     Test method for size()
    -----------------------------------------------------------------*/
    void sizeTest()
    {
        Cs132ArrayList myList = new Cs132ArrayList();

        testHeader("size");
        
        expected(0);
        actual(myList.size());
        
        String myEntry = new String("Hello");
        myList.addFirst(myEntry);

        expected(1);
        actual(myList.size());
        
        String myEntry2 = new String("Bonjour");
        myList.addFirst(myEntry2);
        
        expected(2);
        actual(myList.size());
    }
    
    /*--------------------------------------------------------------
     Test method for expandArrayWhileAdd()
    -----------------------------------------------------------------*/
    void expandArrayWhileAddTest()
    {
        Cs132ArrayList myList = new Cs132ArrayList();
        
        testHeader("expandArrayWhileAdd");
        
        // add the 10 elements 1-10 to the list
        addNum(10, 1, myList);
        
        println("list contains: " + myList);
        
        // adding this element should cause underlying array
        // to be expanded, adding the new element at the end as it does
        expected("list now contains 11 items -- new one at END");
        myList.addLast(new Integer(11));
        actual(myList);
        
        // add 9 more elements --- 12-20
        addNum(9, 12, myList);
        
        // adding this element should cause the underlying array to
        // be expanded, adding the new element at the beginning as it does
        expected("list now contains 21 items --- new one at BEGINNING");
        myList.addFirst(new Integer(21));
        actual(myList);
        
        // add 9 more elements --- 22-30
        addNum(9, 22, myList);
        
        // adding this element should cause the underlying array to be
        // expanded, adding the new element at the end as it does
        expected("list now contains 31 items --- new one at position 4");
        try
        {    
            myList.add(4, new Integer(31));
        }
        catch(Exception ex) {}
        actual(myList);
    }
           
    /*---------------------------------------------------------------
     addNum
     Purpose: helper function for testing expandArrayWhileAdd();
              it simply adds the specified number of elements numElems, 
              consecutive Integers beginning with the Integer version of 
              passed int firstVal, to the END of passed Cs132List aList.
    ------------------------------------------------------------------*/
    void addNum(int numElems, int firstVal, Cs132ArrayList aList)
    {
        Integer currVal = new Integer(firstVal);
        for (int i=0; i<numElems; i++)
        {
            aList.addLast(currVal);
            
            // guess what? you cannot add Integer objects!
            // you can grab their int equivalent using method intValue().
            // however...
            currVal = new Integer(currVal.intValue() + 1);
        }
    }
              
    
    /*---------------------------------------------------------------
     Test method for addFirst()
    -----------------------------------------------------------------*/
    void addFirstTest()
    {
        Cs132ArrayList myList = new Cs132ArrayList();

        testHeader("addFirst");
                
        expected("list is currently empty:");
        actual(myList);
                
        String myEntry = new String("Hello");
        myList.addFirst(myEntry);

        expected("list contains Hello only");
        actual(myList);
        
        String myEntry2 = new String("Bonjour");
        myList.addFirst(myEntry2);
        
        expected("Bonjour should come BEFORE Hello");
        actual(myList);
        
        Integer myEntry3 = new Integer(13);
        myList.addFirst(myEntry3);
        
        expected("13 should come BEFORE Bonjour and Hello");
        actual(myList);
    }
    
    /*---------------------------------------------------------------
     Test method for addLast()
    -----------------------------------------------------------------*/
    void addLastTest()
    {
        Cs132ArrayList myList = new Cs132ArrayList();

        testHeader("addLast");
        
        expected("list is currently empty:");
        actual(myList);
                
        String myEntry = new String("Hello");
        myList.addLast(myEntry);

        expected("list contains Hello only");
        actual(myList);
        
        String myEntry2 = new String("Bonjour");
        myList.addLast(myEntry2);
        
        expected("Bonjour should come AFTER Hello");
        actual(myList);
        
        Integer myEntry3 = new Integer(13);
        myList.addLast(myEntry3);
        
        expected("13 should come AFTER Hello and Bonjour");
        actual(myList);
    }
    
    /*---------------------------------------------------------------
     Test method for add()
    -----------------------------------------------------------------*/
    void addTest()
    {
        Cs132ArrayList myList = new Cs132ArrayList();

        testHeader("add");
        
        expected("list is currently empty:");
        actual(myList);
                
        String myEntry = new String("Hello");
        try
        {
            myList.add(0, myEntry);
        }
        catch(Exception ex) {}
        
        expected("list contains Hello only");
        actual(myList);
        
        // add to beginning
        String myEntry2 = new String("Bonjour");
        try
        {
            myList.add(0, myEntry2);
        }
        catch(Exception ex) {}
        
        expected("Bonjour should come BEFORE Hello");
        actual(myList);
        
        // add to end
        Integer myEntry3 = new Integer(13);
        try
        {
            myList.add(2, myEntry3);
        }
        catch(Exception ex) {}
        
        expected("13 should come AFTER Bonjour and Hello");
        actual(myList);
        
        // add to middle
        Double myEntry4 = new Double(13.13);
        try
        {
            myList.add(1, myEntry4);
        }
        catch(Exception ex) {}
        
        expected("13.13 should come AFTER Bonjour, BEFORE Hello and 13");
        actual(myList);
    }
    
    /*---------------------------------------------------------------
     Test method for getFirst()
    -----------------------------------------------------------------*/
    void getFirstTest()
    {
        Cs132ArrayList myList = new Cs132ArrayList();
        Object gottenObj;

        testHeader("getFirst");
        
        // what if try to getFirst() from an empty list?
        expected("ListEmptyException to be thrown");
        try
        {
            myList.getFirst();
        }
        catch(ListEmptyException ex)
        {
            actual("Exception thrown: " + ex);
        }
        catch(Exception ex)
        {
            actual("TEST FAILED --- exception thrown was: " + ex);
        }
                
        myList.addLast("First");
        expected("First");
        try
        {
            actual(myList.getFirst());
        }
        catch(Exception ex) {}
        
        myList.addLast("Second");
        myList.addLast("Third");

        expected("First");
        try
        {
            actual(myList.getFirst());
        }
        catch(Exception ex) {}
    }
    
    /*---------------------------------------------------------------
     Test method for getLast()
    -----------------------------------------------------------------*/
    void getLastTest()
    {
        Cs132ArrayList myList = new Cs132ArrayList();
        Object gottenObj;

        testHeader("getLast");
        
        // what if try to getLast() from an empty list?
        expected("ListEmptyException to be thrown");
        try
        {
            gottenObj = myList.getLast();
        }
        catch(ListEmptyException ex)
        {
            actual("Exception thrown: " + ex);
        }
        catch(Exception ex)
        {
            actual("TEST FAILED --- exception thrown was: " + ex);
        }
                
        myList.addLast("First");
        expected("First");
        try
        {
            actual(myList.getLast());
        }
        catch (Exception ex) {}
        
        myList.addLast("Second");
        myList.addLast("Third");
        expected("Third");
        try
        {
            actual(myList.getLast());
        }
        catch (Exception ex) {}
    }
    
    /*---------------------------------------------------------------
     Test method for get()
    -----------------------------------------------------------------*/
    void getTest()
    {
        Cs132ArrayList myList = new Cs132ArrayList();
        Object gottenObj;

        testHeader("get");
        
        // what if try to get() from an empty list?
        expected("ListEmptyException to be thrown");
        try
        {
            gottenObj = myList.get(0);
        }
        catch(ListEmptyException ex)
        {
            actual("Exception thrown: " + ex);
        }
        catch(Exception ex)
        {
            actual("TEST FAILED --- exception thrown was: " + ex);
        }
                        
        try
        {
            myList.add(0, "Hello");
            myList.add(1, "Bonjour");
            myList.add(2, new Integer(13));
        }
        catch(Exception ex) {}

        try
        {
            expected("Hello");
            actual(myList.get(0));
        
            expected("Bonjour");
            actual(myList.get(1));
        
            expected("13");
            actual(myList.get(2));
        }
        catch (Exception ex) {}
        
        expected("ListIndexOutOfBounds to be thrown");
        try
        {
            gottenObj = myList.get(-1);
        }
        catch(ListIndexOutOfBoundsException ex)
        {
            actual("Exception thrown: " + ex);
        }
        catch(Exception ex)
        {
            actual("TEST FAILED --- exception thrown was: " + ex);
        }
        
        expected("ListIndexOutOfBoundsException to be thrown");
        try
        {
            gottenObj = myList.get(3);
        }
        catch(ListIndexOutOfBoundsException ex)
        {
            actual("Exception thrown: " + ex);
        }
        catch(Exception ex)
        {
            actual("TEST FAILED --- exception thrown was: " + ex);
        }
    }
    
    /*---------------------------------------------------------------
     Test method for remove()
    -----------------------------------------------------------------*/
    void removeTest()
    {
        Cs132ArrayList myList = new Cs132ArrayList();
        Object gottenObj;

        testHeader("remove");
        
        // what if try to remove() from an empty list?
        expected("ListEmptyException to be thrown");
        try
        {
            gottenObj = myList.remove(0);
        }
        catch(ListEmptyException ex)
        {
            actual("Exception thrown: " + ex);
        }
        catch(Exception ex)
        {
            actual("TEST FAILED --- exception thrown was: " + ex);
        }
                    
        try
        {
            myList.add(0, "Hello");
            myList.add(1, "Bonjour");
            myList.add(2, new Integer(13));
            myList.add(3, new Double(13.13));
        }
        catch (Exception ex) {}
        
        println("List contains: " + myList);
        try
        {
            expected("Hello removed");
            actual(myList.remove(0));
            expected("List now contains Bonjour, 13, 13.13");
            actual(myList);
        
            expected(13);
            actual(myList.remove(1));
            expected("List now contains Bonjour, 13.13");
            actual(myList);
        
            expected(13.13);
            actual(myList.remove(1));
            expected("List now contains Bonjour");
            actual(myList);
        
            expected("Bonjour");
            actual(myList.remove(0));
            expected("List is now empty");
            actual(myList);
        }
        catch (Exception ex) {}
        
        try
        {
            myList.add(0, new Integer(45));
            myList.add(1, new Integer(48));
        }
        catch (Exception ex) {}
        
        expected("ListIndexOutOfBounds to be thrown");
        try
        {
            gottenObj = myList.remove(-1);
        }
        catch(ListIndexOutOfBoundsException ex)
        {
            actual("Exception thrown: " + ex);
            expected("List is still 45, 48");
            actual(myList);
        }
        catch(Exception ex)
        {
            actual("TEST FAILED --- exception thrown was: " + ex);
        }
        
        expected("ListIndexOutOfBoundsException to be thrown");
        try
        {
            gottenObj = myList.remove(2);
        }
        catch(ListIndexOutOfBoundsException ex)
        {
            actual("Exception thrown: " + ex);
            expected("List is still 45, 48");
            actual(myList);
        }
        catch(Exception ex)
        {
            actual("TEST FAILED --- exception thrown was: " + ex);
        }    
    }
    
    /*---------------------------------------------------------------
     Test method for clear()
    -----------------------------------------------------------------*/
    void clearTest()
    {
        Cs132ArrayList myList = new Cs132ArrayList();

        testHeader("clear");
        
        // what if list is empty?
        myList.clear();
        
        expected("list should be empty");
        actual(myList);
                
        try
        {
            myList.addLast("Hello");
            myList.addLast("Bonjour");
            myList.addLast(new Integer(13));
        }
        catch (Exception ex) {}
        
        expected("list has 3 elements");
        actual(myList);
        
        myList.clear();
        
        expected("list should be empty");
        actual(myList);
        
        myList.addLast("George");
        myList.addLast(new Integer(27));
        
        expected("list has 2 elements");
        actual(myList);
    }
}