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.28 by jsr166, Wed Dec 31 19:05:42 2014 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.*;
9 > import java.util.ArrayList;
10 > import java.util.Arrays;
11 > import java.util.Collection;
12 > import java.util.Collections;
13 > import java.util.Iterator;
14 > import java.util.NoSuchElementException;
15 > import java.util.Set;
16 > import java.util.Vector;
17 > import java.util.concurrent.CopyOnWriteArraySet;
18 >
19 > import junit.framework.Test;
20 > import junit.framework.TestSuite;
21  
22   public class CopyOnWriteArraySetTest extends JSR166TestCase {
23      public static void main(String[] args) {
24 <        junit.textui.TestRunner.run (suite());
24 >        junit.textui.TestRunner.run(suite());
25      }
26      public static Test suite() {
27          return new TestSuite(CopyOnWriteArraySetTest.class);
28      }
29  
30 <    static CopyOnWriteArraySet populatedSet(int n) {
31 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
30 >    static CopyOnWriteArraySet<Integer> populatedSet(int n) {
31 >        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
32          assertTrue(a.isEmpty());
33 <        for (int i = 0; i < n; ++i)
34 <            a.add(new Integer(i));
33 >        for (int i = 0; i < n; i++)
34 >            a.add(i);
35          assertFalse(a.isEmpty());
36          assertEquals(n, a.size());
37          return a;
38      }
39  
40 +    static CopyOnWriteArraySet populatedSet(Integer[] elements) {
41 +        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
42 +        assertTrue(a.isEmpty());
43 +        for (int i = 0; i < elements.length; i++)
44 +            a.add(elements[i]);
45 +        assertFalse(a.isEmpty());
46 +        assertEquals(elements.length, a.size());
47 +        return a;
48 +    }
49 +
50      /**
51       * Default-constructed set is empty
52       */
# Line 49 | Line 67 | public class CopyOnWriteArraySetTest ext
67              assertTrue(a.contains(ints[i]));
68      }
69  
52
70      /**
71 <     *  addAll adds each element from the given collection
71 >     * addAll adds each element from the given collection
72       */
73      public void testAddAll() {
74          CopyOnWriteArraySet full = populatedSet(3);
# Line 64 | Line 81 | public class CopyOnWriteArraySetTest ext
81      }
82  
83      /**
84 <     *  addAll adds each element from the given collection that did not
85 <     *  already exist in the set
84 >     * addAll adds each element from the given collection that did not
85 >     * already exist in the set
86       */
87      public void testAddAll2() {
88          CopyOnWriteArraySet full = populatedSet(3);
# Line 78 | Line 95 | public class CopyOnWriteArraySetTest ext
95      }
96  
97      /**
98 <     *  add will not add the element if it already exists in the set
98 >     * add will not add the element if it already exists in the set
99       */
100      public void testAdd2() {
101          CopyOnWriteArraySet full = populatedSet(3);
# Line 87 | Line 104 | public class CopyOnWriteArraySetTest ext
104      }
105  
106      /**
107 <     *  add  adds the element when it does not exist
91 <     *  in the set
107 >     * add adds the element when it does not exist in the set
108       */
109      public void testAdd3() {
110          CopyOnWriteArraySet full = populatedSet(3);
# Line 97 | Line 113 | public class CopyOnWriteArraySetTest ext
113      }
114  
115      /**
116 <     *  clear removes all elements from the set
116 >     * clear removes all elements from the set
117       */
118      public void testClear() {
119          CopyOnWriteArraySet full = populatedSet(3);
# Line 106 | Line 122 | public class CopyOnWriteArraySetTest ext
122      }
123  
124      /**
125 <     *   contains returns true for added elements
125 >     * contains returns true for added elements
126       */
127      public void testContains() {
128          CopyOnWriteArraySet full = populatedSet(3);
# Line 132 | Line 148 | public class CopyOnWriteArraySetTest ext
148          assertEquals(a.hashCode(), b.hashCode());
149      }
150  
135
151      /**
152 <     *  containsAll returns true for collections with subset of elements
152 >     * containsAll returns true for collections with subset of elements
153       */
154      public void testContainsAll() {
155          CopyOnWriteArraySet full = populatedSet(3);
# Line 147 | Line 162 | public class CopyOnWriteArraySetTest ext
162      }
163  
164      /**
165 <     *  isEmpty is true when empty, else false
165 >     * isEmpty is true when empty, else false
166       */
167      public void testIsEmpty() {
168          CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
# Line 157 | Line 172 | public class CopyOnWriteArraySetTest ext
172      }
173  
174      /**
175 <     *  iterator() returns an iterator containing the elements of the set
175 >     * iterator() returns an iterator containing the elements of the
176 >     * set in insertion order
177       */
178      public void testIterator() {
179 <        CopyOnWriteArraySet full = populatedSet(3);
180 <        Iterator i = full.iterator();
181 <        int j;
182 <        for (j = 0; i.hasNext(); j++)
183 <            assertEquals(j, ((Integer)i.next()).intValue());
184 <        assertEquals(3, j);
179 >        Collection empty = new CopyOnWriteArraySet();
180 >        assertFalse(empty.iterator().hasNext());
181 >        try {
182 >            empty.iterator().next();
183 >            shouldThrow();
184 >        } catch (NoSuchElementException success) {}
185 >
186 >        Integer[] elements = new Integer[SIZE];
187 >        for (int i = 0; i < SIZE; i++)
188 >            elements[i] = i;
189 >        Collections.shuffle(Arrays.asList(elements));
190 >        Collection<Integer> full = populatedSet(elements);
191 >
192 >        Iterator it = full.iterator();
193 >        for (int j = 0; j < SIZE; j++) {
194 >            assertTrue(it.hasNext());
195 >            assertEquals(elements[j], it.next());
196 >        }
197 >        assertFalse(it.hasNext());
198 >        try {
199 >            it.next();
200 >            shouldThrow();
201 >        } catch (NoSuchElementException success) {}
202      }
203  
204      /**
205       * iterator remove is unsupported
206       */
207 <    public void testIteratorRemove () {
207 >    public void testIteratorRemove() {
208          CopyOnWriteArraySet full = populatedSet(3);
209          Iterator it = full.iterator();
210          it.next();
# Line 185 | Line 218 | public class CopyOnWriteArraySetTest ext
218       * toString holds toString of elements
219       */
220      public void testToString() {
221 +        assertEquals("[]", new CopyOnWriteArraySet().toString());
222          CopyOnWriteArraySet full = populatedSet(3);
223          String s = full.toString();
224 <        for (int i = 0; i < 3; ++i) {
225 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
226 <        }
224 >        for (int i = 0; i < 3; ++i)
225 >            assertTrue(s.contains(String.valueOf(i)));
226 >        assertEquals(new ArrayList(full).toString(),
227 >                     full.toString());
228      }
229  
195
230      /**
231 <     *  removeAll  removes all elements from the given collection
231 >     * removeAll removes all elements from the given collection
232       */
233      public void testRemoveAll() {
234          CopyOnWriteArraySet full = populatedSet(3);
# Line 205 | Line 239 | public class CopyOnWriteArraySetTest ext
239          assertEquals(1, full.size());
240      }
241  
208
242      /**
243       * remove removes an element
244       */
# Line 217 | Line 250 | public class CopyOnWriteArraySetTest ext
250      }
251  
252      /**
253 <     *  size returns the number of elements
253 >     * size returns the number of elements
254       */
255      public void testSize() {
256          CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
# Line 227 | Line 260 | public class CopyOnWriteArraySetTest ext
260      }
261  
262      /**
263 <     *  toArray returns an Object array containing all elements from the set
263 >     * toArray() returns an Object array containing all elements from
264 >     * the set in insertion order
265       */
266      public void testToArray() {
267 <        CopyOnWriteArraySet full = populatedSet(3);
268 <        Object[] o = full.toArray();
269 <        assertEquals(3, o.length);
270 <        assertEquals(0, ((Integer)o[0]).intValue());
271 <        assertEquals(1, ((Integer)o[1]).intValue());
272 <        assertEquals(2, ((Integer)o[2]).intValue());
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 returns an Integer array containing all elements from
283 <     *  the set
282 >     * toArray(Integer array) returns an Integer array containing all
283 >     * elements from the set in insertion order
284       */
285      public void testToArray2() {
286 <        CopyOnWriteArraySet full = populatedSet(3);
287 <        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 <    }
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
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 {
# Line 271 | Line 340 | public class CopyOnWriteArraySetTest ext
340       * A deserialized serialized set is equal
341       */
342      public void testSerialization() throws Exception {
343 <        CopyOnWriteArraySet q = populatedSet(SIZE);
343 >        Set x = populatedSet(SIZE);
344 >        Set y = serialClone(x);
345  
346 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
347 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
348 <        out.writeObject(q);
349 <        out.close();
350 <
351 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
352 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
353 <        CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
354 <        assertEquals(q.size(), r.size());
355 <        assertTrue(q.equals(r));
356 <        assertTrue(r.equals(q));
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines