blob: 2810802325ca3165d2f3966355d7a61a695cdddf [file] [log] [blame]
Calin Juravle8f0d92b2013-08-01 17:26:00 +01001/*
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
9package jsr166;
10
Calin Juravle8f0d92b2013-08-01 17:26:00 +010011import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Collection;
14import java.util.Collections;
15import java.util.Iterator;
16import java.util.NoSuchElementException;
17import java.util.Set;
Calin Juravle8f0d92b2013-08-01 17:26:00 +010018import java.util.concurrent.CopyOnWriteArraySet;
19
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010020import junit.framework.Test;
21import junit.framework.TestSuite;
22
Calin Juravle8f0d92b2013-08-01 17:26:00 +010023public class CopyOnWriteArraySetTest extends JSR166TestCase {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010024 // android-note: Removed because the CTS runner does a bad job of
25 // retrying tests that have suite() declarations.
26 //
27 // public static void main(String[] args) {
28 // main(suite(), args);
29 // }
30 // public static Test suite() {
31 // return new TestSuite(...);
32 // }
Calin Juravle8f0d92b2013-08-01 17:26:00 +010033
34 static CopyOnWriteArraySet<Integer> populatedSet(int n) {
35 CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
36 assertTrue(a.isEmpty());
37 for (int i = 0; i < n; i++)
38 a.add(i);
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010039 assertEquals(n == 0, a.isEmpty());
Calin Juravle8f0d92b2013-08-01 17:26:00 +010040 assertEquals(n, a.size());
41 return a;
42 }
43
44 static CopyOnWriteArraySet populatedSet(Integer[] elements) {
45 CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
46 assertTrue(a.isEmpty());
47 for (int i = 0; i < elements.length; i++)
48 a.add(elements[i]);
49 assertFalse(a.isEmpty());
50 assertEquals(elements.length, a.size());
51 return a;
52 }
53
54 /**
55 * Default-constructed set is empty
56 */
57 public void testConstructor() {
58 CopyOnWriteArraySet a = new CopyOnWriteArraySet();
59 assertTrue(a.isEmpty());
60 }
61
62 /**
63 * Collection-constructed set holds all of its elements
64 */
65 public void testConstructor3() {
66 Integer[] ints = new Integer[SIZE];
67 for (int i = 0; i < SIZE-1; ++i)
68 ints[i] = new Integer(i);
69 CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
70 for (int i = 0; i < SIZE; ++i)
71 assertTrue(a.contains(ints[i]));
72 }
73
74 /**
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010075 * addAll adds each non-duplicate element from the given collection
Calin Juravle8f0d92b2013-08-01 17:26:00 +010076 */
77 public void testAddAll() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010078 Set full = populatedSet(3);
79 assertTrue(full.addAll(Arrays.asList(three, four, five)));
80 assertEquals(6, full.size());
81 assertFalse(full.addAll(Arrays.asList(three, four, five)));
Calin Juravle8f0d92b2013-08-01 17:26:00 +010082 assertEquals(6, full.size());
83 }
84
85 /**
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010086 * addAll adds each non-duplicate element from the given collection
Calin Juravle8f0d92b2013-08-01 17:26:00 +010087 */
88 public void testAddAll2() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +010089 Set full = populatedSet(3);
90 // "one" is duplicate and will not be added
91 assertTrue(full.addAll(Arrays.asList(three, four, one)));
92 assertEquals(5, full.size());
93 assertFalse(full.addAll(Arrays.asList(three, four, one)));
Calin Juravle8f0d92b2013-08-01 17:26:00 +010094 assertEquals(5, full.size());
95 }
96
97 /**
98 * add will not add the element if it already exists in the set
99 */
100 public void testAdd2() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100101 Set full = populatedSet(3);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100102 full.add(one);
103 assertEquals(3, full.size());
104 }
105
106 /**
107 * add adds the element when it does not exist in the set
108 */
109 public void testAdd3() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100110 Set full = populatedSet(3);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100111 full.add(three);
112 assertTrue(full.contains(three));
113 }
114
115 /**
116 * clear removes all elements from the set
117 */
118 public void testClear() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100119 Collection full = populatedSet(3);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100120 full.clear();
121 assertEquals(0, full.size());
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100122 assertTrue(full.isEmpty());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100123 }
124
125 /**
126 * contains returns true for added elements
127 */
128 public void testContains() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100129 Collection full = populatedSet(3);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100130 assertTrue(full.contains(one));
131 assertFalse(full.contains(five));
132 }
133
134 /**
135 * Sets with equal elements are equal
136 */
137 public void testEquals() {
138 CopyOnWriteArraySet a = populatedSet(3);
139 CopyOnWriteArraySet b = populatedSet(3);
140 assertTrue(a.equals(b));
141 assertTrue(b.equals(a));
142 assertEquals(a.hashCode(), b.hashCode());
143 a.add(m1);
144 assertFalse(a.equals(b));
145 assertFalse(b.equals(a));
146 b.add(m1);
147 assertTrue(a.equals(b));
148 assertTrue(b.equals(a));
149 assertEquals(a.hashCode(), b.hashCode());
150 }
151
152 /**
153 * containsAll returns true for collections with subset of elements
154 */
155 public void testContainsAll() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100156 Collection full = populatedSet(3);
157 assertTrue(full.containsAll(Arrays.asList()));
158 assertTrue(full.containsAll(Arrays.asList(one)));
159 assertTrue(full.containsAll(Arrays.asList(one, two)));
160 assertFalse(full.containsAll(Arrays.asList(one, two, six)));
161 assertFalse(full.containsAll(Arrays.asList(six)));
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100162 }
163
164 /**
165 * isEmpty is true when empty, else false
166 */
167 public void testIsEmpty() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100168 assertTrue(populatedSet(0).isEmpty());
169 assertFalse(populatedSet(3).isEmpty());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100170 }
171
172 /**
173 * iterator() returns an iterator containing the elements of the
174 * set in insertion order
175 */
176 public void testIterator() {
177 Collection empty = new CopyOnWriteArraySet();
178 assertFalse(empty.iterator().hasNext());
179 try {
180 empty.iterator().next();
181 shouldThrow();
182 } catch (NoSuchElementException success) {}
183
184 Integer[] elements = new Integer[SIZE];
185 for (int i = 0; i < SIZE; i++)
186 elements[i] = i;
187 Collections.shuffle(Arrays.asList(elements));
188 Collection<Integer> full = populatedSet(elements);
189
190 Iterator it = full.iterator();
191 for (int j = 0; j < SIZE; j++) {
192 assertTrue(it.hasNext());
193 assertEquals(elements[j], it.next());
194 }
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100195 assertIteratorExhausted(it);
196 }
197
198 /**
199 * iterator of empty collection has no elements
200 */
201 public void testEmptyIterator() {
202 assertIteratorExhausted(new CopyOnWriteArraySet().iterator());
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100203 }
204
205 /**
206 * iterator remove is unsupported
207 */
208 public void testIteratorRemove() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100209 Collection full = populatedSet(3);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100210 Iterator it = full.iterator();
211 it.next();
212 try {
213 it.remove();
214 shouldThrow();
215 } catch (UnsupportedOperationException success) {}
216 }
217
218 /**
219 * toString holds toString of elements
220 */
221 public void testToString() {
222 assertEquals("[]", new CopyOnWriteArraySet().toString());
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100223 Collection full = populatedSet(3);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100224 String s = full.toString();
225 for (int i = 0; i < 3; ++i)
226 assertTrue(s.contains(String.valueOf(i)));
227 assertEquals(new ArrayList(full).toString(),
228 full.toString());
229 }
230
231 /**
232 * removeAll removes all elements from the given collection
233 */
234 public void testRemoveAll() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100235 Set full = populatedSet(3);
236 assertTrue(full.removeAll(Arrays.asList(one, two)));
237 assertEquals(1, full.size());
238 assertFalse(full.removeAll(Arrays.asList(one, two)));
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100239 assertEquals(1, full.size());
240 }
241
242 /**
243 * remove removes an element
244 */
245 public void testRemove() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100246 Collection full = populatedSet(3);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100247 full.remove(one);
248 assertFalse(full.contains(one));
249 assertEquals(2, full.size());
250 }
251
252 /**
253 * size returns the number of elements
254 */
255 public void testSize() {
Narayan Kamath8e9a0e92015-04-28 11:40:00 +0100256 Collection empty = new CopyOnWriteArraySet();
257 Collection full = populatedSet(3);
Calin Juravle8f0d92b2013-08-01 17:26:00 +0100258 assertEquals(3, full.size());
259 assertEquals(0, empty.size());
260 }
261
262 /**
263 * toArray() returns an Object array containing all elements from
264 * the set in insertion order
265 */
266 public void testToArray() {
267 Object[] a = new CopyOnWriteArraySet().toArray();
268 assertTrue(Arrays.equals(new Object[0], a));
269 assertSame(Object[].class, a.getClass());
270
271 Integer[] elements = new Integer[SIZE];
272 for (int i = 0; i < SIZE; i++)
273 elements[i] = i;
274 Collections.shuffle(Arrays.asList(elements));
275 Collection<Integer> full = populatedSet(elements);
276
277 assertTrue(Arrays.equals(elements, full.toArray()));
278 assertSame(Object[].class, full.toArray().getClass());
279 }
280
281 /**
282 * toArray(Integer array) returns an Integer array containing all
283 * elements from the set in insertion order
284 */
285 public void testToArray2() {
286 Collection empty = new CopyOnWriteArraySet();
287 Integer[] a;
288
289 a = new Integer[0];
290 assertSame(a, empty.toArray(a));
291
292 a = new Integer[SIZE/2];
293 Arrays.fill(a, 42);
294 assertSame(a, empty.toArray(a));
295 assertNull(a[0]);
296 for (int i = 1; i < a.length; i++)
297 assertEquals(42, (int) a[i]);
298
299 Integer[] elements = new Integer[SIZE];
300 for (int i = 0; i < SIZE; i++)
301 elements[i] = i;
302 Collections.shuffle(Arrays.asList(elements));
303 Collection<Integer> full = populatedSet(elements);
304
305 Arrays.fill(a, 42);
306 assertTrue(Arrays.equals(elements, full.toArray(a)));
307 for (int i = 0; i < a.length; i++)
308 assertEquals(42, (int) a[i]);
309 assertSame(Integer[].class, full.toArray(a).getClass());
310
311 a = new Integer[SIZE];
312 Arrays.fill(a, 42);
313 assertSame(a, full.toArray(a));
314 assertTrue(Arrays.equals(elements, a));
315
316 a = new Integer[2*SIZE];
317 Arrays.fill(a, 42);
318 assertSame(a, full.toArray(a));
319 assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
320 assertNull(a[SIZE]);
321 for (int i = SIZE + 1; i < a.length; i++)
322 assertEquals(42, (int) a[i]);
323 }
324
325 /**
326 * toArray throws an ArrayStoreException when the given array can
327 * not store the objects inside the set
328 */
329 public void testToArray_ArrayStoreException() {
330 try {
331 CopyOnWriteArraySet c = new CopyOnWriteArraySet();
332 c.add("zfasdfsdf");
333 c.add("asdadasd");
334 c.toArray(new Long[5]);
335 shouldThrow();
336 } catch (ArrayStoreException success) {}
337 }
338
339 /**
340 * A deserialized serialized set is equal
341 */
342 public void testSerialization() throws Exception {
343 Set x = populatedSet(SIZE);
344 Set y = serialClone(x);
345
346 assertNotSame(y, x);
347 assertEquals(x.size(), y.size());
348 assertEquals(x.toString(), y.toString());
349 assertTrue(Arrays.equals(x.toArray(), y.toArray()));
350 assertEquals(x, y);
351 assertEquals(y, x);
352 }
353
354 /**
355 * addAll is idempotent
356 */
357 public void testAddAll_idempotent() throws Exception {
358 Set x = populatedSet(SIZE);
359 Set y = new CopyOnWriteArraySet(x);
360 y.addAll(x);
361 assertEquals(x, y);
362 assertEquals(y, x);
363 }
364
365}