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 |
|
*/ |
13 |
|
|
14 |
|
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run(suite()); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(CopyOnWriteArraySetTest.class); |
49 |
|
assertTrue(a.contains(ints[i])); |
50 |
|
} |
51 |
|
|
52 |
– |
|
52 |
|
/** |
53 |
< |
* addAll adds each element from the given collection |
53 |
> |
* addAll adds each element from the given collection |
54 |
|
*/ |
55 |
|
public void testAddAll() { |
56 |
|
CopyOnWriteArraySet full = populatedSet(3); |
63 |
|
} |
64 |
|
|
65 |
|
/** |
66 |
< |
* addAll adds each element from the given collection that did not |
67 |
< |
* already exist in the set |
66 |
> |
* addAll adds each element from the given collection that did not |
67 |
> |
* already exist in the set |
68 |
|
*/ |
69 |
|
public void testAddAll2() { |
70 |
|
CopyOnWriteArraySet full = populatedSet(3); |
77 |
|
} |
78 |
|
|
79 |
|
/** |
80 |
< |
* add will not add the element if it already exists in the set |
80 |
> |
* add will not add the element if it already exists in the set |
81 |
|
*/ |
82 |
|
public void testAdd2() { |
83 |
|
CopyOnWriteArraySet full = populatedSet(3); |
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
< |
* add adds the element when it does not exist |
91 |
< |
* in the set |
89 |
> |
* add adds the element when it does not exist in the set |
90 |
|
*/ |
91 |
|
public void testAdd3() { |
92 |
|
CopyOnWriteArraySet full = populatedSet(3); |
95 |
|
} |
96 |
|
|
97 |
|
/** |
98 |
< |
* clear removes all elements from the set |
98 |
> |
* clear removes all elements from the set |
99 |
|
*/ |
100 |
|
public void testClear() { |
101 |
|
CopyOnWriteArraySet full = populatedSet(3); |
104 |
|
} |
105 |
|
|
106 |
|
/** |
107 |
< |
* contains returns true for added elements |
107 |
> |
* contains returns true for added elements |
108 |
|
*/ |
109 |
|
public void testContains() { |
110 |
|
CopyOnWriteArraySet full = populatedSet(3); |
130 |
|
assertEquals(a.hashCode(), b.hashCode()); |
131 |
|
} |
132 |
|
|
135 |
– |
|
133 |
|
/** |
134 |
< |
* containsAll returns true for collections with subset of elements |
134 |
> |
* containsAll returns true for collections with subset of elements |
135 |
|
*/ |
136 |
|
public void testContainsAll() { |
137 |
|
CopyOnWriteArraySet full = populatedSet(3); |
144 |
|
} |
145 |
|
|
146 |
|
/** |
147 |
< |
* isEmpty is true when empty, else false |
147 |
> |
* isEmpty is true when empty, else false |
148 |
|
*/ |
149 |
|
public void testIsEmpty() { |
150 |
|
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
< |
* iterator() returns an iterator containing the elements of the set |
157 |
> |
* iterator() returns an iterator containing the elements of the set |
158 |
|
*/ |
159 |
|
public void testIterator() { |
160 |
|
CopyOnWriteArraySet full = populatedSet(3); |
161 |
|
Iterator i = full.iterator(); |
162 |
|
int j; |
163 |
|
for (j = 0; i.hasNext(); j++) |
164 |
< |
assertEquals(j, ((Integer)i.next()).intValue()); |
164 |
> |
assertEquals(j, i.next()); |
165 |
|
assertEquals(3, j); |
166 |
|
} |
167 |
|
|
168 |
|
/** |
169 |
|
* iterator remove is unsupported |
170 |
|
*/ |
171 |
< |
public void testIteratorRemove () { |
171 |
> |
public void testIteratorRemove() { |
172 |
|
CopyOnWriteArraySet full = populatedSet(3); |
173 |
|
Iterator it = full.iterator(); |
174 |
|
it.next(); |
175 |
|
try { |
176 |
|
it.remove(); |
177 |
|
shouldThrow(); |
178 |
< |
} |
182 |
< |
catch (UnsupportedOperationException success) {} |
178 |
> |
} catch (UnsupportedOperationException success) {} |
179 |
|
} |
180 |
|
|
181 |
|
/** |
185 |
|
CopyOnWriteArraySet full = populatedSet(3); |
186 |
|
String s = full.toString(); |
187 |
|
for (int i = 0; i < 3; ++i) { |
188 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
188 |
> |
assertTrue(s.contains(String.valueOf(i))); |
189 |
|
} |
190 |
|
} |
191 |
|
|
196 |
– |
|
192 |
|
/** |
193 |
< |
* removeAll removes all elements from the given collection |
193 |
> |
* removeAll removes all elements from the given collection |
194 |
|
*/ |
195 |
|
public void testRemoveAll() { |
196 |
|
CopyOnWriteArraySet full = populatedSet(3); |
201 |
|
assertEquals(1, full.size()); |
202 |
|
} |
203 |
|
|
209 |
– |
|
204 |
|
/** |
205 |
|
* remove removes an element |
206 |
|
*/ |
212 |
|
} |
213 |
|
|
214 |
|
/** |
215 |
< |
* size returns the number of elements |
215 |
> |
* size returns the number of elements |
216 |
|
*/ |
217 |
|
public void testSize() { |
218 |
|
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
222 |
|
} |
223 |
|
|
224 |
|
/** |
225 |
< |
* toArray returns an Object array containing all elements from the set |
225 |
> |
* toArray returns an Object array containing all elements from the set |
226 |
|
*/ |
227 |
|
public void testToArray() { |
228 |
|
CopyOnWriteArraySet full = populatedSet(3); |
229 |
|
Object[] o = full.toArray(); |
230 |
|
assertEquals(3, o.length); |
231 |
< |
assertEquals(0, ((Integer)o[0]).intValue()); |
232 |
< |
assertEquals(1, ((Integer)o[1]).intValue()); |
233 |
< |
assertEquals(2, ((Integer)o[2]).intValue()); |
231 |
> |
assertEquals(0, o[0]); |
232 |
> |
assertEquals(1, o[1]); |
233 |
> |
assertEquals(2, o[2]); |
234 |
|
} |
235 |
|
|
236 |
|
/** |
237 |
< |
* toArray returns an Integer array containing all elements from |
238 |
< |
* the set |
237 |
> |
* toArray returns an Integer array containing all elements from |
238 |
> |
* the set |
239 |
|
*/ |
240 |
|
public void testToArray2() { |
241 |
|
CopyOnWriteArraySet full = populatedSet(3); |
242 |
|
Integer[] i = new Integer[3]; |
243 |
|
i = (Integer[])full.toArray(i); |
244 |
|
assertEquals(3, i.length); |
245 |
< |
assertEquals(0, i[0].intValue()); |
246 |
< |
assertEquals(1, i[1].intValue()); |
247 |
< |
assertEquals(2, i[2].intValue()); |
245 |
> |
assertEquals(0, (int) i[0]); |
246 |
> |
assertEquals(1, (int) i[1]); |
247 |
> |
assertEquals(2, (int) i[2]); |
248 |
|
} |
249 |
|
|
256 |
– |
|
250 |
|
/** |
251 |
< |
* toArray throws an ArrayStoreException when the given array can |
252 |
< |
* not store the objects inside the set |
251 |
> |
* toArray throws an ArrayStoreException when the given array can |
252 |
> |
* not store the objects inside the set |
253 |
|
*/ |
254 |
|
public void testToArray_ArrayStoreException() { |
255 |
|
try { |
258 |
|
c.add("asdadasd"); |
259 |
|
c.toArray(new Long[5]); |
260 |
|
shouldThrow(); |
261 |
< |
} catch (ArrayStoreException e) {} |
261 |
> |
} catch (ArrayStoreException success) {} |
262 |
|
} |
263 |
|
|
264 |
|
/** |
265 |
|
* A deserialized serialized set is equal |
266 |
|
*/ |
267 |
< |
public void testSerialization() { |
267 |
> |
public void testSerialization() throws Exception { |
268 |
|
CopyOnWriteArraySet q = populatedSet(SIZE); |
269 |
|
|
270 |
< |
try { |
271 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
272 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
273 |
< |
out.writeObject(q); |
274 |
< |
out.close(); |
275 |
< |
|
276 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
277 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
278 |
< |
CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject(); |
279 |
< |
assertEquals(q.size(), r.size()); |
280 |
< |
assertTrue(q.equals(r)); |
288 |
< |
assertTrue(r.equals(q)); |
289 |
< |
} catch (Exception e) { |
290 |
< |
unexpectedException(); |
291 |
< |
} |
270 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
271 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
272 |
> |
out.writeObject(q); |
273 |
> |
out.close(); |
274 |
> |
|
275 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
276 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
277 |
> |
CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject(); |
278 |
> |
assertEquals(q.size(), r.size()); |
279 |
> |
assertTrue(q.equals(r)); |
280 |
> |
assertTrue(r.equals(q)); |
281 |
|
} |
282 |
|
|
283 |
|
} |