ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
Revision: 1.30
Committed: Fri Feb 27 19:28:23 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.29: +15 -21 lines
Log Message:
replace Vector with Arrays.asList; add more assertions

File Contents

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