import java.util.NoSuchElementException;


/**
 * This is a driver class for testing a K_SimpleLL implementation (a
 * simplified list that does not implement the full Java List interface).
 * 
 * @Author Nathan Sprague (April 2010)
 * @Author Alyce Brady (most recently on Sept 30, 2025)
 * @Author Your Name
 * @version The Date
 */
public class K_SimpleLLTester
{

    /**
     * Runs a series of tests.
     */
    public static void main(String[] args)
    {
        testSize();
        testAddRemove();
        testRemoveFromEmpty();
        testRemoveNegativeIndex();
        testRemoveTooLargeIndex();
        System.out.println("\nTests complete.");
    }


    /**
     * A helper function that creates a list containing all of the letters
     * in the alphabet.  Letters are added to the beginning of the list,
     * starting with A.  Items should end up in reverse alphabetical order.
     * 
     * @return list of letters
     */
    public static K_SimpleLL<String> buildTestList()
    {
        String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        K_SimpleLL<String> stringList = new K_SimpleLL<String>();

        for (int i = 0; i< alphabet.length(); i++)
        {
            stringList.addFirst(alphabet.substring(i, i+1));
        }
        return stringList;
    }


    /**
     * Run tests to see if size() is calculated correctly by the K_SimpleLL class.
     */
    public static void testSize()
    {
        K_SimpleLL<String> stringList = new K_SimpleLL<String>();
        System.out.println("\nTesting size method...");
        System.out.println("Initial size of list: Expected 0; Actual: "
                + stringList.size());

        stringList = buildTestList();

        System.out.println("After all elements are added: Expected 26; Actual: "
                + stringList.size());
        stringList.removeElement(0);
        System.out.println("After removing first element: Expected 25; Actual: "
                + stringList.size());
        stringList.removeElement(stringList.size()-1);
        System.out.println("After removing last element: Expected 24; Actual: "
                + stringList.size());
    }

    /**
     * Run tests to see if items are added and removed correctly by the
     * K_SimpleLL class.
     */
    public static void testAddRemove()
    {
        System.out.println("\nTesting effect of removeElement method...");
        K_SimpleLL<String> stringList = buildTestList();

        String cur = stringList.removeElement(0);
        System.out.println("First element: Expected Z; Actual: " + cur);

        cur = stringList.removeElement(0);
        System.out.println("New first element: Expected Y; Actual: " + cur);

        cur = stringList.removeElement(stringList.size()-1);
        System.out.println("Last element: Expected A; Actual: " + cur);

        cur = stringList.removeElement(stringList.size()-1);
        System.out.println("New last element: Expected B; Actual: " + cur);
    }

    /**
     * Test to see if the appropriate exception is thrown if there is an 
     * attempt to remove an item from an empty list.
     */
    public static void testRemoveFromEmpty()
    {
        K_SimpleLL<String> stringList = new K_SimpleLL<String>();
        System.out.println("Removing element from an empty list should throw an exception: ");

        try
        {
            String cur = stringList.removeElement(0);
        }
        catch(NoSuchElementException e)
        {
            System.out.println("   Caught: " + e);
        }                    
    }

    /**
     * Test to see if the appropriate exception is thrown if there is an 
     * attempt to remove an item at a negative index.
     */
    public static void testRemoveNegativeIndex()
    {
        K_SimpleLL<String> stringList = buildTestList();
        System.out.println("Removing element at negative index should throw an exception: ");

        try
        {
            String cur = stringList.removeElement(-1);
        }
        catch(NoSuchElementException e)
        {
            System.out.println("   Caught: " + e);
        }                    
    }

    /**
     * Test to see if the appropriate exception is thrown if there is an 
     * attempt to remove an item at an index beyond the end of the list.
     */
    public static void testRemoveTooLargeIndex()
    {
        K_SimpleLL<String> stringList = buildTestList();
        System.out.println("Removing element at index 30 should throw an exception: ");

        try
        {
            String cur = stringList.removeElement(30);
        }
        catch(NoSuchElementException e)
        {
            System.out.println("   Caught: " + e);
        }                    
    }

}    


