8 |
|
|
9 |
|
import junit.framework.*; |
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; |
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 |
|
*/ |
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<Integer> full = populatedSet(SIZE); |
243 |
< |
Object[] array = full.toArray(); |
244 |
< |
Iterator<Integer> it = full.iterator(); |
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 |
< |
assertSame(array[i], it.next()); |
249 |
< |
assertFalse(it.hasNext()); |
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<Integer> full = populatedSet(SIZE); |
262 |
< |
Integer[] ints = new Integer[SIZE]; |
263 |
< |
assertSame(ints, full.toArray(ints)); |
264 |
< |
Iterator<Integer> it = full.iterator(); |
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 |
< |
assertSame(ints[i], it.next()); |
277 |
< |
assertFalse(it.hasNext()); |
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 |
|
/** |