15 |
|
import java.util.List; |
16 |
|
import java.util.ListIterator; |
17 |
|
import java.util.NoSuchElementException; |
18 |
– |
import java.util.Vector; |
18 |
|
import java.util.concurrent.CopyOnWriteArrayList; |
19 |
|
|
20 |
|
import junit.framework.Test; |
23 |
|
public class CopyOnWriteArrayListTest extends JSR166TestCase { |
24 |
|
|
25 |
|
public static void main(String[] args) { |
26 |
< |
junit.textui.TestRunner.run(suite()); |
26 |
> |
main(suite(), args); |
27 |
|
} |
28 |
|
|
29 |
|
public static Test suite() { |
63 |
|
*/ |
64 |
|
public void testConstructor2() { |
65 |
|
Integer[] ints = new Integer[SIZE]; |
66 |
< |
for (int i = 0; i < SIZE-1; ++i) |
66 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
67 |
|
ints[i] = new Integer(i); |
68 |
|
CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints); |
69 |
|
for (int i = 0; i < SIZE; ++i) |
75 |
|
*/ |
76 |
|
public void testConstructor3() { |
77 |
|
Integer[] ints = new Integer[SIZE]; |
78 |
< |
for (int i = 0; i < SIZE-1; ++i) |
78 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
79 |
|
ints[i] = new Integer(i); |
80 |
|
CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints)); |
81 |
|
for (int i = 0; i < SIZE; ++i) |
83 |
|
} |
84 |
|
|
85 |
|
/** |
86 |
< |
* addAll adds each element from the given collection |
86 |
> |
* addAll adds each element from the given collection, including duplicates |
87 |
|
*/ |
88 |
|
public void testAddAll() { |
89 |
|
CopyOnWriteArrayList full = populatedArray(3); |
90 |
< |
Vector v = new Vector(); |
92 |
< |
v.add(three); |
93 |
< |
v.add(four); |
94 |
< |
v.add(five); |
95 |
< |
full.addAll(v); |
90 |
> |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
91 |
|
assertEquals(6, full.size()); |
92 |
+ |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
93 |
+ |
assertEquals(9, full.size()); |
94 |
|
} |
95 |
|
|
96 |
|
/** |
99 |
|
*/ |
100 |
|
public void testAddAllAbsent() { |
101 |
|
CopyOnWriteArrayList full = populatedArray(3); |
102 |
< |
Vector v = new Vector(); |
103 |
< |
v.add(three); |
104 |
< |
v.add(four); |
105 |
< |
v.add(one); // will not add this element |
109 |
< |
full.addAllAbsent(v); |
102 |
> |
// "one" is duplicate and will not be added |
103 |
> |
assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one))); |
104 |
> |
assertEquals(5, full.size()); |
105 |
> |
assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one))); |
106 |
|
assertEquals(5, full.size()); |
107 |
|
} |
108 |
|
|
177 |
|
CopyOnWriteArrayList b = populatedArray(3); |
178 |
|
assertTrue(a.equals(b)); |
179 |
|
assertTrue(b.equals(a)); |
180 |
+ |
assertTrue(a.containsAll(b)); |
181 |
+ |
assertTrue(b.containsAll(a)); |
182 |
|
assertEquals(a.hashCode(), b.hashCode()); |
183 |
|
a.add(m1); |
184 |
|
assertFalse(a.equals(b)); |
185 |
|
assertFalse(b.equals(a)); |
186 |
+ |
assertTrue(a.containsAll(b)); |
187 |
+ |
assertFalse(b.containsAll(a)); |
188 |
|
b.add(m1); |
189 |
|
assertTrue(a.equals(b)); |
190 |
|
assertTrue(b.equals(a)); |
191 |
+ |
assertTrue(a.containsAll(b)); |
192 |
+ |
assertTrue(b.containsAll(a)); |
193 |
|
assertEquals(a.hashCode(), b.hashCode()); |
194 |
+ |
|
195 |
+ |
assertFalse(a.equals(null)); |
196 |
|
} |
197 |
|
|
198 |
|
/** |
199 |
< |
* containsAll returns true for collection with subset of elements |
199 |
> |
* containsAll returns true for collections with subset of elements |
200 |
|
*/ |
201 |
|
public void testContainsAll() { |
202 |
|
CopyOnWriteArrayList full = populatedArray(3); |
203 |
< |
Vector v = new Vector(); |
204 |
< |
v.add(one); |
205 |
< |
v.add(two); |
206 |
< |
assertTrue(full.containsAll(v)); |
207 |
< |
v.add(six); |
208 |
< |
assertFalse(full.containsAll(v)); |
203 |
> |
assertTrue(full.containsAll(Arrays.asList())); |
204 |
> |
assertTrue(full.containsAll(Arrays.asList(one))); |
205 |
> |
assertTrue(full.containsAll(Arrays.asList(one, two))); |
206 |
> |
assertFalse(full.containsAll(Arrays.asList(one, two, six))); |
207 |
> |
assertFalse(full.containsAll(Arrays.asList(six))); |
208 |
> |
|
209 |
> |
try { |
210 |
> |
full.containsAll(null); |
211 |
> |
shouldThrow(); |
212 |
> |
} catch (NullPointerException success) {} |
213 |
|
} |
214 |
|
|
215 |
|
/** |
264 |
|
Integer[] elements = new Integer[SIZE]; |
265 |
|
for (int i = 0; i < SIZE; i++) |
266 |
|
elements[i] = i; |
267 |
< |
Collections.shuffle(Arrays.asList(elements)); |
267 |
> |
shuffle(elements); |
268 |
|
Collection<Integer> full = populatedArray(elements); |
269 |
|
|
270 |
|
Iterator it = full.iterator(); |
272 |
|
assertTrue(it.hasNext()); |
273 |
|
assertEquals(elements[j], it.next()); |
274 |
|
} |
275 |
< |
assertFalse(it.hasNext()); |
276 |
< |
try { |
277 |
< |
it.next(); |
278 |
< |
shouldThrow(); |
279 |
< |
} catch (NoSuchElementException success) {} |
275 |
> |
assertIteratorExhausted(it); |
276 |
> |
} |
277 |
> |
|
278 |
> |
/** |
279 |
> |
* iterator of empty collection has no elements |
280 |
> |
*/ |
281 |
> |
public void testEmptyIterator() { |
282 |
> |
Collection c = new CopyOnWriteArrayList(); |
283 |
> |
assertIteratorExhausted(c.iterator()); |
284 |
|
} |
285 |
|
|
286 |
|
/** |
351 |
|
ListIterator i = full.listIterator(1); |
352 |
|
int j; |
353 |
|
for (j = 0; i.hasNext(); j++) |
354 |
< |
assertEquals(j+1, i.next()); |
354 |
> |
assertEquals(j + 1, i.next()); |
355 |
|
assertEquals(2, j); |
356 |
|
} |
357 |
|
|
395 |
|
*/ |
396 |
|
public void testRemoveAll() { |
397 |
|
CopyOnWriteArrayList full = populatedArray(3); |
398 |
< |
Vector v = new Vector(); |
399 |
< |
v.add(one); |
400 |
< |
v.add(two); |
389 |
< |
full.removeAll(v); |
398 |
> |
assertTrue(full.removeAll(Arrays.asList(one, two))); |
399 |
> |
assertEquals(1, full.size()); |
400 |
> |
assertFalse(full.removeAll(Arrays.asList(one, two))); |
401 |
|
assertEquals(1, full.size()); |
402 |
|
} |
403 |
|
|
432 |
|
Integer[] elements = new Integer[SIZE]; |
433 |
|
for (int i = 0; i < SIZE; i++) |
434 |
|
elements[i] = i; |
435 |
< |
Collections.shuffle(Arrays.asList(elements)); |
435 |
> |
shuffle(elements); |
436 |
|
Collection<Integer> full = populatedArray(elements); |
437 |
|
|
438 |
|
assertTrue(Arrays.equals(elements, full.toArray())); |
450 |
|
a = new Integer[0]; |
451 |
|
assertSame(a, empty.toArray(a)); |
452 |
|
|
453 |
< |
a = new Integer[SIZE/2]; |
453 |
> |
a = new Integer[SIZE / 2]; |
454 |
|
Arrays.fill(a, 42); |
455 |
|
assertSame(a, empty.toArray(a)); |
456 |
|
assertNull(a[0]); |
460 |
|
Integer[] elements = new Integer[SIZE]; |
461 |
|
for (int i = 0; i < SIZE; i++) |
462 |
|
elements[i] = i; |
463 |
< |
Collections.shuffle(Arrays.asList(elements)); |
463 |
> |
shuffle(elements); |
464 |
|
Collection<Integer> full = populatedArray(elements); |
465 |
|
|
466 |
|
Arrays.fill(a, 42); |
474 |
|
assertSame(a, full.toArray(a)); |
475 |
|
assertTrue(Arrays.equals(elements, a)); |
476 |
|
|
477 |
< |
a = new Integer[2*SIZE]; |
477 |
> |
a = new Integer[2 * SIZE]; |
478 |
|
Arrays.fill(a, 42); |
479 |
|
assertSame(a, full.toArray(a)); |
480 |
|
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |