ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArraySetTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CopyOnWriteArraySetTest.java (file contents):
Revision 1.12 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.27 by jsr166, Thu May 30 03:28:55 2013 UTC

# Line 1 | Line 1
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/licenses/publicdomain
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.*;
11 < import java.util.concurrent.*;
12 < import java.io.*;
10 > import java.util.ArrayList;
11 > import java.util.Arrays;
12 > import java.util.Collection;
13 > import java.util.Collections;
14 > import java.util.Iterator;
15 > import java.util.NoSuchElementException;
16 > import java.util.Set;
17 > import java.util.Vector;
18 > import java.util.concurrent.CopyOnWriteArraySet;
19  
20   public class CopyOnWriteArraySetTest extends JSR166TestCase {
21      public static void main(String[] args) {
22 <        junit.textui.TestRunner.run (suite());
22 >        junit.textui.TestRunner.run(suite());
23      }
24      public static Test suite() {
25          return new TestSuite(CopyOnWriteArraySetTest.class);
26      }
27  
28 <    static CopyOnWriteArraySet populatedSet(int n) {
29 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
28 >    static CopyOnWriteArraySet<Integer> populatedSet(int n) {
29 >        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
30          assertTrue(a.isEmpty());
31 <        for (int i = 0; i < n; ++i)
32 <            a.add(new Integer(i));
31 >        for (int i = 0; i < n; i++)
32 >            a.add(i);
33          assertFalse(a.isEmpty());
34          assertEquals(n, a.size());
35          return a;
36      }
37  
38 +    static CopyOnWriteArraySet populatedSet(Integer[] elements) {
39 +        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
40 +        assertTrue(a.isEmpty());
41 +        for (int i = 0; i < elements.length; i++)
42 +            a.add(elements[i]);
43 +        assertFalse(a.isEmpty());
44 +        assertEquals(elements.length, a.size());
45 +        return a;
46 +    }
47 +
48      /**
49       * Default-constructed set is empty
50       */
# Line 49 | Line 65 | public class CopyOnWriteArraySetTest ext
65              assertTrue(a.contains(ints[i]));
66      }
67  
52
68      /**
69 <     *  addAll adds each element from the given collection
69 >     * addAll adds each element from the given collection
70       */
71      public void testAddAll() {
72          CopyOnWriteArraySet full = populatedSet(3);
# Line 64 | Line 79 | public class CopyOnWriteArraySetTest ext
79      }
80  
81      /**
82 <     *  addAll adds each element from the given collection that did not
83 <     *  already exist in the set
82 >     * addAll adds each element from the given collection that did not
83 >     * already exist in the set
84       */
85      public void testAddAll2() {
86          CopyOnWriteArraySet full = populatedSet(3);
# Line 78 | Line 93 | public class CopyOnWriteArraySetTest ext
93      }
94  
95      /**
96 <     *  add will not add the element if it already exists in the set
96 >     * add will not add the element if it already exists in the set
97       */
98      public void testAdd2() {
99          CopyOnWriteArraySet full = populatedSet(3);
# Line 87 | Line 102 | public class CopyOnWriteArraySetTest ext
102      }
103  
104      /**
105 <     *  add  adds the element when it does not exist
91 <     *  in the set
105 >     * add adds the element when it does not exist in the set
106       */
107      public void testAdd3() {
108          CopyOnWriteArraySet full = populatedSet(3);
# Line 97 | Line 111 | public class CopyOnWriteArraySetTest ext
111      }
112  
113      /**
114 <     *  clear removes all elements from the set
114 >     * clear removes all elements from the set
115       */
116      public void testClear() {
117          CopyOnWriteArraySet full = populatedSet(3);
# Line 106 | Line 120 | public class CopyOnWriteArraySetTest ext
120      }
121  
122      /**
123 <     *   contains returns true for added elements
123 >     * contains returns true for added elements
124       */
125      public void testContains() {
126          CopyOnWriteArraySet full = populatedSet(3);
# Line 132 | Line 146 | public class CopyOnWriteArraySetTest ext
146          assertEquals(a.hashCode(), b.hashCode());
147      }
148  
135
149      /**
150 <     *  containsAll returns true for collections with subset of elements
150 >     * containsAll returns true for collections with subset of elements
151       */
152      public void testContainsAll() {
153          CopyOnWriteArraySet full = populatedSet(3);
# Line 147 | Line 160 | public class CopyOnWriteArraySetTest ext
160      }
161  
162      /**
163 <     *  isEmpty is true when empty, else false
163 >     * isEmpty is true when empty, else false
164       */
165      public void testIsEmpty() {
166          CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
# Line 157 | Line 170 | public class CopyOnWriteArraySetTest ext
170      }
171  
172      /**
173 <     *  iterator() returns an iterator containing the elements of the set
173 >     * iterator() returns an iterator containing the elements of the
174 >     * set in insertion order
175       */
176      public void testIterator() {
177 <        CopyOnWriteArraySet full = populatedSet(3);
178 <        Iterator i = full.iterator();
179 <        int j;
180 <        for (j = 0; i.hasNext(); j++)
181 <            assertEquals(j, ((Integer)i.next()).intValue());
182 <        assertEquals(3, j);
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 >        }
195 >        assertFalse(it.hasNext());
196 >        try {
197 >            it.next();
198 >            shouldThrow();
199 >        } catch (NoSuchElementException success) {}
200      }
201  
202      /**
203       * iterator remove is unsupported
204       */
205 <    public void testIteratorRemove () {
205 >    public void testIteratorRemove() {
206          CopyOnWriteArraySet full = populatedSet(3);
207          Iterator it = full.iterator();
208          it.next();
# Line 185 | Line 216 | public class CopyOnWriteArraySetTest ext
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.indexOf(String.valueOf(i)) >= 0);
224 <        }
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  
195
228      /**
229 <     *  removeAll  removes all elements from the given collection
229 >     * removeAll removes all elements from the given collection
230       */
231      public void testRemoveAll() {
232          CopyOnWriteArraySet full = populatedSet(3);
# Line 205 | Line 237 | public class CopyOnWriteArraySetTest ext
237          assertEquals(1, full.size());
238      }
239  
208
240      /**
241       * remove removes an element
242       */
# Line 217 | Line 248 | public class CopyOnWriteArraySetTest ext
248      }
249  
250      /**
251 <     *  size returns the number of elements
251 >     * size returns the number of elements
252       */
253      public void testSize() {
254          CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
# Line 227 | Line 258 | public class CopyOnWriteArraySetTest ext
258      }
259  
260      /**
261 <     *  toArray returns an Object array containing all elements from the set
261 >     * toArray() returns an Object array containing all elements from
262 >     * the set in insertion order
263       */
264      public void testToArray() {
265 <        CopyOnWriteArraySet full = populatedSet(3);
266 <        Object[] o = full.toArray();
267 <        assertEquals(3, o.length);
268 <        assertEquals(0, ((Integer)o[0]).intValue());
269 <        assertEquals(1, ((Integer)o[1]).intValue());
270 <        assertEquals(2, ((Integer)o[2]).intValue());
265 >        Object[] a = new CopyOnWriteArraySet().toArray();
266 >        assertTrue(Arrays.equals(new Object[0], a));
267 >        assertSame(Object[].class, a.getClass());
268 >
269 >        Integer[] elements = new Integer[SIZE];
270 >        for (int i = 0; i < SIZE; i++)
271 >            elements[i] = i;
272 >        Collections.shuffle(Arrays.asList(elements));
273 >        Collection<Integer> full = populatedSet(elements);
274 >
275 >        assertTrue(Arrays.equals(elements, full.toArray()));
276 >        assertSame(Object[].class, full.toArray().getClass());
277      }
278  
279      /**
280 <     *  toArray returns an Integer array containing all elements from
281 <     *  the set
280 >     * toArray(Integer array) returns an Integer array containing all
281 >     * elements from the set in insertion order
282       */
283      public void testToArray2() {
284 <        CopyOnWriteArraySet full = populatedSet(3);
285 <        Integer[] i = new Integer[3];
248 <        i = (Integer[])full.toArray(i);
249 <        assertEquals(3, i.length);
250 <        assertEquals(0, i[0].intValue());
251 <        assertEquals(1, i[1].intValue());
252 <        assertEquals(2, i[2].intValue());
253 <    }
284 >        Collection empty = new CopyOnWriteArraySet();
285 >        Integer[] a;
286  
287 +        a = new Integer[0];
288 +        assertSame(a, empty.toArray(a));
289 +
290 +        a = new Integer[SIZE/2];
291 +        Arrays.fill(a, 42);
292 +        assertSame(a, empty.toArray(a));
293 +        assertNull(a[0]);
294 +        for (int i = 1; i < a.length; i++)
295 +            assertEquals(42, (int) a[i]);
296 +
297 +        Integer[] elements = new Integer[SIZE];
298 +        for (int i = 0; i < SIZE; i++)
299 +            elements[i] = i;
300 +        Collections.shuffle(Arrays.asList(elements));
301 +        Collection<Integer> full = populatedSet(elements);
302 +
303 +        Arrays.fill(a, 42);
304 +        assertTrue(Arrays.equals(elements, full.toArray(a)));
305 +        for (int i = 0; i < a.length; i++)
306 +            assertEquals(42, (int) a[i]);
307 +        assertSame(Integer[].class, full.toArray(a).getClass());
308 +
309 +        a = new Integer[SIZE];
310 +        Arrays.fill(a, 42);
311 +        assertSame(a, full.toArray(a));
312 +        assertTrue(Arrays.equals(elements, a));
313 +
314 +        a = new Integer[2*SIZE];
315 +        Arrays.fill(a, 42);
316 +        assertSame(a, full.toArray(a));
317 +        assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
318 +        assertNull(a[SIZE]);
319 +        for (int i = SIZE + 1; i < a.length; i++)
320 +            assertEquals(42, (int) a[i]);
321 +    }
322  
323      /**
324 <     *  toArray throws an ArrayStoreException when the given array can
325 <     *  not store the objects inside the set
324 >     * toArray throws an ArrayStoreException when the given array can
325 >     * not store the objects inside the set
326       */
327      public void testToArray_ArrayStoreException() {
328          try {
# Line 264 | Line 331 | public class CopyOnWriteArraySetTest ext
331              c.add("asdadasd");
332              c.toArray(new Long[5]);
333              shouldThrow();
334 <        } catch (ArrayStoreException e) {}
334 >        } catch (ArrayStoreException success) {}
335      }
336  
337      /**
338       * A deserialized serialized set is equal
339       */
340      public void testSerialization() throws Exception {
341 <        CopyOnWriteArraySet q = populatedSet(SIZE);
341 >        Set x = populatedSet(SIZE);
342 >        Set y = serialClone(x);
343  
344 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
345 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
346 <        out.writeObject(q);
347 <        out.close();
348 <
349 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
350 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
351 <        CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
352 <        assertEquals(q.size(), r.size());
353 <        assertTrue(q.equals(r));
354 <        assertTrue(r.equals(q));
344 >        assertNotSame(y, x);
345 >        assertEquals(x.size(), y.size());
346 >        assertEquals(x.toString(), y.toString());
347 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
348 >        assertEquals(x, y);
349 >        assertEquals(y, x);
350 >    }
351 >
352 >    /**
353 >     * addAll is idempotent
354 >     */
355 >    public void testAddAll_idempotent() throws Exception {
356 >        Set x = populatedSet(SIZE);
357 >        Set y = new CopyOnWriteArraySet(x);
358 >        y.addAll(x);
359 >        assertEquals(x, y);
360 >        assertEquals(y, x);
361      }
362  
363   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines