ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
Revision: 1.37
Committed: Wed Aug 24 22:22:39 2016 UTC (7 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.36: +0 -1 lines
Log Message:
fix imports

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.11 return new TestSuite(CopyOnWriteArraySetTest.class);
26 dl 1.1 }
27    
28 jsr166 1.21 static CopyOnWriteArraySet<Integer> populatedSet(int n) {
29     CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
30 dl 1.1 assertTrue(a.isEmpty());
31 jsr166 1.22 for (int i = 0; i < n; i++)
32 jsr166 1.21 a.add(i);
33 jsr166 1.31 assertEquals(n == 0, a.isEmpty());
34 dl 1.1 assertEquals(n, a.size());
35     return a;
36     }
37    
38 jsr166 1.21 static CopyOnWriteArraySet populatedSet(Integer[] elements) {
39     CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
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 dl 1.4 /**
49     * Default-constructed set is empty
50     */
51 dl 1.3 public void testConstructor() {
52 jsr166 1.11 CopyOnWriteArraySet a = new CopyOnWriteArraySet();
53 dl 1.3 assertTrue(a.isEmpty());
54     }
55    
56 dl 1.4 /**
57 dl 1.5 * Collection-constructed set holds all of its elements
58 dl 1.4 */
59 dl 1.3 public void testConstructor3() {
60     Integer[] ints = new Integer[SIZE];
61 jsr166 1.34 for (int i = 0; i < SIZE - 1; ++i)
62 dl 1.3 ints[i] = new Integer(i);
63 jsr166 1.11 CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
64 jsr166 1.8 for (int i = 0; i < SIZE; ++i)
65 dl 1.3 assertTrue(a.contains(ints[i]));
66     }
67 jsr166 1.8
68 dl 1.1 /**
69 jsr166 1.31 * addAll adds each non-duplicate element from the given collection
70 dl 1.1 */
71 dl 1.4 public void testAddAll() {
72 jsr166 1.31 Set full = populatedSet(3);
73 jsr166 1.30 assertTrue(full.addAll(Arrays.asList(three, four, five)));
74     assertEquals(6, full.size());
75     assertFalse(full.addAll(Arrays.asList(three, four, five)));
76 jsr166 1.11 assertEquals(6, full.size());
77 dl 1.1 }
78    
79     /**
80 jsr166 1.31 * addAll adds each non-duplicate element from the given collection
81 dl 1.1 */
82 dl 1.4 public void testAddAll2() {
83 jsr166 1.31 Set full = populatedSet(3);
84 jsr166 1.30 // "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 jsr166 1.11 assertEquals(5, full.size());
89 dl 1.1 }
90    
91     /**
92 jsr166 1.16 * add will not add the element if it already exists in the set
93 dl 1.1 */
94 dl 1.4 public void testAdd2() {
95 jsr166 1.31 Set full = populatedSet(3);
96 jsr166 1.11 full.add(one);
97     assertEquals(3, full.size());
98 dl 1.1 }
99    
100     /**
101 jsr166 1.16 * add adds the element when it does not exist in the set
102 dl 1.1 */
103 dl 1.4 public void testAdd3() {
104 jsr166 1.31 Set full = populatedSet(3);
105 dl 1.3 full.add(three);
106     assertTrue(full.contains(three));
107 dl 1.1 }
108    
109     /**
110 jsr166 1.16 * clear removes all elements from the set
111 dl 1.1 */
112 dl 1.4 public void testClear() {
113 jsr166 1.31 Collection full = populatedSet(3);
114 jsr166 1.11 full.clear();
115     assertEquals(0, full.size());
116 jsr166 1.31 assertTrue(full.isEmpty());
117 dl 1.1 }
118    
119     /**
120 jsr166 1.16 * contains returns true for added elements
121 dl 1.1 */
122 dl 1.4 public void testContains() {
123 jsr166 1.31 Collection full = populatedSet(3);
124 jsr166 1.11 assertTrue(full.contains(one));
125     assertFalse(full.contains(five));
126 dl 1.1 }
127    
128 dl 1.4 /**
129     * Sets with equal elements are equal
130     */
131 dl 1.1 public void testEquals() {
132 jsr166 1.11 CopyOnWriteArraySet a = populatedSet(3);
133     CopyOnWriteArraySet b = populatedSet(3);
134 dl 1.1 assertTrue(a.equals(b));
135     assertTrue(b.equals(a));
136 jsr166 1.35 assertTrue(a.containsAll(b));
137     assertTrue(b.containsAll(a));
138 dl 1.1 assertEquals(a.hashCode(), b.hashCode());
139 jsr166 1.35 assertEquals(a.size(), b.size());
140    
141 dl 1.3 a.add(m1);
142 dl 1.1 assertFalse(a.equals(b));
143     assertFalse(b.equals(a));
144 jsr166 1.35 assertTrue(a.containsAll(b));
145     assertFalse(b.containsAll(a));
146 dl 1.3 b.add(m1);
147 dl 1.1 assertTrue(a.equals(b));
148     assertTrue(b.equals(a));
149 jsr166 1.35 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 dl 1.1 assertEquals(a.hashCode(), b.hashCode());
165 jsr166 1.35 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 dl 1.1 }
177    
178     /**
179 jsr166 1.16 * containsAll returns true for collections with subset of elements
180 dl 1.1 */
181 dl 1.4 public void testContainsAll() {
182 jsr166 1.31 Collection full = populatedSet(3);
183 jsr166 1.35 assertTrue(full.containsAll(full));
184 jsr166 1.30 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 jsr166 1.35
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 dl 1.1 }
202    
203     /**
204 jsr166 1.16 * isEmpty is true when empty, else false
205 dl 1.1 */
206 dl 1.4 public void testIsEmpty() {
207 jsr166 1.31 assertTrue(populatedSet(0).isEmpty());
208     assertFalse(populatedSet(3).isEmpty());
209 dl 1.1 }
210    
211     /**
212 jsr166 1.24 * iterator() returns an iterator containing the elements of the
213     * set in insertion order
214 dl 1.1 */
215 dl 1.4 public void testIterator() {
216 jsr166 1.25 Collection empty = new CopyOnWriteArraySet();
217 jsr166 1.24 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 jsr166 1.36 shuffle(elements);
227 jsr166 1.24 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 jsr166 1.29 assertIteratorExhausted(it);
235     }
236    
237     /**
238     * iterator of empty collection has no elements
239     */
240     public void testEmptyIterator() {
241     assertIteratorExhausted(new CopyOnWriteArraySet().iterator());
242 dl 1.1 }
243    
244 dl 1.4 /**
245     * iterator remove is unsupported
246     */
247 jsr166 1.15 public void testIteratorRemove() {
248 jsr166 1.31 Collection full = populatedSet(3);
249 dl 1.1 Iterator it = full.iterator();
250     it.next();
251     try {
252     it.remove();
253 dl 1.4 shouldThrow();
254 jsr166 1.12 } catch (UnsupportedOperationException success) {}
255 dl 1.1 }
256    
257 dl 1.4 /**
258     * toString holds toString of elements
259     */
260     public void testToString() {
261 jsr166 1.25 assertEquals("[]", new CopyOnWriteArraySet().toString());
262 jsr166 1.31 Collection full = populatedSet(3);
263 dl 1.1 String s = full.toString();
264 jsr166 1.25 for (int i = 0; i < 3; ++i)
265 jsr166 1.18 assertTrue(s.contains(String.valueOf(i)));
266 jsr166 1.25 assertEquals(new ArrayList(full).toString(),
267     full.toString());
268 jsr166 1.8 }
269 dl 1.1
270     /**
271 jsr166 1.16 * removeAll removes all elements from the given collection
272 dl 1.1 */
273 dl 1.4 public void testRemoveAll() {
274 jsr166 1.31 Set full = populatedSet(3);
275 jsr166 1.30 assertTrue(full.removeAll(Arrays.asList(one, two)));
276     assertEquals(1, full.size());
277     assertFalse(full.removeAll(Arrays.asList(one, two)));
278 jsr166 1.11 assertEquals(1, full.size());
279 dl 1.1 }
280    
281 dl 1.4 /**
282     * remove removes an element
283     */
284     public void testRemove() {
285 jsr166 1.31 Collection full = populatedSet(3);
286 jsr166 1.11 full.remove(one);
287 dl 1.3 assertFalse(full.contains(one));
288 jsr166 1.11 assertEquals(2, full.size());
289 dl 1.1 }
290    
291     /**
292 jsr166 1.16 * size returns the number of elements
293 dl 1.1 */
294 dl 1.4 public void testSize() {
295 jsr166 1.31 Collection empty = new CopyOnWriteArraySet();
296     Collection full = populatedSet(3);
297 jsr166 1.11 assertEquals(3, full.size());
298     assertEquals(0, empty.size());
299 dl 1.1 }
300    
301     /**
302 jsr166 1.21 * toArray() returns an Object array containing all elements from
303     * the set in insertion order
304 dl 1.1 */
305 dl 1.4 public void testToArray() {
306 jsr166 1.21 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 jsr166 1.20 for (int i = 0; i < SIZE; i++)
312 jsr166 1.21 elements[i] = i;
313 jsr166 1.36 shuffle(elements);
314 jsr166 1.22 Collection<Integer> full = populatedSet(elements);
315 jsr166 1.21
316     assertTrue(Arrays.equals(elements, full.toArray()));
317     assertSame(Object[].class, full.toArray().getClass());
318 dl 1.1 }
319    
320     /**
321 jsr166 1.21 * toArray(Integer array) returns an Integer array containing all
322     * elements from the set in insertion order
323 dl 1.1 */
324 dl 1.4 public void testToArray2() {
325 jsr166 1.22 Collection empty = new CopyOnWriteArraySet();
326 jsr166 1.21 Integer[] a;
327    
328     a = new Integer[0];
329     assertSame(a, empty.toArray(a));
330    
331 jsr166 1.34 a = new Integer[SIZE / 2];
332 jsr166 1.21 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 jsr166 1.20 for (int i = 0; i < SIZE; i++)
340 jsr166 1.21 elements[i] = i;
341 jsr166 1.36 shuffle(elements);
342 jsr166 1.22 Collection<Integer> full = populatedSet(elements);
343 jsr166 1.21
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 jsr166 1.34 a = new Integer[2 * SIZE];
356 jsr166 1.21 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 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 dl 1.4 public void testToArray_ArrayStoreException() {
369 jsr166 1.33 CopyOnWriteArraySet c = new CopyOnWriteArraySet();
370     c.add("zfasdfsdf");
371     c.add("asdadasd");
372 dl 1.4 try {
373 dl 1.1 c.toArray(new Long[5]);
374 jsr166 1.11 shouldThrow();
375 jsr166 1.13 } catch (ArrayStoreException success) {}
376 dl 1.2 }
377    
378 dl 1.4 /**
379 dl 1.7 * A deserialized serialized set is equal
380 dl 1.4 */
381 jsr166 1.12 public void testSerialization() throws Exception {
382 jsr166 1.19 Set x = populatedSet(SIZE);
383     Set y = serialClone(x);
384 dl 1.2
385 jsr166 1.27 assertNotSame(y, x);
386 jsr166 1.19 assertEquals(x.size(), y.size());
387 jsr166 1.23 assertEquals(x.toString(), y.toString());
388     assertTrue(Arrays.equals(x.toArray(), y.toArray()));
389 jsr166 1.19 assertEquals(x, y);
390     assertEquals(y, x);
391 dl 1.1 }
392    
393 jsr166 1.26 /**
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 dl 1.1 }