ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
Revision: 1.34
Committed: Sat May 23 00:53:08 2015 UTC (8 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.33: +3 -3 lines
Log Message:
whitespace

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