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

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