1 |
dl |
1.1 |
/* |
2 |
dl |
1.6 |
* 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 |
jsr166 |
1.17 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.8 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.25 |
import java.util.ArrayList; |
10 |
jsr166 |
1.19 |
import java.util.Arrays; |
11 |
jsr166 |
1.22 |
import java.util.Collection; |
12 |
jsr166 |
1.21 |
import java.util.Collections; |
13 |
jsr166 |
1.19 |
import java.util.Iterator; |
14 |
jsr166 |
1.24 |
import java.util.NoSuchElementException; |
15 |
jsr166 |
1.19 |
import java.util.Set; |
16 |
|
|
import java.util.Vector; |
17 |
|
|
import java.util.concurrent.CopyOnWriteArraySet; |
18 |
dl |
1.1 |
|
19 |
jsr166 |
1.28 |
import junit.framework.Test; |
20 |
|
|
import junit.framework.TestSuite; |
21 |
|
|
|
22 |
dl |
1.3 |
public class CopyOnWriteArraySetTest extends JSR166TestCase { |
23 |
dl |
1.1 |
public static void main(String[] args) { |
24 |
jsr166 |
1.15 |
junit.textui.TestRunner.run(suite()); |
25 |
dl |
1.1 |
} |
26 |
|
|
public static Test suite() { |
27 |
jsr166 |
1.11 |
return new TestSuite(CopyOnWriteArraySetTest.class); |
28 |
dl |
1.1 |
} |
29 |
|
|
|
30 |
jsr166 |
1.21 |
static CopyOnWriteArraySet<Integer> populatedSet(int n) { |
31 |
|
|
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); |
32 |
dl |
1.1 |
assertTrue(a.isEmpty()); |
33 |
jsr166 |
1.22 |
for (int i = 0; i < n; i++) |
34 |
jsr166 |
1.21 |
a.add(i); |
35 |
dl |
1.1 |
assertFalse(a.isEmpty()); |
36 |
|
|
assertEquals(n, a.size()); |
37 |
|
|
return a; |
38 |
|
|
} |
39 |
|
|
|
40 |
jsr166 |
1.21 |
static CopyOnWriteArraySet populatedSet(Integer[] elements) { |
41 |
|
|
CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>(); |
42 |
|
|
assertTrue(a.isEmpty()); |
43 |
|
|
for (int i = 0; i < elements.length; i++) |
44 |
|
|
a.add(elements[i]); |
45 |
|
|
assertFalse(a.isEmpty()); |
46 |
|
|
assertEquals(elements.length, a.size()); |
47 |
|
|
return a; |
48 |
|
|
} |
49 |
|
|
|
50 |
dl |
1.4 |
/** |
51 |
|
|
* Default-constructed set is empty |
52 |
|
|
*/ |
53 |
dl |
1.3 |
public void testConstructor() { |
54 |
jsr166 |
1.11 |
CopyOnWriteArraySet a = new CopyOnWriteArraySet(); |
55 |
dl |
1.3 |
assertTrue(a.isEmpty()); |
56 |
|
|
} |
57 |
|
|
|
58 |
dl |
1.4 |
/** |
59 |
dl |
1.5 |
* Collection-constructed set holds all of its elements |
60 |
dl |
1.4 |
*/ |
61 |
dl |
1.3 |
public void testConstructor3() { |
62 |
|
|
Integer[] ints = new Integer[SIZE]; |
63 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
64 |
|
|
ints[i] = new Integer(i); |
65 |
jsr166 |
1.11 |
CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints)); |
66 |
jsr166 |
1.8 |
for (int i = 0; i < SIZE; ++i) |
67 |
dl |
1.3 |
assertTrue(a.contains(ints[i])); |
68 |
|
|
} |
69 |
jsr166 |
1.8 |
|
70 |
dl |
1.1 |
/** |
71 |
jsr166 |
1.16 |
* addAll adds each element from the given collection |
72 |
dl |
1.1 |
*/ |
73 |
dl |
1.4 |
public void testAddAll() { |
74 |
jsr166 |
1.11 |
CopyOnWriteArraySet full = populatedSet(3); |
75 |
|
|
Vector v = new Vector(); |
76 |
|
|
v.add(three); |
77 |
|
|
v.add(four); |
78 |
|
|
v.add(five); |
79 |
|
|
full.addAll(v); |
80 |
|
|
assertEquals(6, full.size()); |
81 |
dl |
1.1 |
} |
82 |
|
|
|
83 |
|
|
/** |
84 |
jsr166 |
1.16 |
* addAll adds each element from the given collection that did not |
85 |
|
|
* already exist in the set |
86 |
dl |
1.1 |
*/ |
87 |
dl |
1.4 |
public void testAddAll2() { |
88 |
jsr166 |
1.11 |
CopyOnWriteArraySet full = populatedSet(3); |
89 |
|
|
Vector v = new Vector(); |
90 |
|
|
v.add(three); |
91 |
|
|
v.add(four); |
92 |
|
|
v.add(one); // will not add this element |
93 |
|
|
full.addAll(v); |
94 |
|
|
assertEquals(5, full.size()); |
95 |
dl |
1.1 |
} |
96 |
|
|
|
97 |
|
|
/** |
98 |
jsr166 |
1.16 |
* add will not add the element if it already exists in the set |
99 |
dl |
1.1 |
*/ |
100 |
dl |
1.4 |
public void testAdd2() { |
101 |
jsr166 |
1.11 |
CopyOnWriteArraySet full = populatedSet(3); |
102 |
|
|
full.add(one); |
103 |
|
|
assertEquals(3, full.size()); |
104 |
dl |
1.1 |
} |
105 |
|
|
|
106 |
|
|
/** |
107 |
jsr166 |
1.16 |
* add adds the element when it does not exist in the set |
108 |
dl |
1.1 |
*/ |
109 |
dl |
1.4 |
public void testAdd3() { |
110 |
jsr166 |
1.11 |
CopyOnWriteArraySet full = populatedSet(3); |
111 |
dl |
1.3 |
full.add(three); |
112 |
|
|
assertTrue(full.contains(three)); |
113 |
dl |
1.1 |
} |
114 |
|
|
|
115 |
|
|
/** |
116 |
jsr166 |
1.16 |
* clear removes all elements from the set |
117 |
dl |
1.1 |
*/ |
118 |
dl |
1.4 |
public void testClear() { |
119 |
jsr166 |
1.11 |
CopyOnWriteArraySet full = populatedSet(3); |
120 |
|
|
full.clear(); |
121 |
|
|
assertEquals(0, full.size()); |
122 |
dl |
1.1 |
} |
123 |
|
|
|
124 |
|
|
/** |
125 |
jsr166 |
1.16 |
* contains returns true for added elements |
126 |
dl |
1.1 |
*/ |
127 |
dl |
1.4 |
public void testContains() { |
128 |
jsr166 |
1.11 |
CopyOnWriteArraySet full = populatedSet(3); |
129 |
|
|
assertTrue(full.contains(one)); |
130 |
|
|
assertFalse(full.contains(five)); |
131 |
dl |
1.1 |
} |
132 |
|
|
|
133 |
dl |
1.4 |
/** |
134 |
|
|
* Sets with equal elements are equal |
135 |
|
|
*/ |
136 |
dl |
1.1 |
public void testEquals() { |
137 |
jsr166 |
1.11 |
CopyOnWriteArraySet a = populatedSet(3); |
138 |
|
|
CopyOnWriteArraySet b = populatedSet(3); |
139 |
dl |
1.1 |
assertTrue(a.equals(b)); |
140 |
|
|
assertTrue(b.equals(a)); |
141 |
|
|
assertEquals(a.hashCode(), b.hashCode()); |
142 |
dl |
1.3 |
a.add(m1); |
143 |
dl |
1.1 |
assertFalse(a.equals(b)); |
144 |
|
|
assertFalse(b.equals(a)); |
145 |
dl |
1.3 |
b.add(m1); |
146 |
dl |
1.1 |
assertTrue(a.equals(b)); |
147 |
|
|
assertTrue(b.equals(a)); |
148 |
|
|
assertEquals(a.hashCode(), b.hashCode()); |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
/** |
152 |
jsr166 |
1.16 |
* containsAll returns true for collections with subset of elements |
153 |
dl |
1.1 |
*/ |
154 |
dl |
1.4 |
public void testContainsAll() { |
155 |
jsr166 |
1.11 |
CopyOnWriteArraySet full = populatedSet(3); |
156 |
|
|
Vector v = new Vector(); |
157 |
|
|
v.add(one); |
158 |
|
|
v.add(two); |
159 |
|
|
assertTrue(full.containsAll(v)); |
160 |
|
|
v.add(six); |
161 |
|
|
assertFalse(full.containsAll(v)); |
162 |
dl |
1.1 |
} |
163 |
|
|
|
164 |
|
|
/** |
165 |
jsr166 |
1.16 |
* isEmpty is true when empty, else false |
166 |
dl |
1.1 |
*/ |
167 |
dl |
1.4 |
public void testIsEmpty() { |
168 |
jsr166 |
1.11 |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
169 |
|
|
CopyOnWriteArraySet full = populatedSet(3); |
170 |
|
|
assertTrue(empty.isEmpty()); |
171 |
|
|
assertFalse(full.isEmpty()); |
172 |
dl |
1.1 |
} |
173 |
|
|
|
174 |
|
|
/** |
175 |
jsr166 |
1.24 |
* iterator() returns an iterator containing the elements of the |
176 |
|
|
* set in insertion order |
177 |
dl |
1.1 |
*/ |
178 |
dl |
1.4 |
public void testIterator() { |
179 |
jsr166 |
1.25 |
Collection empty = new CopyOnWriteArraySet(); |
180 |
jsr166 |
1.24 |
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 |
|
|
assertFalse(it.hasNext()); |
198 |
|
|
try { |
199 |
|
|
it.next(); |
200 |
|
|
shouldThrow(); |
201 |
|
|
} catch (NoSuchElementException success) {} |
202 |
dl |
1.1 |
} |
203 |
|
|
|
204 |
dl |
1.4 |
/** |
205 |
|
|
* iterator remove is unsupported |
206 |
|
|
*/ |
207 |
jsr166 |
1.15 |
public void testIteratorRemove() { |
208 |
jsr166 |
1.11 |
CopyOnWriteArraySet full = populatedSet(3); |
209 |
dl |
1.1 |
Iterator it = full.iterator(); |
210 |
|
|
it.next(); |
211 |
|
|
try { |
212 |
|
|
it.remove(); |
213 |
dl |
1.4 |
shouldThrow(); |
214 |
jsr166 |
1.12 |
} catch (UnsupportedOperationException success) {} |
215 |
dl |
1.1 |
} |
216 |
|
|
|
217 |
dl |
1.4 |
/** |
218 |
|
|
* toString holds toString of elements |
219 |
|
|
*/ |
220 |
|
|
public void testToString() { |
221 |
jsr166 |
1.25 |
assertEquals("[]", new CopyOnWriteArraySet().toString()); |
222 |
jsr166 |
1.11 |
CopyOnWriteArraySet full = populatedSet(3); |
223 |
dl |
1.1 |
String s = full.toString(); |
224 |
jsr166 |
1.25 |
for (int i = 0; i < 3; ++i) |
225 |
jsr166 |
1.18 |
assertTrue(s.contains(String.valueOf(i))); |
226 |
jsr166 |
1.25 |
assertEquals(new ArrayList(full).toString(), |
227 |
|
|
full.toString()); |
228 |
jsr166 |
1.8 |
} |
229 |
dl |
1.1 |
|
230 |
|
|
/** |
231 |
jsr166 |
1.16 |
* removeAll removes all elements from the given collection |
232 |
dl |
1.1 |
*/ |
233 |
dl |
1.4 |
public void testRemoveAll() { |
234 |
jsr166 |
1.11 |
CopyOnWriteArraySet full = populatedSet(3); |
235 |
|
|
Vector v = new Vector(); |
236 |
|
|
v.add(one); |
237 |
|
|
v.add(two); |
238 |
|
|
full.removeAll(v); |
239 |
|
|
assertEquals(1, full.size()); |
240 |
dl |
1.1 |
} |
241 |
|
|
|
242 |
dl |
1.4 |
/** |
243 |
|
|
* remove removes an element |
244 |
|
|
*/ |
245 |
|
|
public void testRemove() { |
246 |
jsr166 |
1.11 |
CopyOnWriteArraySet full = populatedSet(3); |
247 |
|
|
full.remove(one); |
248 |
dl |
1.3 |
assertFalse(full.contains(one)); |
249 |
jsr166 |
1.11 |
assertEquals(2, full.size()); |
250 |
dl |
1.1 |
} |
251 |
|
|
|
252 |
|
|
/** |
253 |
jsr166 |
1.16 |
* size returns the number of elements |
254 |
dl |
1.1 |
*/ |
255 |
dl |
1.4 |
public void testSize() { |
256 |
jsr166 |
1.11 |
CopyOnWriteArraySet empty = new CopyOnWriteArraySet(); |
257 |
|
|
CopyOnWriteArraySet full = populatedSet(3); |
258 |
|
|
assertEquals(3, full.size()); |
259 |
|
|
assertEquals(0, empty.size()); |
260 |
dl |
1.1 |
} |
261 |
|
|
|
262 |
|
|
/** |
263 |
jsr166 |
1.21 |
* toArray() returns an Object array containing all elements from |
264 |
|
|
* the set in insertion order |
265 |
dl |
1.1 |
*/ |
266 |
dl |
1.4 |
public void testToArray() { |
267 |
jsr166 |
1.21 |
Object[] a = new CopyOnWriteArraySet().toArray(); |
268 |
|
|
assertTrue(Arrays.equals(new Object[0], a)); |
269 |
|
|
assertSame(Object[].class, a.getClass()); |
270 |
|
|
|
271 |
|
|
Integer[] elements = new Integer[SIZE]; |
272 |
jsr166 |
1.20 |
for (int i = 0; i < SIZE; i++) |
273 |
jsr166 |
1.21 |
elements[i] = i; |
274 |
|
|
Collections.shuffle(Arrays.asList(elements)); |
275 |
jsr166 |
1.22 |
Collection<Integer> full = populatedSet(elements); |
276 |
jsr166 |
1.21 |
|
277 |
|
|
assertTrue(Arrays.equals(elements, full.toArray())); |
278 |
|
|
assertSame(Object[].class, full.toArray().getClass()); |
279 |
dl |
1.1 |
} |
280 |
|
|
|
281 |
|
|
/** |
282 |
jsr166 |
1.21 |
* toArray(Integer array) returns an Integer array containing all |
283 |
|
|
* elements from the set in insertion order |
284 |
dl |
1.1 |
*/ |
285 |
dl |
1.4 |
public void testToArray2() { |
286 |
jsr166 |
1.22 |
Collection empty = new CopyOnWriteArraySet(); |
287 |
jsr166 |
1.21 |
Integer[] a; |
288 |
|
|
|
289 |
|
|
a = new Integer[0]; |
290 |
|
|
assertSame(a, empty.toArray(a)); |
291 |
|
|
|
292 |
|
|
a = new Integer[SIZE/2]; |
293 |
|
|
Arrays.fill(a, 42); |
294 |
|
|
assertSame(a, empty.toArray(a)); |
295 |
|
|
assertNull(a[0]); |
296 |
|
|
for (int i = 1; i < a.length; i++) |
297 |
|
|
assertEquals(42, (int) a[i]); |
298 |
|
|
|
299 |
|
|
Integer[] elements = new Integer[SIZE]; |
300 |
jsr166 |
1.20 |
for (int i = 0; i < SIZE; i++) |
301 |
jsr166 |
1.21 |
elements[i] = i; |
302 |
|
|
Collections.shuffle(Arrays.asList(elements)); |
303 |
jsr166 |
1.22 |
Collection<Integer> full = populatedSet(elements); |
304 |
jsr166 |
1.21 |
|
305 |
|
|
Arrays.fill(a, 42); |
306 |
|
|
assertTrue(Arrays.equals(elements, full.toArray(a))); |
307 |
|
|
for (int i = 0; i < a.length; i++) |
308 |
|
|
assertEquals(42, (int) a[i]); |
309 |
|
|
assertSame(Integer[].class, full.toArray(a).getClass()); |
310 |
|
|
|
311 |
|
|
a = new Integer[SIZE]; |
312 |
|
|
Arrays.fill(a, 42); |
313 |
|
|
assertSame(a, full.toArray(a)); |
314 |
|
|
assertTrue(Arrays.equals(elements, a)); |
315 |
|
|
|
316 |
|
|
a = new Integer[2*SIZE]; |
317 |
|
|
Arrays.fill(a, 42); |
318 |
|
|
assertSame(a, full.toArray(a)); |
319 |
|
|
assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE))); |
320 |
|
|
assertNull(a[SIZE]); |
321 |
|
|
for (int i = SIZE + 1; i < a.length; i++) |
322 |
|
|
assertEquals(42, (int) a[i]); |
323 |
dl |
1.1 |
} |
324 |
|
|
|
325 |
|
|
/** |
326 |
jsr166 |
1.16 |
* toArray throws an ArrayStoreException when the given array can |
327 |
|
|
* not store the objects inside the set |
328 |
dl |
1.1 |
*/ |
329 |
dl |
1.4 |
public void testToArray_ArrayStoreException() { |
330 |
|
|
try { |
331 |
dl |
1.1 |
CopyOnWriteArraySet c = new CopyOnWriteArraySet(); |
332 |
|
|
c.add("zfasdfsdf"); |
333 |
|
|
c.add("asdadasd"); |
334 |
|
|
c.toArray(new Long[5]); |
335 |
jsr166 |
1.11 |
shouldThrow(); |
336 |
jsr166 |
1.13 |
} catch (ArrayStoreException success) {} |
337 |
dl |
1.2 |
} |
338 |
|
|
|
339 |
dl |
1.4 |
/** |
340 |
dl |
1.7 |
* A deserialized serialized set is equal |
341 |
dl |
1.4 |
*/ |
342 |
jsr166 |
1.12 |
public void testSerialization() throws Exception { |
343 |
jsr166 |
1.19 |
Set x = populatedSet(SIZE); |
344 |
|
|
Set y = serialClone(x); |
345 |
dl |
1.2 |
|
346 |
jsr166 |
1.27 |
assertNotSame(y, x); |
347 |
jsr166 |
1.19 |
assertEquals(x.size(), y.size()); |
348 |
jsr166 |
1.23 |
assertEquals(x.toString(), y.toString()); |
349 |
|
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
350 |
jsr166 |
1.19 |
assertEquals(x, y); |
351 |
|
|
assertEquals(y, x); |
352 |
dl |
1.1 |
} |
353 |
|
|
|
354 |
jsr166 |
1.26 |
/** |
355 |
|
|
* addAll is idempotent |
356 |
|
|
*/ |
357 |
|
|
public void testAddAll_idempotent() throws Exception { |
358 |
|
|
Set x = populatedSet(SIZE); |
359 |
|
|
Set y = new CopyOnWriteArraySet(x); |
360 |
|
|
y.addAll(x); |
361 |
|
|
assertEquals(x, y); |
362 |
|
|
assertEquals(y, x); |
363 |
|
|
} |
364 |
|
|
|
365 |
dl |
1.1 |
} |