001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.list;
017:
018: import java.util.Arrays;
019: import java.util.LinkedList;
020: import java.util.List;
021:
022: import junit.framework.Test;
023:
024: import org.apache.commons.collections.BulkTest;
025:
026: /**
027: * Test class for NodeCachingLinkedList, a performance optimised LinkedList.
028: *
029: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
030: *
031: * @author Jeff Varszegi
032: * @author Phil Steitz
033: */
034: public class TestNodeCachingLinkedList extends TestAbstractLinkedList {
035:
036: public TestNodeCachingLinkedList(String testName) {
037: super (testName);
038: }
039:
040: public static void main(String args[]) {
041: compareSpeed();
042: String[] testCaseName = { TestNodeCachingLinkedList.class
043: .getName() };
044: junit.textui.TestRunner.main(testCaseName);
045: }
046:
047: public static Test suite() {
048: return BulkTest.makeSuite(TestNodeCachingLinkedList.class);
049: }
050:
051: //-----------------------------------------------------------------------
052: public List makeEmptyList() {
053: return new NodeCachingLinkedList();
054: }
055:
056: public String getCompatibilityVersion() {
057: return "3";
058: }
059:
060: //-----------------------------------------------------------------------
061: public void testShrinkCache() {
062: if (isRemoveSupported() == false || isAddSupported() == false)
063: return;
064: resetEmpty();
065: NodeCachingLinkedList list = (NodeCachingLinkedList) collection;
066:
067: list.addAll(Arrays.asList(new String[] { "1", "2", "3", "4" }));
068: list.removeAllNodes(); // Will dump all 4 elements into cache
069: ((NodeCachingLinkedList) list).setMaximumCacheSize(2); // shrink cache
070: list.addAll(Arrays.asList(new String[] { "1", "2", "3", "4" }));
071: checkNodes();
072: list.removeNode(list.getNode(0, false)); // no room in cache
073: list.removeNode(list.getNode(0, false));
074: list.removeNode(list.getNode(0, false));
075: checkNodes();
076: list.addAll(Arrays.asList(new String[] { "1", "2", "3", "4" }));
077: checkNodes();
078: }
079:
080: //-----------------------------------------------------------------------
081: public static void compareSpeed() {
082: NodeCachingLinkedList ncll = new NodeCachingLinkedList();
083: LinkedList ll = new LinkedList();
084:
085: Object o1 = new Object();
086: Object o2 = new Object();
087:
088: int loopCount = 4000000;
089:
090: long startTime, endTime;
091:
092: System.out
093: .println("Testing relative execution time of commonly-used methods...");
094:
095: startTime = System.currentTimeMillis();
096: for (int x = loopCount; x > 0; x--) {
097: // unrolled a few times to minimize effect of loop
098: ll.addFirst(o1);
099: ll.addLast(o2);
100: ll.removeFirst();
101: ll.removeLast();
102: ll.add(o1);
103: ll.remove(0);
104: //
105: ll.addFirst(o1);
106: ll.addLast(o2);
107: ll.removeFirst();
108: ll.removeLast();
109: ll.add(o1);
110: ll.remove(0);
111: //
112: ll.addFirst(o1);
113: ll.addLast(o2);
114: ll.removeFirst();
115: ll.removeLast();
116: ll.add(o1);
117: ll.remove(0);
118: }
119: endTime = System.currentTimeMillis();
120: System.out.println("Time with LinkedList: "
121: + (endTime - startTime) + " ms");
122:
123: startTime = System.currentTimeMillis();
124: for (int x = loopCount; x > 0; x--) {
125: ncll.addFirst(o1);
126: ncll.addLast(o2);
127: ncll.removeFirst();
128: ncll.removeLast();
129: ncll.add(o1);
130: ncll.remove(0);
131: //
132: ncll.addFirst(o1);
133: ncll.addLast(o2);
134: ncll.removeFirst();
135: ncll.removeLast();
136: ncll.add(o1);
137: ncll.remove(0);
138: //
139: ncll.addFirst(o1);
140: ncll.addLast(o2);
141: ncll.removeFirst();
142: ncll.removeLast();
143: ncll.add(o1);
144: ncll.remove(0);
145: }
146: endTime = System.currentTimeMillis();
147: System.out.println("Time with NodeCachingLinkedList: "
148: + (endTime - startTime) + " ms");
149:
150: }
151:
152: // public void testCreate() throws Exception {
153: // resetEmpty();
154: // writeExternalFormToDisk((java.io.Serializable) collection,
155: // "D:/dev/collections/data/test/NodeCachingLinkedList.emptyCollection.version3.obj");
156: // resetFull();
157: // writeExternalFormToDisk((java.io.Serializable) collection,
158: // "D:/dev/collections/data/test/NodeCachingLinkedList.fullCollection.version3.obj");
159: // }
160: }
|