1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
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.Iterator; |
12 |
> |
import java.util.Set; |
13 |
> |
import java.util.Vector; |
14 |
> |
import java.util.concurrent.CopyOnWriteArraySet; |
15 |
|
|
16 |
|
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
51 |
|
assertTrue(a.contains(ints[i])); |
52 |
|
} |
53 |
|
|
52 |
– |
|
54 |
|
/** |
55 |
|
* addAll adds each element from the given collection |
56 |
|
*/ |
132 |
|
assertEquals(a.hashCode(), b.hashCode()); |
133 |
|
} |
134 |
|
|
134 |
– |
|
135 |
|
/** |
136 |
|
* containsAll returns true for collections with subset of elements |
137 |
|
*/ |
187 |
|
CopyOnWriteArraySet full = populatedSet(3); |
188 |
|
String s = full.toString(); |
189 |
|
for (int i = 0; i < 3; ++i) { |
190 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
190 |
> |
assertTrue(s.contains(String.valueOf(i))); |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
– |
|
194 |
|
/** |
195 |
|
* removeAll removes all elements from the given collection |
196 |
|
*/ |
203 |
|
assertEquals(1, full.size()); |
204 |
|
} |
205 |
|
|
207 |
– |
|
206 |
|
/** |
207 |
|
* remove removes an element |
208 |
|
*/ |
227 |
|
* toArray returns an Object array containing all elements from the set |
228 |
|
*/ |
229 |
|
public void testToArray() { |
230 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
231 |
< |
Object[] o = full.toArray(); |
232 |
< |
assertEquals(3, o.length); |
233 |
< |
assertEquals(0, o[0]); |
234 |
< |
assertEquals(1, o[1]); |
235 |
< |
assertEquals(2, o[2]); |
230 |
> |
CopyOnWriteArraySet<Integer> full = populatedSet(SIZE); |
231 |
> |
Object[] array = full.toArray(); |
232 |
> |
Iterator<Integer> it = full.iterator(); |
233 |
> |
for (int i = 0; i < SIZE; i++) |
234 |
> |
assertSame(array[i], it.next()); |
235 |
> |
assertFalse(it.hasNext()); |
236 |
|
} |
237 |
|
|
238 |
|
/** |
240 |
|
* the set |
241 |
|
*/ |
242 |
|
public void testToArray2() { |
243 |
< |
CopyOnWriteArraySet full = populatedSet(3); |
244 |
< |
Integer[] i = new Integer[3]; |
245 |
< |
i = (Integer[])full.toArray(i); |
246 |
< |
assertEquals(3, i.length); |
247 |
< |
assertEquals(0, (int) i[0]); |
248 |
< |
assertEquals(1, (int) i[1]); |
249 |
< |
assertEquals(2, (int) i[2]); |
243 |
> |
CopyOnWriteArraySet<Integer> full = populatedSet(SIZE); |
244 |
> |
Integer[] ints = new Integer[SIZE]; |
245 |
> |
assertSame(ints, full.toArray(ints)); |
246 |
> |
Iterator<Integer> it = full.iterator(); |
247 |
> |
for (int i = 0; i < SIZE; i++) |
248 |
> |
assertSame(ints[i], it.next()); |
249 |
> |
assertFalse(it.hasNext()); |
250 |
|
} |
251 |
|
|
254 |
– |
|
252 |
|
/** |
253 |
|
* toArray throws an ArrayStoreException when the given array can |
254 |
|
* not store the objects inside the set |
267 |
|
* A deserialized serialized set is equal |
268 |
|
*/ |
269 |
|
public void testSerialization() throws Exception { |
270 |
< |
CopyOnWriteArraySet q = populatedSet(SIZE); |
270 |
> |
Set x = populatedSet(SIZE); |
271 |
> |
Set y = serialClone(x); |
272 |
|
|
273 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
274 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
275 |
< |
out.writeObject(q); |
276 |
< |
out.close(); |
279 |
< |
|
280 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
281 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
282 |
< |
CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject(); |
283 |
< |
assertEquals(q.size(), r.size()); |
284 |
< |
assertTrue(q.equals(r)); |
285 |
< |
assertTrue(r.equals(q)); |
273 |
> |
assertTrue(x != y); |
274 |
> |
assertEquals(x.size(), y.size()); |
275 |
> |
assertEquals(x, y); |
276 |
> |
assertEquals(y, x); |
277 |
|
} |
278 |
|
|
279 |
|
} |