Lines 17-73
Link Here
|
17 |
package org.apache.commons.collections4.list; |
17 |
package org.apache.commons.collections4.list; |
18 |
|
18 |
|
19 |
import java.util.ArrayList; |
19 |
import java.util.ArrayList; |
|
|
20 |
import java.util.Arrays; |
21 |
import java.util.HashSet; |
20 |
import java.util.List; |
22 |
import java.util.List; |
21 |
import java.util.ListIterator; |
23 |
import java.util.ListIterator; |
|
|
24 |
import java.util.Random; |
25 |
import java.util.Set; |
22 |
|
26 |
|
|
|
27 |
import org.apache.commons.collections4.BulkTest; |
28 |
|
23 |
import junit.framework.Test; |
29 |
import junit.framework.Test; |
24 |
|
30 |
|
25 |
import org.apache.commons.collections4.BulkTest; |
|
|
26 |
|
27 |
/** |
31 |
/** |
28 |
* JUnit tests |
32 |
* JUnit tests |
29 |
* |
33 |
* |
30 |
* @since 3.1 |
34 |
* @since FIXME |
31 |
* @version $Id$ |
35 |
* @version $Id$ |
32 |
*/ |
36 |
*/ |
33 |
public class TreeListTest<E> extends AbstractListTest<E> { |
37 |
public class MappedListTest<E> extends AbstractListTest<E> { |
34 |
|
38 |
|
35 |
public TreeListTest(final String name) { |
39 |
public MappedListTest(final String name) { |
36 |
super(name); |
40 |
super(name); |
37 |
} |
41 |
} |
|
|
42 |
|
43 |
public void testRunBenchmark() { |
44 |
main(new String[] {}); |
45 |
} |
38 |
|
46 |
|
39 |
// public static void main(String[] args) { |
47 |
public static void main(String[] args) { |
40 |
// junit.textui.TestRunner.run(suite()); |
48 |
final int[] initialSize = new int[]{10, 100, 1000, 10000, 100000}; |
41 |
// System.out.println(" add; toArray; iterator; insert; get; indexOf; remove"); |
49 |
final int[] count = new int[]{10, 100, 1000, 10000, 100000}; |
42 |
// System.out.print(" TreeList = "); |
50 |
|
43 |
// benchmark(new TreeList()); |
51 |
//junit.textui.TestRunner.run(suite()); |
44 |
// System.out.print("\n ArrayList = "); |
52 |
for (int sz : initialSize) { |
45 |
// benchmark(new java.util.ArrayList()); |
53 |
for (int cnt : count) { |
46 |
// System.out.print("\n LinkedList = "); |
54 |
final long start = System.currentTimeMillis(); |
47 |
// benchmark(new java.util.LinkedList()); |
55 |
System.out.print("\n" + sz + ", " + cnt + ":"); |
48 |
// System.out.print("\n NodeCachingLinkedList = "); |
56 |
System.out.print(" add; toArray; iterator; insert; get; indexOf; contains; remove"); |
49 |
// benchmark(new NodeCachingLinkedList()); |
57 |
System.out.print("\n ArrayList = "); |
50 |
// } |
58 |
benchmark(new java.util.ArrayList(), sz, cnt); |
|
|
59 |
System.out.print("\n LinkedList = "); |
60 |
benchmark(new java.util.LinkedList(), sz, cnt); |
61 |
System.out.print("\n NodeCachingLinkedList = "); |
62 |
benchmark(new NodeCachingLinkedList(), sz, cnt); |
63 |
System.out.print("\n TreeList = "); |
64 |
benchmark(new TreeList(), sz, cnt); |
65 |
System.out.print("\n MappedList = "); |
66 |
benchmark(new MappedList(), sz, cnt); |
67 |
final long elapsed = (System.currentTimeMillis() - start) / 1000; |
68 |
final long minutes = elapsed / 60; |
69 |
final long seconds = elapsed % 60; |
70 |
System.out.println("\n" + minutes + "m" + seconds + "s"); |
71 |
} |
72 |
} |
73 |
} |
51 |
|
74 |
|
52 |
public static Test suite() { |
75 |
public static Test suite() { |
53 |
return BulkTest.makeSuite(TreeListTest.class); |
76 |
return BulkTest.makeSuite(MappedListTest.class); |
54 |
} |
77 |
} |
55 |
|
78 |
|
56 |
public static void benchmark(final List<? super Integer> l) { |
79 |
public static void benchmark(final List<? super Integer> l) { |
|
|
80 |
final int initialSize = 100000; |
81 |
final int count = 400; |
82 |
benchmark(l, initialSize, count); |
83 |
} |
84 |
|
85 |
public static void benchmark(final List<? super Integer> l, final int initialSize, final int count) { |
86 |
|
87 |
final Random rand = new Random(0L); |
88 |
|
89 |
for (int i = 0; i < initialSize; i++) { |
90 |
l.add(Integer.valueOf(i)); |
91 |
} |
92 |
|
93 |
// add |
57 |
long start = System.currentTimeMillis(); |
94 |
long start = System.currentTimeMillis(); |
58 |
for (int i = 0; i < 100000; i++) { |
95 |
for (int i = 0; i < count; i++) { |
59 |
l.add(Integer.valueOf(i)); |
96 |
l.add(Integer.valueOf(i)); |
60 |
} |
97 |
} |
61 |
System.out.print(System.currentTimeMillis() - start + ";"); |
98 |
System.out.print(System.currentTimeMillis() - start + ";"); |
62 |
|
99 |
|
|
|
100 |
// toArray |
63 |
start = System.currentTimeMillis(); |
101 |
start = System.currentTimeMillis(); |
64 |
for (int i = 0; i < 200; i++) { |
102 |
for (int i = 0; i < count; i++) { |
65 |
l.toArray(); |
103 |
l.toArray(); |
66 |
} |
104 |
} |
67 |
System.out.print(System.currentTimeMillis() - start + ";"); |
105 |
System.out.print(System.currentTimeMillis() - start + ";"); |
68 |
|
106 |
|
|
|
107 |
// iterator |
69 |
start = System.currentTimeMillis(); |
108 |
start = System.currentTimeMillis(); |
70 |
for (int i = 0; i < 100; i++) { |
109 |
for (int i = 0; i < count; i++) { |
71 |
final java.util.Iterator<? super Integer> it = l.iterator(); |
110 |
final java.util.Iterator<? super Integer> it = l.iterator(); |
72 |
while (it.hasNext()) { |
111 |
while (it.hasNext()) { |
73 |
it.next(); |
112 |
it.next(); |
Lines 75-104
Link Here
|
75 |
} |
114 |
} |
76 |
System.out.print(System.currentTimeMillis() - start + ";"); |
115 |
System.out.print(System.currentTimeMillis() - start + ";"); |
77 |
|
116 |
|
|
|
117 |
// insert |
78 |
start = System.currentTimeMillis(); |
118 |
start = System.currentTimeMillis(); |
79 |
for (int i = 0; i < 10000; i++) { |
119 |
for (int i = 0; i < count; i++) { |
80 |
final int j = (int) (Math.random() * 100000); |
120 |
final int j = rand.nextInt(initialSize); |
81 |
l.add(j, Integer.valueOf(-j)); |
121 |
l.add(j, Integer.valueOf(-j)); |
82 |
} |
122 |
} |
83 |
System.out.print(System.currentTimeMillis() - start + ";"); |
123 |
System.out.print(System.currentTimeMillis() - start + ";"); |
84 |
|
124 |
|
|
|
125 |
// get |
85 |
start = System.currentTimeMillis(); |
126 |
start = System.currentTimeMillis(); |
86 |
for (int i = 0; i < 50000; i++) { |
127 |
for (int i = 0; i < count; i++) { |
87 |
final int j = (int) (Math.random() * 110000); |
128 |
final int j = rand.nextInt(initialSize); |
88 |
l.get(j); |
129 |
l.get(j); |
89 |
} |
130 |
} |
90 |
System.out.print(System.currentTimeMillis() - start + ";"); |
131 |
System.out.print(System.currentTimeMillis() - start + ";"); |
91 |
|
132 |
|
|
|
133 |
// indexOf |
92 |
start = System.currentTimeMillis(); |
134 |
start = System.currentTimeMillis(); |
93 |
for (int i = 0; i < 200; i++) { |
135 |
for (int i = 0; i < count; i++) { |
94 |
final int j = (int) (Math.random() * 100000); |
136 |
final int j = rand.nextInt(initialSize); |
95 |
l.indexOf(Integer.valueOf(j)); |
137 |
l.indexOf(Integer.valueOf(j)); |
96 |
} |
138 |
} |
97 |
System.out.print(System.currentTimeMillis() - start + ";"); |
139 |
System.out.print(System.currentTimeMillis() - start + ";"); |
|
|
140 |
|
141 |
// contains |
142 |
start = System.currentTimeMillis(); |
143 |
for (int i = 0; i < count; i++) { |
144 |
final int j = rand.nextInt(initialSize); |
145 |
l.contains(Integer.valueOf(j)); |
146 |
} |
147 |
System.out.print(System.currentTimeMillis() - start + ";"); |
98 |
|
148 |
|
|
|
149 |
// remove |
99 |
start = System.currentTimeMillis(); |
150 |
start = System.currentTimeMillis(); |
100 |
for (int i = 0; i < 10000; i++) { |
151 |
for (int i = 0; i < count; i++) { |
101 |
final int j = (int) (Math.random() * 100000); |
152 |
final int j = rand.nextInt(initialSize); |
102 |
l.remove(j); |
153 |
l.remove(j); |
103 |
} |
154 |
} |
104 |
System.out.print(System.currentTimeMillis() - start + ";"); |
155 |
System.out.print(System.currentTimeMillis() - start + ";"); |
Lines 106-113
Link Here
|
106 |
|
157 |
|
107 |
//----------------------------------------------------------------------- |
158 |
//----------------------------------------------------------------------- |
108 |
@Override |
159 |
@Override |
109 |
public TreeList<E> makeObject() { |
160 |
public MappedList<E> makeObject() { |
110 |
return new TreeList<E>(); |
161 |
return new MappedList<E>(); |
111 |
} |
162 |
} |
112 |
|
163 |
|
113 |
//----------------------------------------------------------------------- |
164 |
//----------------------------------------------------------------------- |
Lines 220-226
Link Here
|
220 |
public void testBug35258() { |
271 |
public void testBug35258() { |
221 |
final Object objectToRemove = Integer.valueOf(3); |
272 |
final Object objectToRemove = Integer.valueOf(3); |
222 |
|
273 |
|
223 |
final List<Integer> treelist = new TreeList<Integer>(); |
274 |
final List<Integer> treelist = new MappedList<Integer>(); |
224 |
treelist.add(Integer.valueOf(0)); |
275 |
treelist.add(Integer.valueOf(0)); |
225 |
treelist.add(Integer.valueOf(1)); |
276 |
treelist.add(Integer.valueOf(1)); |
226 |
treelist.add(Integer.valueOf(2)); |
277 |
treelist.add(Integer.valueOf(2)); |
Lines 248-254
Link Here
|
248 |
} |
299 |
} |
249 |
|
300 |
|
250 |
public void testBugCollections447() { |
301 |
public void testBugCollections447() { |
251 |
final List<String> treeList = new TreeList<String>(); |
302 |
final List<String> treeList = new MappedList<String>(); |
252 |
treeList.add("A"); |
303 |
treeList.add("A"); |
253 |
treeList.add("B"); |
304 |
treeList.add("B"); |
254 |
treeList.add("C"); |
305 |
treeList.add("C"); |
Lines 271-277
Link Here
|
271 |
public void testIterationOrder() { |
322 |
public void testIterationOrder() { |
272 |
// COLLECTIONS-433: |
323 |
// COLLECTIONS-433: |
273 |
// ensure that the iteration order of elements is correct |
324 |
// ensure that the iteration order of elements is correct |
274 |
// when initializing the TreeList with another collection |
325 |
// when initializing the MappedList with another collection |
275 |
|
326 |
|
276 |
for (int size = 1; size < 1000; size++) { |
327 |
for (int size = 1; size < 1000; size++) { |
277 |
List<Integer> other = new ArrayList<Integer>(size); |
328 |
List<Integer> other = new ArrayList<Integer>(size); |
Lines 278-284
Link Here
|
278 |
for (int i = 0; i < size; i++) { |
329 |
for (int i = 0; i < size; i++) { |
279 |
other.add(i); |
330 |
other.add(i); |
280 |
} |
331 |
} |
281 |
TreeList<Integer> l = new TreeList<Integer>(other); |
332 |
MappedList<Integer> l = new MappedList<Integer>(other); |
282 |
ListIterator<Integer> it = l.listIterator(); |
333 |
ListIterator<Integer> it = l.listIterator(); |
283 |
int i = 0; |
334 |
int i = 0; |
284 |
while (it.hasNext()) { |
335 |
while (it.hasNext()) { |
Lines 297-303
Link Here
|
297 |
public void testIterationOrderAfterAddAll() { |
348 |
public void testIterationOrderAfterAddAll() { |
298 |
// COLLECTIONS-433: |
349 |
// COLLECTIONS-433: |
299 |
// ensure that the iteration order of elements is correct |
350 |
// ensure that the iteration order of elements is correct |
300 |
// when calling addAll on the TreeList |
351 |
// when calling addAll on the MappedList |
301 |
|
352 |
|
302 |
// to simulate different cases in addAll, do different runs where |
353 |
// to simulate different cases in addAll, do different runs where |
303 |
// the number of elements already in the list and being added by addAll differ |
354 |
// the number of elements already in the list and being added by addAll differ |
Lines 308-314
Link Here
|
308 |
for (int j = i; j < size; j++) { |
359 |
for (int j = i; j < size; j++) { |
309 |
other.add(j); |
360 |
other.add(j); |
310 |
} |
361 |
} |
311 |
TreeList<Integer> l = new TreeList<Integer>(); |
362 |
MappedList<Integer> l = new MappedList<Integer>(); |
312 |
for (int j = 0; j < i; j++) { |
363 |
for (int j = 0; j < i; j++) { |
313 |
l.add(j); |
364 |
l.add(j); |
314 |
} |
365 |
} |
Lines 328-332
Link Here
|
328 |
} |
379 |
} |
329 |
} |
380 |
} |
330 |
} |
381 |
} |
|
|
382 |
|
383 |
private static <E> Set<E> makeSet(E... values) { |
384 |
Set<E> set = new HashSet<E>(); |
385 |
for (E value : values) { |
386 |
set.add(value); |
387 |
} |
388 |
return set; |
389 |
} |
390 |
|
391 |
@SuppressWarnings("unchecked") |
392 |
public void testGetIndices() { |
393 |
final MappedList<E> l = makeObject(); |
394 |
l.add((E) "A"); //0 |
395 |
l.add((E) "B"); //1 |
396 |
l.add((E) "C"); //2 |
397 |
l.add((E) "A"); //3 |
398 |
l.add((E) "B"); //4 |
399 |
l.add((E) "A"); //5 |
400 |
l.add((E) "A"); //6 |
401 |
|
402 |
assertEquals(makeSet(0, 3, 5, 6), l.getIndices("A")); |
403 |
assertEquals(makeSet(1, 4), l.getIndices("B")); |
404 |
assertEquals(makeSet(2), l.getIndices("C")); |
405 |
assertEquals(makeSet(), l.getIndices("D")); |
406 |
} |
331 |
|
407 |
|
332 |
} |
408 |
} |