7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
10 |
> |
import java.util.ArrayList; |
11 |
> |
import java.util.Arrays; |
12 |
> |
import java.util.Collection; |
13 |
> |
import java.util.Collections; |
14 |
> |
import java.util.Iterator; |
15 |
> |
import java.util.NoSuchElementException; |
16 |
> |
import java.util.Set; |
17 |
> |
import java.util.Vector; |
18 |
> |
import java.util.concurrent.CopyOnWriteArraySet; |
19 |
|
|
20 |
|
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
21 |
|
public static void main(String[] args) { |
25 |
|
return new TestSuite(CopyOnWriteArraySetTest.class); |
26 |
|
} |
27 |
|
|
28 |
< |
static CopyOnWriteArraySet populatedSet(int n) { |
29 |
< |
CopyOnWriteArraySet a = new CopyOnWriteArraySet(); |
28 |
> |
static CopyOnWriteArraySet<Integer> populatedSet(int n) { |
29 |
> |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); |
30 |
|
assertTrue(a.isEmpty()); |
31 |
< |
for (int i = 0; i < n; ++i) |
32 |
< |
a.add(new Integer(i)); |
31 |
> |
for (int i = 0; i < n; i++) |
32 |
> |
a.add(i); |
33 |
|
assertFalse(a.isEmpty()); |
34 |
|
assertEquals(n, a.size()); |
35 |
|
return a; |
36 |
|
} |
37 |
|
|
38 |
+ |
static CopyOnWriteArraySet populatedSet(Integer[] elements) { |
39 |
+ |
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); |
40 |
+ |
assertTrue(a.isEmpty()); |
41 |
+ |
for (int i = 0; i < elements.length; i++) |
42 |
+ |
a.add(elements[i]); |
43 |
+ |
assertFalse(a.isEmpty()); |
44 |
+ |
assertEquals(elements.length, a.size()); |
45 |
+ |
return a; |
46 |
+ |
} |
47 |
+ |
|
48 |
|
/** |
49 |
|
* Default-constructed set is empty |
50 |
|
*/ |
170 |
|
} |
171 |
|
|
172 |
|
/** |
173 |
< |
* iterator() returns an iterator containing the elements of the set |
173 |
> |
* iterator() returns an iterator containing the elements of the |
174 |
> |
* set in insertion order |
175 |
|
*/ |
176 |
|
public void testIterator() { |
177 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
178 |
< |
Iterator i = full.iterator(); |
179 |
< |
int j; |
180 |
< |
for (j = 0; i.hasNext(); j++) |
181 |
< |
assertEquals(j, i.next()); |
182 |
< |
assertEquals(3, j); |
177 |
> |
Collection empty = new CopyOnWriteArraySet(); |
178 |
> |
assertFalse(empty.iterator().hasNext()); |
179 |
> |
try { |
180 |
> |
empty.iterator().next(); |
181 |
> |
shouldThrow(); |
182 |
> |
} catch (NoSuchElementException success) {} |
183 |
> |
|
184 |
> |
Integer[] elements = new Integer[SIZE]; |
185 |
> |
for (int i = 0; i < SIZE; i++) |
186 |
> |
elements[i] = i; |
187 |
> |
Collections.shuffle(Arrays.asList(elements)); |
188 |
> |
Collection<Integer> full = populatedSet(elements); |
189 |
> |
|
190 |
> |
Iterator it = full.iterator(); |
191 |
> |
for (int j = 0; j < SIZE; j++) { |
192 |
> |
assertTrue(it.hasNext()); |
193 |
> |
assertEquals(elements[j], it.next()); |
194 |
> |
} |
195 |
> |
assertFalse(it.hasNext()); |
196 |
> |
try { |
197 |
> |
it.next(); |
198 |
> |
shouldThrow(); |
199 |
> |
} catch (NoSuchElementException success) {} |
200 |
|
} |
201 |
|
|
202 |
|
/** |
216 |
|
* toString holds toString of elements |
217 |
|
*/ |
218 |
|
public void testToString() { |
219 |
+ |
assertEquals("[]", new CopyOnWriteArraySet().toString()); |
220 |
|
CopyOnWriteArraySet full = populatedSet(3); |
221 |
|
String s = full.toString(); |
222 |
< |
for (int i = 0; i < 3; ++i) { |
222 |
> |
for (int i = 0; i < 3; ++i) |
223 |
|
assertTrue(s.contains(String.valueOf(i))); |
224 |
< |
} |
224 |
> |
assertEquals(new ArrayList(full).toString(), |
225 |
> |
full.toString()); |
226 |
|
} |
227 |
|
|
228 |
|
/** |
258 |
|
} |
259 |
|
|
260 |
|
/** |
261 |
< |
* toArray returns an Object array containing all elements from the set |
261 |
> |
* toArray() returns an Object array containing all elements from |
262 |
> |
* the set in insertion order |
263 |
|
*/ |
264 |
|
public void testToArray() { |
265 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
266 |
< |
Object[] o = full.toArray(); |
267 |
< |
assertEquals(3, o.length); |
268 |
< |
assertEquals(0, o[0]); |
269 |
< |
assertEquals(1, o[1]); |
270 |
< |
assertEquals(2, o[2]); |
265 |
> |
Object[] a = new CopyOnWriteArraySet().toArray(); |
266 |
> |
assertTrue(Arrays.equals(new Object[0], a)); |
267 |
> |
assertSame(Object[].class, a.getClass()); |
268 |
> |
|
269 |
> |
Integer[] elements = new Integer[SIZE]; |
270 |
> |
for (int i = 0; i < SIZE; i++) |
271 |
> |
elements[i] = i; |
272 |
> |
Collections.shuffle(Arrays.asList(elements)); |
273 |
> |
Collection<Integer> full = populatedSet(elements); |
274 |
> |
|
275 |
> |
assertTrue(Arrays.equals(elements, full.toArray())); |
276 |
> |
assertSame(Object[].class, full.toArray().getClass()); |
277 |
|
} |
278 |
|
|
279 |
|
/** |
280 |
< |
* toArray returns an Integer array containing all elements from |
281 |
< |
* the set |
280 |
> |
* toArray(Integer array) returns an Integer array containing all |
281 |
> |
* elements from the set in insertion order |
282 |
|
*/ |
283 |
|
public void testToArray2() { |
284 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
285 |
< |
Integer[] i = new Integer[3]; |
286 |
< |
i = (Integer[])full.toArray(i); |
287 |
< |
assertEquals(3, i.length); |
288 |
< |
assertEquals(0, (int) i[0]); |
289 |
< |
assertEquals(1, (int) i[1]); |
290 |
< |
assertEquals(2, (int) i[2]); |
284 |
> |
Collection empty = new CopyOnWriteArraySet(); |
285 |
> |
Integer[] a; |
286 |
> |
|
287 |
> |
a = new Integer[0]; |
288 |
> |
assertSame(a, empty.toArray(a)); |
289 |
> |
|
290 |
> |
a = new Integer[SIZE/2]; |
291 |
> |
Arrays.fill(a, 42); |
292 |
> |
assertSame(a, empty.toArray(a)); |
293 |
> |
assertNull(a[0]); |
294 |
> |
for (int i = 1; i < a.length; i++) |
295 |
> |
assertEquals(42, (int) a[i]); |
296 |
> |
|
297 |
> |
Integer[] elements = new Integer[SIZE]; |
298 |
> |
for (int i = 0; i < SIZE; i++) |
299 |
> |
elements[i] = i; |
300 |
> |
Collections.shuffle(Arrays.asList(elements)); |
301 |
> |
Collection<Integer> full = populatedSet(elements); |
302 |
> |
|
303 |
> |
Arrays.fill(a, 42); |
304 |
> |
assertTrue(Arrays.equals(elements, full.toArray(a))); |
305 |
> |
for (int i = 0; i < a.length; i++) |
306 |
> |
assertEquals(42, (int) a[i]); |
307 |
> |
assertSame(Integer[].class, full.toArray(a).getClass()); |
308 |
> |
|
309 |
> |
a = new Integer[SIZE]; |
310 |
> |
Arrays.fill(a, 42); |
311 |
> |
assertSame(a, full.toArray(a)); |
312 |
> |
assertTrue(Arrays.equals(elements, a)); |
313 |
> |
|
314 |
> |
a = new Integer[2*SIZE]; |
315 |
> |
Arrays.fill(a, 42); |
316 |
> |
assertSame(a, full.toArray(a)); |
317 |
> |
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
318 |
> |
assertNull(a[SIZE]); |
319 |
> |
for (int i = SIZE + 1; i < a.length; i++) |
320 |
> |
assertEquals(42, (int) a[i]); |
321 |
|
} |
322 |
|
|
323 |
|
/** |
338 |
|
* A deserialized serialized set is equal |
339 |
|
*/ |
340 |
|
public void testSerialization() throws Exception { |
341 |
< |
CopyOnWriteArraySet q = populatedSet(SIZE); |
341 |
> |
Set x = populatedSet(SIZE); |
342 |
> |
Set y = serialClone(x); |
343 |
|
|
344 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
345 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
346 |
< |
out.writeObject(q); |
347 |
< |
out.close(); |
348 |
< |
|
349 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
350 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
351 |
< |
CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject(); |
352 |
< |
assertEquals(q.size(), r.size()); |
353 |
< |
assertTrue(q.equals(r)); |
354 |
< |
assertTrue(r.equals(q)); |
344 |
> |
assertNotSame(y, x); |
345 |
> |
assertEquals(x.size(), y.size()); |
346 |
> |
assertEquals(x.toString(), y.toString()); |
347 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
348 |
> |
assertEquals(x, y); |
349 |
> |
assertEquals(y, x); |
350 |
> |
} |
351 |
> |
|
352 |
> |
/** |
353 |
> |
* addAll is idempotent |
354 |
> |
*/ |
355 |
> |
public void testAddAll_idempotent() throws Exception { |
356 |
> |
Set x = populatedSet(SIZE); |
357 |
> |
Set y = new CopyOnWriteArraySet(x); |
358 |
> |
y.addAll(x); |
359 |
> |
assertEquals(x, y); |
360 |
> |
assertEquals(y, x); |
361 |
|
} |
362 |
|
|
363 |
|
} |