6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
9 |
> |
import java.util.ArrayList; |
10 |
|
import java.util.Arrays; |
11 |
+ |
import java.util.Collection; |
12 |
|
import java.util.Collections; |
13 |
|
import java.util.Iterator; |
14 |
+ |
import java.util.NoSuchElementException; |
15 |
|
import java.util.Set; |
16 |
|
import java.util.Vector; |
17 |
|
import java.util.concurrent.CopyOnWriteArraySet; |
18 |
|
|
19 |
+ |
import junit.framework.Test; |
20 |
+ |
import junit.framework.TestSuite; |
21 |
+ |
|
22 |
|
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
23 |
|
public static void main(String[] args) { |
24 |
|
junit.textui.TestRunner.run(suite()); |
30 |
|
static CopyOnWriteArraySet<Integer> populatedSet(int n) { |
31 |
|
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); |
32 |
|
assertTrue(a.isEmpty()); |
33 |
< |
for (int i = 0; i < n; ++i) |
33 |
> |
for (int i = 0; i < n; i++) |
34 |
|
a.add(i); |
35 |
|
assertFalse(a.isEmpty()); |
36 |
|
assertEquals(n, a.size()); |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* iterator() returns an iterator containing the elements of the set |
175 |
> |
* iterator() returns an iterator containing the elements of the |
176 |
> |
* set in insertion order |
177 |
|
*/ |
178 |
|
public void testIterator() { |
179 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
180 |
< |
Iterator i = full.iterator(); |
181 |
< |
int j; |
182 |
< |
for (j = 0; i.hasNext(); j++) |
183 |
< |
assertEquals(j, i.next()); |
184 |
< |
assertEquals(3, j); |
179 |
> |
Collection empty = new CopyOnWriteArraySet(); |
180 |
> |
assertFalse(empty.iterator().hasNext()); |
181 |
> |
try { |
182 |
> |
empty.iterator().next(); |
183 |
> |
shouldThrow(); |
184 |
> |
} catch (NoSuchElementException success) {} |
185 |
> |
|
186 |
> |
Integer[] elements = new Integer[SIZE]; |
187 |
> |
for (int i = 0; i < SIZE; i++) |
188 |
> |
elements[i] = i; |
189 |
> |
Collections.shuffle(Arrays.asList(elements)); |
190 |
> |
Collection<Integer> full = populatedSet(elements); |
191 |
> |
|
192 |
> |
Iterator it = full.iterator(); |
193 |
> |
for (int j = 0; j < SIZE; j++) { |
194 |
> |
assertTrue(it.hasNext()); |
195 |
> |
assertEquals(elements[j], it.next()); |
196 |
> |
} |
197 |
> |
assertIteratorExhausted(it); |
198 |
> |
} |
199 |
> |
|
200 |
> |
/** |
201 |
> |
* iterator of empty collection has no elements |
202 |
> |
*/ |
203 |
> |
public void testEmptyIterator() { |
204 |
> |
assertIteratorExhausted(new CopyOnWriteArraySet().iterator()); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
221 |
|
* toString holds toString of elements |
222 |
|
*/ |
223 |
|
public void testToString() { |
224 |
+ |
assertEquals("[]", new CopyOnWriteArraySet().toString()); |
225 |
|
CopyOnWriteArraySet full = populatedSet(3); |
226 |
|
String s = full.toString(); |
227 |
< |
for (int i = 0; i < 3; ++i) { |
227 |
> |
for (int i = 0; i < 3; ++i) |
228 |
|
assertTrue(s.contains(String.valueOf(i))); |
229 |
< |
} |
229 |
> |
assertEquals(new ArrayList(full).toString(), |
230 |
> |
full.toString()); |
231 |
|
} |
232 |
|
|
233 |
|
/** |
275 |
|
for (int i = 0; i < SIZE; i++) |
276 |
|
elements[i] = i; |
277 |
|
Collections.shuffle(Arrays.asList(elements)); |
278 |
< |
CopyOnWriteArraySet<Integer> full = populatedSet(elements); |
278 |
> |
Collection<Integer> full = populatedSet(elements); |
279 |
|
|
280 |
|
assertTrue(Arrays.equals(elements, full.toArray())); |
281 |
|
assertSame(Object[].class, full.toArray().getClass()); |
286 |
|
* elements from the set in insertion order |
287 |
|
*/ |
288 |
|
public void testToArray2() { |
289 |
< |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
289 |
> |
Collection empty = new CopyOnWriteArraySet(); |
290 |
|
Integer[] a; |
291 |
|
|
292 |
|
a = new Integer[0]; |
303 |
|
for (int i = 0; i < SIZE; i++) |
304 |
|
elements[i] = i; |
305 |
|
Collections.shuffle(Arrays.asList(elements)); |
306 |
< |
CopyOnWriteArraySet<Integer> full = populatedSet(elements); |
306 |
> |
Collection<Integer> full = populatedSet(elements); |
307 |
|
|
308 |
|
Arrays.fill(a, 42); |
309 |
|
assertTrue(Arrays.equals(elements, full.toArray(a))); |
346 |
|
Set x = populatedSet(SIZE); |
347 |
|
Set y = serialClone(x); |
348 |
|
|
349 |
< |
assertTrue(x != y); |
349 |
> |
assertNotSame(y, x); |
350 |
|
assertEquals(x.size(), y.size()); |
351 |
+ |
assertEquals(x.toString(), y.toString()); |
352 |
+ |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
353 |
+ |
assertEquals(x, y); |
354 |
+ |
assertEquals(y, x); |
355 |
+ |
} |
356 |
+ |
|
357 |
+ |
/** |
358 |
+ |
* addAll is idempotent |
359 |
+ |
*/ |
360 |
+ |
public void testAddAll_idempotent() throws Exception { |
361 |
+ |
Set x = populatedSet(SIZE); |
362 |
+ |
Set y = new CopyOnWriteArraySet(x); |
363 |
+ |
y.addAll(x); |
364 |
|
assertEquals(x, y); |
365 |
|
assertEquals(y, x); |
366 |
|
} |