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.10 by jsr166, Mon Nov 16 05:30:07 2009 UTC vs.
Revision 1.26 by jsr166, Tue Apr 2 22:18:26 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);
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       */
51      public void testConstructor() {
52 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
52 >        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
53          assertTrue(a.isEmpty());
54      }
55  
# Line 44 | Line 60 | public class CopyOnWriteArraySetTest ext
60          Integer[] ints = new Integer[SIZE];
61          for (int i = 0; i < SIZE-1; ++i)
62              ints[i] = new Integer(i);
63 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
63 >        CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
64          for (int i = 0; i < SIZE; ++i)
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);
73 <        Vector v = new Vector();
74 <        v.add(three);
75 <        v.add(four);
76 <        v.add(five);
77 <        full.addAll(v);
78 <        assertEquals(6, full.size());
72 >        CopyOnWriteArraySet full = populatedSet(3);
73 >        Vector v = new Vector();
74 >        v.add(three);
75 >        v.add(four);
76 >        v.add(five);
77 >        full.addAll(v);
78 >        assertEquals(6, full.size());
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);
87 <        Vector v = new Vector();
88 <        v.add(three);
89 <        v.add(four);
90 <        v.add(one); // will not add this element
91 <        full.addAll(v);
92 <        assertEquals(5, full.size());
86 >        CopyOnWriteArraySet full = populatedSet(3);
87 >        Vector v = new Vector();
88 >        v.add(three);
89 >        v.add(four);
90 >        v.add(one); // will not add this element
91 >        full.addAll(v);
92 >        assertEquals(5, full.size());
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);
100 <        full.add(one);
101 <        assertEquals(3, full.size());
99 >        CopyOnWriteArraySet full = populatedSet(3);
100 >        full.add(one);
101 >        assertEquals(3, full.size());
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);
108 >        CopyOnWriteArraySet full = populatedSet(3);
109          full.add(three);
110          assertTrue(full.contains(three));
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);
118 <        full.clear();
119 <        assertEquals(0, full.size());
117 >        CopyOnWriteArraySet full = populatedSet(3);
118 >        full.clear();
119 >        assertEquals(0, full.size());
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);
127 <        assertTrue(full.contains(one));
128 <        assertFalse(full.contains(five));
126 >        CopyOnWriteArraySet full = populatedSet(3);
127 >        assertTrue(full.contains(one));
128 >        assertFalse(full.contains(five));
129      }
130  
131      /**
132       * Sets with equal elements are equal
133       */
134      public void testEquals() {
135 <        CopyOnWriteArraySet a = populatedSet(3);
136 <        CopyOnWriteArraySet b = populatedSet(3);
135 >        CopyOnWriteArraySet a = populatedSet(3);
136 >        CopyOnWriteArraySet b = populatedSet(3);
137          assertTrue(a.equals(b));
138          assertTrue(b.equals(a));
139          assertEquals(a.hashCode(), b.hashCode());
# 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);
154 <        Vector v = new Vector();
155 <        v.add(one);
156 <        v.add(two);
157 <        assertTrue(full.containsAll(v));
158 <        v.add(six);
159 <        assertFalse(full.containsAll(v));
153 >        CopyOnWriteArraySet full = populatedSet(3);
154 >        Vector v = new Vector();
155 >        v.add(one);
156 >        v.add(two);
157 >        assertTrue(full.containsAll(v));
158 >        v.add(six);
159 >        assertFalse(full.containsAll(v));
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();
167 <        CopyOnWriteArraySet full = populatedSet(3);
168 <        assertTrue(empty.isEmpty());
169 <        assertFalse(full.isEmpty());
166 >        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
167 >        CopyOnWriteArraySet full = populatedSet(3);
168 >        assertTrue(empty.isEmpty());
169 >        assertFalse(full.isEmpty());
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 () {
206 <        CopyOnWriteArraySet full = populatedSet(3);
205 >    public void testIteratorRemove() {
206 >        CopyOnWriteArraySet full = populatedSet(3);
207          Iterator it = full.iterator();
208          it.next();
209          try {
210              it.remove();
211              shouldThrow();
212 <        }
182 <        catch (UnsupportedOperationException success) {}
212 >        } catch (UnsupportedOperationException success) {}
213      }
214  
215      /**
216       * toString holds toString of elements
217       */
218      public void testToString() {
219 <        CopyOnWriteArraySet full = populatedSet(3);
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  
196
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);
233 <        Vector v = new Vector();
234 <        v.add(one);
235 <        v.add(two);
236 <        full.removeAll(v);
237 <        assertEquals(1, full.size());
232 >        CopyOnWriteArraySet full = populatedSet(3);
233 >        Vector v = new Vector();
234 >        v.add(one);
235 >        v.add(two);
236 >        full.removeAll(v);
237 >        assertEquals(1, full.size());
238      }
239  
209
240      /**
241       * remove removes an element
242       */
243      public void testRemove() {
244 <        CopyOnWriteArraySet full = populatedSet(3);
245 <        full.remove(one);
244 >        CopyOnWriteArraySet full = populatedSet(3);
245 >        full.remove(one);
246          assertFalse(full.contains(one));
247 <        assertEquals(2, full.size());
247 >        assertEquals(2, full.size());
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();
255 <        CopyOnWriteArraySet full = populatedSet(3);
256 <        assertEquals(3, full.size());
257 <        assertEquals(0, empty.size());
254 >        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
255 >        CopyOnWriteArraySet full = populatedSet(3);
256 >        assertEquals(3, full.size());
257 >        assertEquals(0, empty.size());
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];
249 <        i = (Integer[])full.toArray(i);
250 <        assertEquals(3, i.length);
251 <        assertEquals(0, i[0].intValue());
252 <        assertEquals(1, i[1].intValue());
253 <        assertEquals(2, i[2].intValue());
254 <    }
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 330 | public class CopyOnWriteArraySetTest ext
330              c.add("zfasdfsdf");
331              c.add("asdadasd");
332              c.toArray(new Long[5]);
333 <            shouldThrow();
334 <        } catch (ArrayStoreException e) {}
333 >            shouldThrow();
334 >        } catch (ArrayStoreException success) {}
335      }
336  
337      /**
338       * A deserialized serialized set is equal
339       */
340 <    public void testSerialization() {
341 <        CopyOnWriteArraySet q = populatedSet(SIZE);
340 >    public void testSerialization() throws Exception {
341 >        Set x = populatedSet(SIZE);
342 >        Set y = serialClone(x);
343 >
344 >        assertTrue(x != y);
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 <        try {
353 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
354 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
355 <            out.writeObject(q);
356 <            out.close();
357 <
358 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
359 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
360 <            CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
286 <            assertEquals(q.size(), r.size());
287 <            assertTrue(q.equals(r));
288 <            assertTrue(r.equals(q));
289 <        } catch (Exception e) {
290 <            unexpectedException();
291 <        }
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