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.Collection; |
12 |
> |
import java.util.Collections; |
13 |
> |
import java.util.Iterator; |
14 |
> |
import java.util.Set; |
15 |
> |
import java.util.Vector; |
16 |
> |
import java.util.concurrent.CopyOnWriteArraySet; |
17 |
|
|
18 |
|
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
19 |
|
public static void main(String[] args) { |
23 |
|
return new TestSuite(CopyOnWriteArraySetTest.class); |
24 |
|
} |
25 |
|
|
26 |
< |
static CopyOnWriteArraySet populatedSet(int n) { |
27 |
< |
CopyOnWriteArraySet a = new CopyOnWriteArraySet(); |
26 |
> |
static CopyOnWriteArraySet<Integer> populatedSet(int n) { |
27 |
> |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); |
28 |
|
assertTrue(a.isEmpty()); |
29 |
< |
for (int i = 0; i < n; ++i) |
30 |
< |
a.add(new Integer(i)); |
29 |
> |
for (int i = 0; i < n; i++) |
30 |
> |
a.add(i); |
31 |
|
assertFalse(a.isEmpty()); |
32 |
|
assertEquals(n, a.size()); |
33 |
|
return a; |
34 |
|
} |
35 |
|
|
36 |
+ |
static CopyOnWriteArraySet populatedSet(Integer[] elements) { |
37 |
+ |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); |
38 |
+ |
assertTrue(a.isEmpty()); |
39 |
+ |
for (int i = 0; i < elements.length; i++) |
40 |
+ |
a.add(elements[i]); |
41 |
+ |
assertFalse(a.isEmpty()); |
42 |
+ |
assertEquals(elements.length, a.size()); |
43 |
+ |
return a; |
44 |
+ |
} |
45 |
+ |
|
46 |
|
/** |
47 |
|
* Default-constructed set is empty |
48 |
|
*/ |
236 |
|
} |
237 |
|
|
238 |
|
/** |
239 |
< |
* toArray returns an Object array containing all elements from the set |
239 |
> |
* toArray() returns an Object array containing all elements from |
240 |
> |
* the set in insertion order |
241 |
|
*/ |
242 |
|
public void testToArray() { |
243 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
244 |
< |
Object[] o = full.toArray(); |
245 |
< |
assertEquals(3, o.length); |
246 |
< |
assertEquals(0, o[0]); |
247 |
< |
assertEquals(1, o[1]); |
248 |
< |
assertEquals(2, o[2]); |
243 |
> |
Object[] a = new CopyOnWriteArraySet().toArray(); |
244 |
> |
assertTrue(Arrays.equals(new Object[0], a)); |
245 |
> |
assertSame(Object[].class, a.getClass()); |
246 |
> |
|
247 |
> |
Integer[] elements = new Integer[SIZE]; |
248 |
> |
for (int i = 0; i < SIZE; i++) |
249 |
> |
elements[i] = i; |
250 |
> |
Collections.shuffle(Arrays.asList(elements)); |
251 |
> |
Collection<Integer> full = populatedSet(elements); |
252 |
> |
|
253 |
> |
assertTrue(Arrays.equals(elements, full.toArray())); |
254 |
> |
assertSame(Object[].class, full.toArray().getClass()); |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
< |
* toArray returns an Integer array containing all elements from |
259 |
< |
* the set |
258 |
> |
* toArray(Integer array) returns an Integer array containing all |
259 |
> |
* elements from the set in insertion order |
260 |
|
*/ |
261 |
|
public void testToArray2() { |
262 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
263 |
< |
Integer[] i = new Integer[3]; |
264 |
< |
i = (Integer[])full.toArray(i); |
265 |
< |
assertEquals(3, i.length); |
266 |
< |
assertEquals(0, (int) i[0]); |
267 |
< |
assertEquals(1, (int) i[1]); |
268 |
< |
assertEquals(2, (int) i[2]); |
262 |
> |
Collection empty = new CopyOnWriteArraySet(); |
263 |
> |
Integer[] a; |
264 |
> |
|
265 |
> |
a = new Integer[0]; |
266 |
> |
assertSame(a, empty.toArray(a)); |
267 |
> |
|
268 |
> |
a = new Integer[SIZE/2]; |
269 |
> |
Arrays.fill(a, 42); |
270 |
> |
assertSame(a, empty.toArray(a)); |
271 |
> |
assertNull(a[0]); |
272 |
> |
for (int i = 1; i < a.length; i++) |
273 |
> |
assertEquals(42, (int) a[i]); |
274 |
> |
|
275 |
> |
Integer[] elements = new Integer[SIZE]; |
276 |
> |
for (int i = 0; i < SIZE; i++) |
277 |
> |
elements[i] = i; |
278 |
> |
Collections.shuffle(Arrays.asList(elements)); |
279 |
> |
Collection<Integer> full = populatedSet(elements); |
280 |
> |
|
281 |
> |
Arrays.fill(a, 42); |
282 |
> |
assertTrue(Arrays.equals(elements, full.toArray(a))); |
283 |
> |
for (int i = 0; i < a.length; i++) |
284 |
> |
assertEquals(42, (int) a[i]); |
285 |
> |
assertSame(Integer[].class, full.toArray(a).getClass()); |
286 |
> |
|
287 |
> |
a = new Integer[SIZE]; |
288 |
> |
Arrays.fill(a, 42); |
289 |
> |
assertSame(a, full.toArray(a)); |
290 |
> |
assertTrue(Arrays.equals(elements, a)); |
291 |
> |
|
292 |
> |
a = new Integer[2*SIZE]; |
293 |
> |
Arrays.fill(a, 42); |
294 |
> |
assertSame(a, full.toArray(a)); |
295 |
> |
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
296 |
> |
assertNull(a[SIZE]); |
297 |
> |
for (int i = SIZE + 1; i < a.length; i++) |
298 |
> |
assertEquals(42, (int) a[i]); |
299 |
|
} |
300 |
|
|
301 |
|
/** |
316 |
|
* A deserialized serialized set is equal |
317 |
|
*/ |
318 |
|
public void testSerialization() throws Exception { |
319 |
< |
CopyOnWriteArraySet q = populatedSet(SIZE); |
319 |
> |
Set x = populatedSet(SIZE); |
320 |
> |
Set y = serialClone(x); |
321 |
|
|
322 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
323 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
324 |
< |
out.writeObject(q); |
325 |
< |
out.close(); |
274 |
< |
|
275 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
276 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
277 |
< |
CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject(); |
278 |
< |
assertEquals(q.size(), r.size()); |
279 |
< |
assertTrue(q.equals(r)); |
280 |
< |
assertTrue(r.equals(q)); |
322 |
> |
assertTrue(x != y); |
323 |
> |
assertEquals(x.size(), y.size()); |
324 |
> |
assertEquals(x, y); |
325 |
> |
assertEquals(y, x); |
326 |
|
} |
327 |
|
|
328 |
|
} |