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; |
17 |
|
import java.util.concurrent.CopyOnWriteArrayList; |
18 |
|
|
19 |
|
import junit.framework.Test; |
21 |
– |
import junit.framework.TestSuite; |
20 |
|
|
21 |
|
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
22 |
|
|
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]); |
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); |
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 |
|
/** |
277 |
|
Integer[] elements = new Integer[SIZE]; |
278 |
|
for (int i = 0; i < SIZE; i++) |
279 |
|
elements[i] = i; |
280 |
< |
Collections.shuffle(Arrays.asList(elements)); |
280 |
> |
shuffle(elements); |
281 |
|
Collection<Integer> full = populatedArray(elements); |
282 |
|
|
283 |
|
Iterator it = full.iterator(); |
364 |
|
ListIterator i = full.listIterator(1); |
365 |
|
int j; |
366 |
|
for (j = 0; i.hasNext(); j++) |
367 |
< |
assertEquals(j+1, i.next()); |
367 |
> |
assertEquals(j + 1, i.next()); |
368 |
|
assertEquals(2, j); |
369 |
|
} |
370 |
|
|
445 |
|
Integer[] elements = new Integer[SIZE]; |
446 |
|
for (int i = 0; i < SIZE; i++) |
447 |
|
elements[i] = i; |
448 |
< |
Collections.shuffle(Arrays.asList(elements)); |
448 |
> |
shuffle(elements); |
449 |
|
Collection<Integer> full = populatedArray(elements); |
450 |
|
|
451 |
|
assertTrue(Arrays.equals(elements, full.toArray())); |
473 |
|
Integer[] elements = new Integer[SIZE]; |
474 |
|
for (int i = 0; i < SIZE; i++) |
475 |
|
elements[i] = i; |
476 |
< |
Collections.shuffle(Arrays.asList(elements)); |
476 |
> |
shuffle(elements); |
477 |
|
Collection<Integer> full = populatedArray(elements); |
478 |
|
|
479 |
|
Arrays.fill(a, 42); |
747 |
|
} |
748 |
|
|
749 |
|
/** |
750 |
< |
* a deserialized serialized list is equal |
750 |
> |
* a deserialized/reserialized list equals original |
751 |
|
*/ |
752 |
|
public void testSerialization() throws Exception { |
753 |
|
List x = populatedArray(SIZE); |