Source of LinkedListUnitTest.java


  1: 
  2: package unittest; 
  3: 
  4: import junit.framework.*;
  5: 
  6: import mylist.*;
  7: import test.TestUtil;
  8: 
  9: public class LinkedListUnitTest extends TestCase { 
 10: 
 11:   public LinkedListUnitTest(String name) { 
 12:     super(name);
 13:   }
 14: 
 15:   public void testInsert() {
 16:     LinkedList l = new LinkedList(); 
 17:     l.insertHead(new Integer(1)); 
 18:     l.insertHead(new Integer(2)); 
 19:     l.insertLast(new Integer(3)); 
 20:     l.insertLast(new Integer(4));
 21:     l.insert(new Integer(5), 3);
 22:     l.insert(new Integer(6), 3);
 23:     l.insert(new Integer(7), 3);
 24: 
 25:     assertTrue(TestUtil.match(l, TestUtil.toIntegerArray(new int[] {2, 1, 3, 7, 6, 5, 4 }))); 
 26:   }
 27: 
 28:   public void testRemove() {
 29:     LinkedList l = new LinkedList(); 
 30:     for (int i = 1; i <= 7; i++) { 
 31:       l.insertLast(new Integer(i));       
 32:     }
 33:     l.removeHead(); 
 34:     l.removeLast();
 35:     l.remove(2); 
 36: 
 37:     assertTrue(TestUtil.match(l, TestUtil.toIntegerArray(new int[] {2, 3, 5, 6}))); 
 38:   }
 39: 
 40:   public void testClone() throws CloneNotSupportedException {
 41:     LinkedList l1 = new LinkedList(); 
 42:     for (int i = 1; i <= 7; i++) { 
 43:       l1.insertLast(new Integer(i));       
 44:     }
 45: 
 46:     int[] ia1 = {1, 2, 3, 4, 5, 6, 7}; 
 47:     int[] ia2 = {2, 3, 4, 5, 6, 7}; 
 48: 
 49:     LinkedList l2 = (LinkedList) l1.clone(); 
 50:     assertTrue("Clone is not identity", l2 != l1);
 51:     assertTrue("Clone equals to original", l1.equals(l2));
 52:     assertTrue("Match 1", TestUtil.match(l1, TestUtil.toIntegerArray(ia1))); 
 53:     assertTrue("Match 2", TestUtil.match(l2, TestUtil.toIntegerArray(ia1))); 
 54:     
 55:     l2.removeHead(); 
 56:     assertTrue("Match 3", TestUtil.match(l1, TestUtil.toIntegerArray(ia1))); 
 57:     assertTrue("Match 4", TestUtil.match(l2, TestUtil.toIntegerArray(ia2))); 
 58:     assertTrue("Not equal", !l1.equals(l2));
 59:   }
 60: 
 61:   public static Test suite() {
 62:     return new TestSuite(LinkedListUnitTest.class);
 63:   } 
 64: 
 65: }