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; |
13 |
– |
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.NoSuchElementException; |
19 |
– |
import java.util.Vector; |
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 CopyOnWriteArrayList.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<Integer> populatedArray(int n) { |
48 |
< |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
48 |
> |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>(); |
49 |
|
assertTrue(a.isEmpty()); |
50 |
|
for (int i = 0; i < n; i++) |
51 |
|
a.add(i); |
55 |
|
} |
56 |
|
|
57 |
|
static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) { |
58 |
< |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>(); |
58 |
> |
CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>(); |
59 |
|
assertTrue(a.isEmpty()); |
60 |
|
for (int i = 0; i < elements.length; i++) |
61 |
|
a.add(elements[i]); |
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(); |
90 |
< |
v.add(three); |
91 |
< |
v.add(four); |
92 |
< |
v.add(five); |
93 |
< |
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 |
107 |
< |
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 |
|
/** |
278 |
|
Integer[] elements = new Integer[SIZE]; |
279 |
|
for (int i = 0; i < SIZE; i++) |
280 |
|
elements[i] = i; |
281 |
< |
Collections.shuffle(Arrays.asList(elements)); |
281 |
> |
shuffle(elements); |
282 |
|
Collection<Integer> full = populatedArray(elements); |
283 |
|
|
284 |
|
Iterator it = full.iterator(); |
286 |
|
assertTrue(it.hasNext()); |
287 |
|
assertEquals(elements[j], it.next()); |
288 |
|
} |
289 |
< |
assertFalse(it.hasNext()); |
290 |
< |
try { |
291 |
< |
it.next(); |
292 |
< |
shouldThrow(); |
293 |
< |
} catch (NoSuchElementException success) {} |
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 |
|
/** |
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 |
|
|
409 |
|
*/ |
410 |
|
public void testRemoveAll() { |
411 |
|
CopyOnWriteArrayList full = populatedArray(3); |
412 |
< |
Vector v = new Vector(); |
413 |
< |
v.add(one); |
414 |
< |
v.add(two); |
387 |
< |
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 |
|
|
446 |
|
Integer[] elements = new Integer[SIZE]; |
447 |
|
for (int i = 0; i < SIZE; i++) |
448 |
|
elements[i] = i; |
449 |
< |
Collections.shuffle(Arrays.asList(elements)); |
449 |
> |
shuffle(elements); |
450 |
|
Collection<Integer> full = populatedArray(elements); |
451 |
|
|
452 |
|
assertTrue(Arrays.equals(elements, full.toArray())); |
464 |
|
a = new Integer[0]; |
465 |
|
assertSame(a, empty.toArray(a)); |
466 |
|
|
467 |
< |
a = new Integer[SIZE/2]; |
467 |
> |
a = new Integer[SIZE / 2]; |
468 |
|
Arrays.fill(a, 42); |
469 |
|
assertSame(a, empty.toArray(a)); |
470 |
|
assertNull(a[0]); |
474 |
|
Integer[] elements = new Integer[SIZE]; |
475 |
|
for (int i = 0; i < SIZE; i++) |
476 |
|
elements[i] = i; |
477 |
< |
Collections.shuffle(Arrays.asList(elements)); |
477 |
> |
shuffle(elements); |
478 |
|
Collection<Integer> full = populatedArray(elements); |
479 |
|
|
480 |
|
Arrays.fill(a, 42); |
488 |
|
assertSame(a, full.toArray(a)); |
489 |
|
assertTrue(Arrays.equals(elements, a)); |
490 |
|
|
491 |
< |
a = new Integer[2*SIZE]; |
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))); |