6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
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.Vector; |
16 |
> |
import java.util.NoSuchElementException; |
17 |
|
import java.util.concurrent.CopyOnWriteArrayList; |
18 |
|
|
19 |
+ |
import junit.framework.Test; |
20 |
+ |
import junit.framework.TestSuite; |
21 |
+ |
|
22 |
|
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
23 |
|
|
24 |
|
public static void main(String[] args) { |
25 |
< |
junit.textui.TestRunner.run(suite()); |
25 |
> |
main(suite(), args); |
26 |
|
} |
27 |
|
|
28 |
|
public static Test suite() { |
29 |
< |
return new TestSuite(CopyOnWriteArrayListTest.class); |
29 |
> |
class Implementation implements CollectionImplementation { |
30 |
> |
public Class<?> klazz() { return ArrayList.class; } |
31 |
> |
public List emptyCollection() { return new CopyOnWriteArrayList(); } |
32 |
> |
public Object makeElement(int i) { return i; } |
33 |
> |
public boolean isConcurrent() { return true; } |
34 |
> |
public boolean permitsNulls() { return true; } |
35 |
> |
} |
36 |
> |
class SubListImplementation extends Implementation { |
37 |
> |
public List emptyCollection() { |
38 |
> |
return super.emptyCollection().subList(0, 0); |
39 |
> |
} |
40 |
> |
} |
41 |
> |
return newTestSuite( |
42 |
> |
CopyOnWriteArrayListTest.class, |
43 |
> |
CollectionTest.testSuite(new Implementation())); |
44 |
> |
//CollectionTest.testSuite(new SubListImplementation())); |
45 |
|
} |
46 |
|
|
47 |
< |
static CopyOnWriteArrayList populatedArray(int n) { |
48 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
47 |
> |
static CopyOnWriteArrayList<Integer> populatedArray(int n) { |
48 |
> |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
49 |
|
assertTrue(a.isEmpty()); |
50 |
< |
for (int i = 0; i < n; ++i) |
51 |
< |
a.add(new Integer(i)); |
50 |
> |
for (int i = 0; i < n; i++) |
51 |
> |
a.add(i); |
52 |
|
assertFalse(a.isEmpty()); |
53 |
|
assertEquals(n, a.size()); |
54 |
|
return a; |
55 |
|
} |
56 |
|
|
57 |
+ |
static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) { |
58 |
+ |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
59 |
+ |
assertTrue(a.isEmpty()); |
60 |
+ |
for (int i = 0; i < elements.length; i++) |
61 |
+ |
a.add(elements[i]); |
62 |
+ |
assertFalse(a.isEmpty()); |
63 |
+ |
assertEquals(elements.length, a.size()); |
64 |
+ |
return a; |
65 |
+ |
} |
66 |
+ |
|
67 |
|
/** |
68 |
|
* a new list is empty |
69 |
|
*/ |
77 |
|
*/ |
78 |
|
public void testConstructor2() { |
79 |
|
Integer[] ints = new Integer[SIZE]; |
80 |
< |
for (int i = 0; i < SIZE-1; ++i) |
80 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
81 |
|
ints[i] = new Integer(i); |
82 |
|
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); |
83 |
|
for (int i = 0; i < SIZE; ++i) |
89 |
|
*/ |
90 |
|
public void testConstructor3() { |
91 |
|
Integer[] ints = new Integer[SIZE]; |
92 |
< |
for (int i = 0; i < SIZE-1; ++i) |
92 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
93 |
|
ints[i] = new Integer(i); |
94 |
|
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); |
95 |
|
for (int i = 0; i < SIZE; ++i) |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
< |
* addAll adds each element from the given collection |
100 |
> |
* addAll adds each element from the given collection, including duplicates |
101 |
|
*/ |
102 |
|
public void testAddAll() { |
103 |
|
CopyOnWriteArrayList full = populatedArray(3); |
104 |
< |
Vector v = new Vector(); |
76 |
< |
v.add(three); |
77 |
< |
v.add(four); |
78 |
< |
v.add(five); |
79 |
< |
full.addAll(v); |
104 |
> |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
105 |
|
assertEquals(6, full.size()); |
106 |
+ |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
107 |
+ |
assertEquals(9, full.size()); |
108 |
|
} |
109 |
|
|
110 |
|
/** |
113 |
|
*/ |
114 |
|
public void testAddAllAbsent() { |
115 |
|
CopyOnWriteArrayList full = populatedArray(3); |
116 |
< |
Vector v = new Vector(); |
117 |
< |
v.add(three); |
118 |
< |
v.add(four); |
119 |
< |
v.add(one); // will not add this element |
93 |
< |
full.addAllAbsent(v); |
116 |
> |
// "one" is duplicate and will not be added |
117 |
> |
assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one))); |
118 |
> |
assertEquals(5, full.size()); |
119 |
> |
assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one))); |
120 |
|
assertEquals(5, full.size()); |
121 |
|
} |
122 |
|
|
191 |
|
CopyOnWriteArrayList b = populatedArray(3); |
192 |
|
assertTrue(a.equals(b)); |
193 |
|
assertTrue(b.equals(a)); |
194 |
+ |
assertTrue(a.containsAll(b)); |
195 |
+ |
assertTrue(b.containsAll(a)); |
196 |
|
assertEquals(a.hashCode(), b.hashCode()); |
197 |
|
a.add(m1); |
198 |
|
assertFalse(a.equals(b)); |
199 |
|
assertFalse(b.equals(a)); |
200 |
+ |
assertTrue(a.containsAll(b)); |
201 |
+ |
assertFalse(b.containsAll(a)); |
202 |
|
b.add(m1); |
203 |
|
assertTrue(a.equals(b)); |
204 |
|
assertTrue(b.equals(a)); |
205 |
+ |
assertTrue(a.containsAll(b)); |
206 |
+ |
assertTrue(b.containsAll(a)); |
207 |
|
assertEquals(a.hashCode(), b.hashCode()); |
208 |
+ |
|
209 |
+ |
assertFalse(a.equals(null)); |
210 |
|
} |
211 |
|
|
212 |
|
/** |
213 |
< |
* containsAll returns true for collection with subset of elements |
213 |
> |
* containsAll returns true for collections with subset of elements |
214 |
|
*/ |
215 |
|
public void testContainsAll() { |
216 |
|
CopyOnWriteArrayList full = populatedArray(3); |
217 |
< |
Vector v = new Vector(); |
218 |
< |
v.add(one); |
219 |
< |
v.add(two); |
220 |
< |
assertTrue(full.containsAll(v)); |
221 |
< |
v.add(six); |
222 |
< |
assertFalse(full.containsAll(v)); |
217 |
> |
assertTrue(full.containsAll(Arrays.asList())); |
218 |
> |
assertTrue(full.containsAll(Arrays.asList(one))); |
219 |
> |
assertTrue(full.containsAll(Arrays.asList(one, two))); |
220 |
> |
assertFalse(full.containsAll(Arrays.asList(one, two, six))); |
221 |
> |
assertFalse(full.containsAll(Arrays.asList(six))); |
222 |
> |
|
223 |
> |
try { |
224 |
> |
full.containsAll(null); |
225 |
> |
shouldThrow(); |
226 |
> |
} catch (NullPointerException success) {} |
227 |
|
} |
228 |
|
|
229 |
|
/** |
264 |
|
} |
265 |
|
|
266 |
|
/** |
267 |
< |
* iterator() returns an iterator containing the elements of the list |
267 |
> |
* iterator() returns an iterator containing the elements of the |
268 |
> |
* list in insertion order |
269 |
|
*/ |
270 |
|
public void testIterator() { |
271 |
< |
CopyOnWriteArrayList full = populatedArray(SIZE); |
272 |
< |
Iterator i = full.iterator(); |
273 |
< |
int j; |
274 |
< |
for (j = 0; i.hasNext(); j++) |
275 |
< |
assertEquals(j, i.next()); |
276 |
< |
assertEquals(SIZE, j); |
271 |
> |
Collection empty = new CopyOnWriteArrayList(); |
272 |
> |
assertFalse(empty.iterator().hasNext()); |
273 |
> |
try { |
274 |
> |
empty.iterator().next(); |
275 |
> |
shouldThrow(); |
276 |
> |
} catch (NoSuchElementException success) {} |
277 |
> |
|
278 |
> |
Integer[] elements = new Integer[SIZE]; |
279 |
> |
for (int i = 0; i < SIZE; i++) |
280 |
> |
elements[i] = i; |
281 |
> |
shuffle(elements); |
282 |
> |
Collection<Integer> full = populatedArray(elements); |
283 |
> |
|
284 |
> |
Iterator it = full.iterator(); |
285 |
> |
for (int j = 0; j < SIZE; j++) { |
286 |
> |
assertTrue(it.hasNext()); |
287 |
> |
assertEquals(elements[j], it.next()); |
288 |
> |
} |
289 |
> |
assertIteratorExhausted(it); |
290 |
> |
} |
291 |
> |
|
292 |
> |
/** |
293 |
> |
* iterator of empty collection has no elements |
294 |
> |
*/ |
295 |
> |
public void testEmptyIterator() { |
296 |
> |
Collection c = new CopyOnWriteArrayList(); |
297 |
> |
assertIteratorExhausted(c.iterator()); |
298 |
|
} |
299 |
|
|
300 |
|
/** |
314 |
|
* toString contains toString of elements |
315 |
|
*/ |
316 |
|
public void testToString() { |
317 |
+ |
assertEquals("[]", new CopyOnWriteArrayList().toString()); |
318 |
|
CopyOnWriteArrayList full = populatedArray(3); |
319 |
|
String s = full.toString(); |
320 |
< |
for (int i = 0; i < 3; ++i) { |
320 |
> |
for (int i = 0; i < 3; ++i) |
321 |
|
assertTrue(s.contains(String.valueOf(i))); |
322 |
< |
} |
322 |
> |
assertEquals(new ArrayList(full).toString(), |
323 |
> |
full.toString()); |
324 |
|
} |
325 |
|
|
326 |
|
/** |
365 |
|
ListIterator i = full.listIterator(1); |
366 |
|
int j; |
367 |
|
for (j = 0; i.hasNext(); j++) |
368 |
< |
assertEquals(j+1, i.next()); |
368 |
> |
assertEquals(j + 1, i.next()); |
369 |
|
assertEquals(2, j); |
370 |
|
} |
371 |
|
|
372 |
|
/** |
373 |
< |
* remove removes and returns the object at the given index |
373 |
> |
* remove(int) removes and returns the object at the given index |
374 |
|
*/ |
375 |
< |
public void testRemove() { |
376 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
377 |
< |
assertEquals(2, full.remove(2)); |
378 |
< |
assertEquals(2, full.size()); |
375 |
> |
public void testRemove_int() { |
376 |
> |
int SIZE = 3; |
377 |
> |
for (int i = 0; i < SIZE; i++) { |
378 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
379 |
> |
assertEquals(i, full.remove(i)); |
380 |
> |
assertEquals(SIZE - 1, full.size()); |
381 |
> |
assertFalse(full.contains(new Integer(i))); |
382 |
> |
} |
383 |
> |
} |
384 |
> |
|
385 |
> |
/** |
386 |
> |
* remove(Object) removes the object if found and returns true |
387 |
> |
*/ |
388 |
> |
public void testRemove_Object() { |
389 |
> |
int SIZE = 3; |
390 |
> |
for (int i = 0; i < SIZE; i++) { |
391 |
> |
CopyOnWriteArrayList full = populatedArray(SIZE); |
392 |
> |
assertFalse(full.remove(new Integer(-42))); |
393 |
> |
assertTrue(full.remove(new Integer(i))); |
394 |
> |
assertEquals(SIZE - 1, full.size()); |
395 |
> |
assertFalse(full.contains(new Integer(i))); |
396 |
> |
} |
397 |
> |
CopyOnWriteArrayList x = new CopyOnWriteArrayList(Arrays.asList(4, 5, 6)); |
398 |
> |
assertTrue(x.remove(new Integer(6))); |
399 |
> |
assertEquals(x, Arrays.asList(4, 5)); |
400 |
> |
assertTrue(x.remove(new Integer(4))); |
401 |
> |
assertEquals(x, Arrays.asList(5)); |
402 |
> |
assertTrue(x.remove(new Integer(5))); |
403 |
> |
assertEquals(x, Arrays.asList()); |
404 |
> |
assertFalse(x.remove(new Integer(5))); |
405 |
|
} |
406 |
|
|
407 |
|
/** |
409 |
|
*/ |
410 |
|
public void testRemoveAll() { |
411 |
|
CopyOnWriteArrayList full = populatedArray(3); |
412 |
< |
Vector v = new Vector(); |
413 |
< |
v.add(one); |
414 |
< |
v.add(two); |
327 |
< |
full.removeAll(v); |
412 |
> |
assertTrue(full.removeAll(Arrays.asList(one, two))); |
413 |
> |
assertEquals(1, full.size()); |
414 |
> |
assertFalse(full.removeAll(Arrays.asList(one, two))); |
415 |
|
assertEquals(1, full.size()); |
416 |
|
} |
417 |
|
|
435 |
|
} |
436 |
|
|
437 |
|
/** |
438 |
< |
* toArray returns an Object array containing all elements from the list |
438 |
> |
* toArray() returns an Object array containing all elements from |
439 |
> |
* the list in insertion order |
440 |
|
*/ |
441 |
|
public void testToArray() { |
442 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
443 |
< |
Object[] o = full.toArray(); |
444 |
< |
assertEquals(3, o.length); |
445 |
< |
assertEquals(0, o[0]); |
446 |
< |
assertEquals(1, o[1]); |
447 |
< |
assertEquals(2, o[2]); |
442 |
> |
Object[] a = new CopyOnWriteArrayList().toArray(); |
443 |
> |
assertTrue(Arrays.equals(new Object[0], a)); |
444 |
> |
assertSame(Object[].class, a.getClass()); |
445 |
> |
|
446 |
> |
Integer[] elements = new Integer[SIZE]; |
447 |
> |
for (int i = 0; i < SIZE; i++) |
448 |
> |
elements[i] = i; |
449 |
> |
shuffle(elements); |
450 |
> |
Collection<Integer> full = populatedArray(elements); |
451 |
> |
|
452 |
> |
assertTrue(Arrays.equals(elements, full.toArray())); |
453 |
> |
assertSame(Object[].class, full.toArray().getClass()); |
454 |
|
} |
455 |
|
|
456 |
|
/** |
457 |
< |
* toArray returns an Integer array containing all elements from |
458 |
< |
* the list |
457 |
> |
* toArray(Integer array) returns an Integer array containing all |
458 |
> |
* elements from the list in insertion order |
459 |
|
*/ |
460 |
|
public void testToArray2() { |
461 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
462 |
< |
Integer[] i = new Integer[3]; |
463 |
< |
i = (Integer[])full.toArray(i); |
464 |
< |
assertEquals(3, i.length); |
465 |
< |
assertEquals(0, i[0].intValue()); |
466 |
< |
assertEquals(1, i[1].intValue()); |
467 |
< |
assertEquals(2, i[2].intValue()); |
461 |
> |
Collection empty = new CopyOnWriteArrayList(); |
462 |
> |
Integer[] a; |
463 |
> |
|
464 |
> |
a = new Integer[0]; |
465 |
> |
assertSame(a, empty.toArray(a)); |
466 |
> |
|
467 |
> |
a = new Integer[SIZE / 2]; |
468 |
> |
Arrays.fill(a, 42); |
469 |
> |
assertSame(a, empty.toArray(a)); |
470 |
> |
assertNull(a[0]); |
471 |
> |
for (int i = 1; i < a.length; i++) |
472 |
> |
assertEquals(42, (int) a[i]); |
473 |
> |
|
474 |
> |
Integer[] elements = new Integer[SIZE]; |
475 |
> |
for (int i = 0; i < SIZE; i++) |
476 |
> |
elements[i] = i; |
477 |
> |
shuffle(elements); |
478 |
> |
Collection<Integer> full = populatedArray(elements); |
479 |
> |
|
480 |
> |
Arrays.fill(a, 42); |
481 |
> |
assertTrue(Arrays.equals(elements, full.toArray(a))); |
482 |
> |
for (int i = 0; i < a.length; i++) |
483 |
> |
assertEquals(42, (int) a[i]); |
484 |
> |
assertSame(Integer[].class, full.toArray(a).getClass()); |
485 |
> |
|
486 |
> |
a = new Integer[SIZE]; |
487 |
> |
Arrays.fill(a, 42); |
488 |
> |
assertSame(a, full.toArray(a)); |
489 |
> |
assertTrue(Arrays.equals(elements, a)); |
490 |
> |
|
491 |
> |
a = new Integer[2 * SIZE]; |
492 |
> |
Arrays.fill(a, 42); |
493 |
> |
assertSame(a, full.toArray(a)); |
494 |
> |
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
495 |
> |
assertNull(a[SIZE]); |
496 |
> |
for (int i = SIZE + 1; i < a.length; i++) |
497 |
> |
assertEquals(42, (int) a[i]); |
498 |
|
} |
499 |
|
|
500 |
|
/** |
513 |
|
} |
514 |
|
|
515 |
|
List s = a.subList(2, 5); |
516 |
< |
assertEquals(s.size(), 3); |
516 |
> |
assertEquals(3, s.size()); |
517 |
|
s.set(2, m1); |
518 |
|
assertEquals(a.get(4), m1); |
519 |
|
s.clear(); |
520 |
< |
assertEquals(a.size(), 7); |
520 |
> |
assertEquals(7, a.size()); |
521 |
|
} |
522 |
|
|
523 |
|
// Exception tests |
527 |
|
* can not store the objects inside the list |
528 |
|
*/ |
529 |
|
public void testToArray_ArrayStoreException() { |
530 |
+ |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
531 |
+ |
c.add("zfasdfsdf"); |
532 |
+ |
c.add("asdadasd"); |
533 |
|
try { |
407 |
– |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
408 |
– |
c.add("zfasdfsdf"); |
409 |
– |
c.add("asdadasd"); |
534 |
|
c.toArray(new Long[5]); |
535 |
|
shouldThrow(); |
536 |
|
} catch (ArrayStoreException success) {} |
540 |
|
* get throws an IndexOutOfBoundsException on a negative index |
541 |
|
*/ |
542 |
|
public void testGet1_IndexOutOfBoundsException() { |
543 |
< |
try { |
544 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
545 |
< |
c.get(-1); |
546 |
< |
shouldThrow(); |
547 |
< |
} catch (IndexOutOfBoundsException success) {} |
543 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
544 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
545 |
> |
for (List list : lists) { |
546 |
> |
try { |
547 |
> |
list.get(-1); |
548 |
> |
shouldThrow(); |
549 |
> |
} catch (IndexOutOfBoundsException success) {} |
550 |
> |
} |
551 |
|
} |
552 |
|
|
553 |
|
/** |
554 |
|
* get throws an IndexOutOfBoundsException on a too high index |
555 |
|
*/ |
556 |
|
public void testGet2_IndexOutOfBoundsException() { |
557 |
< |
try { |
558 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
559 |
< |
c.add("asdasd"); |
560 |
< |
c.add("asdad"); |
561 |
< |
c.get(100); |
562 |
< |
shouldThrow(); |
563 |
< |
} catch (IndexOutOfBoundsException success) {} |
557 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
558 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
559 |
> |
for (List list : lists) { |
560 |
> |
try { |
561 |
> |
list.get(list.size()); |
562 |
> |
shouldThrow(); |
563 |
> |
} catch (IndexOutOfBoundsException success) {} |
564 |
> |
} |
565 |
|
} |
566 |
|
|
567 |
|
/** |
568 |
|
* set throws an IndexOutOfBoundsException on a negative index |
569 |
|
*/ |
570 |
|
public void testSet1_IndexOutOfBoundsException() { |
571 |
< |
try { |
572 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
573 |
< |
c.set(-1,"qwerty"); |
574 |
< |
shouldThrow(); |
575 |
< |
} catch (IndexOutOfBoundsException success) {} |
571 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
572 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
573 |
> |
for (List list : lists) { |
574 |
> |
try { |
575 |
> |
list.set(-1, "qwerty"); |
576 |
> |
shouldThrow(); |
577 |
> |
} catch (IndexOutOfBoundsException success) {} |
578 |
> |
} |
579 |
|
} |
580 |
|
|
581 |
|
/** |
582 |
|
* set throws an IndexOutOfBoundsException on a too high index |
583 |
|
*/ |
584 |
|
public void testSet2() { |
585 |
< |
try { |
586 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
587 |
< |
c.add("asdasd"); |
588 |
< |
c.add("asdad"); |
589 |
< |
c.set(100, "qwerty"); |
590 |
< |
shouldThrow(); |
591 |
< |
} catch (IndexOutOfBoundsException success) {} |
585 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
586 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
587 |
> |
for (List list : lists) { |
588 |
> |
try { |
589 |
> |
list.set(list.size(), "qwerty"); |
590 |
> |
shouldThrow(); |
591 |
> |
} catch (IndexOutOfBoundsException success) {} |
592 |
> |
} |
593 |
|
} |
594 |
|
|
595 |
|
/** |
596 |
|
* add throws an IndexOutOfBoundsException on a negative index |
597 |
|
*/ |
598 |
|
public void testAdd1_IndexOutOfBoundsException() { |
599 |
< |
try { |
600 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
601 |
< |
c.add(-1,"qwerty"); |
602 |
< |
shouldThrow(); |
603 |
< |
} catch (IndexOutOfBoundsException success) {} |
599 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
600 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
601 |
> |
for (List list : lists) { |
602 |
> |
try { |
603 |
> |
list.add(-1, "qwerty"); |
604 |
> |
shouldThrow(); |
605 |
> |
} catch (IndexOutOfBoundsException success) {} |
606 |
> |
} |
607 |
|
} |
608 |
|
|
609 |
|
/** |
610 |
|
* add throws an IndexOutOfBoundsException on a too high index |
611 |
|
*/ |
612 |
|
public void testAdd2_IndexOutOfBoundsException() { |
613 |
< |
try { |
614 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
615 |
< |
c.add("asdasd"); |
616 |
< |
c.add("asdasdasd"); |
617 |
< |
c.add(100, "qwerty"); |
618 |
< |
shouldThrow(); |
619 |
< |
} catch (IndexOutOfBoundsException success) {} |
613 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
614 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
615 |
> |
for (List list : lists) { |
616 |
> |
try { |
617 |
> |
list.add(list.size() + 1, "qwerty"); |
618 |
> |
shouldThrow(); |
619 |
> |
} catch (IndexOutOfBoundsException success) {} |
620 |
> |
} |
621 |
|
} |
622 |
|
|
623 |
|
/** |
624 |
|
* remove throws an IndexOutOfBoundsException on a negative index |
625 |
|
*/ |
626 |
|
public void testRemove1_IndexOutOfBounds() { |
627 |
< |
try { |
628 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
629 |
< |
c.remove(-1); |
630 |
< |
shouldThrow(); |
631 |
< |
} catch (IndexOutOfBoundsException success) {} |
627 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
628 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
629 |
> |
for (List list : lists) { |
630 |
> |
try { |
631 |
> |
list.remove(-1); |
632 |
> |
shouldThrow(); |
633 |
> |
} catch (IndexOutOfBoundsException success) {} |
634 |
> |
} |
635 |
|
} |
636 |
|
|
637 |
|
/** |
638 |
|
* remove throws an IndexOutOfBoundsException on a too high index |
639 |
|
*/ |
640 |
|
public void testRemove2_IndexOutOfBounds() { |
641 |
< |
try { |
642 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
643 |
< |
c.add("asdasd"); |
644 |
< |
c.add("adasdasd"); |
645 |
< |
c.remove(100); |
646 |
< |
shouldThrow(); |
647 |
< |
} catch (IndexOutOfBoundsException success) {} |
641 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
642 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
643 |
> |
for (List list : lists) { |
644 |
> |
try { |
645 |
> |
list.remove(list.size()); |
646 |
> |
shouldThrow(); |
647 |
> |
} catch (IndexOutOfBoundsException success) {} |
648 |
> |
} |
649 |
|
} |
650 |
|
|
651 |
|
/** |
652 |
|
* addAll throws an IndexOutOfBoundsException on a negative index |
653 |
|
*/ |
654 |
|
public void testAddAll1_IndexOutOfBoundsException() { |
655 |
< |
try { |
656 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
657 |
< |
c.addAll(-1,new LinkedList()); |
658 |
< |
shouldThrow(); |
659 |
< |
} catch (IndexOutOfBoundsException success) {} |
655 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
656 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
657 |
> |
for (List list : lists) { |
658 |
> |
try { |
659 |
> |
list.addAll(-1, new LinkedList()); |
660 |
> |
shouldThrow(); |
661 |
> |
} catch (IndexOutOfBoundsException success) {} |
662 |
> |
} |
663 |
|
} |
664 |
|
|
665 |
|
/** |
666 |
|
* addAll throws an IndexOutOfBoundsException on a too high index |
667 |
|
*/ |
668 |
|
public void testAddAll2_IndexOutOfBoundsException() { |
669 |
< |
try { |
670 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
671 |
< |
c.add("asdasd"); |
672 |
< |
c.add("asdasdasd"); |
673 |
< |
c.addAll(100, new LinkedList()); |
674 |
< |
shouldThrow(); |
675 |
< |
} catch (IndexOutOfBoundsException success) {} |
669 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
670 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
671 |
> |
for (List list : lists) { |
672 |
> |
try { |
673 |
> |
list.addAll(list.size() + 1, new LinkedList()); |
674 |
> |
shouldThrow(); |
675 |
> |
} catch (IndexOutOfBoundsException success) {} |
676 |
> |
} |
677 |
|
} |
678 |
|
|
679 |
|
/** |
680 |
|
* listIterator throws an IndexOutOfBoundsException on a negative index |
681 |
|
*/ |
682 |
|
public void testListIterator1_IndexOutOfBoundsException() { |
683 |
< |
try { |
684 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
685 |
< |
c.listIterator(-1); |
686 |
< |
shouldThrow(); |
687 |
< |
} catch (IndexOutOfBoundsException success) {} |
683 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
684 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
685 |
> |
for (List list : lists) { |
686 |
> |
try { |
687 |
> |
list.listIterator(-1); |
688 |
> |
shouldThrow(); |
689 |
> |
} catch (IndexOutOfBoundsException success) {} |
690 |
> |
} |
691 |
|
} |
692 |
|
|
693 |
|
/** |
694 |
|
* listIterator throws an IndexOutOfBoundsException on a too high index |
695 |
|
*/ |
696 |
|
public void testListIterator2_IndexOutOfBoundsException() { |
697 |
< |
try { |
698 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
699 |
< |
c.add("adasd"); |
700 |
< |
c.add("asdasdas"); |
701 |
< |
c.listIterator(100); |
702 |
< |
shouldThrow(); |
703 |
< |
} catch (IndexOutOfBoundsException success) {} |
697 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
698 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
699 |
> |
for (List list : lists) { |
700 |
> |
try { |
701 |
> |
list.listIterator(list.size() + 1); |
702 |
> |
shouldThrow(); |
703 |
> |
} catch (IndexOutOfBoundsException success) {} |
704 |
> |
} |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
|
* subList throws an IndexOutOfBoundsException on a negative index |
709 |
|
*/ |
710 |
|
public void testSubList1_IndexOutOfBoundsException() { |
711 |
< |
try { |
712 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
713 |
< |
c.subList(-1,100); |
714 |
< |
shouldThrow(); |
715 |
< |
} catch (IndexOutOfBoundsException success) {} |
711 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
712 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
713 |
> |
for (List list : lists) { |
714 |
> |
try { |
715 |
> |
list.subList(-1, list.size()); |
716 |
> |
shouldThrow(); |
717 |
> |
} catch (IndexOutOfBoundsException success) {} |
718 |
> |
} |
719 |
|
} |
720 |
|
|
721 |
|
/** |
722 |
|
* subList throws an IndexOutOfBoundsException on a too high index |
723 |
|
*/ |
724 |
|
public void testSubList2_IndexOutOfBoundsException() { |
725 |
< |
try { |
726 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
727 |
< |
c.add("asdasd"); |
728 |
< |
c.subList(1,100); |
729 |
< |
shouldThrow(); |
730 |
< |
} catch (IndexOutOfBoundsException success) {} |
725 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
726 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
727 |
> |
for (List list : lists) { |
728 |
> |
try { |
729 |
> |
list.subList(0, list.size() + 1); |
730 |
> |
shouldThrow(); |
731 |
> |
} catch (IndexOutOfBoundsException success) {} |
732 |
> |
} |
733 |
|
} |
734 |
|
|
735 |
|
/** |
737 |
|
* is lower then the first |
738 |
|
*/ |
739 |
|
public void testSubList3_IndexOutOfBoundsException() { |
740 |
< |
try { |
741 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
742 |
< |
c.subList(3,1); |
743 |
< |
shouldThrow(); |
744 |
< |
} catch (IndexOutOfBoundsException success) {} |
740 |
> |
CopyOnWriteArrayList c = populatedArray(5); |
741 |
> |
List[] lists = { c, c.subList(1, c.size() - 1) }; |
742 |
> |
for (List list : lists) { |
743 |
> |
try { |
744 |
> |
list.subList(list.size() - 1, 1); |
745 |
> |
shouldThrow(); |
746 |
> |
} catch (IndexOutOfBoundsException success) {} |
747 |
> |
} |
748 |
|
} |
749 |
|
|
750 |
|
/** |
754 |
|
List x = populatedArray(SIZE); |
755 |
|
List y = serialClone(x); |
756 |
|
|
757 |
< |
assertTrue(x != y); |
757 |
> |
assertNotSame(x, y); |
758 |
|
assertEquals(x.size(), y.size()); |
759 |
|
assertEquals(x.toString(), y.toString()); |
760 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |