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 static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.ListIterator;
26  
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * JUnit tests
31   */
32  public class TreeListTest<E> extends AbstractListTest<E> {
33  
34      public static void benchmark(final List<? super Integer> l) {
35          long startMillis = System.currentTimeMillis();
36          for (int i = 0; i < 100000; i++) {
37              l.add(Integer.valueOf(i));
38          }
39          System.out.print(System.currentTimeMillis() - startMillis + ";");
40  
41          startMillis = System.currentTimeMillis();
42          for (int i = 0; i < 200; i++) {
43              l.toArray();
44          }
45          System.out.print(System.currentTimeMillis() - startMillis + ";");
46  
47          startMillis = System.currentTimeMillis();
48          for (int i = 0; i < 100; i++) {
49              final java.util.Iterator<? super Integer> it = l.iterator();
50              while (it.hasNext()) {
51                  it.next();
52              }
53          }
54          System.out.print(System.currentTimeMillis() - startMillis + ";");
55  
56          startMillis = System.currentTimeMillis();
57          for (int i = 0; i < 10000; i++) {
58              final int j = (int) (Math.random() * 100000);
59              l.add(j, Integer.valueOf(-j));
60          }
61          System.out.print(System.currentTimeMillis() - startMillis + ";");
62  
63          startMillis = System.currentTimeMillis();
64          for (int i = 0; i < 50000; i++) {
65              final int j = (int) (Math.random() * 110000);
66              l.get(j);
67          }
68          System.out.print(System.currentTimeMillis() - startMillis + ";");
69  
70          startMillis = System.currentTimeMillis();
71          for (int i = 0; i < 200; i++) {
72              final int j = (int) (Math.random() * 100000);
73              l.indexOf(Integer.valueOf(j));
74          }
75          System.out.print(System.currentTimeMillis() - startMillis + ";");
76  
77          startMillis = System.currentTimeMillis();
78          for (int i = 0; i < 10000; i++) {
79              final int j = (int) (Math.random() * 100000);
80              l.remove(j);
81          }
82          System.out.print(System.currentTimeMillis() - startMillis + ";");
83      }
84  
85  //    public static void main(String[] args) {
86  //        junit.textui.TestRunner.run(suite());
87  //        System.out.println("         add; toArray; iterator; insert; get; indexOf; remove");
88  //        System.out.print("   TreeList = ");
89  //        benchmark(new TreeList());
90  //        System.out.print("\n  ArrayList = ");
91  //        benchmark(new java.util.ArrayList());
92  //        System.out.print("\n LinkedList = ");
93  //        benchmark(new java.util.LinkedList());
94  //        System.out.print("\n NodeCachingLinkedList = ");
95  //        benchmark(new NodeCachingLinkedList());
96  //    }
97  
98      @Override
99      public TreeList<E> makeObject() {
100         return new TreeList<>();
101     }
102 
103     @Test
104     @SuppressWarnings("unchecked")
105     public void testAddMultiple() {
106         final List<E> l = makeObject();
107         l.add((E) "hugo");
108         l.add((E) "erna");
109         l.add((E) "daniel");
110         l.add((E) "andres");
111         l.add((E) "harald");
112         l.add(0, null);
113         assertNull(l.get(0));
114         assertEquals("hugo", l.get(1));
115         assertEquals("erna", l.get(2));
116         assertEquals("daniel", l.get(3));
117         assertEquals("andres", l.get(4));
118         assertEquals("harald", l.get(5));
119     }
120 
121     @Test
122     public void testBug35258() {
123         final Object objectToRemove = Integer.valueOf(3);
124 
125         final List<Integer> treelist = new TreeList<>();
126         treelist.add(Integer.valueOf(0));
127         treelist.add(Integer.valueOf(1));
128         treelist.add(Integer.valueOf(2));
129         treelist.add(Integer.valueOf(3));
130         treelist.add(Integer.valueOf(4));
131 
132         // this cause inconsistency in ListIterator()
133         treelist.remove(objectToRemove);
134 
135         final ListIterator<Integer> li = treelist.listIterator();
136         assertEquals(Integer.valueOf(0), li.next());
137         assertEquals(Integer.valueOf(0), li.previous());
138         assertEquals(Integer.valueOf(0), li.next());
139         assertEquals(Integer.valueOf(1), li.next());
140         // this caused error in bug 35258
141         assertEquals(Integer.valueOf(1), li.previous());
142         assertEquals(Integer.valueOf(1), li.next());
143         assertEquals(Integer.valueOf(2), li.next());
144         assertEquals(Integer.valueOf(2), li.previous());
145         assertEquals(Integer.valueOf(2), li.next());
146         assertEquals(Integer.valueOf(4), li.next());
147         assertEquals(Integer.valueOf(4), li.previous());
148         assertEquals(Integer.valueOf(4), li.next());
149         assertFalse(li.hasNext());
150     }
151 
152     @Test
153     public void testBugCollections447() {
154         final List<String> treeList = new TreeList<>();
155         treeList.add("A");
156         treeList.add("B");
157         treeList.add("C");
158         treeList.add("D");
159 
160         final ListIterator<String> li = treeList.listIterator();
161         assertEquals("A", li.next());
162         assertEquals("B", li.next());
163 
164         assertEquals("B", li.previous());
165 
166         li.remove(); // Deletes "B"
167 
168         // previous() after remove() should move to
169         // the element before the one just removed
170         assertEquals("A", li.previous());
171     }
172 
173     @Test
174     @SuppressWarnings("unchecked")
175     public void testIndexOf() {
176         final List<E> l = makeObject();
177         l.add((E) "0");
178         l.add((E) "1");
179         l.add((E) "2");
180         l.add((E) "3");
181         l.add((E) "4");
182         l.add((E) "5");
183         l.add((E) "6");
184         assertEquals(0, l.indexOf("0"));
185         assertEquals(1, l.indexOf("1"));
186         assertEquals(2, l.indexOf("2"));
187         assertEquals(3, l.indexOf("3"));
188         assertEquals(4, l.indexOf("4"));
189         assertEquals(5, l.indexOf("5"));
190         assertEquals(6, l.indexOf("6"));
191 
192         l.set(1, (E) "0");
193         assertEquals(0, l.indexOf("0"));
194 
195         l.set(3, (E) "3");
196         assertEquals(3, l.indexOf("3"));
197         l.set(2, (E) "3");
198         assertEquals(2, l.indexOf("3"));
199         l.set(1, (E) "3");
200         assertEquals(1, l.indexOf("3"));
201         l.set(0, (E) "3");
202         assertEquals(0, l.indexOf("3"));
203     }
204 
205 //    public void testCheck() {
206 //        List l = makeEmptyList();
207 //        l.add("A1");
208 //        l.add("A2");
209 //        l.add("A3");
210 //        l.add("A4");
211 //        l.add("A5");
212 //        l.add("A6");
213 //    }
214 
215     @Test
216     @SuppressWarnings("unchecked")
217     public void testInsertBefore() {
218         final List<E> l = makeObject();
219         l.add((E) "erna");
220         l.add(0, (E) "hugo");
221         assertEquals("hugo", l.get(0));
222         assertEquals("erna", l.get(1));
223     }
224 
225     @Test
226     @SuppressWarnings("boxing") // OK in test code
227     public void testIterationOrder() {
228         // COLLECTIONS-433:
229         // ensure that the iteration order of elements is correct
230         // when initializing the TreeList with another collection
231 
232         for (int size = 1; size < 1000; size++) {
233             final List<Integer> other = new ArrayList<>(size);
234             for (int i = 0; i < size; i++) {
235                 other.add(i);
236             }
237             final TreeList<Integer> l = new TreeList<>(other);
238             final ListIterator<Integer> it = l.listIterator();
239             int i = 0;
240             while (it.hasNext()) {
241                 final Integer val = it.next();
242                 assertEquals(i++, val.intValue());
243             }
244 
245             while (it.hasPrevious()) {
246                 final Integer val = it.previous();
247                 assertEquals(--i, val.intValue());
248             }
249         }
250     }
251 
252     @Test
253     @SuppressWarnings("boxing") // OK in test code
254     public void testIterationOrderAfterAddAll() {
255         // COLLECTIONS-433:
256         // ensure that the iteration order of elements is correct
257         // when calling addAll on the TreeList
258 
259         // to simulate different cases in addAll, do different runs where
260         // the number of elements already in the list and being added by addAll differ
261 
262         final int size = 1000;
263         for (int i = 0; i < 100; i++) {
264             final List<Integer> other = new ArrayList<>(size);
265             for (int j = i; j < size; j++) {
266                 other.add(j);
267             }
268             final TreeList<Integer> l = new TreeList<>();
269             for (int j = 0; j < i; j++) {
270                 l.add(j);
271             }
272 
273             l.addAll(other);
274 
275             final ListIterator<Integer> it = l.listIterator();
276             int cnt = 0;
277             while (it.hasNext()) {
278                 final Integer val = it.next();
279                 assertEquals(cnt++, val.intValue());
280             }
281 
282             while (it.hasPrevious()) {
283                 final Integer val = it.previous();
284                 assertEquals(--cnt, val.intValue());
285             }
286         }
287     }
288 
289     @Test
290     @SuppressWarnings("unchecked")
291     public void testRemove() {
292         final List<E> l = makeObject();
293         l.add((E) "hugo");
294         l.add((E) "erna");
295         l.add((E) "daniel");
296         l.add((E) "andres");
297         l.add((E) "harald");
298         l.add(0, null);
299         int i = 0;
300         assertNull(l.get(i++));
301         assertEquals("hugo", l.get(i++));
302         assertEquals("erna", l.get(i++));
303         assertEquals("daniel", l.get(i++));
304         assertEquals("andres", l.get(i++));
305         assertEquals("harald", l.get(i++));
306 
307         l.remove(0);
308         i = 0;
309         assertEquals("hugo", l.get(i++));
310         assertEquals("erna", l.get(i++));
311         assertEquals("daniel", l.get(i++));
312         assertEquals("andres", l.get(i++));
313         assertEquals("harald", l.get(i++));
314 
315         i = 0;
316         l.remove(1);
317         assertEquals("hugo", l.get(i++));
318         assertEquals("daniel", l.get(i++));
319         assertEquals("andres", l.get(i++));
320         assertEquals("harald", l.get(i++));
321 
322         i = 0;
323         l.remove(2);
324         assertEquals("hugo", l.get(i++));
325         assertEquals("daniel", l.get(i++));
326         assertEquals("harald", l.get(i++));
327     }
328 
329 }