ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
Revision: 1.40
Committed: Thu Apr 5 02:20:21 2018 UTC (6 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.39: +10 -1 lines
Log Message:
test CopyOnWriteArraySet with CollectionTest infrastrure

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