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.Iterator; |
12 |
> |
import java.util.LinkedList; |
13 |
> |
import java.util.List; |
14 |
> |
import java.util.ListIterator; |
15 |
> |
import java.util.Vector; |
16 |
> |
import java.util.concurrent.CopyOnWriteArrayList; |
17 |
|
|
18 |
|
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
19 |
|
|
25 |
|
return new TestSuite(CopyOnWriteArrayListTest.class); |
26 |
|
} |
27 |
|
|
28 |
< |
static CopyOnWriteArrayList populatedArray(int n) { |
29 |
< |
CopyOnWriteArrayList a = new CopyOnWriteArrayList(); |
28 |
> |
static CopyOnWriteArrayList<Integer> populatedArray(int n) { |
29 |
> |
CopyOnWriteArrayList<Integer> a |
30 |
> |
= new CopyOnWriteArrayList<Integer>(); |
31 |
|
assertTrue(a.isEmpty()); |
32 |
|
for (int i = 0; i < n; ++i) |
33 |
< |
a.add(new Integer(i)); |
33 |
> |
a.add(i); |
34 |
|
assertFalse(a.isEmpty()); |
35 |
|
assertEquals(n, a.size()); |
36 |
|
return a; |
37 |
|
} |
38 |
|
|
34 |
– |
|
39 |
|
/** |
40 |
|
* a new list is empty |
41 |
|
*/ |
68 |
|
assertEquals(ints[i], a.get(i)); |
69 |
|
} |
70 |
|
|
67 |
– |
|
71 |
|
/** |
72 |
|
* addAll adds each element from the given collection |
73 |
|
*/ |
122 |
|
assertEquals(0, full.size()); |
123 |
|
} |
124 |
|
|
122 |
– |
|
125 |
|
/** |
126 |
|
* Cloned list is equal |
127 |
|
*/ |
176 |
|
assertEquals(a.hashCode(), b.hashCode()); |
177 |
|
} |
178 |
|
|
177 |
– |
|
179 |
|
/** |
180 |
|
* containsAll returns true for collection with subset of elements |
181 |
|
*/ |
365 |
|
* the list |
366 |
|
*/ |
367 |
|
public void testToArray2() { |
368 |
< |
CopyOnWriteArrayList full = populatedArray(3); |
369 |
< |
Integer[] i = new Integer[3]; |
370 |
< |
i = (Integer[])full.toArray(i); |
371 |
< |
assertEquals(3, i.length); |
372 |
< |
assertEquals(0, i[0].intValue()); |
373 |
< |
assertEquals(1, i[1].intValue()); |
374 |
< |
assertEquals(2, i[2].intValue()); |
368 |
> |
final int size = 3; |
369 |
> |
CopyOnWriteArrayList<Integer> full = populatedArray(size); |
370 |
> |
Integer[] ints = new Integer[size]; |
371 |
> |
assertSame(ints, full.toArray(ints)); |
372 |
> |
Iterator<Integer> it = full.iterator(); |
373 |
> |
for (int i = 0; i < size; i++) |
374 |
> |
assertSame(ints[i], it.next()); |
375 |
> |
assertFalse(it.hasNext()); |
376 |
|
} |
377 |
|
|
376 |
– |
|
378 |
|
/** |
379 |
|
* sublists contains elements at indexes offset from their base |
380 |
|
*/ |
391 |
|
} |
392 |
|
|
393 |
|
List s = a.subList(2, 5); |
394 |
< |
assertEquals(s.size(), 3); |
394 |
> |
assertEquals(3, s.size()); |
395 |
|
s.set(2, m1); |
396 |
|
assertEquals(a.get(4), m1); |
397 |
|
s.clear(); |
398 |
< |
assertEquals(a.size(), 7); |
398 |
> |
assertEquals(7, a.size()); |
399 |
|
} |
400 |
|
|
401 |
|
// Exception tests |
597 |
|
* a deserialized serialized list is equal |
598 |
|
*/ |
599 |
|
public void testSerialization() throws Exception { |
600 |
< |
CopyOnWriteArrayList q = populatedArray(SIZE); |
600 |
> |
List x = populatedArray(SIZE); |
601 |
> |
List y = serialClone(x); |
602 |
|
|
603 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
604 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
605 |
< |
out.writeObject(q); |
606 |
< |
out.close(); |
607 |
< |
|
608 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
609 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
610 |
< |
CopyOnWriteArrayList r = (CopyOnWriteArrayList)in.readObject(); |
611 |
< |
assertEquals(q.size(), r.size()); |
612 |
< |
assertTrue(q.equals(r)); |
613 |
< |
assertTrue(r.equals(q)); |
603 |
> |
assertTrue(x != y); |
604 |
> |
assertEquals(x.size(), y.size()); |
605 |
> |
assertEquals(x.toString(), y.toString()); |
606 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
607 |
> |
assertEquals(x, y); |
608 |
> |
assertEquals(y, x); |
609 |
> |
while (!x.isEmpty()) { |
610 |
> |
assertFalse(y.isEmpty()); |
611 |
> |
assertEquals(x.remove(0), y.remove(0)); |
612 |
> |
} |
613 |
> |
assertTrue(y.isEmpty()); |
614 |
|
} |
615 |
|
|
616 |
|
} |