ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
Revision: 1.39
Committed: Fri Aug 4 03:30:21 2017 UTC (6 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.38: +1 -1 lines
Log Message:
improve javadoc wording for testSerialization methods

File Contents

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