ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
Revision: 1.32
Committed: Sat Jan 17 22:55:06 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.31: +9 -5 lines
Log Message:
add more tests of exhausted iterators

File Contents

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