ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
Revision: 1.23
Committed: Tue Nov 29 20:49:39 2011 UTC (12 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.22: +2 -0 lines
Log Message:
improve testSerialization

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 junit.framework.*;
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.Iterator;
14 import java.util.Set;
15 import java.util.Vector;
16 import java.util.concurrent.CopyOnWriteArraySet;
17
18 public class CopyOnWriteArraySetTest extends JSR166TestCase {
19 public static void main(String[] args) {
20 junit.textui.TestRunner.run(suite());
21 }
22 public static Test suite() {
23 return new TestSuite(CopyOnWriteArraySetTest.class);
24 }
25
26 static CopyOnWriteArraySet<Integer> populatedSet(int n) {
27 CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
28 assertTrue(a.isEmpty());
29 for (int i = 0; i < n; i++)
30 a.add(i);
31 assertFalse(a.isEmpty());
32 assertEquals(n, a.size());
33 return a;
34 }
35
36 static CopyOnWriteArraySet populatedSet(Integer[] elements) {
37 CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
38 assertTrue(a.isEmpty());
39 for (int i = 0; i < elements.length; i++)
40 a.add(elements[i]);
41 assertFalse(a.isEmpty());
42 assertEquals(elements.length, a.size());
43 return a;
44 }
45
46 /**
47 * Default-constructed set is empty
48 */
49 public void testConstructor() {
50 CopyOnWriteArraySet a = new CopyOnWriteArraySet();
51 assertTrue(a.isEmpty());
52 }
53
54 /**
55 * Collection-constructed set holds all of its elements
56 */
57 public void testConstructor3() {
58 Integer[] ints = new Integer[SIZE];
59 for (int i = 0; i < SIZE-1; ++i)
60 ints[i] = new Integer(i);
61 CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
62 for (int i = 0; i < SIZE; ++i)
63 assertTrue(a.contains(ints[i]));
64 }
65
66 /**
67 * addAll adds each element from the given collection
68 */
69 public void testAddAll() {
70 CopyOnWriteArraySet full = populatedSet(3);
71 Vector v = new Vector();
72 v.add(three);
73 v.add(four);
74 v.add(five);
75 full.addAll(v);
76 assertEquals(6, full.size());
77 }
78
79 /**
80 * addAll adds each element from the given collection that did not
81 * already exist in the set
82 */
83 public void testAddAll2() {
84 CopyOnWriteArraySet full = populatedSet(3);
85 Vector v = new Vector();
86 v.add(three);
87 v.add(four);
88 v.add(one); // will not add this element
89 full.addAll(v);
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 Vector v = new Vector();
153 v.add(one);
154 v.add(two);
155 assertTrue(full.containsAll(v));
156 v.add(six);
157 assertFalse(full.containsAll(v));
158 }
159
160 /**
161 * isEmpty is true when empty, else false
162 */
163 public void testIsEmpty() {
164 CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
165 CopyOnWriteArraySet full = populatedSet(3);
166 assertTrue(empty.isEmpty());
167 assertFalse(full.isEmpty());
168 }
169
170 /**
171 * iterator() returns an iterator containing the elements of the set
172 */
173 public void testIterator() {
174 CopyOnWriteArraySet full = populatedSet(3);
175 Iterator i = full.iterator();
176 int j;
177 for (j = 0; i.hasNext(); j++)
178 assertEquals(j, i.next());
179 assertEquals(3, j);
180 }
181
182 /**
183 * iterator remove is unsupported
184 */
185 public void testIteratorRemove() {
186 CopyOnWriteArraySet full = populatedSet(3);
187 Iterator it = full.iterator();
188 it.next();
189 try {
190 it.remove();
191 shouldThrow();
192 } catch (UnsupportedOperationException success) {}
193 }
194
195 /**
196 * toString holds toString of elements
197 */
198 public void testToString() {
199 CopyOnWriteArraySet full = populatedSet(3);
200 String s = full.toString();
201 for (int i = 0; i < 3; ++i) {
202 assertTrue(s.contains(String.valueOf(i)));
203 }
204 }
205
206 /**
207 * removeAll removes all elements from the given collection
208 */
209 public void testRemoveAll() {
210 CopyOnWriteArraySet full = populatedSet(3);
211 Vector v = new Vector();
212 v.add(one);
213 v.add(two);
214 full.removeAll(v);
215 assertEquals(1, full.size());
216 }
217
218 /**
219 * remove removes an element
220 */
221 public void testRemove() {
222 CopyOnWriteArraySet full = populatedSet(3);
223 full.remove(one);
224 assertFalse(full.contains(one));
225 assertEquals(2, full.size());
226 }
227
228 /**
229 * size returns the number of elements
230 */
231 public void testSize() {
232 CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
233 CopyOnWriteArraySet full = populatedSet(3);
234 assertEquals(3, full.size());
235 assertEquals(0, empty.size());
236 }
237
238 /**
239 * toArray() returns an Object array containing all elements from
240 * the set in insertion order
241 */
242 public void testToArray() {
243 Object[] a = new CopyOnWriteArraySet().toArray();
244 assertTrue(Arrays.equals(new Object[0], a));
245 assertSame(Object[].class, a.getClass());
246
247 Integer[] elements = new Integer[SIZE];
248 for (int i = 0; i < SIZE; i++)
249 elements[i] = i;
250 Collections.shuffle(Arrays.asList(elements));
251 Collection<Integer> full = populatedSet(elements);
252
253 assertTrue(Arrays.equals(elements, full.toArray()));
254 assertSame(Object[].class, full.toArray().getClass());
255 }
256
257 /**
258 * toArray(Integer array) returns an Integer array containing all
259 * elements from the set in insertion order
260 */
261 public void testToArray2() {
262 Collection empty = new CopyOnWriteArraySet();
263 Integer[] a;
264
265 a = new Integer[0];
266 assertSame(a, empty.toArray(a));
267
268 a = new Integer[SIZE/2];
269 Arrays.fill(a, 42);
270 assertSame(a, empty.toArray(a));
271 assertNull(a[0]);
272 for (int i = 1; i < a.length; i++)
273 assertEquals(42, (int) a[i]);
274
275 Integer[] elements = new Integer[SIZE];
276 for (int i = 0; i < SIZE; i++)
277 elements[i] = i;
278 Collections.shuffle(Arrays.asList(elements));
279 Collection<Integer> full = populatedSet(elements);
280
281 Arrays.fill(a, 42);
282 assertTrue(Arrays.equals(elements, full.toArray(a)));
283 for (int i = 0; i < a.length; i++)
284 assertEquals(42, (int) a[i]);
285 assertSame(Integer[].class, full.toArray(a).getClass());
286
287 a = new Integer[SIZE];
288 Arrays.fill(a, 42);
289 assertSame(a, full.toArray(a));
290 assertTrue(Arrays.equals(elements, a));
291
292 a = new Integer[2*SIZE];
293 Arrays.fill(a, 42);
294 assertSame(a, full.toArray(a));
295 assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
296 assertNull(a[SIZE]);
297 for (int i = SIZE + 1; i < a.length; i++)
298 assertEquals(42, (int) a[i]);
299 }
300
301 /**
302 * toArray throws an ArrayStoreException when the given array can
303 * not store the objects inside the set
304 */
305 public void testToArray_ArrayStoreException() {
306 try {
307 CopyOnWriteArraySet c = new CopyOnWriteArraySet();
308 c.add("zfasdfsdf");
309 c.add("asdadasd");
310 c.toArray(new Long[5]);
311 shouldThrow();
312 } catch (ArrayStoreException success) {}
313 }
314
315 /**
316 * A deserialized serialized set is equal
317 */
318 public void testSerialization() throws Exception {
319 Set x = populatedSet(SIZE);
320 Set y = serialClone(x);
321
322 assertTrue(x != y);
323 assertEquals(x.size(), y.size());
324 assertEquals(x.toString(), y.toString());
325 assertTrue(Arrays.equals(x.toArray(), y.toArray()));
326 assertEquals(x, y);
327 assertEquals(y, x);
328 }
329
330 }