/*
 * TestLists.java
 *
 * Version:
 *    $Id: TestLists.java,v 1.1 2001/11/28 14:40:01 ptt Exp $
 *
 * Revisions:
 *    $Log: TestLists.java,v $
 *    Revision 1.1  2001/11/28 14:40:01  ptt
 *    INITIAL COMMIT
 *
 */

import java.util.Iterator;

/**
 * A simple test program for the Linked and Array based implementations
 * of the SimpleList interface.
 *
 * @author Paul Tymann
 */

public class TestLists {

    /**
     * Basic sanity check for SimpleList implementations.
     *
     * @param args types of lists to use
     */

    public static void main( String args[] ) {
	SimpleList list = new SimpleLinkedList();
	SimpleList dup = new SimpleLinkedList();

	Integer ninetyNine = new Integer( 99 );

	// Create the appropriate lists
	/*
	if ( args.length > 0 ) {
	    if ( args[ 0 ].equals( "a" ) ) {
		list = new SimpleArrayList();
	    }
	}

	if ( args.length > 1 ) {
	    if ( args[ 1 ].equals( "a" ) ) {
		dup = new SimpleArrayList();
	    }
	}
	*/
	// Should be able to print and iterate over an empty list

	System.out.println( list + " Size:  " + list.size() );

	for ( Iterator i = list.iterator(); i.hasNext(); ) {
	    System.out.println( "Should not print" );
	}

	// Add a bunch of stuff

	for ( int i = 0; i < 10; i++ ) {
	    list.add( new Integer( i ) );
	    dup.add( new Integer( i ) );
	}

	System.out.println( list + " Size:  " + list.size() );

	// Should see the same things the toString() method did

	for ( Iterator i = list.iterator(); i.hasNext(); ) {
	    System.out.print( i.next() + " " );
	}

	System.out.println();

	// Manually walk through the list using get.  Make sure
	// get throws exceptions when it is supposed to.

	for ( int i = -1; i <= list.size(); i++ ) {
	    try {
		System.out.print( list.get( i ) );
	    }
	    catch ( IndexOutOfBoundsException e ) {
		System.out.print( "IOB!!" );
	    }

	    System.out.print( " " );
	}

	System.out.println();

	// Test equals (dup and list should be the same)

	System.out.println( dup.equals( dup ) );
	System.out.println( dup.equals( list ) );

	// Try removing every other element starting at the tail and
	// working backwards.  Overshoot and undershoot to verify exceptions

	for ( int i = list.size() + 1; i >=-2; i = i - 2 ) {
	    try {
		list.remove( i );
	    } catch ( IndexOutOfBoundsException e ) {
		System.out.println( "IOB at i = " + i );
	    }
	}

	System.out.println( list + " Size:  " + list.size() );

	// They should now be different

	System.out.println( dup.equals( list ) );

	// Add at every position in the list.  Overshoot and undershoot
	// to verify IOB is thrown correctly.

	for ( int i = list.size() + 1; i >= -1; i = i - 1 ) {
	    try {
		list.add( i, ninetyNine );
	    } catch ( IndexOutOfBoundsException e ) {
		System.out.println( "IOB at i=" + i );
	    }
	}

	System.out.println( list + " Size:  " + list.size() );

	// Remove the 99s using contains(), indexOf(), and remove()

	while ( list.contains( ninetyNine ) ) {
	    System.out.println( "Removing 99 at " + 
				list.indexOf( ninetyNine ) );
	    list.remove( ninetyNine );
	}

	// There should be no more 99s

	System.out.println( list.indexOf( ninetyNine ) );
	System.out.println( list + " Size:  " + list.size() );

	// Add the odd numbers back into the list

	for ( int i = 1; i < 10; i = i + 2 ) {
	    list.add( i, new Integer( i ) );
	}

	// The two lists should be the same again

	System.out.println( dup.equals( list ) );

	// Change the last element in one of the list

	list.remove( list.size() - 1 );
	list.add( ninetyNine );
	System.out.println( list );
	System.out.println( dup );

	System.out.println( list.equals( dup ) );

	// clear()

	list.clear();
	System.out.println( list + " Size:  " + list.size() );

    }

} // TestLists

