import junit.framework.*; import java.util.*; /** * Unit tests for SimpleArrayList. CSE143 demonstration Wi05 * * @author Hal Perkins * @version 11/09/03, 2/11/05 */ public class SimpleArrayListTest extends TestCase { private SimpleArrayList testList; public void setUp() { testList = new SimpleArrayList(); testList.add("Bugs"); testList.add("Elmer"); testList.add("Tweety"); } // verify empty list created successfully public void testConstructor() { SimpleArrayList a = new SimpleArrayList(); assertEquals(0, a.size()); assertTrue(a.isEmpty()); } public void testClear() { testList.clear(); assertTrue(testList.isEmpty()); } // verify add increases the list size public void testAdd() { assertEquals(3, testList.size()); assertFalse(testList.isEmpty()); } //verify array expands public void testDynamicAdd() { for (int k = 0; k < 5000; k++) { testList.add("woof"); } assertEquals(5003, testList.size()); } // Verify that a SimpleArrayList contains // the number and values in the expected arra private void checkEquals(Object[] expected, SimpleArrayList a) { assertEquals(expected.length, a.size()); for (int k = 0; k < expected.length; k++) { assertEquals(expected[k], a.get(k)); } } // Verify get return right values and checks positions public void testGet() { Object[] expected = {"Bugs", "Elmer", "Tweety"}; checkEquals(expected, testList); try { Object x = testList.get(-1); fail(); } catch (IndexOutOfBoundsException e) {} try { Object x = testList.get(testList.size()); fail(); } catch (IndexOutOfBoundsException e) {} } // Verify that set works properly public void testSet() { Object result = testList.set(0, "Mickey"); assertEquals("Bugs", result); Object[] expected = {"Mickey", "Elmer", "Tweety"}; checkEquals(expected, testList); result = testList.set(1, "Minnie"); assertEquals("Elmer", result); expected[1] = "Minnie"; checkEquals(expected, testList); result = testList.set(2, "Goofy"); assertEquals("Tweety", result); expected[2] = "Goofy"; checkEquals(expected, testList); try { Object x = testList.set(-1, "blah"); fail(); } catch (IndexOutOfBoundsException e) {} try { Object x = testList.set(testList.size(), "blah"); fail(); } catch (IndexOutOfBoundsException e) {} } // Verify search operations public void testIndexOf() { assertEquals(0, testList.indexOf("Bugs")); assertEquals(1, testList.indexOf("Elmer")); assertEquals(2, testList.indexOf("Tweety")); assertEquals(-1, testList.indexOf("C3PO")); } public void testContains() { assertTrue(testList.contains("Bugs")); assertTrue(testList.contains("Elmer")); assertTrue(testList.contains("Tweety")); assertFalse(testList.contains("R2D2")); } // verify remove operations // in the middle public void testRemove1() { testList.remove(1); Object[] expected = {"Bugs", "Tweety"}; checkEquals(expected, testList); } // at the beginning public void testRemove2() { testList.remove(0); Object[] expected = {"Elmer", "Tweety"}; checkEquals(expected, testList); } // at the end public void testRemove3() { testList.remove(2); Object[] expected = {"Bugs", "Elmer"}; checkEquals(expected, testList); } public void testRemove4() { try { testList.remove(3); fail(); } catch (IndexOutOfBoundsException e) {} try { testList.remove(-1); fail(); } catch (IndexOutOfBoundsException e) {} } // Verify equals public void testEquals() { assertFalse(testList.equals(null)); assertFalse(testList.equals("testList")); assertFalse(testList.equals(new SimpleArrayList())); SimpleArrayList lst = new SimpleArrayList(); assertFalse(lst.equals(testList)); lst.add("Bugs"); lst.add("Elmer"); assertFalse(testList.equals(lst)); lst.add("Tweety"); assertFalse(testList == lst); assertTrue(testList.equals(lst)); assertTrue(lst.equals(testList)); lst.add("Bullwinkle"); assertFalse(testList.equals(lst)); } // Verify iterator public void testIterator() { Iterator it = testList.iterator(); assertTrue(it.hasNext()); assertEquals("Bugs", it.next()); assertTrue(it.hasNext()); assertEquals("Elmer", it.next());; assertTrue(it.hasNext()); assertEquals("Tweety", it.next());; assertFalse(it.hasNext()); try { Object notThere = it.next(); fail("NoSuchElementException not thrown"); } catch (NoSuchElementException e) { // success } } }