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.13 by jsr166, Sat Nov 21 17:38:05 2009 UTC vs.
Revision 1.23 by jsr166, Tue Nov 29 20:49:39 2011 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.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());
20 >        junit.textui.TestRunner.run(suite());
21      }
22      public static Test suite() {
23          return new TestSuite(CopyOnWriteArraySetTest.class);
24      }
25  
26 <    static CopyOnWriteArraySet populatedSet(int n) {
27 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
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(new Integer(i));
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       */
# Line 49 | Line 63 | public class CopyOnWriteArraySetTest ext
63              assertTrue(a.contains(ints[i]));
64      }
65  
52
66      /**
67 <     *  addAll adds each element from the given collection
67 >     * addAll adds each element from the given collection
68       */
69      public void testAddAll() {
70          CopyOnWriteArraySet full = populatedSet(3);
# Line 64 | Line 77 | public class CopyOnWriteArraySetTest ext
77      }
78  
79      /**
80 <     *  addAll adds each element from the given collection that did not
81 <     *  already exist in the set
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);
# Line 78 | Line 91 | public class CopyOnWriteArraySetTest ext
91      }
92  
93      /**
94 <     *  add will not add the element if it already exists in the set
94 >     * add will not add the element if it already exists in the set
95       */
96      public void testAdd2() {
97          CopyOnWriteArraySet full = populatedSet(3);
# Line 87 | Line 100 | public class CopyOnWriteArraySetTest ext
100      }
101  
102      /**
103 <     *  add  adds the element when it does not exist
91 <     *  in the set
103 >     * add adds the element when it does not exist in the set
104       */
105      public void testAdd3() {
106          CopyOnWriteArraySet full = populatedSet(3);
# Line 97 | Line 109 | public class CopyOnWriteArraySetTest ext
109      }
110  
111      /**
112 <     *  clear removes all elements from the set
112 >     * clear removes all elements from the set
113       */
114      public void testClear() {
115          CopyOnWriteArraySet full = populatedSet(3);
# Line 106 | Line 118 | public class CopyOnWriteArraySetTest ext
118      }
119  
120      /**
121 <     *   contains returns true for added elements
121 >     * contains returns true for added elements
122       */
123      public void testContains() {
124          CopyOnWriteArraySet full = populatedSet(3);
# Line 132 | Line 144 | public class CopyOnWriteArraySetTest ext
144          assertEquals(a.hashCode(), b.hashCode());
145      }
146  
135
147      /**
148 <     *  containsAll returns true for collections with subset of elements
148 >     * containsAll returns true for collections with subset of elements
149       */
150      public void testContainsAll() {
151          CopyOnWriteArraySet full = populatedSet(3);
# Line 147 | Line 158 | public class CopyOnWriteArraySetTest ext
158      }
159  
160      /**
161 <     *  isEmpty is true when empty, else false
161 >     * isEmpty is true when empty, else false
162       */
163      public void testIsEmpty() {
164          CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
# Line 157 | Line 168 | public class CopyOnWriteArraySetTest ext
168      }
169  
170      /**
171 <     *  iterator() returns an iterator containing the elements of the set
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, ((Integer)i.next()).intValue());
178 >            assertEquals(j, i.next());
179          assertEquals(3, j);
180      }
181  
182      /**
183       * iterator remove is unsupported
184       */
185 <    public void testIteratorRemove () {
185 >    public void testIteratorRemove() {
186          CopyOnWriteArraySet full = populatedSet(3);
187          Iterator it = full.iterator();
188          it.next();
# Line 188 | Line 199 | public class CopyOnWriteArraySetTest ext
199          CopyOnWriteArraySet full = populatedSet(3);
200          String s = full.toString();
201          for (int i = 0; i < 3; ++i) {
202 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
202 >            assertTrue(s.contains(String.valueOf(i)));
203          }
204      }
205  
195
206      /**
207 <     *  removeAll  removes all elements from the given collection
207 >     * removeAll removes all elements from the given collection
208       */
209      public void testRemoveAll() {
210          CopyOnWriteArraySet full = populatedSet(3);
# Line 205 | Line 215 | public class CopyOnWriteArraySetTest ext
215          assertEquals(1, full.size());
216      }
217  
208
218      /**
219       * remove removes an element
220       */
# Line 217 | Line 226 | public class CopyOnWriteArraySetTest ext
226      }
227  
228      /**
229 <     *  size returns the number of elements
229 >     * size returns the number of elements
230       */
231      public void testSize() {
232          CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
# Line 227 | Line 236 | public class CopyOnWriteArraySetTest ext
236      }
237  
238      /**
239 <     *  toArray returns an Object array containing all elements from the set
239 >     * toArray() returns an Object array containing all elements from
240 >     * the set in insertion order
241       */
242      public void testToArray() {
243 <        CopyOnWriteArraySet full = populatedSet(3);
244 <        Object[] o = full.toArray();
245 <        assertEquals(3, o.length);
246 <        assertEquals(0, ((Integer)o[0]).intValue());
247 <        assertEquals(1, ((Integer)o[1]).intValue());
248 <        assertEquals(2, ((Integer)o[2]).intValue());
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 returns an Integer array containing all elements from
259 <     *  the set
258 >     * toArray(Integer array) returns an Integer array containing all
259 >     * elements from the set in insertion order
260       */
261      public void testToArray2() {
262 <        CopyOnWriteArraySet full = populatedSet(3);
263 <        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 <    }
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
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 {
# Line 271 | Line 316 | public class CopyOnWriteArraySetTest ext
316       * A deserialized serialized set is equal
317       */
318      public void testSerialization() throws Exception {
319 <        CopyOnWriteArraySet q = populatedSet(SIZE);
319 >        Set x = populatedSet(SIZE);
320 >        Set y = serialClone(x);
321  
322 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
323 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
324 <        out.writeObject(q);
325 <        out.close();
326 <
327 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
282 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
283 <        CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
284 <        assertEquals(q.size(), r.size());
285 <        assertTrue(q.equals(r));
286 <        assertTrue(r.equals(q));
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines