import student.TestCase; /* * Created on Feb 16, 2005, modified Sept 19, 2005 * based on LinkedSetTest from 1706 by mapq */ /** * Test cases for the SetADT class defined in Lewis & Chase * @author manuel */ public class ReferenceArraySetTest extends TestCase { private Set set; /** * Create a set * @see TestCase#setUp() */ protected void setUp() throws Exception { super.setUp(); set = new ArraySet(); } /** * testing the default state of the set */ public void testOneA() { // first two should be true assertEquals(set.size(), 0); assertTrue(set.isEmpty()); try { set.add(null); fail("hint: precondition failure was not detected in add()"); } catch (AssertionError e) { // life is good } assertEquals(set.size(), 0); assertTrue(set.isEmpty()); } /** * testing adding and other general methods */ public void testOne() { assertEquals("Hint: Test failure: size() should be zero on a new set", set.size(), 0); assertTrue("Hint: Test failure: set should be empty on a new set", set .isEmpty()); set.add("hello"); assertEquals("Hint: Bag should have 1 element.", set.size(), 1); assertFalse("Hint: after adding 1 element, set should not be empty", set.isEmpty()); assertTrue("Hint: after adding 1 element, contains should find it", set .contains("hello")); set.remove("hello"); assertEquals("Hint: added 1, removed 1, size() not zero", set.size(), 0); assertTrue("Hint: added 1, removed 1, set not empty", set.isEmpty()); } /** * Testing the precondition in the add() method */ public void testAddContract() { assertEquals("Hint: Test failure: size() should be zero on a new set", set.size(), 0); assertTrue("Hint: Test failure: set should be empty on a new set", set .isEmpty()); try { set.add(null); fail("hint: precondition failure was not detected in add()"); } catch (AssertionError e) { // life is good } assertEquals("Hint: Test failure: size() should be zero on a new set", set.size(), 0); assertTrue("Hint: Test failure: set should be empty on a new set", set .isEmpty()); try { set.add("something"); assertEquals("Hint: Bag should have 1 element.", set.size(), 1); assertFalse( "Hint: after adding 1 element, set should not be empty", set.isEmpty()); } catch (AssertionError e) { fail("hint: assertion error generated unexpectedly. Message: "+e.getMessage()); } } /** * Testing the precondition in the contains method */ public void testContainsContract() { assertTrue("Hint: Test failure: set should be empty on a new set", set .isEmpty()); try { set.contains(null); fail("hint: precondition failure was not detected in contains()"); } catch (AssertionError e) { // life is good } assertTrue("Hint: Test failure: set should be empty on a new set", set .isEmpty()); } /** * testing adding lots of elements to the set. */ public void testAddLots() { assertEquals("Hint: Test failure: size() should be zero on a new set", set.size(), 0); assertTrue("Hint: Test failure: set should be empty on a new set", set .isEmpty()); for (int i = 0; i < 300; i++) set.add("" + i); assertFalse( "hint: set should not be empty after adding lots of elements", set.isEmpty()); assertEquals("hint: set size is wrong after adding lots of elements", set.size(), 300); for (int j = 0; j < 300; j++) assertTrue("hint: contains() failed", set.contains("" + j)); assertFalse( "hint: set should not be empty after adding lots of elements", set.isEmpty()); assertEquals("hint: set size is wrong after adding lots of elements", set.size(), 300); for (int k = 0; k < 300; k++) set.remove("" + k); assertTrue("hint: set should be empty after removing all elements", set .isEmpty()); assertEquals("hint: set size should be 0 after removing all elements", set.size(), 0); for (int i = 0; i < 300; i++) set.add("" + i); assertFalse( "hint: set should not be empty after adding lots of elements", set.isEmpty()); assertEquals("hint: set size is wrong after adding lots of elements", set.size(), 300); } /** * testing iterators when the set is empty */ public void testIterators1() { java.util.Iterator itr = set.iterator(); // should work even if the set is empty assertNotNull("hint: iterator() returned null", itr); assertFalse("hint: hasNext() return wrong value for empty collection", itr.hasNext()); while (itr.hasNext()) assertNotNull("hint: iterator() returned null", itr.next()); assertFalse("hint: hasNext() return wrong value for empty collection", itr.hasNext()); assertNull("hint: iterator() should have returned null but did not", itr.next()); } /** * testing the set property that no duplicate members are stored */ public void testDuplicates() { for (int i = 0; i < 10; i++) set.add("Just once"); assertEquals("hint: size() should be 1", 1, set.size()); set.remove("Just once"); assertEquals("hint: size() should be 0", 0, set.size()); assertTrue("hint: set should be empty", set.isEmpty()); } /** * testing iterators when the set has something in it */ public void testIterators2() { // now the set has something, they should still work set.add("hi"); set.add("bye"); java.util.Iterator itr = set.iterator(); assertNotNull("hint: iterator() returned null", itr); assertTrue("hint: hasNext() return false for non empty collection", itr .hasNext()); int i = 0; while (itr.hasNext()) { assertNotNull("hint: iterator() returned null", itr.next()); i++; } assertEquals("hint: iterator repeated loop wrong number of times", i, 2); assertFalse("hint: hasNext() return wrong value after the loop exited", itr.hasNext()); assertNull( "hint: iterator() should have returned null after loop exited", itr.next()); } /** * testing the toString method */ public void testBogus() { String str = set.toString(); assertNotNull("hint: toString returned null", str); } }