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.15 by jsr166, Wed Aug 25 00:07:03 2010 UTC vs.
Revision 1.21 by jsr166, Tue Nov 29 05:09:05 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.Collections;
12 > import java.util.Iterator;
13 > import java.util.Set;
14 > import java.util.Vector;
15 > import java.util.concurrent.CopyOnWriteArraySet;
16  
17   public class CopyOnWriteArraySetTest extends JSR166TestCase {
18      public static void main(String[] args) {
# Line 19 | Line 22 | public class CopyOnWriteArraySetTest ext
22          return new TestSuite(CopyOnWriteArraySetTest.class);
23      }
24  
25 <    static CopyOnWriteArraySet populatedSet(int n) {
26 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
25 >    static CopyOnWriteArraySet<Integer> populatedSet(int n) {
26 >        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
27          assertTrue(a.isEmpty());
28          for (int i = 0; i < n; ++i)
29 <            a.add(new Integer(i));
29 >            a.add(i);
30          assertFalse(a.isEmpty());
31          assertEquals(n, a.size());
32          return a;
33      }
34  
35 +    static CopyOnWriteArraySet populatedSet(Integer[] elements) {
36 +        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
37 +        assertTrue(a.isEmpty());
38 +        for (int i = 0; i < elements.length; i++)
39 +            a.add(elements[i]);
40 +        assertFalse(a.isEmpty());
41 +        assertEquals(elements.length, a.size());
42 +        return a;
43 +    }
44 +
45      /**
46       * Default-constructed set is empty
47       */
# Line 49 | Line 62 | public class CopyOnWriteArraySetTest ext
62              assertTrue(a.contains(ints[i]));
63      }
64  
52
65      /**
66 <     *  addAll adds each element from the given collection
66 >     * addAll adds each element from the given collection
67       */
68      public void testAddAll() {
69          CopyOnWriteArraySet full = populatedSet(3);
# Line 64 | Line 76 | public class CopyOnWriteArraySetTest ext
76      }
77  
78      /**
79 <     *  addAll adds each element from the given collection that did not
80 <     *  already exist in the set
79 >     * addAll adds each element from the given collection that did not
80 >     * already exist in the set
81       */
82      public void testAddAll2() {
83          CopyOnWriteArraySet full = populatedSet(3);
# Line 78 | Line 90 | public class CopyOnWriteArraySetTest ext
90      }
91  
92      /**
93 <     *  add will not add the element if it already exists in the set
93 >     * add will not add the element if it already exists in the set
94       */
95      public void testAdd2() {
96          CopyOnWriteArraySet full = populatedSet(3);
# Line 87 | Line 99 | public class CopyOnWriteArraySetTest ext
99      }
100  
101      /**
102 <     *  add  adds the element when it does not exist
91 <     *  in the set
102 >     * add adds the element when it does not exist in the set
103       */
104      public void testAdd3() {
105          CopyOnWriteArraySet full = populatedSet(3);
# Line 97 | Line 108 | public class CopyOnWriteArraySetTest ext
108      }
109  
110      /**
111 <     *  clear removes all elements from the set
111 >     * clear removes all elements from the set
112       */
113      public void testClear() {
114          CopyOnWriteArraySet full = populatedSet(3);
# Line 106 | Line 117 | public class CopyOnWriteArraySetTest ext
117      }
118  
119      /**
120 <     *   contains returns true for added elements
120 >     * contains returns true for added elements
121       */
122      public void testContains() {
123          CopyOnWriteArraySet full = populatedSet(3);
# Line 132 | Line 143 | public class CopyOnWriteArraySetTest ext
143          assertEquals(a.hashCode(), b.hashCode());
144      }
145  
135
146      /**
147 <     *  containsAll returns true for collections with subset of elements
147 >     * containsAll returns true for collections with subset of elements
148       */
149      public void testContainsAll() {
150          CopyOnWriteArraySet full = populatedSet(3);
# Line 147 | Line 157 | public class CopyOnWriteArraySetTest ext
157      }
158  
159      /**
160 <     *  isEmpty is true when empty, else false
160 >     * isEmpty is true when empty, else false
161       */
162      public void testIsEmpty() {
163          CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
# Line 157 | Line 167 | public class CopyOnWriteArraySetTest ext
167      }
168  
169      /**
170 <     *  iterator() returns an iterator containing the elements of the set
170 >     * iterator() returns an iterator containing the elements of the set
171       */
172      public void testIterator() {
173          CopyOnWriteArraySet full = populatedSet(3);
# Line 188 | Line 198 | public class CopyOnWriteArraySetTest ext
198          CopyOnWriteArraySet full = populatedSet(3);
199          String s = full.toString();
200          for (int i = 0; i < 3; ++i) {
201 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
201 >            assertTrue(s.contains(String.valueOf(i)));
202          }
203      }
204  
195
205      /**
206 <     *  removeAll  removes all elements from the given collection
206 >     * removeAll removes all elements from the given collection
207       */
208      public void testRemoveAll() {
209          CopyOnWriteArraySet full = populatedSet(3);
# Line 205 | Line 214 | public class CopyOnWriteArraySetTest ext
214          assertEquals(1, full.size());
215      }
216  
208
217      /**
218       * remove removes an element
219       */
# Line 217 | Line 225 | public class CopyOnWriteArraySetTest ext
225      }
226  
227      /**
228 <     *  size returns the number of elements
228 >     * size returns the number of elements
229       */
230      public void testSize() {
231          CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
# Line 227 | Line 235 | public class CopyOnWriteArraySetTest ext
235      }
236  
237      /**
238 <     *  toArray returns an Object array containing all elements from the set
238 >     * toArray() returns an Object array containing all elements from
239 >     * the set in insertion order
240       */
241      public void testToArray() {
242 <        CopyOnWriteArraySet full = populatedSet(3);
243 <        Object[] o = full.toArray();
244 <        assertEquals(3, o.length);
245 <        assertEquals(0, o[0]);
246 <        assertEquals(1, o[1]);
247 <        assertEquals(2, o[2]);
242 >        Object[] a = new CopyOnWriteArraySet().toArray();
243 >        assertTrue(Arrays.equals(new Object[0], a));
244 >        assertSame(Object[].class, a.getClass());
245 >
246 >        Integer[] elements = new Integer[SIZE];
247 >        for (int i = 0; i < SIZE; i++)
248 >            elements[i] = i;
249 >        Collections.shuffle(Arrays.asList(elements));
250 >        CopyOnWriteArraySet<Integer> full = populatedSet(elements);
251 >
252 >        assertTrue(Arrays.equals(elements, full.toArray()));
253 >        assertSame(Object[].class, full.toArray().getClass());
254      }
255  
256      /**
257 <     *  toArray returns an Integer array containing all elements from
258 <     *  the set
257 >     * toArray(Integer array) returns an Integer array containing all
258 >     * elements from the set in insertion order
259       */
260      public void testToArray2() {
261 <        CopyOnWriteArraySet full = populatedSet(3);
262 <        Integer[] i = new Integer[3];
248 <        i = (Integer[])full.toArray(i);
249 <        assertEquals(3, i.length);
250 <        assertEquals(0, (int) i[0]);
251 <        assertEquals(1, (int) i[1]);
252 <        assertEquals(2, (int) i[2]);
253 <    }
261 >        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
262 >        Integer[] a;
263  
264 +        a = new Integer[0];
265 +        assertSame(a, empty.toArray(a));
266 +
267 +        a = new Integer[SIZE/2];
268 +        Arrays.fill(a, 42);
269 +        assertSame(a, empty.toArray(a));
270 +        assertNull(a[0]);
271 +        for (int i = 1; i < a.length; i++)
272 +            assertEquals(42, (int) a[i]);
273 +
274 +        Integer[] elements = new Integer[SIZE];
275 +        for (int i = 0; i < SIZE; i++)
276 +            elements[i] = i;
277 +        Collections.shuffle(Arrays.asList(elements));
278 +        CopyOnWriteArraySet<Integer> full = populatedSet(elements);
279 +
280 +        Arrays.fill(a, 42);
281 +        assertTrue(Arrays.equals(elements, full.toArray(a)));
282 +        for (int i = 0; i < a.length; i++)
283 +            assertEquals(42, (int) a[i]);
284 +        assertSame(Integer[].class, full.toArray(a).getClass());
285 +
286 +        a = new Integer[SIZE];
287 +        Arrays.fill(a, 42);
288 +        assertSame(a, full.toArray(a));
289 +        assertTrue(Arrays.equals(elements, a));
290 +
291 +        a = new Integer[2*SIZE];
292 +        Arrays.fill(a, 42);
293 +        assertSame(a, full.toArray(a));
294 +        assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
295 +        assertNull(a[SIZE]);
296 +        for (int i = SIZE + 1; i < a.length; i++)
297 +            assertEquals(42, (int) a[i]);
298 +    }
299  
300      /**
301 <     *  toArray throws an ArrayStoreException when the given array can
302 <     *  not store the objects inside the set
301 >     * toArray throws an ArrayStoreException when the given array can
302 >     * not store the objects inside the set
303       */
304      public void testToArray_ArrayStoreException() {
305          try {
# Line 271 | Line 315 | public class CopyOnWriteArraySetTest ext
315       * A deserialized serialized set is equal
316       */
317      public void testSerialization() throws Exception {
318 <        CopyOnWriteArraySet q = populatedSet(SIZE);
318 >        Set x = populatedSet(SIZE);
319 >        Set y = serialClone(x);
320  
321 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
322 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
323 <        out.writeObject(q);
324 <        out.close();
280 <
281 <        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));
321 >        assertTrue(x != y);
322 >        assertEquals(x.size(), y.size());
323 >        assertEquals(x, y);
324 >        assertEquals(y, x);
325      }
326  
327   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines