ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
Revision: 1.41
Committed: Tue May 8 00:25:33 2018 UTC (6 years ago) by jsr166
Branch: MAIN
Changes since 1.40: +0 -1 lines
Log Message:
remove unused import

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