SE251Ex:Chain TestChain
Jump to navigation
Jump to search
Copy and past the code below.
import java.util.*; import junit.framework.TestCase; public class TestChain extends TestCase { private Chain<String> chain; public void setUp() throws Exception { chain = new Chain<String>(); } public void testAddUnique() { assertTrue(chain.add("a")); assertTrue(chain.add("b")); assertTrue(chain.add("c")); assertEquals("[a, b, c]",chain.toString()); } public void testAddDuplicate() { assertTrue(chain.add("a")); assertTrue(chain.add("b")); assertTrue(chain.add("c")); assertFalse(chain.add("b")); assertFalse(chain.add("a")); assertEquals("[a, b, c]",chain.toString()); } public void testAddIndexUnique() { assertTrue(chain.add(0, "c")); assertTrue(chain.add(0, "a")); assertTrue(chain.add(1, "b")); assertEquals("[a, b, c]",chain.toString()); } public void testAddIndexOutOfBounds() { try { assertTrue(chain.add(0, "c")); chain.add(4, "a"); fail("IndexOutOfBoundsException should be thrown here!"); } catch (IndexOutOfBoundsException e) { //This is supposed to happen. } } public void testAddIndexDuplicate() { assertTrue(chain.add(0, "a")); assertTrue(chain.add(1, "c")); assertTrue(chain.add(1, "b")); assertFalse(chain.add(0, "c")); assertFalse(chain.add(2, "c")); assertEquals("[a, b, c]",chain.toString()); } public void testRemove() { assertTrue(chain.add("a")); assertTrue(chain.add("b")); assertTrue(chain.add("c")); assertTrue(chain.add("d")); assertTrue(chain.add("e")); assertTrue(chain.remove("b")); assertFalse(chain.remove("h")); assertTrue(chain.remove("e")); assertFalse(chain.remove("b")); assertEquals("[a, c, d]",chain.toString()); } public void testRemoveIndex() { assertTrue(chain.add("a")); assertTrue(chain.add("b")); assertTrue(chain.add("c")); assertTrue(chain.add("d")); assertTrue(chain.add("e")); assertEquals("a",chain.remove(0)); assertEquals("e",chain.remove(3)); assertEquals("c",chain.remove(1)); assertEquals("[b, d]",chain.toString()); } public void testGet() { assertTrue(chain.add("a")); assertTrue(chain.add("b")); assertTrue(chain.add("c")); assertEquals("a",chain.get(0)); assertEquals("b",chain.get(1)); assertEquals("c",chain.get(2)); } public void testGetIndexOutOfBounds() { try { assertTrue(chain.add("a")); assertTrue(chain.add("b")); chain.get(3); fail("IndexOutOfBoundsException should be thrown here!"); } catch (IndexOutOfBoundsException e) { //This is supposed to happen. } } public void testContains() { assertTrue(chain.add("a")); assertTrue(chain.add("b")); assertTrue(chain.add("c")); assertTrue(chain.contains("a")); assertTrue(chain.contains("b")); assertTrue(chain.contains("c")); } public void testIteration() { assertTrue(chain.add("a")); assertTrue(chain.add("l")); assertTrue(chain.add("g")); assertTrue(chain.add("e")); assertFalse(chain.add("l")); assertTrue(chain.add("b")); assertTrue(chain.add("r")); assertFalse(chain.add("a")); Iterator<String> it = chain.iterator(); assertEquals("a", it.next()); assertEquals("l", it.next()); assertEquals("g", it.next()); assertEquals("e", it.next()); assertEquals("b", it.next()); assertEquals("r", it.next()); assertFalse(it.hasNext()); for (String str : chain) { //Just demonstrating this is possible! System.out.println(str); } } /** Tests stuff you can do to chain only because it implements Collection */ public void testCollectionsUtility() { List<String> bulk = new ArrayList<String>(); bulk.add("a"); bulk.add("l"); bulk.add("g"); bulk.add("e"); bulk.add("b"); bulk.add("r"); assertTrue(chain.addAll(bulk)); //inherited from AbstractCollection String max = Collections.max(chain); //chain is a Collection. Yay! assertEquals("r", max); Object[] arrayVersion = chain.toArray(); //inherited from AbstractCollection assertEquals("[a, l, g, e, b, r]", Arrays.toString(arrayVersion)); } public void testSize() { assertTrue(chain.add("a")); assertTrue(chain.add("b")); assertTrue(chain.add("c")); assertEquals(3, chain.size()); } }