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