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