ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
Revision: 1.44
Committed: Wed Jan 27 02:55:18 2021 UTC (3 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.43: +1 -0 lines
Log Message:
Suppress all new errorprone "errors"

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