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) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
|
return new TestSuite(CopyOnWriteArraySetTest.class); |
51 |
|
assertTrue(a.contains(ints[i])); |
52 |
|
} |
53 |
|
|
52 |
– |
|
54 |
|
/** |
55 |
< |
* addAll adds each element from the given collection |
55 |
> |
* addAll adds each element from the given collection |
56 |
|
*/ |
57 |
|
public void testAddAll() { |
58 |
|
CopyOnWriteArraySet full = populatedSet(3); |
65 |
|
} |
66 |
|
|
67 |
|
/** |
68 |
< |
* addAll adds each element from the given collection that did not |
69 |
< |
* already exist in the set |
68 |
> |
* addAll adds each element from the given collection that did not |
69 |
> |
* already exist in the set |
70 |
|
*/ |
71 |
|
public void testAddAll2() { |
72 |
|
CopyOnWriteArraySet full = populatedSet(3); |
79 |
|
} |
80 |
|
|
81 |
|
/** |
82 |
< |
* add will not add the element if it already exists in the set |
82 |
> |
* add will not add the element if it already exists in the set |
83 |
|
*/ |
84 |
|
public void testAdd2() { |
85 |
|
CopyOnWriteArraySet full = populatedSet(3); |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
< |
* add adds the element when it does not exist |
91 |
< |
* in the set |
91 |
> |
* add adds the element when it does not exist in the set |
92 |
|
*/ |
93 |
|
public void testAdd3() { |
94 |
|
CopyOnWriteArraySet full = populatedSet(3); |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
< |
* clear removes all elements from the set |
100 |
> |
* clear removes all elements from the set |
101 |
|
*/ |
102 |
|
public void testClear() { |
103 |
|
CopyOnWriteArraySet full = populatedSet(3); |
106 |
|
} |
107 |
|
|
108 |
|
/** |
109 |
< |
* contains returns true for added elements |
109 |
> |
* contains returns true for added elements |
110 |
|
*/ |
111 |
|
public void testContains() { |
112 |
|
CopyOnWriteArraySet full = populatedSet(3); |
132 |
|
assertEquals(a.hashCode(), b.hashCode()); |
133 |
|
} |
134 |
|
|
135 |
– |
|
135 |
|
/** |
136 |
< |
* containsAll returns true for collections with subset of elements |
136 |
> |
* containsAll returns true for collections with subset of elements |
137 |
|
*/ |
138 |
|
public void testContainsAll() { |
139 |
|
CopyOnWriteArraySet full = populatedSet(3); |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
< |
* isEmpty is true when empty, else false |
149 |
> |
* isEmpty is true when empty, else false |
150 |
|
*/ |
151 |
|
public void testIsEmpty() { |
152 |
|
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
156 |
|
} |
157 |
|
|
158 |
|
/** |
159 |
< |
* iterator() returns an iterator containing the elements of the set |
159 |
> |
* iterator() returns an iterator containing the elements of the set |
160 |
|
*/ |
161 |
|
public void testIterator() { |
162 |
|
CopyOnWriteArraySet full = populatedSet(3); |
170 |
|
/** |
171 |
|
* iterator remove is unsupported |
172 |
|
*/ |
173 |
< |
public void testIteratorRemove () { |
173 |
> |
public void testIteratorRemove() { |
174 |
|
CopyOnWriteArraySet full = populatedSet(3); |
175 |
|
Iterator it = full.iterator(); |
176 |
|
it.next(); |
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 |
|
|
195 |
– |
|
194 |
|
/** |
195 |
< |
* removeAll removes all elements from the given collection |
195 |
> |
* removeAll removes all elements from the given collection |
196 |
|
*/ |
197 |
|
public void testRemoveAll() { |
198 |
|
CopyOnWriteArraySet full = populatedSet(3); |
203 |
|
assertEquals(1, full.size()); |
204 |
|
} |
205 |
|
|
208 |
– |
|
206 |
|
/** |
207 |
|
* remove removes an element |
208 |
|
*/ |
214 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
< |
* size returns the number of elements |
217 |
> |
* size returns the number of elements |
218 |
|
*/ |
219 |
|
public void testSize() { |
220 |
|
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
< |
* toArray returns an Object array containing all elements from the set |
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 |
|
/** |
239 |
< |
* toArray returns an Integer array containing all elements from |
240 |
< |
* the set |
239 |
> |
* toArray returns an Integer array containing all elements from |
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 |
|
|
255 |
– |
|
252 |
|
/** |
253 |
< |
* toArray throws an ArrayStoreException when the given array can |
254 |
< |
* not store the objects inside the set |
253 |
> |
* toArray throws an ArrayStoreException when the given array can |
254 |
> |
* not store the objects inside the set |
255 |
|
*/ |
256 |
|
public void testToArray_ArrayStoreException() { |
257 |
|
try { |
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(); |
280 |
< |
|
281 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
282 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
283 |
< |
CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject(); |
284 |
< |
assertEquals(q.size(), r.size()); |
285 |
< |
assertTrue(q.equals(r)); |
286 |
< |
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 |
|
} |