1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.Collections; |
12 |
> |
import java.util.Iterator; |
13 |
> |
import java.util.Set; |
14 |
> |
import java.util.Vector; |
15 |
> |
import java.util.concurrent.CopyOnWriteArraySet; |
16 |
|
|
17 |
|
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
22 |
|
return new TestSuite(CopyOnWriteArraySetTest.class); |
23 |
|
} |
24 |
|
|
25 |
< |
static CopyOnWriteArraySet populatedSet(int n) { |
26 |
< |
CopyOnWriteArraySet a = new CopyOnWriteArraySet(); |
25 |
> |
static CopyOnWriteArraySet<Integer> populatedSet(int n) { |
26 |
> |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); |
27 |
|
assertTrue(a.isEmpty()); |
28 |
|
for (int i = 0; i < n; ++i) |
29 |
< |
a.add(new Integer(i)); |
29 |
> |
a.add(i); |
30 |
|
assertFalse(a.isEmpty()); |
31 |
|
assertEquals(n, a.size()); |
32 |
|
return a; |
33 |
|
} |
34 |
|
|
35 |
+ |
static CopyOnWriteArraySet populatedSet(Integer[] elements) { |
36 |
+ |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); |
37 |
+ |
assertTrue(a.isEmpty()); |
38 |
+ |
for (int i = 0; i < elements.length; i++) |
39 |
+ |
a.add(elements[i]); |
40 |
+ |
assertFalse(a.isEmpty()); |
41 |
+ |
assertEquals(elements.length, a.size()); |
42 |
+ |
return a; |
43 |
+ |
} |
44 |
+ |
|
45 |
|
/** |
46 |
|
* Default-constructed set is empty |
47 |
|
*/ |
62 |
|
assertTrue(a.contains(ints[i])); |
63 |
|
} |
64 |
|
|
52 |
– |
|
65 |
|
/** |
66 |
|
* addAll adds each element from the given collection |
67 |
|
*/ |
143 |
|
assertEquals(a.hashCode(), b.hashCode()); |
144 |
|
} |
145 |
|
|
134 |
– |
|
146 |
|
/** |
147 |
|
* containsAll returns true for collections with subset of elements |
148 |
|
*/ |
198 |
|
CopyOnWriteArraySet full = populatedSet(3); |
199 |
|
String s = full.toString(); |
200 |
|
for (int i = 0; i < 3; ++i) { |
201 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
201 |
> |
assertTrue(s.contains(String.valueOf(i))); |
202 |
|
} |
203 |
|
} |
204 |
|
|
194 |
– |
|
205 |
|
/** |
206 |
|
* removeAll removes all elements from the given collection |
207 |
|
*/ |
214 |
|
assertEquals(1, full.size()); |
215 |
|
} |
216 |
|
|
207 |
– |
|
217 |
|
/** |
218 |
|
* remove removes an element |
219 |
|
*/ |
235 |
|
} |
236 |
|
|
237 |
|
/** |
238 |
< |
* toArray returns an Object array containing all elements from the set |
238 |
> |
* toArray() returns an Object array containing all elements from |
239 |
> |
* the set in insertion order |
240 |
|
*/ |
241 |
|
public void testToArray() { |
242 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
243 |
< |
Object[] o = full.toArray(); |
244 |
< |
assertEquals(3, o.length); |
245 |
< |
assertEquals(0, o[0]); |
246 |
< |
assertEquals(1, o[1]); |
247 |
< |
assertEquals(2, o[2]); |
242 |
> |
Object[] a = new CopyOnWriteArraySet().toArray(); |
243 |
> |
assertTrue(Arrays.equals(new Object[0], a)); |
244 |
> |
assertSame(Object[].class, a.getClass()); |
245 |
> |
|
246 |
> |
Integer[] elements = new Integer[SIZE]; |
247 |
> |
for (int i = 0; i < SIZE; i++) |
248 |
> |
elements[i] = i; |
249 |
> |
Collections.shuffle(Arrays.asList(elements)); |
250 |
> |
CopyOnWriteArraySet<Integer> full = populatedSet(elements); |
251 |
> |
|
252 |
> |
assertTrue(Arrays.equals(elements, full.toArray())); |
253 |
> |
assertSame(Object[].class, full.toArray().getClass()); |
254 |
|
} |
255 |
|
|
256 |
|
/** |
257 |
< |
* toArray returns an Integer array containing all elements from |
258 |
< |
* the set |
257 |
> |
* toArray(Integer array) returns an Integer array containing all |
258 |
> |
* elements from the set in insertion order |
259 |
|
*/ |
260 |
|
public void testToArray2() { |
261 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
262 |
< |
Integer[] i = new Integer[3]; |
247 |
< |
i = (Integer[])full.toArray(i); |
248 |
< |
assertEquals(3, i.length); |
249 |
< |
assertEquals(0, (int) i[0]); |
250 |
< |
assertEquals(1, (int) i[1]); |
251 |
< |
assertEquals(2, (int) i[2]); |
252 |
< |
} |
261 |
> |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
262 |
> |
Integer[] a; |
263 |
|
|
264 |
+ |
a = new Integer[0]; |
265 |
+ |
assertSame(a, empty.toArray(a)); |
266 |
+ |
|
267 |
+ |
a = new Integer[SIZE/2]; |
268 |
+ |
Arrays.fill(a, 42); |
269 |
+ |
assertSame(a, empty.toArray(a)); |
270 |
+ |
assertNull(a[0]); |
271 |
+ |
for (int i = 1; i < a.length; i++) |
272 |
+ |
assertEquals(42, (int) a[i]); |
273 |
+ |
|
274 |
+ |
Integer[] elements = new Integer[SIZE]; |
275 |
+ |
for (int i = 0; i < SIZE; i++) |
276 |
+ |
elements[i] = i; |
277 |
+ |
Collections.shuffle(Arrays.asList(elements)); |
278 |
+ |
CopyOnWriteArraySet<Integer> full = populatedSet(elements); |
279 |
+ |
|
280 |
+ |
Arrays.fill(a, 42); |
281 |
+ |
assertTrue(Arrays.equals(elements, full.toArray(a))); |
282 |
+ |
for (int i = 0; i < a.length; i++) |
283 |
+ |
assertEquals(42, (int) a[i]); |
284 |
+ |
assertSame(Integer[].class, full.toArray(a).getClass()); |
285 |
+ |
|
286 |
+ |
a = new Integer[SIZE]; |
287 |
+ |
Arrays.fill(a, 42); |
288 |
+ |
assertSame(a, full.toArray(a)); |
289 |
+ |
assertTrue(Arrays.equals(elements, a)); |
290 |
+ |
|
291 |
+ |
a = new Integer[2*SIZE]; |
292 |
+ |
Arrays.fill(a, 42); |
293 |
+ |
assertSame(a, full.toArray(a)); |
294 |
+ |
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
295 |
+ |
assertNull(a[SIZE]); |
296 |
+ |
for (int i = SIZE + 1; i < a.length; i++) |
297 |
+ |
assertEquals(42, (int) a[i]); |
298 |
+ |
} |
299 |
|
|
300 |
|
/** |
301 |
|
* toArray throws an ArrayStoreException when the given array can |
315 |
|
* A deserialized serialized set is equal |
316 |
|
*/ |
317 |
|
public void testSerialization() throws Exception { |
318 |
< |
CopyOnWriteArraySet q = populatedSet(SIZE); |
318 |
> |
Set x = populatedSet(SIZE); |
319 |
> |
Set y = serialClone(x); |
320 |
|
|
321 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
322 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
323 |
< |
out.writeObject(q); |
324 |
< |
out.close(); |
279 |
< |
|
280 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
281 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
282 |
< |
CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject(); |
283 |
< |
assertEquals(q.size(), r.size()); |
284 |
< |
assertTrue(q.equals(r)); |
285 |
< |
assertTrue(r.equals(q)); |
321 |
> |
assertTrue(x != y); |
322 |
> |
assertEquals(x.size(), y.size()); |
323 |
> |
assertEquals(x, y); |
324 |
> |
assertEquals(y, x); |
325 |
|
} |
326 |
|
|
327 |
|
} |