View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.list;
18  
19  import java.util.Arrays;
20  import java.util.LinkedList;
21  
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * Test class for NodeCachingLinkedList, a performance optimized LinkedList.
26   */
27  public class NodeCachingLinkedListTest<E> extends AbstractLinkedListTest<E> {
28  
29      public static void compareSpeed() {
30          final NodeCachingLinkedList<Object> ncll = new NodeCachingLinkedList<>();
31          final LinkedList<Object> ll = new LinkedList<>();
32  
33          final Object o1 = new Object();
34          final Object o2 = new Object();
35  
36          final int loopCount = 4000000;
37  
38          long startTime, endTime;
39  
40          System.out.println("Testing relative execution time of commonly-used methods...");
41  
42          startTime = System.currentTimeMillis();
43          for (int x = loopCount; x > 0; x--) {
44              // unrolled a few times to minimize effect of loop
45              ll.addFirst(o1);
46              ll.addLast(o2);
47              ll.removeFirst();
48              ll.removeLast();
49              ll.add(o1);
50              ll.remove(0);
51              //
52              ll.addFirst(o1);
53              ll.addLast(o2);
54              ll.removeFirst();
55              ll.removeLast();
56              ll.add(o1);
57              ll.remove(0);
58              //
59              ll.addFirst(o1);
60              ll.addLast(o2);
61              ll.removeFirst();
62              ll.removeLast();
63              ll.add(o1);
64              ll.remove(0);
65          }
66          endTime = System.currentTimeMillis();
67          System.out.println("Time with LinkedList: " + (endTime - startTime) + " ms");
68  
69          startTime = System.currentTimeMillis();
70          for (int x = loopCount; x > 0; x--) {
71              ncll.addFirst(o1);
72              ncll.addLast(o2);
73              ncll.removeFirst();
74              ncll.removeLast();
75              ncll.add(o1);
76              ncll.remove(0);
77              //
78              ncll.addFirst(o1);
79              ncll.addLast(o2);
80              ncll.removeFirst();
81              ncll.removeLast();
82              ncll.add(o1);
83              ncll.remove(0);
84              //
85              ncll.addFirst(o1);
86              ncll.addLast(o2);
87              ncll.removeFirst();
88              ncll.removeLast();
89              ncll.add(o1);
90              ncll.remove(0);
91          }
92          endTime = System.currentTimeMillis();
93          System.out.println("Time with NodeCachingLinkedList: " + (endTime - startTime) + " ms");
94  
95      }
96  
97      public NodeCachingLinkedListTest() {
98          super(NodeCachingLinkedListTest.class.getSimpleName());
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public NodeCachingLinkedList<E> getCollection() {
106         return (NodeCachingLinkedList<E>) super.getCollection();
107     }
108 
109     @Override
110     public String getCompatibilityVersion() {
111         return "4";
112     }
113 
114     @Override
115     public NodeCachingLinkedList<E> makeObject() {
116         return new NodeCachingLinkedList<>();
117     }
118 
119 //    public void testCreate() throws Exception {
120 //        resetEmpty();
121 //        writeExternalFormToDisk((java.io.Serializable) getCollection(),
122 //           "src/test/resources/data/test/NodeCachingLinkedList.emptyCollection.version4.obj");
123 //        resetFull();
124 //        writeExternalFormToDisk((java.io.Serializable) getCollection(),
125 //            "src/test/resources/data/test/NodeCachingLinkedList.fullCollection.version4.obj");
126 //    }
127 
128     @Test
129     @SuppressWarnings("unchecked")
130     public void testShrinkCache() {
131         if (!isRemoveSupported() || !isAddSupported()) {
132             return;
133         }
134         resetEmpty();
135         final NodeCachingLinkedList<E> list = getCollection();
136 
137         list.addAll(Arrays.asList((E[]) new String[] { "1", "2", "3", "4" }));
138         list.removeAllNodes(); // Will dump all 4 elements into cache
139         list.setMaximumCacheSize(2); // shrink cache
140         list.addAll(Arrays.asList((E[]) new String[] { "1", "2", "3", "4" }));
141         checkNodes();
142         list.removeNode(list.getNode(0, false)); // no room in cache
143         list.removeNode(list.getNode(0, false));
144         list.removeNode(list.getNode(0, false));
145         checkNodes();
146         list.addAll(Arrays.asList((E[]) new String[] { "1", "2", "3", "4" }));
147         checkNodes();
148     }
149 }