1 16 package org.apache.commons.collections.list; 17 18 import java.util.Arrays ; 19 import java.util.LinkedList ; 20 import java.util.List ; 21 22 import junit.framework.Test; 23 24 import org.apache.commons.collections.BulkTest; 25 26 34 public class TestNodeCachingLinkedList extends TestAbstractLinkedList { 35 36 public TestNodeCachingLinkedList(String testName) { 37 super(testName); 38 } 39 40 public static void main(String args[]) { 41 compareSpeed(); 42 String [] testCaseName = { TestNodeCachingLinkedList.class.getName()}; 43 junit.textui.TestRunner.main(testCaseName); 44 } 45 46 public static Test suite() { 47 return BulkTest.makeSuite(TestNodeCachingLinkedList.class); 48 } 49 50 public List makeEmptyList() { 52 return new NodeCachingLinkedList(); 53 } 54 55 public String getCompatibilityVersion() { 56 return "3"; 57 } 58 59 public void testShrinkCache() { 61 if (isRemoveSupported() == false || isAddSupported() == false) return; 62 resetEmpty(); 63 NodeCachingLinkedList list = (NodeCachingLinkedList) collection; 64 65 list.addAll( Arrays.asList( new String []{"1", "2", "3", "4"})); 66 list.removeAllNodes(); ((NodeCachingLinkedList) list).setMaximumCacheSize(2); list.addAll( Arrays.asList( new String []{"1", "2", "3", "4"})); 69 checkNodes(); 70 list.removeNode(list.getNode(0, false)); list.removeNode(list.getNode(0, false)); 72 list.removeNode(list.getNode(0, false)); 73 checkNodes(); 74 list.addAll( Arrays.asList( new String []{"1", "2", "3", "4"})); 75 checkNodes(); 76 } 77 78 public static void compareSpeed() { 80 NodeCachingLinkedList ncll = new NodeCachingLinkedList(); 81 LinkedList ll = new LinkedList (); 82 83 Object o1 = new Object (); 84 Object o2 = new Object (); 85 86 int loopCount = 4000000; 87 88 long startTime, endTime; 89 90 System.out.println("Testing relative execution time of commonly-used methods..."); 91 92 startTime = System.currentTimeMillis(); 93 for(int x = loopCount; x > 0; x--) { 94 ll.addFirst(o1); 96 ll.addLast(o2); 97 ll.removeFirst(); 98 ll.removeLast(); 99 ll.add(o1); 100 ll.remove(0); 101 ll.addFirst(o1); 103 ll.addLast(o2); 104 ll.removeFirst(); 105 ll.removeLast(); 106 ll.add(o1); 107 ll.remove(0); 108 ll.addFirst(o1); 110 ll.addLast(o2); 111 ll.removeFirst(); 112 ll.removeLast(); 113 ll.add(o1); 114 ll.remove(0); 115 } 116 endTime = System.currentTimeMillis(); 117 System.out.println("Time with LinkedList: " + (endTime - startTime) + " ms"); 118 119 startTime = System.currentTimeMillis(); 120 for(int x = loopCount; x > 0; x--) { 121 ncll.addFirst(o1); 122 ncll.addLast(o2); 123 ncll.removeFirst(); 124 ncll.removeLast(); 125 ncll.add(o1); 126 ncll.remove(0); 127 ncll.addFirst(o1); 129 ncll.addLast(o2); 130 ncll.removeFirst(); 131 ncll.removeLast(); 132 ncll.add(o1); 133 ncll.remove(0); 134 ncll.addFirst(o1); 136 ncll.addLast(o2); 137 ncll.removeFirst(); 138 ncll.removeLast(); 139 ncll.add(o1); 140 ncll.remove(0); 141 } 142 endTime = System.currentTimeMillis(); 143 System.out.println("Time with NodeCachingLinkedList: " + (endTime - startTime) + " ms"); 144 145 } 146 147 } 156 | Popular Tags |