KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > list > TestNodeCachingLinkedList


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.list;
17
18 import java.util.Arrays JavaDoc;
19 import java.util.LinkedList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import junit.framework.Test;
23
24 import org.apache.commons.collections.BulkTest;
25
26 /**
27  * Test class for NodeCachingLinkedList, a performance optimised LinkedList.
28  *
29  * @version $Revision: 1.3 $ $Date: 2004/02/18 01:20:34 $
30  *
31  * @author Jeff Varszegi
32  * @author Phil Steitz
33  */

34 public class TestNodeCachingLinkedList extends TestAbstractLinkedList {
35
36     public TestNodeCachingLinkedList(String JavaDoc testName) {
37         super(testName);
38     }
39
40     public static void main(String JavaDoc args[]) {
41         compareSpeed();
42         String JavaDoc[] 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     //-----------------------------------------------------------------------
51
public List JavaDoc makeEmptyList() {
52         return new NodeCachingLinkedList();
53     }
54
55     public String JavaDoc getCompatibilityVersion() {
56         return "3";
57     }
58     
59     //-----------------------------------------------------------------------
60
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 JavaDoc[]{"1", "2", "3", "4"}));
66         list.removeAllNodes(); // Will dump all 4 elements into cache
67
((NodeCachingLinkedList) list).setMaximumCacheSize(2); // shrink cache
68
list.addAll( Arrays.asList( new String JavaDoc[]{"1", "2", "3", "4"}));
69         checkNodes();
70         list.removeNode(list.getNode(0, false)); // no room in cache
71
list.removeNode(list.getNode(0, false));
72         list.removeNode(list.getNode(0, false));
73         checkNodes();
74         list.addAll( Arrays.asList( new String JavaDoc[]{"1", "2", "3", "4"}));
75         checkNodes();
76     }
77     
78     //-----------------------------------------------------------------------
79
public static void compareSpeed() {
80         NodeCachingLinkedList ncll = new NodeCachingLinkedList();
81         LinkedList JavaDoc ll = new LinkedList JavaDoc();
82         
83         Object JavaDoc o1 = new Object JavaDoc();
84         Object JavaDoc o2 = new Object JavaDoc();
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             // unrolled a few times to minimize effect of loop
95
ll.addFirst(o1);
96             ll.addLast(o2);
97             ll.removeFirst();
98             ll.removeLast();
99             ll.add(o1);
100             ll.remove(0);
101             //
102
ll.addFirst(o1);
103             ll.addLast(o2);
104             ll.removeFirst();
105             ll.removeLast();
106             ll.add(o1);
107             ll.remove(0);
108             //
109
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             //
128
ncll.addFirst(o1);
129             ncll.addLast(o2);
130             ncll.removeFirst();
131             ncll.removeLast();
132             ncll.add(o1);
133             ncll.remove(0);
134             //
135
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 // public void testCreate() throws Exception {
148
// resetEmpty();
149
// writeExternalFormToDisk((java.io.Serializable) collection,
150
// "D:/dev/collections/data/test/NodeCachingLinkedList.emptyCollection.version3.obj");
151
// resetFull();
152
// writeExternalFormToDisk((java.io.Serializable) collection,
153
// "D:/dev/collections/data/test/NodeCachingLinkedList.fullCollection.version3.obj");
154
// }
155
}
156
Popular Tags