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.Collections; |
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 |
+ |
|
21 |
|
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
22 |
|
|
23 |
|
public static void main(String[] args) { |
24 |
< |
junit.textui.TestRunner.run(suite()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
|
27 |
|
public static Test suite() { |
28 |
< |
return new TestSuite(CopyOnWriteArrayListTest.class); |
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<Integer>(); |
47 |
> |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>(); |
48 |
|
assertTrue(a.isEmpty()); |
49 |
|
for (int i = 0; i < n; i++) |
50 |
|
a.add(i); |
54 |
|
} |
55 |
|
|
56 |
|
static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) { |
57 |
< |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
57 |
> |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>(); |
58 |
|
assertTrue(a.isEmpty()); |
59 |
< |
for (int i = 0; i < elements.length; i++) |
60 |
< |
a.add(elements[i]); |
59 |
> |
for (Integer element : elements) |
60 |
> |
a.add(element); |
61 |
|
assertFalse(a.isEmpty()); |
62 |
|
assertEquals(elements.length, a.size()); |
63 |
|
return a; |
76 |
|
*/ |
77 |
|
public void testConstructor2() { |
78 |
|
Integer[] ints = new Integer[SIZE]; |
79 |
< |
for (int i = 0; i < SIZE-1; ++i) |
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) |
88 |
|
*/ |
89 |
|
public void testConstructor3() { |
90 |
|
Integer[] ints = new Integer[SIZE]; |
91 |
< |
for (int i = 0; i < SIZE-1; ++i) |
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) |
96 |
|
} |
97 |
|
|
98 |
|
/** |
99 |
< |
* addAll adds each element from the given collection |
99 |
> |
* addAll adds each element from the given collection, including duplicates |
100 |
|
*/ |
101 |
|
public void testAddAll() { |
102 |
|
CopyOnWriteArrayList full = populatedArray(3); |
103 |
< |
Vector v = new Vector(); |
88 |
< |
v.add(three); |
89 |
< |
v.add(four); |
90 |
< |
v.add(five); |
91 |
< |
full.addAll(v); |
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 |
|
/** |
112 |
|
*/ |
113 |
|
public void testAddAllAbsent() { |
114 |
|
CopyOnWriteArrayList full = populatedArray(3); |
115 |
< |
Vector v = new Vector(); |
116 |
< |
v.add(three); |
117 |
< |
v.add(four); |
118 |
< |
v.add(one); // will not add this element |
105 |
< |
full.addAllAbsent(v); |
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 |
|
|
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 collection with subset of elements |
212 |
> |
* containsAll returns true for collections with subset of elements |
213 |
|
*/ |
214 |
|
public void testContainsAll() { |
215 |
|
CopyOnWriteArrayList full = populatedArray(3); |
216 |
< |
Vector v = new Vector(); |
217 |
< |
v.add(one); |
218 |
< |
v.add(two); |
219 |
< |
assertTrue(full.containsAll(v)); |
220 |
< |
v.add(six); |
221 |
< |
assertFalse(full.containsAll(v)); |
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 |
|
/** |
234 |
|
} |
235 |
|
|
236 |
|
/** |
237 |
< |
* indexOf gives the index for the given object |
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 full = populatedArray(3); |
243 |
< |
assertEquals(1, full.indexOf(one)); |
244 |
< |
assertEquals(-1, full.indexOf("puppies")); |
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 gives the index based on the given index |
265 |
< |
* at which to start searching |
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 full = populatedArray(3); |
270 |
< |
assertEquals(1, full.indexOf(one, 0)); |
271 |
< |
assertEquals(-1, full.indexOf(one, 2)); |
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 |
|
/** |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
< |
* iterator() returns an iterator containing the elements of the list |
306 |
> |
* iterator() returns an iterator containing the elements of the |
307 |
> |
* list in insertion order |
308 |
|
*/ |
309 |
|
public void testIterator() { |
310 |
< |
CopyOnWriteArrayList full = populatedArray(SIZE); |
311 |
< |
Iterator i = full.iterator(); |
312 |
< |
int j; |
313 |
< |
for (j = 0; i.hasNext(); j++) |
314 |
< |
assertEquals(j, i.next()); |
315 |
< |
assertEquals(SIZE, j); |
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 |
|
/** |
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) { |
359 |
> |
for (int i = 0; i < 3; ++i) |
360 |
|
assertTrue(s.contains(String.valueOf(i))); |
361 |
< |
} |
361 |
> |
assertEquals(new ArrayList(full).toString(), |
362 |
> |
full.toString()); |
363 |
|
} |
364 |
|
|
365 |
|
/** |
366 |
< |
* lastIndexOf returns the index for the given object |
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 full = populatedArray(3); |
372 |
< |
full.add(one); |
373 |
< |
full.add(three); |
374 |
< |
assertEquals(3, full.lastIndexOf(one)); |
375 |
< |
assertEquals(-1, full.lastIndexOf(six)); |
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 returns the index from the given starting point |
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 full = populatedArray(3); |
398 |
< |
full.add(one); |
399 |
< |
full.add(three); |
400 |
< |
assertEquals(3, full.lastIndexOf(one, 4)); |
401 |
< |
assertEquals(-1, full.lastIndexOf(three, 3)); |
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 |
|
/** |
440 |
|
ListIterator i = full.listIterator(1); |
441 |
|
int j; |
442 |
|
for (j = 0; i.hasNext(); j++) |
443 |
< |
assertEquals(j+1, i.next()); |
443 |
> |
assertEquals(j + 1, i.next()); |
444 |
|
assertEquals(2, j); |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
< |
* remove removes and returns the object at the given index |
448 |
> |
* remove(int) removes and returns the object at the given index |
449 |
|
*/ |
450 |
< |
public void testRemove() { |
451 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
452 |
< |
assertEquals(2, full.remove(2)); |
453 |
< |
assertEquals(2, full.size()); |
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 |
|
/** |
484 |
|
*/ |
485 |
|
public void testRemoveAll() { |
486 |
|
CopyOnWriteArrayList full = populatedArray(3); |
487 |
< |
Vector v = new Vector(); |
488 |
< |
v.add(one); |
489 |
< |
v.add(two); |
339 |
< |
full.removeAll(v); |
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 |
|
|
521 |
|
Integer[] elements = new Integer[SIZE]; |
522 |
|
for (int i = 0; i < SIZE; i++) |
523 |
|
elements[i] = i; |
524 |
< |
Collections.shuffle(Arrays.asList(elements)); |
524 |
> |
shuffle(elements); |
525 |
|
Collection<Integer> full = populatedArray(elements); |
526 |
|
|
527 |
|
assertTrue(Arrays.equals(elements, full.toArray())); |
539 |
|
a = new Integer[0]; |
540 |
|
assertSame(a, empty.toArray(a)); |
541 |
|
|
542 |
< |
a = new Integer[SIZE/2]; |
542 |
> |
a = new Integer[SIZE / 2]; |
543 |
|
Arrays.fill(a, 42); |
544 |
|
assertSame(a, empty.toArray(a)); |
545 |
|
assertNull(a[0]); |
549 |
|
Integer[] elements = new Integer[SIZE]; |
550 |
|
for (int i = 0; i < SIZE; i++) |
551 |
|
elements[i] = i; |
552 |
< |
Collections.shuffle(Arrays.asList(elements)); |
552 |
> |
shuffle(elements); |
553 |
|
Collection<Integer> full = populatedArray(elements); |
554 |
|
|
555 |
|
Arrays.fill(a, 42); |
563 |
|
assertSame(a, full.toArray(a)); |
564 |
|
assertTrue(Arrays.equals(elements, a)); |
565 |
|
|
566 |
< |
a = new Integer[2*SIZE]; |
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))); |
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 |
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 { |
456 |
– |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
457 |
– |
c.add("zfasdfsdf"); |
458 |
– |
c.add("asdadasd"); |
614 |
|
c.toArray(new Long[5]); |
615 |
|
shouldThrow(); |
616 |
|
} catch (ArrayStoreException success) {} |
620 |
|
* get throws an IndexOutOfBoundsException on a negative index |
621 |
|
*/ |
622 |
|
public void testGet1_IndexOutOfBoundsException() { |
623 |
< |
try { |
624 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
625 |
< |
c.get(-1); |
626 |
< |
shouldThrow(); |
627 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
638 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
639 |
< |
c.add("asdasd"); |
640 |
< |
c.add("asdad"); |
641 |
< |
c.get(100); |
642 |
< |
shouldThrow(); |
643 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
652 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
653 |
< |
c.set(-1,"qwerty"); |
654 |
< |
shouldThrow(); |
655 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
666 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
667 |
< |
c.add("asdasd"); |
668 |
< |
c.add("asdad"); |
669 |
< |
c.set(100, "qwerty"); |
670 |
< |
shouldThrow(); |
671 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
680 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
681 |
< |
c.add(-1,"qwerty"); |
682 |
< |
shouldThrow(); |
683 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
694 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
695 |
< |
c.add("asdasd"); |
696 |
< |
c.add("asdasdasd"); |
697 |
< |
c.add(100, "qwerty"); |
698 |
< |
shouldThrow(); |
699 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
708 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
709 |
< |
c.remove(-1); |
710 |
< |
shouldThrow(); |
711 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
722 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
723 |
< |
c.add("asdasd"); |
724 |
< |
c.add("adasdasd"); |
725 |
< |
c.remove(100); |
726 |
< |
shouldThrow(); |
727 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
736 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
737 |
< |
c.addAll(-1,new LinkedList()); |
738 |
< |
shouldThrow(); |
739 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
750 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
751 |
< |
c.add("asdasd"); |
752 |
< |
c.add("asdasdasd"); |
753 |
< |
c.addAll(100, new LinkedList()); |
754 |
< |
shouldThrow(); |
755 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
764 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
765 |
< |
c.listIterator(-1); |
766 |
< |
shouldThrow(); |
767 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
778 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
779 |
< |
c.add("adasd"); |
780 |
< |
c.add("asdasdas"); |
781 |
< |
c.listIterator(100); |
782 |
< |
shouldThrow(); |
783 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
792 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
793 |
< |
c.subList(-1,100); |
794 |
< |
shouldThrow(); |
795 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
< |
try { |
806 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
807 |
< |
c.add("asdasd"); |
808 |
< |
c.subList(1,100); |
809 |
< |
shouldThrow(); |
810 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 |
|
/** |
817 |
|
* is lower then the first |
818 |
|
*/ |
819 |
|
public void testSubList3_IndexOutOfBoundsException() { |
820 |
< |
try { |
821 |
< |
CopyOnWriteArrayList c = new CopyOnWriteArrayList(); |
822 |
< |
c.subList(3,1); |
823 |
< |
shouldThrow(); |
824 |
< |
} catch (IndexOutOfBoundsException success) {} |
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 serialized list is equal |
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 |
< |
assertTrue(x != y); |
837 |
> |
assertNotSame(x, y); |
838 |
|
assertEquals(x.size(), y.size()); |
839 |
|
assertEquals(x.toString(), y.toString()); |
840 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |