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