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

# 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.Iterator;
13 import java.util.NoSuchElementException;
14 import java.util.Set;
15 import java.util.concurrent.CopyOnWriteArraySet;
16
17 import junit.framework.Test;
18 import junit.framework.TestSuite;
19
20 public class CopyOnWriteArraySetTest extends JSR166TestCase {
21 public static void main(String[] args) {
22 main(suite(), args);
23 }
24 public static Test suite() {
25 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 }
36
37 static CopyOnWriteArraySet<Integer> populatedSet(int n) {
38 CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<>();
39 assertTrue(a.isEmpty());
40 for (int i = 0; i < n; i++)
41 a.add(i);
42 assertEquals(n == 0, a.isEmpty());
43 assertEquals(n, a.size());
44 return a;
45 }
46
47 static CopyOnWriteArraySet populatedSet(Integer[] elements) {
48 CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<>();
49 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 /**
58 * Default-constructed set is empty
59 */
60 public void testConstructor() {
61 CopyOnWriteArraySet a = new CopyOnWriteArraySet();
62 assertTrue(a.isEmpty());
63 }
64
65 /**
66 * Collection-constructed set holds all of its elements
67 */
68 public void testConstructor3() {
69 Integer[] ints = new Integer[SIZE];
70 for (int i = 0; i < SIZE - 1; ++i)
71 ints[i] = new Integer(i);
72 CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
73 for (int i = 0; i < SIZE; ++i)
74 assertTrue(a.contains(ints[i]));
75 }
76
77 /**
78 * addAll adds each non-duplicate element from the given collection
79 */
80 public void testAddAll() {
81 Set full = populatedSet(3);
82 assertTrue(full.addAll(Arrays.asList(three, four, five)));
83 assertEquals(6, full.size());
84 assertFalse(full.addAll(Arrays.asList(three, four, five)));
85 assertEquals(6, full.size());
86 }
87
88 /**
89 * addAll adds each non-duplicate element from the given collection
90 */
91 public void testAddAll2() {
92 Set full = populatedSet(3);
93 // "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 assertEquals(5, full.size());
98 }
99
100 /**
101 * add will not add the element if it already exists in the set
102 */
103 public void testAdd2() {
104 Set full = populatedSet(3);
105 full.add(one);
106 assertEquals(3, full.size());
107 }
108
109 /**
110 * add adds the element when it does not exist in the set
111 */
112 public void testAdd3() {
113 Set full = populatedSet(3);
114 full.add(three);
115 assertTrue(full.contains(three));
116 }
117
118 /**
119 * clear removes all elements from the set
120 */
121 public void testClear() {
122 Collection full = populatedSet(3);
123 full.clear();
124 assertEquals(0, full.size());
125 assertTrue(full.isEmpty());
126 }
127
128 /**
129 * contains returns true for added elements
130 */
131 public void testContains() {
132 Collection full = populatedSet(3);
133 assertTrue(full.contains(one));
134 assertFalse(full.contains(five));
135 }
136
137 /**
138 * Sets with equal elements are equal
139 */
140 public void testEquals() {
141 CopyOnWriteArraySet a = populatedSet(3);
142 CopyOnWriteArraySet b = populatedSet(3);
143 assertTrue(a.equals(b));
144 assertTrue(b.equals(a));
145 assertTrue(a.containsAll(b));
146 assertTrue(b.containsAll(a));
147 assertEquals(a.hashCode(), b.hashCode());
148 assertEquals(a.size(), b.size());
149
150 a.add(m1);
151 assertFalse(a.equals(b));
152 assertFalse(b.equals(a));
153 assertTrue(a.containsAll(b));
154 assertFalse(b.containsAll(a));
155 b.add(m1);
156 assertTrue(a.equals(b));
157 assertTrue(b.equals(a));
158 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 assertEquals(a.hashCode(), b.hashCode());
174 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 }
186
187 /**
188 * containsAll returns true for collections with subset of elements
189 */
190 public void testContainsAll() {
191 Collection full = populatedSet(3);
192 assertTrue(full.containsAll(full));
193 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
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 }
211
212 /**
213 * isEmpty is true when empty, else false
214 */
215 public void testIsEmpty() {
216 assertTrue(populatedSet(0).isEmpty());
217 assertFalse(populatedSet(3).isEmpty());
218 }
219
220 /**
221 * iterator() returns an iterator containing the elements of the
222 * set in insertion order
223 */
224 public void testIterator() {
225 Collection empty = new CopyOnWriteArraySet();
226 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 shuffle(elements);
236 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 assertIteratorExhausted(it);
244 }
245
246 /**
247 * iterator of empty collection has no elements
248 */
249 public void testEmptyIterator() {
250 assertIteratorExhausted(new CopyOnWriteArraySet().iterator());
251 }
252
253 /**
254 * iterator remove is unsupported
255 */
256 public void testIteratorRemove() {
257 Collection full = populatedSet(3);
258 Iterator it = full.iterator();
259 it.next();
260 try {
261 it.remove();
262 shouldThrow();
263 } catch (UnsupportedOperationException success) {}
264 }
265
266 /**
267 * toString holds toString of elements
268 */
269 public void testToString() {
270 assertEquals("[]", new CopyOnWriteArraySet().toString());
271 Collection full = populatedSet(3);
272 String s = full.toString();
273 for (int i = 0; i < 3; ++i)
274 assertTrue(s.contains(String.valueOf(i)));
275 assertEquals(new ArrayList(full).toString(),
276 full.toString());
277 }
278
279 /**
280 * removeAll removes all elements from the given collection
281 */
282 public void testRemoveAll() {
283 Set full = populatedSet(3);
284 assertTrue(full.removeAll(Arrays.asList(one, two)));
285 assertEquals(1, full.size());
286 assertFalse(full.removeAll(Arrays.asList(one, two)));
287 assertEquals(1, full.size());
288 }
289
290 /**
291 * remove removes an element
292 */
293 public void testRemove() {
294 Collection full = populatedSet(3);
295 full.remove(one);
296 assertFalse(full.contains(one));
297 assertEquals(2, full.size());
298 }
299
300 /**
301 * size returns the number of elements
302 */
303 public void testSize() {
304 Collection empty = new CopyOnWriteArraySet();
305 Collection full = populatedSet(3);
306 assertEquals(3, full.size());
307 assertEquals(0, empty.size());
308 }
309
310 /**
311 * toArray() returns an Object array containing all elements from
312 * the set in insertion order
313 */
314 public void testToArray() {
315 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 for (int i = 0; i < SIZE; i++)
321 elements[i] = i;
322 shuffle(elements);
323 Collection<Integer> full = populatedSet(elements);
324
325 assertTrue(Arrays.equals(elements, full.toArray()));
326 assertSame(Object[].class, full.toArray().getClass());
327 }
328
329 /**
330 * toArray(Integer array) returns an Integer array containing all
331 * elements from the set in insertion order
332 */
333 public void testToArray2() {
334 Collection empty = new CopyOnWriteArraySet();
335 Integer[] a;
336
337 a = new Integer[0];
338 assertSame(a, empty.toArray(a));
339
340 a = new Integer[SIZE / 2];
341 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 for (int i = 0; i < SIZE; i++)
349 elements[i] = i;
350 shuffle(elements);
351 Collection<Integer> full = populatedSet(elements);
352
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 a = new Integer[2 * SIZE];
365 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 }
372
373 /**
374 * toArray throws an ArrayStoreException when the given array can
375 * not store the objects inside the set
376 */
377 public void testToArray_ArrayStoreException() {
378 CopyOnWriteArraySet c = new CopyOnWriteArraySet();
379 c.add("zfasdfsdf");
380 c.add("asdadasd");
381 try {
382 c.toArray(new Long[5]);
383 shouldThrow();
384 } catch (ArrayStoreException success) {}
385 }
386
387 /**
388 * A deserialized/reserialized set equals original
389 */
390 public void testSerialization() throws Exception {
391 Set x = populatedSet(SIZE);
392 Set y = serialClone(x);
393
394 assertNotSame(y, x);
395 assertEquals(x.size(), y.size());
396 assertEquals(x.toString(), y.toString());
397 assertTrue(Arrays.equals(x.toArray(), y.toArray()));
398 assertEquals(x, y);
399 assertEquals(y, x);
400 }
401
402 /**
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 }