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.NoSuchElementException; |
14 |
|
import java.util.Set; |
16 |
– |
import java.util.Vector; |
15 |
|
import java.util.concurrent.CopyOnWriteArraySet; |
16 |
|
|
17 |
|
import junit.framework.Test; |
19 |
|
|
20 |
|
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
21 |
|
public static void main(String[] args) { |
22 |
< |
junit.textui.TestRunner.run(suite()); |
22 |
> |
main(suite(), args); |
23 |
|
} |
24 |
|
public static Test suite() { |
25 |
< |
return new TestSuite(CopyOnWriteArraySetTest.class); |
25 |
> |
class Implementation implements CollectionImplementation { |
26 |
> |
public Class<?> klazz() { return CopyOnWriteArraySet.class; } |
27 |
> |
public Set emptyCollection() { return new CopyOnWriteArraySet(); } |
28 |
> |
public Object makeElement(int i) { return i; } |
29 |
> |
public boolean isConcurrent() { return true; } |
30 |
> |
public boolean permitsNulls() { return true; } |
31 |
> |
} |
32 |
> |
return newTestSuite( |
33 |
> |
CopyOnWriteArraySetTest.class, |
34 |
> |
CollectionTest.testSuite(new Implementation())); |
35 |
|
} |
36 |
|
|
37 |
|
static CopyOnWriteArraySet<Integer> populatedSet(int n) { |
38 |
< |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); |
38 |
> |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<>(); |
39 |
|
assertTrue(a.isEmpty()); |
40 |
|
for (int i = 0; i < n; i++) |
41 |
|
a.add(i); |
42 |
< |
assertFalse(a.isEmpty()); |
42 |
> |
assertEquals(n == 0, a.isEmpty()); |
43 |
|
assertEquals(n, a.size()); |
44 |
|
return a; |
45 |
|
} |
46 |
|
|
47 |
|
static CopyOnWriteArraySet populatedSet(Integer[] elements) { |
48 |
< |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); |
48 |
> |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<>(); |
49 |
|
assertTrue(a.isEmpty()); |
50 |
|
for (int i = 0; i < elements.length; i++) |
51 |
|
a.add(elements[i]); |
67 |
|
*/ |
68 |
|
public void testConstructor3() { |
69 |
|
Integer[] ints = new Integer[SIZE]; |
70 |
< |
for (int i = 0; i < SIZE-1; ++i) |
70 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
71 |
|
ints[i] = new Integer(i); |
72 |
|
CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints)); |
73 |
|
for (int i = 0; i < SIZE; ++i) |
75 |
|
} |
76 |
|
|
77 |
|
/** |
78 |
< |
* addAll adds each element from the given collection |
78 |
> |
* addAll adds each non-duplicate element from the given collection |
79 |
|
*/ |
80 |
|
public void testAddAll() { |
81 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
82 |
< |
Vector v = new Vector(); |
83 |
< |
v.add(three); |
84 |
< |
v.add(four); |
78 |
< |
v.add(five); |
79 |
< |
full.addAll(v); |
81 |
> |
Set full = populatedSet(3); |
82 |
> |
assertTrue(full.addAll(Arrays.asList(three, four, five))); |
83 |
> |
assertEquals(6, full.size()); |
84 |
> |
assertFalse(full.addAll(Arrays.asList(three, four, five))); |
85 |
|
assertEquals(6, full.size()); |
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
< |
* addAll adds each element from the given collection that did not |
85 |
< |
* already exist in the set |
89 |
> |
* addAll adds each non-duplicate element from the given collection |
90 |
|
*/ |
91 |
|
public void testAddAll2() { |
92 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
93 |
< |
Vector v = new Vector(); |
94 |
< |
v.add(three); |
95 |
< |
v.add(four); |
96 |
< |
v.add(one); // will not add this element |
93 |
< |
full.addAll(v); |
92 |
> |
Set full = populatedSet(3); |
93 |
> |
// "one" is duplicate and will not be added |
94 |
> |
assertTrue(full.addAll(Arrays.asList(three, four, one))); |
95 |
> |
assertEquals(5, full.size()); |
96 |
> |
assertFalse(full.addAll(Arrays.asList(three, four, one))); |
97 |
|
assertEquals(5, full.size()); |
98 |
|
} |
99 |
|
|
101 |
|
* add will not add the element if it already exists in the set |
102 |
|
*/ |
103 |
|
public void testAdd2() { |
104 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
104 |
> |
Set full = populatedSet(3); |
105 |
|
full.add(one); |
106 |
|
assertEquals(3, full.size()); |
107 |
|
} |
110 |
|
* add adds the element when it does not exist in the set |
111 |
|
*/ |
112 |
|
public void testAdd3() { |
113 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
113 |
> |
Set full = populatedSet(3); |
114 |
|
full.add(three); |
115 |
|
assertTrue(full.contains(three)); |
116 |
|
} |
119 |
|
* clear removes all elements from the set |
120 |
|
*/ |
121 |
|
public void testClear() { |
122 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
122 |
> |
Collection full = populatedSet(3); |
123 |
|
full.clear(); |
124 |
|
assertEquals(0, full.size()); |
125 |
+ |
assertTrue(full.isEmpty()); |
126 |
|
} |
127 |
|
|
128 |
|
/** |
129 |
|
* contains returns true for added elements |
130 |
|
*/ |
131 |
|
public void testContains() { |
132 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
132 |
> |
Collection full = populatedSet(3); |
133 |
|
assertTrue(full.contains(one)); |
134 |
|
assertFalse(full.contains(five)); |
135 |
|
} |
142 |
|
CopyOnWriteArraySet b = populatedSet(3); |
143 |
|
assertTrue(a.equals(b)); |
144 |
|
assertTrue(b.equals(a)); |
145 |
+ |
assertTrue(a.containsAll(b)); |
146 |
+ |
assertTrue(b.containsAll(a)); |
147 |
|
assertEquals(a.hashCode(), b.hashCode()); |
148 |
+ |
assertEquals(a.size(), b.size()); |
149 |
+ |
|
150 |
|
a.add(m1); |
151 |
|
assertFalse(a.equals(b)); |
152 |
|
assertFalse(b.equals(a)); |
153 |
+ |
assertTrue(a.containsAll(b)); |
154 |
+ |
assertFalse(b.containsAll(a)); |
155 |
|
b.add(m1); |
156 |
|
assertTrue(a.equals(b)); |
157 |
|
assertTrue(b.equals(a)); |
158 |
+ |
assertTrue(a.containsAll(b)); |
159 |
+ |
assertTrue(b.containsAll(a)); |
160 |
|
assertEquals(a.hashCode(), b.hashCode()); |
161 |
+ |
|
162 |
+ |
Object x = a.iterator().next(); |
163 |
+ |
a.remove(x); |
164 |
+ |
assertFalse(a.equals(b)); |
165 |
+ |
assertFalse(b.equals(a)); |
166 |
+ |
assertFalse(a.containsAll(b)); |
167 |
+ |
assertTrue(b.containsAll(a)); |
168 |
+ |
a.add(x); |
169 |
+ |
assertTrue(a.equals(b)); |
170 |
+ |
assertTrue(b.equals(a)); |
171 |
+ |
assertTrue(a.containsAll(b)); |
172 |
+ |
assertTrue(b.containsAll(a)); |
173 |
+ |
assertEquals(a.hashCode(), b.hashCode()); |
174 |
+ |
assertEquals(a.size(), b.size()); |
175 |
+ |
|
176 |
+ |
CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList()); |
177 |
+ |
CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList()); |
178 |
+ |
assertTrue(empty1.equals(empty1)); |
179 |
+ |
assertTrue(empty1.equals(empty2)); |
180 |
+ |
|
181 |
+ |
assertFalse(empty1.equals(a)); |
182 |
+ |
assertFalse(a.equals(empty1)); |
183 |
+ |
|
184 |
+ |
assertFalse(a.equals(null)); |
185 |
|
} |
186 |
|
|
187 |
|
/** |
188 |
|
* containsAll returns true for collections with subset of elements |
189 |
|
*/ |
190 |
|
public void testContainsAll() { |
191 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
192 |
< |
Vector v = new Vector(); |
193 |
< |
v.add(one); |
194 |
< |
v.add(two); |
195 |
< |
assertTrue(full.containsAll(v)); |
196 |
< |
v.add(six); |
197 |
< |
assertFalse(full.containsAll(v)); |
191 |
> |
Collection full = populatedSet(3); |
192 |
> |
assertTrue(full.containsAll(full)); |
193 |
> |
assertTrue(full.containsAll(Arrays.asList())); |
194 |
> |
assertTrue(full.containsAll(Arrays.asList(one))); |
195 |
> |
assertTrue(full.containsAll(Arrays.asList(one, two))); |
196 |
> |
assertFalse(full.containsAll(Arrays.asList(one, two, six))); |
197 |
> |
assertFalse(full.containsAll(Arrays.asList(six))); |
198 |
> |
|
199 |
> |
CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList()); |
200 |
> |
CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList()); |
201 |
> |
assertTrue(empty1.containsAll(empty2)); |
202 |
> |
assertTrue(empty1.containsAll(empty1)); |
203 |
> |
assertFalse(empty1.containsAll(full)); |
204 |
> |
assertTrue(full.containsAll(empty1)); |
205 |
> |
|
206 |
> |
try { |
207 |
> |
full.containsAll(null); |
208 |
> |
shouldThrow(); |
209 |
> |
} catch (NullPointerException success) {} |
210 |
|
} |
211 |
|
|
212 |
|
/** |
213 |
|
* isEmpty is true when empty, else false |
214 |
|
*/ |
215 |
|
public void testIsEmpty() { |
216 |
< |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
217 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
170 |
< |
assertTrue(empty.isEmpty()); |
171 |
< |
assertFalse(full.isEmpty()); |
216 |
> |
assertTrue(populatedSet(0).isEmpty()); |
217 |
> |
assertFalse(populatedSet(3).isEmpty()); |
218 |
|
} |
219 |
|
|
220 |
|
/** |
232 |
|
Integer[] elements = new Integer[SIZE]; |
233 |
|
for (int i = 0; i < SIZE; i++) |
234 |
|
elements[i] = i; |
235 |
< |
Collections.shuffle(Arrays.asList(elements)); |
235 |
> |
shuffle(elements); |
236 |
|
Collection<Integer> full = populatedSet(elements); |
237 |
|
|
238 |
|
Iterator it = full.iterator(); |
254 |
|
* iterator remove is unsupported |
255 |
|
*/ |
256 |
|
public void testIteratorRemove() { |
257 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
257 |
> |
Collection full = populatedSet(3); |
258 |
|
Iterator it = full.iterator(); |
259 |
|
it.next(); |
260 |
|
try { |
268 |
|
*/ |
269 |
|
public void testToString() { |
270 |
|
assertEquals("[]", new CopyOnWriteArraySet().toString()); |
271 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
271 |
> |
Collection full = populatedSet(3); |
272 |
|
String s = full.toString(); |
273 |
|
for (int i = 0; i < 3; ++i) |
274 |
|
assertTrue(s.contains(String.valueOf(i))); |
280 |
|
* removeAll removes all elements from the given collection |
281 |
|
*/ |
282 |
|
public void testRemoveAll() { |
283 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
284 |
< |
Vector v = new Vector(); |
285 |
< |
v.add(one); |
286 |
< |
v.add(two); |
241 |
< |
full.removeAll(v); |
283 |
> |
Set full = populatedSet(3); |
284 |
> |
assertTrue(full.removeAll(Arrays.asList(one, two))); |
285 |
> |
assertEquals(1, full.size()); |
286 |
> |
assertFalse(full.removeAll(Arrays.asList(one, two))); |
287 |
|
assertEquals(1, full.size()); |
288 |
|
} |
289 |
|
|
291 |
|
* remove removes an element |
292 |
|
*/ |
293 |
|
public void testRemove() { |
294 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
294 |
> |
Collection full = populatedSet(3); |
295 |
|
full.remove(one); |
296 |
|
assertFalse(full.contains(one)); |
297 |
|
assertEquals(2, full.size()); |
301 |
|
* size returns the number of elements |
302 |
|
*/ |
303 |
|
public void testSize() { |
304 |
< |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
305 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
304 |
> |
Collection empty = new CopyOnWriteArraySet(); |
305 |
> |
Collection full = populatedSet(3); |
306 |
|
assertEquals(3, full.size()); |
307 |
|
assertEquals(0, empty.size()); |
308 |
|
} |
319 |
|
Integer[] elements = new Integer[SIZE]; |
320 |
|
for (int i = 0; i < SIZE; i++) |
321 |
|
elements[i] = i; |
322 |
< |
Collections.shuffle(Arrays.asList(elements)); |
322 |
> |
shuffle(elements); |
323 |
|
Collection<Integer> full = populatedSet(elements); |
324 |
|
|
325 |
|
assertTrue(Arrays.equals(elements, full.toArray())); |
337 |
|
a = new Integer[0]; |
338 |
|
assertSame(a, empty.toArray(a)); |
339 |
|
|
340 |
< |
a = new Integer[SIZE/2]; |
340 |
> |
a = new Integer[SIZE / 2]; |
341 |
|
Arrays.fill(a, 42); |
342 |
|
assertSame(a, empty.toArray(a)); |
343 |
|
assertNull(a[0]); |
347 |
|
Integer[] elements = new Integer[SIZE]; |
348 |
|
for (int i = 0; i < SIZE; i++) |
349 |
|
elements[i] = i; |
350 |
< |
Collections.shuffle(Arrays.asList(elements)); |
350 |
> |
shuffle(elements); |
351 |
|
Collection<Integer> full = populatedSet(elements); |
352 |
|
|
353 |
|
Arrays.fill(a, 42); |
361 |
|
assertSame(a, full.toArray(a)); |
362 |
|
assertTrue(Arrays.equals(elements, a)); |
363 |
|
|
364 |
< |
a = new Integer[2*SIZE]; |
364 |
> |
a = new Integer[2 * SIZE]; |
365 |
|
Arrays.fill(a, 42); |
366 |
|
assertSame(a, full.toArray(a)); |
367 |
|
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
375 |
|
* not store the objects inside the set |
376 |
|
*/ |
377 |
|
public void testToArray_ArrayStoreException() { |
378 |
+ |
CopyOnWriteArraySet c = new CopyOnWriteArraySet(); |
379 |
+ |
c.add("zfasdfsdf"); |
380 |
+ |
c.add("asdadasd"); |
381 |
|
try { |
334 |
– |
CopyOnWriteArraySet c = new CopyOnWriteArraySet(); |
335 |
– |
c.add("zfasdfsdf"); |
336 |
– |
c.add("asdadasd"); |
382 |
|
c.toArray(new Long[5]); |
383 |
|
shouldThrow(); |
384 |
|
} catch (ArrayStoreException success) {} |
385 |
|
} |
386 |
|
|
387 |
|
/** |
388 |
< |
* A deserialized serialized set is equal |
388 |
> |
* A deserialized/reserialized set equals original |
389 |
|
*/ |
390 |
|
public void testSerialization() throws Exception { |
391 |
|
Set x = populatedSet(SIZE); |