1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import java.util.ArrayList; |
10 |
import java.util.Arrays; |
11 |
import java.util.Collection; |
12 |
import java.util.Iterator; |
13 |
import java.util.LinkedList; |
14 |
import java.util.List; |
15 |
import java.util.ListIterator; |
16 |
import java.util.NoSuchElementException; |
17 |
import java.util.concurrent.CopyOnWriteArrayList; |
18 |
|
19 |
import junit.framework.Test; |
20 |
|
21 |
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
22 |
|
23 |
public static void main(String[] args) { |
24 |
main(suite(), args); |
25 |
} |
26 |
|
27 |
public static Test suite() { |
28 |
class Implementation implements CollectionImplementation { |
29 |
public Class<?> klazz() { return CopyOnWriteArrayList.class; } |
30 |
public List emptyCollection() { return new CopyOnWriteArrayList(); } |
31 |
public Object makeElement(int i) { return i; } |
32 |
public boolean isConcurrent() { return true; } |
33 |
public boolean permitsNulls() { return true; } |
34 |
} |
35 |
class SubListImplementation extends Implementation { |
36 |
public List emptyCollection() { |
37 |
return super.emptyCollection().subList(0, 0); |
38 |
} |
39 |
} |
40 |
return newTestSuite( |
41 |
CopyOnWriteArrayListTest.class, |
42 |
CollectionTest.testSuite(new Implementation()), |
43 |
CollectionTest.testSuite(new SubListImplementation())); |
44 |
} |
45 |
|
46 |
static CopyOnWriteArrayList<Integer> populatedArray(int n) { |
47 |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>(); |
48 |
assertTrue(a.isEmpty()); |
49 |
for (int i = 0; i < n; i++) |
50 |
a.add(i); |
51 |
assertFalse(a.isEmpty()); |
52 |
assertEquals(n, a.size()); |
53 |
return a; |
54 |
} |
55 |
|
56 |
static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) { |
57 |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>(); |
58 |
assertTrue(a.isEmpty()); |
59 |
for (Integer element : elements) |
60 |
a.add(element); |
61 |
assertFalse(a.isEmpty()); |
62 |
assertEquals(elements.length, a.size()); |
63 |
return a; |
64 |
} |
65 |
|
66 |
/** |
67 |
* a new list is empty |
68 |
*/ |
69 |
public void testConstructor() { |
70 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
71 |
assertTrue(a.isEmpty()); |
72 |
} |
73 |
|
74 |
/** |
75 |
* new list contains all elements of initializing array |
76 |
*/ |
77 |
public void testConstructor2() { |
78 |
Integer[] ints = new Integer[SIZE]; |
79 |
for (int i = 0; i < SIZE - 1; ++i) |
80 |
ints[i] = new Integer(i); |
81 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); |
82 |
for (int i = 0; i < SIZE; ++i) |
83 |
assertEquals(ints[i], a.get(i)); |
84 |
} |
85 |
|
86 |
/** |
87 |
* new list contains all elements of initializing collection |
88 |
*/ |
89 |
public void testConstructor3() { |
90 |
Integer[] ints = new Integer[SIZE]; |
91 |
for (int i = 0; i < SIZE - 1; ++i) |
92 |
ints[i] = new Integer(i); |
93 |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); |
94 |
for (int i = 0; i < SIZE; ++i) |
95 |
assertEquals(ints[i], a.get(i)); |
96 |
} |
97 |
|
98 |
/** |
99 |
* addAll adds each element from the given collection, including duplicates |
100 |
*/ |
101 |
public void testAddAll() { |
102 |
CopyOnWriteArrayList full = populatedArray(3); |
103 |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
104 |
assertEquals(6, full.size()); |
105 |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
106 |
assertEquals(9, full.size()); |
107 |
} |
108 |
|
109 |
/** |
110 |
* addAllAbsent adds each element from the given collection that did not |
111 |
* already exist in the List |
112 |
*/ |
113 |
public void testAddAllAbsent() { |
114 |
CopyOnWriteArrayList full = populatedArray(3); |
115 |
// "one" is duplicate and will not be added |
116 |
assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one))); |
117 |
assertEquals(5, full.size()); |
118 |
assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one))); |
119 |
assertEquals(5, full.size()); |
120 |
} |
121 |
|
122 |
/** |
123 |
* addIfAbsent will not add the element if it already exists in the list |
124 |
*/ |
125 |
public void testAddIfAbsent() { |
126 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
127 |
full.addIfAbsent(one); |
128 |
assertEquals(SIZE, full.size()); |
129 |
} |
130 |
|
131 |
/** |
132 |
* addIfAbsent adds the element when it does not exist in the list |
133 |
*/ |
134 |
public void testAddIfAbsent2() { |
135 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
136 |
full.addIfAbsent(three); |
137 |
assertTrue(full.contains(three)); |
138 |
} |
139 |
|
140 |
/** |
141 |
* clear removes all elements from the list |
142 |
*/ |
143 |
public void testClear() { |
144 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
145 |
full.clear(); |
146 |
assertEquals(0, full.size()); |
147 |
} |
148 |
|
149 |
/** |
150 |
* Cloned list is equal |
151 |
*/ |
152 |
public void testClone() { |
153 |
CopyOnWriteArrayList l1 = populatedArray(SIZE); |
154 |
CopyOnWriteArrayList l2 = (CopyOnWriteArrayList)(l1.clone()); |
155 |
assertEquals(l1, l2); |
156 |
l1.clear(); |
157 |
assertFalse(l1.equals(l2)); |
158 |
} |
159 |
|
160 |
/** |
161 |
* contains is true for added elements |
162 |
*/ |
163 |
public void testContains() { |
164 |
CopyOnWriteArrayList full = populatedArray(3); |
165 |
assertTrue(full.contains(one)); |
166 |
assertFalse(full.contains(five)); |
167 |
} |
168 |
|
169 |
/** |
170 |
* adding at an index places it in the indicated index |
171 |
*/ |
172 |
public void testAddIndex() { |
173 |
CopyOnWriteArrayList full = populatedArray(3); |
174 |
full.add(0, m1); |
175 |
assertEquals(4, full.size()); |
176 |
assertEquals(m1, full.get(0)); |
177 |
assertEquals(zero, full.get(1)); |
178 |
|
179 |
full.add(2, m2); |
180 |
assertEquals(5, full.size()); |
181 |
assertEquals(m2, full.get(2)); |
182 |
assertEquals(two, full.get(4)); |
183 |
} |
184 |
|
185 |
/** |
186 |
* lists with same elements are equal and have same hashCode |
187 |
*/ |
188 |
public void testEquals() { |
189 |
CopyOnWriteArrayList a = populatedArray(3); |
190 |
CopyOnWriteArrayList b = populatedArray(3); |
191 |
assertTrue(a.equals(b)); |
192 |
assertTrue(b.equals(a)); |
193 |
assertTrue(a.containsAll(b)); |
194 |
assertTrue(b.containsAll(a)); |
195 |
assertEquals(a.hashCode(), b.hashCode()); |
196 |
a.add(m1); |
197 |
assertFalse(a.equals(b)); |
198 |
assertFalse(b.equals(a)); |
199 |
assertTrue(a.containsAll(b)); |
200 |
assertFalse(b.containsAll(a)); |
201 |
b.add(m1); |
202 |
assertTrue(a.equals(b)); |
203 |
assertTrue(b.equals(a)); |
204 |
assertTrue(a.containsAll(b)); |
205 |
assertTrue(b.containsAll(a)); |
206 |
assertEquals(a.hashCode(), b.hashCode()); |
207 |
|
208 |
assertFalse(a.equals(null)); |
209 |
} |
210 |
|
211 |
/** |
212 |
* containsAll returns true for collections with subset of elements |
213 |
*/ |
214 |
public void testContainsAll() { |
215 |
CopyOnWriteArrayList full = populatedArray(3); |
216 |
assertTrue(full.containsAll(Arrays.asList())); |
217 |
assertTrue(full.containsAll(Arrays.asList(one))); |
218 |
assertTrue(full.containsAll(Arrays.asList(one, two))); |
219 |
assertFalse(full.containsAll(Arrays.asList(one, two, six))); |
220 |
assertFalse(full.containsAll(Arrays.asList(six))); |
221 |
|
222 |
try { |
223 |
full.containsAll(null); |
224 |
shouldThrow(); |
225 |
} catch (NullPointerException success) {} |
226 |
} |
227 |
|
228 |
/** |
229 |
* get returns the value at the given index |
230 |
*/ |
231 |
public void testGet() { |
232 |
CopyOnWriteArrayList full = populatedArray(3); |
233 |
assertEquals(0, full.get(0)); |
234 |
} |
235 |
|
236 |
/** |
237 |
* indexOf(Object) returns the index of the first occurrence of the |
238 |
* specified element in this list, or -1 if this list does not |
239 |
* contain the element |
240 |
*/ |
241 |
public void testIndexOf() { |
242 |
CopyOnWriteArrayList list = populatedArray(3); |
243 |
assertEquals(-1, list.indexOf(-42)); |
244 |
int size = list.size(); |
245 |
for (int i = 0; i < size; i++) { |
246 |
assertEquals(i, list.indexOf(i)); |
247 |
assertEquals(i, list.subList(0, size).indexOf(i)); |
248 |
assertEquals(i, list.subList(0, i + 1).indexOf(i)); |
249 |
assertEquals(-1, list.subList(0, i).indexOf(i)); |
250 |
assertEquals(0, list.subList(i, size).indexOf(i)); |
251 |
assertEquals(-1, list.subList(i + 1, size).indexOf(i)); |
252 |
} |
253 |
|
254 |
list.add(1); |
255 |
assertEquals(1, list.indexOf(1)); |
256 |
assertEquals(1, list.subList(0, size + 1).indexOf(1)); |
257 |
assertEquals(0, list.subList(1, size + 1).indexOf(1)); |
258 |
assertEquals(size - 2, list.subList(2, size + 1).indexOf(1)); |
259 |
assertEquals(0, list.subList(size, size + 1).indexOf(1)); |
260 |
assertEquals(-1, list.subList(size + 1, size + 1).indexOf(1)); |
261 |
} |
262 |
|
263 |
/** |
264 |
* indexOf(E, int) returns the index of the first occurrence of the |
265 |
* specified element in this list, searching forwards from index, |
266 |
* or returns -1 if the element is not found |
267 |
*/ |
268 |
public void testIndexOf2() { |
269 |
CopyOnWriteArrayList list = populatedArray(3); |
270 |
int size = list.size(); |
271 |
assertEquals(-1, list.indexOf(-42, 0)); |
272 |
|
273 |
// we might expect IOOBE, but spec says otherwise |
274 |
assertEquals(-1, list.indexOf(0, size)); |
275 |
assertEquals(-1, list.indexOf(0, Integer.MAX_VALUE)); |
276 |
|
277 |
assertThrows( |
278 |
IndexOutOfBoundsException.class, |
279 |
() -> list.indexOf(0, -1), |
280 |
() -> list.indexOf(0, Integer.MIN_VALUE)); |
281 |
|
282 |
for (int i = 0; i < size; i++) { |
283 |
assertEquals(i, list.indexOf(i, 0)); |
284 |
assertEquals(i, list.indexOf(i, i)); |
285 |
assertEquals(-1, list.indexOf(i, i + 1)); |
286 |
} |
287 |
|
288 |
list.add(1); |
289 |
assertEquals(1, list.indexOf(1, 0)); |
290 |
assertEquals(1, list.indexOf(1, 1)); |
291 |
assertEquals(size, list.indexOf(1, 2)); |
292 |
assertEquals(size, list.indexOf(1, size)); |
293 |
} |
294 |
|
295 |
/** |
296 |
* isEmpty returns true when empty, else false |
297 |
*/ |
298 |
public void testIsEmpty() { |
299 |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
300 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
301 |
assertTrue(empty.isEmpty()); |
302 |
assertFalse(full.isEmpty()); |
303 |
} |
304 |
|
305 |
/** |
306 |
* iterator() returns an iterator containing the elements of the |
307 |
* list in insertion order |
308 |
*/ |
309 |
public void testIterator() { |
310 |
Collection empty = new CopyOnWriteArrayList(); |
311 |
assertFalse(empty.iterator().hasNext()); |
312 |
try { |
313 |
empty.iterator().next(); |
314 |
shouldThrow(); |
315 |
} catch (NoSuchElementException success) {} |
316 |
|
317 |
Integer[] elements = new Integer[SIZE]; |
318 |
for (int i = 0; i < SIZE; i++) |
319 |
elements[i] = i; |
320 |
shuffle(elements); |
321 |
Collection<Integer> full = populatedArray(elements); |
322 |
|
323 |
Iterator it = full.iterator(); |
324 |
for (int j = 0; j < SIZE; j++) { |
325 |
assertTrue(it.hasNext()); |
326 |
assertEquals(elements[j], it.next()); |
327 |
} |
328 |
assertIteratorExhausted(it); |
329 |
} |
330 |
|
331 |
/** |
332 |
* iterator of empty collection has no elements |
333 |
*/ |
334 |
public void testEmptyIterator() { |
335 |
Collection c = new CopyOnWriteArrayList(); |
336 |
assertIteratorExhausted(c.iterator()); |
337 |
} |
338 |
|
339 |
/** |
340 |
* iterator.remove throws UnsupportedOperationException |
341 |
*/ |
342 |
public void testIteratorRemove() { |
343 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
344 |
Iterator it = full.iterator(); |
345 |
it.next(); |
346 |
try { |
347 |
it.remove(); |
348 |
shouldThrow(); |
349 |
} catch (UnsupportedOperationException success) {} |
350 |
} |
351 |
|
352 |
/** |
353 |
* toString contains toString of elements |
354 |
*/ |
355 |
public void testToString() { |
356 |
assertEquals("[]", new CopyOnWriteArrayList().toString()); |
357 |
CopyOnWriteArrayList full = populatedArray(3); |
358 |
String s = full.toString(); |
359 |
for (int i = 0; i < 3; ++i) |
360 |
assertTrue(s.contains(String.valueOf(i))); |
361 |
assertEquals(new ArrayList(full).toString(), |
362 |
full.toString()); |
363 |
} |
364 |
|
365 |
/** |
366 |
* lastIndexOf(Object) returns the index of the last occurrence of |
367 |
* the specified element in this list, or -1 if this list does not |
368 |
* contain the element |
369 |
*/ |
370 |
public void testLastIndexOf1() { |
371 |
CopyOnWriteArrayList list = populatedArray(3); |
372 |
assertEquals(-1, list.lastIndexOf(-42)); |
373 |
int size = list.size(); |
374 |
for (int i = 0; i < size; i++) { |
375 |
assertEquals(i, list.lastIndexOf(i)); |
376 |
assertEquals(i, list.subList(0, size).lastIndexOf(i)); |
377 |
assertEquals(i, list.subList(0, i + 1).lastIndexOf(i)); |
378 |
assertEquals(-1, list.subList(0, i).lastIndexOf(i)); |
379 |
assertEquals(0, list.subList(i, size).lastIndexOf(i)); |
380 |
assertEquals(-1, list.subList(i + 1, size).lastIndexOf(i)); |
381 |
} |
382 |
|
383 |
list.add(1); |
384 |
assertEquals(size, list.lastIndexOf(1)); |
385 |
assertEquals(size, list.subList(0, size + 1).lastIndexOf(1)); |
386 |
assertEquals(1, list.subList(0, size).lastIndexOf(1)); |
387 |
assertEquals(0, list.subList(1, 2).lastIndexOf(1)); |
388 |
assertEquals(-1, list.subList(0, 1).indexOf(1)); |
389 |
} |
390 |
|
391 |
/** |
392 |
* lastIndexOf(E, int) returns the index of the last occurrence of the |
393 |
* specified element in this list, searching backwards from index, or |
394 |
* returns -1 if the element is not found |
395 |
*/ |
396 |
public void testLastIndexOf2() { |
397 |
CopyOnWriteArrayList list = populatedArray(3); |
398 |
|
399 |
// we might expect IOOBE, but spec says otherwise |
400 |
assertEquals(-1, list.lastIndexOf(0, -1)); |
401 |
|
402 |
int size = list.size(); |
403 |
assertThrows( |
404 |
IndexOutOfBoundsException.class, |
405 |
() -> list.lastIndexOf(0, size), |
406 |
() -> list.lastIndexOf(0, Integer.MAX_VALUE)); |
407 |
|
408 |
for (int i = 0; i < size; i++) { |
409 |
assertEquals(i, list.lastIndexOf(i, i)); |
410 |
assertEquals(list.indexOf(i), list.lastIndexOf(i, i)); |
411 |
if (i > 0) |
412 |
assertEquals(-1, list.lastIndexOf(i, i - 1)); |
413 |
} |
414 |
list.add(one); |
415 |
list.add(three); |
416 |
assertEquals(1, list.lastIndexOf(one, 1)); |
417 |
assertEquals(1, list.lastIndexOf(one, 2)); |
418 |
assertEquals(3, list.lastIndexOf(one, 3)); |
419 |
assertEquals(3, list.lastIndexOf(one, 4)); |
420 |
assertEquals(-1, list.lastIndexOf(three, 3)); |
421 |
} |
422 |
|
423 |
/** |
424 |
* listIterator traverses all elements |
425 |
*/ |
426 |
public void testListIterator1() { |
427 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
428 |
ListIterator i = full.listIterator(); |
429 |
int j; |
430 |
for (j = 0; i.hasNext(); j++) |
431 |
assertEquals(j, i.next()); |
432 |
assertEquals(SIZE, j); |
433 |
} |
434 |
|
435 |
/** |
436 |
* listIterator only returns those elements after the given index |
437 |
*/ |
438 |
public void testListIterator2() { |
439 |
CopyOnWriteArrayList full = populatedArray(3); |
440 |
ListIterator i = full.listIterator(1); |
441 |
int j; |
442 |
for (j = 0; i.hasNext(); j++) |
443 |
assertEquals(j + 1, i.next()); |
444 |
assertEquals(2, j); |
445 |
} |
446 |
|
447 |
/** |
448 |
* remove(int) removes and returns the object at the given index |
449 |
*/ |
450 |
public void testRemove_int() { |
451 |
int SIZE = 3; |
452 |
for (int i = 0; i < SIZE; i++) { |
453 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
454 |
assertEquals(i, full.remove(i)); |
455 |
assertEquals(SIZE - 1, full.size()); |
456 |
assertFalse(full.contains(new Integer(i))); |
457 |
} |
458 |
} |
459 |
|
460 |
/** |
461 |
* remove(Object) removes the object if found and returns true |
462 |
*/ |
463 |
public void testRemove_Object() { |
464 |
int SIZE = 3; |
465 |
for (int i = 0; i < SIZE; i++) { |
466 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
467 |
assertFalse(full.remove(new Integer(-42))); |
468 |
assertTrue(full.remove(new Integer(i))); |
469 |
assertEquals(SIZE - 1, full.size()); |
470 |
assertFalse(full.contains(new Integer(i))); |
471 |
} |
472 |
CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6)); |
473 |
assertTrue(x.remove(new Integer(6))); |
474 |
assertEquals(x, Arrays.asList(4, 5)); |
475 |
assertTrue(x.remove(new Integer(4))); |
476 |
assertEquals(x, Arrays.asList(5)); |
477 |
assertTrue(x.remove(new Integer(5))); |
478 |
assertEquals(x, Arrays.asList()); |
479 |
assertFalse(x.remove(new Integer(5))); |
480 |
} |
481 |
|
482 |
/** |
483 |
* removeAll removes all elements from the given collection |
484 |
*/ |
485 |
public void testRemoveAll() { |
486 |
CopyOnWriteArrayList full = populatedArray(3); |
487 |
assertTrue(full.removeAll(Arrays.asList(one, two))); |
488 |
assertEquals(1, full.size()); |
489 |
assertFalse(full.removeAll(Arrays.asList(one, two))); |
490 |
assertEquals(1, full.size()); |
491 |
} |
492 |
|
493 |
/** |
494 |
* set changes the element at the given index |
495 |
*/ |
496 |
public void testSet() { |
497 |
CopyOnWriteArrayList full = populatedArray(3); |
498 |
assertEquals(2, full.set(2, four)); |
499 |
assertEquals(4, full.get(2)); |
500 |
} |
501 |
|
502 |
/** |
503 |
* size returns the number of elements |
504 |
*/ |
505 |
public void testSize() { |
506 |
CopyOnWriteArrayList empty = new CopyOnWriteArrayList(); |
507 |
CopyOnWriteArrayList full = populatedArray(SIZE); |
508 |
assertEquals(SIZE, full.size()); |
509 |
assertEquals(0, empty.size()); |
510 |
} |
511 |
|
512 |
/** |
513 |
* toArray() returns an Object array containing all elements from |
514 |
* the list in insertion order |
515 |
*/ |
516 |
public void testToArray() { |
517 |
Object[] a = new CopyOnWriteArrayList().toArray(); |
518 |
assertTrue(Arrays.equals(new Object[0], a)); |
519 |
assertSame(Object[].class, a.getClass()); |
520 |
|
521 |
Integer[] elements = new Integer[SIZE]; |
522 |
for (int i = 0; i < SIZE; i++) |
523 |
elements[i] = i; |
524 |
shuffle(elements); |
525 |
Collection<Integer> full = populatedArray(elements); |
526 |
|
527 |
assertTrue(Arrays.equals(elements, full.toArray())); |
528 |
assertSame(Object[].class, full.toArray().getClass()); |
529 |
} |
530 |
|
531 |
/** |
532 |
* toArray(Integer array) returns an Integer array containing all |
533 |
* elements from the list in insertion order |
534 |
*/ |
535 |
public void testToArray2() { |
536 |
Collection empty = new CopyOnWriteArrayList(); |
537 |
Integer[] a; |
538 |
|
539 |
a = new Integer[0]; |
540 |
assertSame(a, empty.toArray(a)); |
541 |
|
542 |
a = new Integer[SIZE / 2]; |
543 |
Arrays.fill(a, 42); |
544 |
assertSame(a, empty.toArray(a)); |
545 |
assertNull(a[0]); |
546 |
for (int i = 1; i < a.length; i++) |
547 |
assertEquals(42, (int) a[i]); |
548 |
|
549 |
Integer[] elements = new Integer[SIZE]; |
550 |
for (int i = 0; i < SIZE; i++) |
551 |
elements[i] = i; |
552 |
shuffle(elements); |
553 |
Collection<Integer> full = populatedArray(elements); |
554 |
|
555 |
Arrays.fill(a, 42); |
556 |
assertTrue(Arrays.equals(elements, full.toArray(a))); |
557 |
for (int i = 0; i < a.length; i++) |
558 |
assertEquals(42, (int) a[i]); |
559 |
assertSame(Integer[].class, full.toArray(a).getClass()); |
560 |
|
561 |
a = new Integer[SIZE]; |
562 |
Arrays.fill(a, 42); |
563 |
assertSame(a, full.toArray(a)); |
564 |
assertTrue(Arrays.equals(elements, a)); |
565 |
|
566 |
a = new Integer[2 * SIZE]; |
567 |
Arrays.fill(a, 42); |
568 |
assertSame(a, full.toArray(a)); |
569 |
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
570 |
assertNull(a[SIZE]); |
571 |
for (int i = SIZE + 1; i < a.length; i++) |
572 |
assertEquals(42, (int) a[i]); |
573 |
} |
574 |
|
575 |
/** |
576 |
* sublists contains elements at indexes offset from their base |
577 |
*/ |
578 |
public void testSubList() { |
579 |
CopyOnWriteArrayList a = populatedArray(10); |
580 |
assertTrue(a.subList(1,1).isEmpty()); |
581 |
for (int j = 0; j < 9; ++j) { |
582 |
for (int i = j ; i < 10; ++i) { |
583 |
List b = a.subList(j,i); |
584 |
for (int k = j; k < i; ++k) { |
585 |
assertEquals(new Integer(k), b.get(k-j)); |
586 |
} |
587 |
} |
588 |
} |
589 |
|
590 |
List s = a.subList(2, 5); |
591 |
assertEquals(3, s.size()); |
592 |
s.set(2, m1); |
593 |
assertEquals(a.get(4), m1); |
594 |
s.clear(); |
595 |
assertEquals(7, a.size()); |
596 |
|
597 |
assertThrows( |
598 |
IndexOutOfBoundsException.class, |
599 |
() -> s.get(0), |
600 |
() -> s.set(0, 42)); |
601 |
} |
602 |
|
603 |
// Exception tests |
604 |
|
605 |
/** |
606 |
* toArray throws an ArrayStoreException when the given array |
607 |
* can not store the objects inside the list |
608 |
*/ |
609 |
public void testToArray_ArrayStoreException() { |
610 |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
611 |
c.add("zfasdfsdf"); |
612 |
c.add("asdadasd"); |
613 |
try { |
614 |
c.toArray(new Long[5]); |
615 |
shouldThrow(); |
616 |
} catch (ArrayStoreException success) {} |
617 |
} |
618 |
|
619 |
/** |
620 |
* get throws an IndexOutOfBoundsException on a negative index |
621 |
*/ |
622 |
public void testGet1_IndexOutOfBoundsException() { |
623 |
CopyOnWriteArrayList c = populatedArray(5); |
624 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
625 |
for (List list : lists) { |
626 |
try { |
627 |
list.get(-1); |
628 |
shouldThrow(); |
629 |
} catch (IndexOutOfBoundsException success) {} |
630 |
} |
631 |
} |
632 |
|
633 |
/** |
634 |
* get throws an IndexOutOfBoundsException on a too high index |
635 |
*/ |
636 |
public void testGet2_IndexOutOfBoundsException() { |
637 |
CopyOnWriteArrayList c = populatedArray(5); |
638 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
639 |
for (List list : lists) { |
640 |
try { |
641 |
list.get(list.size()); |
642 |
shouldThrow(); |
643 |
} catch (IndexOutOfBoundsException success) {} |
644 |
} |
645 |
} |
646 |
|
647 |
/** |
648 |
* set throws an IndexOutOfBoundsException on a negative index |
649 |
*/ |
650 |
public void testSet1_IndexOutOfBoundsException() { |
651 |
CopyOnWriteArrayList c = populatedArray(5); |
652 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
653 |
for (List list : lists) { |
654 |
try { |
655 |
list.set(-1, "qwerty"); |
656 |
shouldThrow(); |
657 |
} catch (IndexOutOfBoundsException success) {} |
658 |
} |
659 |
} |
660 |
|
661 |
/** |
662 |
* set throws an IndexOutOfBoundsException on a too high index |
663 |
*/ |
664 |
public void testSet2() { |
665 |
CopyOnWriteArrayList c = populatedArray(5); |
666 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
667 |
for (List list : lists) { |
668 |
try { |
669 |
list.set(list.size(), "qwerty"); |
670 |
shouldThrow(); |
671 |
} catch (IndexOutOfBoundsException success) {} |
672 |
} |
673 |
} |
674 |
|
675 |
/** |
676 |
* add throws an IndexOutOfBoundsException on a negative index |
677 |
*/ |
678 |
public void testAdd1_IndexOutOfBoundsException() { |
679 |
CopyOnWriteArrayList c = populatedArray(5); |
680 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
681 |
for (List list : lists) { |
682 |
try { |
683 |
list.add(-1, "qwerty"); |
684 |
shouldThrow(); |
685 |
} catch (IndexOutOfBoundsException success) {} |
686 |
} |
687 |
} |
688 |
|
689 |
/** |
690 |
* add throws an IndexOutOfBoundsException on a too high index |
691 |
*/ |
692 |
public void testAdd2_IndexOutOfBoundsException() { |
693 |
CopyOnWriteArrayList c = populatedArray(5); |
694 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
695 |
for (List list : lists) { |
696 |
try { |
697 |
list.add(list.size() + 1, "qwerty"); |
698 |
shouldThrow(); |
699 |
} catch (IndexOutOfBoundsException success) {} |
700 |
} |
701 |
} |
702 |
|
703 |
/** |
704 |
* remove throws an IndexOutOfBoundsException on a negative index |
705 |
*/ |
706 |
public void testRemove1_IndexOutOfBounds() { |
707 |
CopyOnWriteArrayList c = populatedArray(5); |
708 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
709 |
for (List list : lists) { |
710 |
try { |
711 |
list.remove(-1); |
712 |
shouldThrow(); |
713 |
} catch (IndexOutOfBoundsException success) {} |
714 |
} |
715 |
} |
716 |
|
717 |
/** |
718 |
* remove throws an IndexOutOfBoundsException on a too high index |
719 |
*/ |
720 |
public void testRemove2_IndexOutOfBounds() { |
721 |
CopyOnWriteArrayList c = populatedArray(5); |
722 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
723 |
for (List list : lists) { |
724 |
try { |
725 |
list.remove(list.size()); |
726 |
shouldThrow(); |
727 |
} catch (IndexOutOfBoundsException success) {} |
728 |
} |
729 |
} |
730 |
|
731 |
/** |
732 |
* addAll throws an IndexOutOfBoundsException on a negative index |
733 |
*/ |
734 |
public void testAddAll1_IndexOutOfBoundsException() { |
735 |
CopyOnWriteArrayList c = populatedArray(5); |
736 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
737 |
for (List list : lists) { |
738 |
try { |
739 |
list.addAll(-1, new LinkedList()); |
740 |
shouldThrow(); |
741 |
} catch (IndexOutOfBoundsException success) {} |
742 |
} |
743 |
} |
744 |
|
745 |
/** |
746 |
* addAll throws an IndexOutOfBoundsException on a too high index |
747 |
*/ |
748 |
public void testAddAll2_IndexOutOfBoundsException() { |
749 |
CopyOnWriteArrayList c = populatedArray(5); |
750 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
751 |
for (List list : lists) { |
752 |
try { |
753 |
list.addAll(list.size() + 1, new LinkedList()); |
754 |
shouldThrow(); |
755 |
} catch (IndexOutOfBoundsException success) {} |
756 |
} |
757 |
} |
758 |
|
759 |
/** |
760 |
* listIterator throws an IndexOutOfBoundsException on a negative index |
761 |
*/ |
762 |
public void testListIterator1_IndexOutOfBoundsException() { |
763 |
CopyOnWriteArrayList c = populatedArray(5); |
764 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
765 |
for (List list : lists) { |
766 |
try { |
767 |
list.listIterator(-1); |
768 |
shouldThrow(); |
769 |
} catch (IndexOutOfBoundsException success) {} |
770 |
} |
771 |
} |
772 |
|
773 |
/** |
774 |
* listIterator throws an IndexOutOfBoundsException on a too high index |
775 |
*/ |
776 |
public void testListIterator2_IndexOutOfBoundsException() { |
777 |
CopyOnWriteArrayList c = populatedArray(5); |
778 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
779 |
for (List list : lists) { |
780 |
try { |
781 |
list.listIterator(list.size() + 1); |
782 |
shouldThrow(); |
783 |
} catch (IndexOutOfBoundsException success) {} |
784 |
} |
785 |
} |
786 |
|
787 |
/** |
788 |
* subList throws an IndexOutOfBoundsException on a negative index |
789 |
*/ |
790 |
public void testSubList1_IndexOutOfBoundsException() { |
791 |
CopyOnWriteArrayList c = populatedArray(5); |
792 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
793 |
for (List list : lists) { |
794 |
try { |
795 |
list.subList(-1, list.size()); |
796 |
shouldThrow(); |
797 |
} catch (IndexOutOfBoundsException success) {} |
798 |
} |
799 |
} |
800 |
|
801 |
/** |
802 |
* subList throws an IndexOutOfBoundsException on a too high index |
803 |
*/ |
804 |
public void testSubList2_IndexOutOfBoundsException() { |
805 |
CopyOnWriteArrayList c = populatedArray(5); |
806 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
807 |
for (List list : lists) { |
808 |
try { |
809 |
list.subList(0, list.size() + 1); |
810 |
shouldThrow(); |
811 |
} catch (IndexOutOfBoundsException success) {} |
812 |
} |
813 |
} |
814 |
|
815 |
/** |
816 |
* subList throws IndexOutOfBoundsException when the second index |
817 |
* is lower then the first |
818 |
*/ |
819 |
public void testSubList3_IndexOutOfBoundsException() { |
820 |
CopyOnWriteArrayList c = populatedArray(5); |
821 |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
822 |
for (List list : lists) { |
823 |
try { |
824 |
list.subList(list.size() - 1, 1); |
825 |
shouldThrow(); |
826 |
} catch (IndexOutOfBoundsException success) {} |
827 |
} |
828 |
} |
829 |
|
830 |
/** |
831 |
* a deserialized/reserialized list equals original |
832 |
*/ |
833 |
public void testSerialization() throws Exception { |
834 |
List x = populatedArray(SIZE); |
835 |
List y = serialClone(x); |
836 |
|
837 |
assertNotSame(x, y); |
838 |
assertEquals(x.size(), y.size()); |
839 |
assertEquals(x.toString(), y.toString()); |
840 |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
841 |
assertEquals(x, y); |
842 |
assertEquals(y, x); |
843 |
while (!x.isEmpty()) { |
844 |
assertFalse(y.isEmpty()); |
845 |
assertEquals(x.remove(0), y.remove(0)); |
846 |
} |
847 |
assertTrue(y.isEmpty()); |
848 |
} |
849 |
|
850 |
} |