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; |
12 |
|
import java.util.Collections; |
13 |
|
import java.util.Iterator; |
14 |
|
import java.util.NoSuchElementException; |
15 |
|
import java.util.Set; |
16 |
– |
import java.util.Vector; |
16 |
|
import java.util.concurrent.CopyOnWriteArraySet; |
17 |
|
|
18 |
+ |
import junit.framework.Test; |
19 |
+ |
import junit.framework.TestSuite; |
20 |
+ |
|
21 |
|
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
22 |
|
public static void main(String[] args) { |
23 |
|
junit.textui.TestRunner.run(suite()); |
71 |
|
*/ |
72 |
|
public void testAddAll() { |
73 |
|
CopyOnWriteArraySet full = populatedSet(3); |
74 |
< |
Vector v = new Vector(); |
75 |
< |
v.add(three); |
76 |
< |
v.add(four); |
75 |
< |
v.add(five); |
76 |
< |
full.addAll(v); |
74 |
> |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
75 |
> |
assertEquals(6, full.size()); |
76 |
> |
assertFalse(full.addAll(Arrays.asList(three, four, five))); |
77 |
|
assertEquals(6, full.size()); |
78 |
|
} |
79 |
|
|
83 |
|
*/ |
84 |
|
public void testAddAll2() { |
85 |
|
CopyOnWriteArraySet full = populatedSet(3); |
86 |
< |
Vector v = new Vector(); |
87 |
< |
v.add(three); |
88 |
< |
v.add(four); |
89 |
< |
v.add(one); // will not add this element |
90 |
< |
full.addAll(v); |
86 |
> |
// "one" is duplicate and will not be added |
87 |
> |
assertTrue(full.addAll(Arrays.asList(three, four, one))); |
88 |
> |
assertEquals(5, full.size()); |
89 |
> |
assertFalse(full.addAll(Arrays.asList(three, four, one))); |
90 |
|
assertEquals(5, full.size()); |
91 |
|
} |
92 |
|
|
149 |
|
*/ |
150 |
|
public void testContainsAll() { |
151 |
|
CopyOnWriteArraySet full = populatedSet(3); |
152 |
< |
Vector v = new Vector(); |
153 |
< |
v.add(one); |
154 |
< |
v.add(two); |
155 |
< |
assertTrue(full.containsAll(v)); |
156 |
< |
v.add(six); |
158 |
< |
assertFalse(full.containsAll(v)); |
152 |
> |
assertTrue(full.containsAll(Arrays.asList())); |
153 |
> |
assertTrue(full.containsAll(Arrays.asList(one))); |
154 |
> |
assertTrue(full.containsAll(Arrays.asList(one, two))); |
155 |
> |
assertFalse(full.containsAll(Arrays.asList(one, two, six))); |
156 |
> |
assertFalse(full.containsAll(Arrays.asList(six))); |
157 |
|
} |
158 |
|
|
159 |
|
/** |
171 |
|
* set in insertion order |
172 |
|
*/ |
173 |
|
public void testIterator() { |
174 |
< |
Set empty = new CopyOnWriteArraySet(); |
174 |
> |
Collection empty = new CopyOnWriteArraySet(); |
175 |
|
assertFalse(empty.iterator().hasNext()); |
176 |
|
try { |
177 |
|
empty.iterator().next(); |
189 |
|
assertTrue(it.hasNext()); |
190 |
|
assertEquals(elements[j], it.next()); |
191 |
|
} |
192 |
< |
assertFalse(it.hasNext()); |
193 |
< |
try { |
194 |
< |
it.next(); |
195 |
< |
shouldThrow(); |
196 |
< |
} catch (NoSuchElementException success) {} |
192 |
> |
assertIteratorExhausted(it); |
193 |
> |
} |
194 |
> |
|
195 |
> |
/** |
196 |
> |
* iterator of empty collection has no elements |
197 |
> |
*/ |
198 |
> |
public void testEmptyIterator() { |
199 |
> |
assertIteratorExhausted(new CopyOnWriteArraySet().iterator()); |
200 |
|
} |
201 |
|
|
202 |
|
/** |
216 |
|
* toString holds toString of elements |
217 |
|
*/ |
218 |
|
public void testToString() { |
219 |
+ |
assertEquals("[]", new CopyOnWriteArraySet().toString()); |
220 |
|
CopyOnWriteArraySet full = populatedSet(3); |
221 |
|
String s = full.toString(); |
222 |
< |
for (int i = 0; i < 3; ++i) { |
222 |
> |
for (int i = 0; i < 3; ++i) |
223 |
|
assertTrue(s.contains(String.valueOf(i))); |
224 |
< |
} |
224 |
> |
assertEquals(new ArrayList(full).toString(), |
225 |
> |
full.toString()); |
226 |
|
} |
227 |
|
|
228 |
|
/** |
230 |
|
*/ |
231 |
|
public void testRemoveAll() { |
232 |
|
CopyOnWriteArraySet full = populatedSet(3); |
233 |
< |
Vector v = new Vector(); |
234 |
< |
v.add(one); |
235 |
< |
v.add(two); |
233 |
< |
full.removeAll(v); |
233 |
> |
assertTrue(full.removeAll(Arrays.asList(one, two))); |
234 |
> |
assertEquals(1, full.size()); |
235 |
> |
assertFalse(full.removeAll(Arrays.asList(one, two))); |
236 |
|
assertEquals(1, full.size()); |
237 |
|
} |
238 |
|
|
340 |
|
Set x = populatedSet(SIZE); |
341 |
|
Set y = serialClone(x); |
342 |
|
|
343 |
< |
assertTrue(x != y); |
343 |
> |
assertNotSame(y, x); |
344 |
|
assertEquals(x.size(), y.size()); |
345 |
|
assertEquals(x.toString(), y.toString()); |
346 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
347 |
|
assertEquals(x, y); |
348 |
|
assertEquals(y, x); |
349 |
|
} |
350 |
+ |
|
351 |
+ |
/** |
352 |
+ |
* addAll is idempotent |
353 |
+ |
*/ |
354 |
+ |
public void testAddAll_idempotent() throws Exception { |
355 |
+ |
Set x = populatedSet(SIZE); |
356 |
+ |
Set y = new CopyOnWriteArraySet(x); |
357 |
+ |
y.addAll(x); |
358 |
+ |
assertEquals(x, y); |
359 |
+ |
assertEquals(y, x); |
360 |
+ |
} |
361 |
|
|
362 |
|
} |