import junit.framework.*;

/**
 * JUnit test class for ListADT.  Currently tests ArrayList.  Do a global find/replace
 * to LinkedList to use the other list.<p>
 * 
 * Could definitely write more tests.
 */
public class ListTest extends TestCase 
{	
	public ListTest( String str )
	{
		super( str );
	}	
	
	//*********************  Test methods ************************
	
	public void testConstructor1()
	{
		assertNotNull(new LinkedList());
	}
	
	public void testConstructor2()
	{
		assertNotNull(new LinkedList( 10 ));
	}
	
	public void testAppend()
	{
		LinkedList list = new LinkedList( 10 );
		for(int i = 0; i < 500; ++i)
		{
			Integer a = new Integer(i);
			assertSame(a,list.append( a ));
		}
	}
	public void testAppendAndGet()
	{
		try{
		LinkedList list = new LinkedList( 10 );
		for(int i = 0; i < 500; ++i)
		{
			list.append( new Integer(i) );
		}
		for(int i = 0; i < 500; ++i)
		{
			assertEquals( new Integer(i),(Integer)(list.get(i)) );
		}
		}catch(ListLocationException e){fail("Should not have thrown exception");}
		
	}
	
	public void testAddAtZero()
	{
		try{
		LinkedList list = new LinkedList( 10 );
		for(int i = 1; i <= 500; ++i)
			list.add(0, new Integer(i) );
		}catch(ListLocationException e){fail("Should not have thrown exception");}
	}
	
	public void testAddAndRemoveAtZero()
	{
		try{
		LinkedList list = new LinkedList( 10 );
		for(int i = 1; i <= 500; ++i)
			list.add(0, new Integer(i) );
		for(int i = 500; i >=1; --i)
			assertEquals(new Integer(i),(Integer)(list.remove(0)));
		}catch(ListLocationException e){fail("Should not have thrown exception");}
	}
	
	public void testAddAtPosition()
	{
		LinkedList list = new LinkedList( 10 );
		for(int i = 1; i <= 500; ++i)
			list.append( "Test" );
		try{
		assertEquals("first",list.add( 1, "first"));
		assertEquals("fifth",list.add( 5, "fifth"));
		assertEquals("gib",list.add( 200, "gib" ));
		}catch(ListLocationException e){fail("Should not have thrown exception");}
		try{list.add( -1, "zero" );fail("Should have thrown an exception");}
			catch(ListLocationException e){}
		try{
		assertEquals("501",list.add( 501, "501" ));
		}catch(ListLocationException e){fail("Should not have thrown exception");}
		try{list.add( 1000,"1000" );fail("Should have thrown an exception");}
			catch(ListLocationException e){}
	}
	
	public void testRemove()
	{
		LinkedList list = new LinkedList( 10 );
		for(int i = 1; i <= 500; ++i)
			list.append( "Test" );
		try{
		assertNotNull(list.remove(499));
		assertNotNull(list.remove(1));
		assertNotNull(list.remove(20));
		}catch(ListLocationException e){fail("Should not have thrown exception");}
		try{list.remove(50000);fail("Should have thrown an exception");}
			catch(ListLocationException e){}
		try{list.remove(-4);fail("Should have thrown an exception");}
			catch(ListLocationException e){}
	}
	
	public void testClear()
	{
		try{
		  LinkedList list = new LinkedList( 10 );
		  list.add(0, "Test" );
		  for(int i = 1; i <= 10; ++i)
			list.add(1, new Integer(i) );
		  list.clear();
		  assertTrue( list.isEmpty() );
		  }catch(ListLocationException e){fail("Should not have thrown exception");}
	}
	
	public void testReplace()
	{
		LinkedList list = new LinkedList( 10 );
		for(int i = 1; i <= 500; ++i)
			list.append( "Test" );
		try{
		list.add(22,"Hello");
		assertEquals("Hello",list.replace(22,"hello"));
		assertEquals("hello",(String)(list.get(22)));
		}catch(ListLocationException e){fail("Should not have thrown exception");}
		try{list.replace(50000,"end");fail("Should have thrown an exception");}
			catch(ListLocationException e){}
	}
	
	public void testGetEntry()
	{
		LinkedList list = new LinkedList( 10 );
		for(int i = 1; i <= 500; ++i)
			list.append( "Test" );
		try{
		assertSame(list.get(4),list.get(4));
		assertEquals(list.get(5), list.get(30));
		}catch(ListLocationException e){fail("Should not have thrown exception");}
	}
	
	public void testContains()
	{
		LinkedList list = new LinkedList( 10 );
		list.append("1");
		list.append("2");
		list.append("3");
		list.append("4");
		assertTrue(list.contains("3"));
		assertTrue(list.contains("4"));
		assertTrue(list.contains("1"));
		assertFalse(list.contains("20"));
	}
	
	public void testGetLength()
	{
		
		LinkedList list = new LinkedList( 10 );
		list.append("1");
		list.append("2");
		list.append("3");
		list.append("4");
		assertEquals(list.size(),4);
		try{
		for( int i = 1; i < 100; ++i )
			list.add(2,"hello");
		for( int i = 1; i < 100; ++i )
			list.remove(3);
		assertEquals(list.size(),4);
		}catch(ListLocationException e){fail("Should not have thrown exception");}
	}
	
	public void testIsEmpty()
	{
		LinkedList list = new LinkedList( 10 );
		assertTrue(list.isEmpty());
		list.append("4");
		assertFalse(list.isEmpty());
	}
}

