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          System.out.println("Testing relative execution time of commonly-used methods...");
39  
40          long startTime = System.currentTimeMillis();
41          for (int x = loopCount; x > 0; x--) {
42              // unrolled a few times to minimize effect of loop
43              ll.addFirst(o1);
44              ll.addLast(o2);
45              ll.removeFirst();
46              ll.removeLast();
47              ll.add(o1);
48              ll.remove(0);
49              //
50              ll.addFirst(o1);
51              ll.addLast(o2);
52              ll.removeFirst();
53              ll.removeLast();
54              ll.add(o1);
55              ll.remove(0);
56              //
57              ll.addFirst(o1);
58              ll.addLast(o2);
59              ll.removeFirst();
60              ll.removeLast();
61              ll.add(o1);
62              ll.remove(0);
63          }
64          long endTime = System.currentTimeMillis();
65          System.out.println("Time with LinkedList: " + (endTime - startTime) + " ms");
66  
67          startTime = System.currentTimeMillis();
68          for (int x = loopCount; x > 0; x--) {
69              ncll.addFirst(o1);
70              ncll.addLast(o2);
71              ncll.removeFirst();
72              ncll.removeLast();
73              ncll.add(o1);
74              ncll.remove(0);
75              //
76              ncll.addFirst(o1);
77              ncll.addLast(o2);
78              ncll.removeFirst();
79              ncll.removeLast();
80              ncll.add(o1);
81              ncll.remove(0);
82              //
83              ncll.addFirst(o1);
84              ncll.addLast(o2);
85              ncll.removeFirst();
86              ncll.removeLast();
87              ncll.add(o1);
88              ncll.remove(0);
89          }
90          endTime = System.currentTimeMillis();
91          System.out.println("Time with NodeCachingLinkedList: " + (endTime - startTime) + " ms");
92  
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public NodeCachingLinkedList<E> getCollection() {
100         return (NodeCachingLinkedList<E>) super.getCollection();
101     }
102 
103     @Override
104     public String getCompatibilityVersion() {
105         return "4";
106     }
107 
108     @Override
109     public NodeCachingLinkedList<E> makeObject() {
110         return new NodeCachingLinkedList<>();
111     }
112 
113 //    public void testCreate() throws Exception {
114 //        resetEmpty();
115 //        writeExternalFormToDisk((java.io.Serializable) getCollection(),
116 //           "src/test/resources/data/test/NodeCachingLinkedList.emptyCollection.version4.obj");
117 //        resetFull();
118 //        writeExternalFormToDisk((java.io.Serializable) getCollection(),
119 //            "src/test/resources/data/test/NodeCachingLinkedList.fullCollection.version4.obj");
120 //    }
121 
122     @Test
123     @SuppressWarnings("unchecked")
124     public void testShrinkCache() {
125         if (!isRemoveSupported() || !isAddSupported()) {
126             return;
127         }
128         resetEmpty();
129         final NodeCachingLinkedList<E> list = getCollection();
130 
131         list.addAll(Arrays.asList((E[]) new String[] { "1", "2", "3", "4" }));
132         list.removeAllNodes(); // Will dump all 4 elements into cache
133         list.setMaximumCacheSize(2); // shrink cache
134         list.addAll(Arrays.asList((E[]) new String[] { "1", "2", "3", "4" }));
135         checkNodes();
136         list.removeNode(list.getNode(0, false)); // no room in cache
137         list.removeNode(list.getNode(0, false));
138         list.removeNode(list.getNode(0, false));
139         checkNodes();
140         list.addAll(Arrays.asList((E[]) new String[] { "1", "2", "3", "4" }));
141         checkNodes();
142     }
143 }