Source of LinkedListTest2.java


  1: 
  2: package test; 
  3: 
  4: import mylist.*;
  5: 
  6: public class LinkedListTest2 { 
  7: 
  8:   protected static int[][] results = { 
  9:     { 2, 1, 3, 7, 6, 5, 4 }, // result after insertion 
 10:     { 1, 3, 6, 5 },          // result after removal
 11:     { 1, 3, 6, 5 },          // the cloned list  
 12:     { 1, 3, 6, 5 },          // the original list after removing the head from the cloned list 
 13:     { 3, 6, 5 },             // the cloned list after removing the head from the cloned list   
 14:   };
 15: 
 16:   public static void main(String args[]) 
 17:     throws CloneNotSupportedException {
 18:     boolean testPassed = true; 
 19: 
 20:     LinkedList l = new LinkedList(); 
 21:     l.insertHead(new Integer(1)); 
 22:     l.insertHead(new Integer(2)); 
 23:     l.insertLast(new Integer(3)); 
 24:     l.insertLast(new Integer(4));
 25:     l.insert(new Integer(5), 3);
 26:     l.insert(new Integer(6), 3);
 27:     l.insert(new Integer(7), 3);
 28:   
 29:     
 30:     System.out.println("First pass"); 
 31:     System.out.println(l); 
 32:     if (!TestUtil.match(l, TestUtil.toIntegerArray(results[0]))) { 
 33:       System.out.println("Result mismatch"); 
 34:       testPassed = false;
 35:     }
 36:   
 37:     l.removeHead(); 
 38:     l.removeLast();
 39:     l.remove(2); 
 40:   
 41:     System.out.println("Second pass"); 
 42:     System.out.println(l); 
 43:     if (!TestUtil.match(l, TestUtil.toIntegerArray(results[1]))) { 
 44:       System.out.println("Result mismatch"); 
 45:       testPassed = false;
 46:     }
 47: 
 48:     LinkedList l2 = (LinkedList) l.clone(); 
 49:     System.out.println("Cloned list"); 
 50:     System.out.println(l2); 
 51:     if (!TestUtil.match(l2, TestUtil.toIntegerArray(results[2]))) { 
 52:       System.out.println("Result mismatch"); 
 53:       testPassed = false;
 54:     }
 55: 
 56:     l2.removeHead(); 
 57:     System.out.println("Original list"); 
 58:     System.out.println(l); 
 59:     if (!TestUtil.match(l, TestUtil.toIntegerArray(results[3]))) { 
 60:       System.out.println("Result mismatch"); 
 61:       testPassed = false;
 62:     }
 63:     System.out.println("Cloned list"); 
 64:     System.out.println(l2); 
 65:     if (!TestUtil.match(l2, TestUtil.toIntegerArray(results[4]))) { 
 66:       System.out.println("Result mismatch"); 
 67:       testPassed = false;
 68:     }
 69: 
 70:     if (testPassed) { 
 71:       System.out.println("Test passed.");
 72:     } else { 
 73:       System.out.println("Test failed.");
 74:     }
 75:     
 76:   } 
 77: 
 78: }