7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
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.Vector; |
18 |
> |
import java.util.concurrent.CopyOnWriteArrayList; |
19 |
|
|
20 |
|
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
21 |
|
|
27 |
|
return new TestSuite(CopyOnWriteArrayListTest.class); |
28 |
|
} |
29 |
|
|
30 |
< |
static CopyOnWriteArrayList populatedArray(int n) { |
31 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
30 |
> |
static CopyOnWriteArrayList<Integer> populatedArray(int n) { |
31 |
> |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
32 |
|
assertTrue(a.isEmpty()); |
33 |
< |
for (int i = 0; i < n; ++i) |
34 |
< |
a.add(new Integer(i)); |
33 |
> |
for (int i = 0; i < n; i++) |
34 |
> |
a.add(i); |
35 |
|
assertFalse(a.isEmpty()); |
36 |
|
assertEquals(n, a.size()); |
37 |
|
return a; |
38 |
|
} |
39 |
|
|
40 |
+ |
static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) { |
41 |
+ |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
42 |
+ |
assertTrue(a.isEmpty()); |
43 |
+ |
for (int i = 0; i < elements.length; i++) |
44 |
+ |
a.add(elements[i]); |
45 |
+ |
assertFalse(a.isEmpty()); |
46 |
+ |
assertEquals(elements.length, a.size()); |
47 |
+ |
return a; |
48 |
+ |
} |
49 |
|
|
50 |
|
/** |
51 |
|
* a new list is empty |
79 |
|
assertEquals(ints[i], a.get(i)); |
80 |
|
} |
81 |
|
|
67 |
– |
|
82 |
|
/** |
83 |
|
* addAll adds each element from the given collection |
84 |
|
*/ |
133 |
|
assertEquals(0, full.size()); |
134 |
|
} |
135 |
|
|
122 |
– |
|
136 |
|
/** |
137 |
|
* Cloned list is equal |
138 |
|
*/ |
187 |
|
assertEquals(a.hashCode(), b.hashCode()); |
188 |
|
} |
189 |
|
|
177 |
– |
|
190 |
|
/** |
191 |
|
* containsAll returns true for collection with subset of elements |
192 |
|
*/ |
269 |
|
CopyOnWriteArrayList full = populatedArray(3); |
270 |
|
String s = full.toString(); |
271 |
|
for (int i = 0; i < 3; ++i) { |
272 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
272 |
> |
assertTrue(s.contains(String.valueOf(i))); |
273 |
|
} |
274 |
|
} |
275 |
|
|
287 |
|
/** |
288 |
|
* lastIndexOf returns the index from the given starting point |
289 |
|
*/ |
290 |
< |
public void testlastIndexOf2() { |
290 |
> |
public void testLastIndexOf2() { |
291 |
|
CopyOnWriteArrayList full = populatedArray(3); |
292 |
|
full.add(one); |
293 |
|
full.add(three); |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
< |
* toArray returns an Object array containing all elements from the list |
363 |
> |
* toArray() returns an Object array containing all elements from |
364 |
> |
* the list in insertion order |
365 |
|
*/ |
366 |
|
public void testToArray() { |
367 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
368 |
< |
Object[] o = full.toArray(); |
369 |
< |
assertEquals(3, o.length); |
370 |
< |
assertEquals(0, o[0]); |
371 |
< |
assertEquals(1, o[1]); |
372 |
< |
assertEquals(2, o[2]); |
367 |
> |
Object[] a = new CopyOnWriteArrayList().toArray(); |
368 |
> |
assertTrue(Arrays.equals(new Object[0], a)); |
369 |
> |
assertSame(Object[].class, a.getClass()); |
370 |
> |
|
371 |
> |
Integer[] elements = new Integer[SIZE]; |
372 |
> |
for (int i = 0; i < SIZE; i++) |
373 |
> |
elements[i] = i; |
374 |
> |
Collections.shuffle(Arrays.asList(elements)); |
375 |
> |
Collection<Integer> full = populatedArray(elements); |
376 |
> |
|
377 |
> |
assertTrue(Arrays.equals(elements, full.toArray())); |
378 |
> |
assertSame(Object[].class, full.toArray().getClass()); |
379 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
< |
* toArray returns an Integer array containing all elements from |
383 |
< |
* the list |
382 |
> |
* toArray(Integer array) returns an Integer array containing all |
383 |
> |
* elements from the list in insertion order |
384 |
|
*/ |
385 |
|
public void testToArray2() { |
386 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
387 |
< |
Integer[] i = new Integer[3]; |
388 |
< |
i = (Integer[])full.toArray(i); |
389 |
< |
assertEquals(3, i.length); |
390 |
< |
assertEquals(0, i[0].intValue()); |
372 |
< |
assertEquals(1, i[1].intValue()); |
373 |
< |
assertEquals(2, i[2].intValue()); |
374 |
< |
} |
386 |
> |
Collection empty = new CopyOnWriteArrayList(); |
387 |
> |
Integer[] a; |
388 |
> |
|
389 |
> |
a = new Integer[0]; |
390 |
> |
assertSame(a, empty.toArray(a)); |
391 |
|
|
392 |
+ |
a = new Integer[SIZE/2]; |
393 |
+ |
Arrays.fill(a, 42); |
394 |
+ |
assertSame(a, empty.toArray(a)); |
395 |
+ |
assertNull(a[0]); |
396 |
+ |
for (int i = 1; i < a.length; i++) |
397 |
+ |
assertEquals(42, (int) a[i]); |
398 |
+ |
|
399 |
+ |
Integer[] elements = new Integer[SIZE]; |
400 |
+ |
for (int i = 0; i < SIZE; i++) |
401 |
+ |
elements[i] = i; |
402 |
+ |
Collections.shuffle(Arrays.asList(elements)); |
403 |
+ |
Collection<Integer> full = populatedArray(elements); |
404 |
+ |
|
405 |
+ |
Arrays.fill(a, 42); |
406 |
+ |
assertTrue(Arrays.equals(elements, full.toArray(a))); |
407 |
+ |
for (int i = 0; i < a.length; i++) |
408 |
+ |
assertEquals(42, (int) a[i]); |
409 |
+ |
assertSame(Integer[].class, full.toArray(a).getClass()); |
410 |
+ |
|
411 |
+ |
a = new Integer[SIZE]; |
412 |
+ |
Arrays.fill(a, 42); |
413 |
+ |
assertSame(a, full.toArray(a)); |
414 |
+ |
assertTrue(Arrays.equals(elements, a)); |
415 |
+ |
|
416 |
+ |
a = new Integer[2*SIZE]; |
417 |
+ |
Arrays.fill(a, 42); |
418 |
+ |
assertSame(a, full.toArray(a)); |
419 |
+ |
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
420 |
+ |
assertNull(a[SIZE]); |
421 |
+ |
for (int i = SIZE + 1; i < a.length; i++) |
422 |
+ |
assertEquals(42, (int) a[i]); |
423 |
+ |
} |
424 |
|
|
425 |
|
/** |
426 |
|
* sublists contains elements at indexes offset from their base |
438 |
|
} |
439 |
|
|
440 |
|
List s = a.subList(2, 5); |
441 |
< |
assertEquals(s.size(), 3); |
441 |
> |
assertEquals(3, s.size()); |
442 |
|
s.set(2, m1); |
443 |
|
assertEquals(a.get(4), m1); |
444 |
|
s.clear(); |
445 |
< |
assertEquals(a.size(), 7); |
445 |
> |
assertEquals(7, a.size()); |
446 |
|
} |
447 |
|
|
448 |
|
// Exception tests |
644 |
|
* a deserialized serialized list is equal |
645 |
|
*/ |
646 |
|
public void testSerialization() throws Exception { |
647 |
< |
CopyOnWriteArrayList q = populatedArray(SIZE); |
647 |
> |
List x = populatedArray(SIZE); |
648 |
> |
List y = serialClone(x); |
649 |
|
|
650 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
651 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
652 |
< |
out.writeObject(q); |
653 |
< |
out.close(); |
654 |
< |
|
655 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
656 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
657 |
< |
CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject(); |
658 |
< |
assertEquals(q.size(), r.size()); |
659 |
< |
assertTrue(q.equals(r)); |
660 |
< |
assertTrue(r.equals(q)); |
650 |
> |
assertTrue(x != y); |
651 |
> |
assertEquals(x.size(), y.size()); |
652 |
> |
assertEquals(x.toString(), y.toString()); |
653 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
654 |
> |
assertEquals(x, y); |
655 |
> |
assertEquals(y, x); |
656 |
> |
while (!x.isEmpty()) { |
657 |
> |
assertFalse(y.isEmpty()); |
658 |
> |
assertEquals(x.remove(0), y.remove(0)); |
659 |
> |
} |
660 |
> |
assertTrue(y.isEmpty()); |
661 |
|
} |
662 |
|
|
663 |
|
} |