package hw4; import junit.framework.*; public class LinkedListTest extends TestCase { public LinkedListTest(String pName) { super(pName); } public static void main(String[] args) { junit.textui.TestRunner.run(LinkedListTest.class); } public static Test suite() { return new TestSuite(LinkedListTest.class); } /** * Executed before each testX(). * Resets static fields of each user class (except this). */ protected void setUp() { //my setUp() code goes here.. } /** * Executed after each testX() */ protected void tearDown() { //my tearDown() code goes here.. } static int size = 0; /********** Generated testcases **********/ public void testConstructor() { LinkedList t = new LinkedList(); assertTrue(t.size() == 0); assertTrue(t.toString().equals("[]")); } /**** the following tests are derived from the classification tree presented in the class *****/ public void testContainsWhenLenghtZero() { LinkedList t = new LinkedList(); String s = t.toString(); assertTrue(!t.contains(new Integer(1)));//e not in A assertTrue(s.equals(t.toString()));//make sure the object state is not modified } public void testContainsWhenLenghtEven() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); t.addFirst(new Integer(2)); t.addFirst(new Integer(3)); t.addFirst(new Integer(4)); String s = t.toString(); assertTrue(!t.contains(new Integer(8)));//e not in A assertTrue(t.contains(new Integer(1)));//e first in A assertTrue(t.contains(new Integer(4)));//e last in A assertTrue(t.contains(new Integer(2)));//e not first not last in A assertTrue(s.equals(t.toString()));//make sure the object state is not modified } public void testContainsWhenLenghtOne() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); String s = t.toString(); assertTrue(t.contains(new Integer(1)));//e first in A assertTrue(!t.contains(new Integer(2)));//e not first not last in A assertTrue(s.equals(t.toString()));//make sure the object state is not modified } public void testContainsWhenLenghtOdd() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); t.addFirst(new Integer(2)); t.addFirst(new Integer(3)); String s = t.toString(); assertTrue(!t.contains(new Integer(8)));//e not in A assertTrue(t.contains(new Integer(1)));//e first in A assertTrue(t.contains(new Integer(3)));//e last in A assertTrue(t.contains(new Integer(2)));//e not first not last in A assertTrue(s.equals(t.toString()));//make sure the object state is not modified } /****** the following tests are for testing addFirst ******/ public void testAddFirstWhenLengthZero() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); assertTrue(t.getFirst().equals(new Integer(1)));//1 is in the first position assertTrue(t.toString().equals("[1]"));//1 is added into A } public void testAddFirstWhenLengthEven() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); t.addFirst(new Integer(2)); t.addFirst(new Integer(3)); t.addFirst(new Integer(4)); t.addFirst(new Integer(5)); assertTrue(t.getFirst().equals(new Integer(5)));//5 is in the first position assertTrue(t.toString().equals("[5, 4, 3, 2, 1]"));//1 is added into A } public void testAddFirstWhenLenghtOne() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); t.addFirst(new Integer(2)); assertTrue(t.contains(new Integer(2)));//e first in A assertTrue(t.getFirst().equals(new Integer(2)));//1 is the first position assertTrue(t.toString().equals("[2, 1]"));//1 is added into A } public void testAddFirstWhenLengthOdd() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); t.addFirst(new Integer(2)); t.addFirst(new Integer(4)); t.addFirst(new Integer(5)); assertTrue(t.getFirst().equals(new Integer(5)));//5 is in the first position assertTrue(t.toString().equals("[5, 4, 2, 1]"));//5 is added into A } /****** the following tests are for testing removeFirst ******/ public void testRemoveFirstWhenLengthZero() { try { LinkedList t = new LinkedList(); t.removeFirst(); fail("removeFirst hasn't caused an exception when it should"); } catch (Exception e) { } } public void testRemoveFirstWhenLengthEven() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); t.addFirst(new Integer(2)); t.addFirst(new Integer(3)); t.addFirst(new Integer(4)); t.removeFirst(); assertTrue(t.toString().equals("[3, 2, 1]"));//4 is removed } public void testRemoveFirstWhenLengthOne() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); t.removeFirst(); assertTrue(t.toString().equals("[]"));//1 is removed } public void testRemoveFirstWhenLengthOdd() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); t.addFirst(new Integer(2)); t.addFirst(new Integer(4)); t.removeFirst(); assertTrue(t.toString().equals("[2, 1]"));//4 is removed } /****** the following tests are for testing reverse ******/ public void testReverseWhenLengthZero() { try { LinkedList t = new LinkedList(); t.reverse(); assertTrue(t.toString().equals("[]"));//still empty } catch (Exception e) { } } public void testReverseWhenLengthEven() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); t.addFirst(new Integer(2)); t.addFirst(new Integer(3)); t.addFirst(new Integer(4)); t.reverse(); assertTrue(t.toString().equals("[1, 2, 3, 4]")); } public void testReverseWhenLengthOne() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); t.reverse(); assertTrue(t.toString().equals("[1]")); } public void testReverseWhenLengthOdd() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); t.addFirst(new Integer(2)); t.addFirst(new Integer(4)); t.reverse(); assertTrue(t.toString().equals("[1, 2, 4]")); } /**** the following test is created to achieve better code coverage ****/ public void testGetFirst() { try { LinkedList t = new LinkedList(); t.getFirst(); fail("getFirst hasn't caused an exception when it should"); } catch (Exception e) { } } public void testEquals1() { LinkedList t = new LinkedList(); assertTrue(!t.equals(new Integer(1))); } public void testEquals2() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); t.addFirst(new Integer(2)); t.addFirst(new Integer(4)); LinkedList t2 = new LinkedList(); t2.addFirst(new Integer(1)); t2.addFirst(new Integer(2)); t2.addFirst(new Integer(4)); assertTrue(t.equals(t2)); } public void testEquals3() { LinkedList t = new LinkedList(); t.addFirst(new Integer(1)); t.addFirst(new Integer(2)); t.addFirst(new Integer(4)); LinkedList t2 = new LinkedList(); t2.addFirst(new Integer(1)); t2.addFirst(new Integer(2)); t2.addFirst(new Integer(5)); assertTrue(!t.equals(t2)); } }