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.18 by jsr166, Fri May 27 19:26:42 2011 UTC vs.
Revision 1.30 by jsr166, Fri Feb 27 19:28:23 2015 UTC

# Line 6 | Line 6
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.concurrent.CopyOnWriteArraySet;
17 >
18 > import junit.framework.Test;
19 > import junit.framework.TestSuite;
20  
21   public class CopyOnWriteArraySetTest extends JSR166TestCase {
22      public static void main(String[] args) {
# Line 19 | Line 26 | public class CopyOnWriteArraySetTest ext
26          return new TestSuite(CopyOnWriteArraySetTest.class);
27      }
28  
29 <    static CopyOnWriteArraySet populatedSet(int n) {
30 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
29 >    static CopyOnWriteArraySet<Integer> populatedSet(int n) {
30 >        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
31          assertTrue(a.isEmpty());
32 <        for (int i = 0; i < n; ++i)
33 <            a.add(new Integer(i));
32 >        for (int i = 0; i < n; i++)
33 >            a.add(i);
34          assertFalse(a.isEmpty());
35          assertEquals(n, a.size());
36          return a;
37      }
38  
39 +    static CopyOnWriteArraySet populatedSet(Integer[] elements) {
40 +        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
41 +        assertTrue(a.isEmpty());
42 +        for (int i = 0; i < elements.length; i++)
43 +            a.add(elements[i]);
44 +        assertFalse(a.isEmpty());
45 +        assertEquals(elements.length, a.size());
46 +        return a;
47 +    }
48 +
49      /**
50       * Default-constructed set is empty
51       */
# Line 54 | Line 71 | public class CopyOnWriteArraySetTest ext
71       */
72      public void testAddAll() {
73          CopyOnWriteArraySet full = populatedSet(3);
74 <        Vector v = new Vector();
75 <        v.add(three);
76 <        v.add(four);
60 <        v.add(five);
61 <        full.addAll(v);
74 >        assertTrue(full.addAll(Arrays.asList(three, four, five)));
75 >        assertEquals(6, full.size());
76 >        assertFalse(full.addAll(Arrays.asList(three, four, five)));
77          assertEquals(6, full.size());
78      }
79  
# Line 68 | Line 83 | public class CopyOnWriteArraySetTest ext
83       */
84      public void testAddAll2() {
85          CopyOnWriteArraySet full = populatedSet(3);
86 <        Vector v = new Vector();
87 <        v.add(three);
88 <        v.add(four);
89 <        v.add(one); // will not add this element
75 <        full.addAll(v);
86 >        // "one" is duplicate and will not be added
87 >        assertTrue(full.addAll(Arrays.asList(three, four, one)));
88 >        assertEquals(5, full.size());
89 >        assertFalse(full.addAll(Arrays.asList(three, four, one)));
90          assertEquals(5, full.size());
91      }
92  
# Line 135 | Line 149 | public class CopyOnWriteArraySetTest ext
149       */
150      public void testContainsAll() {
151          CopyOnWriteArraySet full = populatedSet(3);
152 <        Vector v = new Vector();
153 <        v.add(one);
154 <        v.add(two);
155 <        assertTrue(full.containsAll(v));
156 <        v.add(six);
143 <        assertFalse(full.containsAll(v));
152 >        assertTrue(full.containsAll(Arrays.asList()));
153 >        assertTrue(full.containsAll(Arrays.asList(one)));
154 >        assertTrue(full.containsAll(Arrays.asList(one, two)));
155 >        assertFalse(full.containsAll(Arrays.asList(one, two, six)));
156 >        assertFalse(full.containsAll(Arrays.asList(six)));
157      }
158  
159      /**
# Line 154 | 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
171 >     * set in insertion order
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, i.next());
179 <        assertEquals(3, j);
174 >        Collection empty = new CopyOnWriteArraySet();
175 >        assertFalse(empty.iterator().hasNext());
176 >        try {
177 >            empty.iterator().next();
178 >            shouldThrow();
179 >        } catch (NoSuchElementException success) {}
180 >
181 >        Integer[] elements = new Integer[SIZE];
182 >        for (int i = 0; i < SIZE; i++)
183 >            elements[i] = i;
184 >        Collections.shuffle(Arrays.asList(elements));
185 >        Collection<Integer> full = populatedSet(elements);
186 >
187 >        Iterator it = full.iterator();
188 >        for (int j = 0; j < SIZE; j++) {
189 >            assertTrue(it.hasNext());
190 >            assertEquals(elements[j], it.next());
191 >        }
192 >        assertIteratorExhausted(it);
193 >    }
194 >
195 >    /**
196 >     * iterator of empty collection has no elements
197 >     */
198 >    public void testEmptyIterator() {
199 >        assertIteratorExhausted(new CopyOnWriteArraySet().iterator());
200      }
201  
202      /**
# Line 182 | 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) {
222 >        for (int i = 0; i < 3; ++i)
223              assertTrue(s.contains(String.valueOf(i)));
224 <        }
224 >        assertEquals(new ArrayList(full).toString(),
225 >                     full.toString());
226      }
227  
228      /**
# Line 194 | Line 230 | public class CopyOnWriteArraySetTest ext
230       */
231      public void testRemoveAll() {
232          CopyOnWriteArraySet full = populatedSet(3);
233 <        Vector v = new Vector();
234 <        v.add(one);
235 <        v.add(two);
200 <        full.removeAll(v);
233 >        assertTrue(full.removeAll(Arrays.asList(one, two)));
234 >        assertEquals(1, full.size());
235 >        assertFalse(full.removeAll(Arrays.asList(one, two)));
236          assertEquals(1, full.size());
237      }
238  
# Line 222 | Line 257 | public class CopyOnWriteArraySetTest ext
257      }
258  
259      /**
260 <     * toArray returns an Object array containing all elements from the set
260 >     * toArray() returns an Object array containing all elements from
261 >     * the set in insertion order
262       */
263      public void testToArray() {
264 <        CopyOnWriteArraySet full = populatedSet(3);
265 <        Object[] o = full.toArray();
266 <        assertEquals(3, o.length);
267 <        assertEquals(0, o[0]);
268 <        assertEquals(1, o[1]);
269 <        assertEquals(2, o[2]);
264 >        Object[] a = new CopyOnWriteArraySet().toArray();
265 >        assertTrue(Arrays.equals(new Object[0], a));
266 >        assertSame(Object[].class, a.getClass());
267 >
268 >        Integer[] elements = new Integer[SIZE];
269 >        for (int i = 0; i < SIZE; i++)
270 >            elements[i] = i;
271 >        Collections.shuffle(Arrays.asList(elements));
272 >        Collection<Integer> full = populatedSet(elements);
273 >
274 >        assertTrue(Arrays.equals(elements, full.toArray()));
275 >        assertSame(Object[].class, full.toArray().getClass());
276      }
277  
278      /**
279 <     * toArray returns an Integer array containing all elements from
280 <     * the set
279 >     * toArray(Integer array) returns an Integer array containing all
280 >     * elements from the set in insertion order
281       */
282      public void testToArray2() {
283 <        CopyOnWriteArraySet full = populatedSet(3);
284 <        Integer[] i = new Integer[3];
285 <        i = (Integer[])full.toArray(i);
286 <        assertEquals(3, i.length);
287 <        assertEquals(0, (int) i[0]);
288 <        assertEquals(1, (int) i[1]);
289 <        assertEquals(2, (int) i[2]);
283 >        Collection empty = new CopyOnWriteArraySet();
284 >        Integer[] a;
285 >
286 >        a = new Integer[0];
287 >        assertSame(a, empty.toArray(a));
288 >
289 >        a = new Integer[SIZE/2];
290 >        Arrays.fill(a, 42);
291 >        assertSame(a, empty.toArray(a));
292 >        assertNull(a[0]);
293 >        for (int i = 1; i < a.length; i++)
294 >            assertEquals(42, (int) a[i]);
295 >
296 >        Integer[] elements = new Integer[SIZE];
297 >        for (int i = 0; i < SIZE; i++)
298 >            elements[i] = i;
299 >        Collections.shuffle(Arrays.asList(elements));
300 >        Collection<Integer> full = populatedSet(elements);
301 >
302 >        Arrays.fill(a, 42);
303 >        assertTrue(Arrays.equals(elements, full.toArray(a)));
304 >        for (int i = 0; i < a.length; i++)
305 >            assertEquals(42, (int) a[i]);
306 >        assertSame(Integer[].class, full.toArray(a).getClass());
307 >
308 >        a = new Integer[SIZE];
309 >        Arrays.fill(a, 42);
310 >        assertSame(a, full.toArray(a));
311 >        assertTrue(Arrays.equals(elements, a));
312 >
313 >        a = new Integer[2*SIZE];
314 >        Arrays.fill(a, 42);
315 >        assertSame(a, full.toArray(a));
316 >        assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
317 >        assertNull(a[SIZE]);
318 >        for (int i = SIZE + 1; i < a.length; i++)
319 >            assertEquals(42, (int) a[i]);
320      }
321  
322      /**
# Line 265 | Line 337 | public class CopyOnWriteArraySetTest ext
337       * A deserialized serialized set is equal
338       */
339      public void testSerialization() throws Exception {
340 <        CopyOnWriteArraySet q = populatedSet(SIZE);
340 >        Set x = populatedSet(SIZE);
341 >        Set y = serialClone(x);
342  
343 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
344 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
345 <        out.writeObject(q);
346 <        out.close();
347 <
348 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
349 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
350 <        CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
351 <        assertEquals(q.size(), r.size());
352 <        assertTrue(q.equals(r));
353 <        assertTrue(r.equals(q));
343 >        assertNotSame(y, x);
344 >        assertEquals(x.size(), y.size());
345 >        assertEquals(x.toString(), y.toString());
346 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
347 >        assertEquals(x, y);
348 >        assertEquals(y, x);
349 >    }
350 >
351 >    /**
352 >     * addAll is idempotent
353 >     */
354 >    public void testAddAll_idempotent() throws Exception {
355 >        Set x = populatedSet(SIZE);
356 >        Set y = new CopyOnWriteArraySet(x);
357 >        y.addAll(x);
358 >        assertEquals(x, y);
359 >        assertEquals(y, x);
360      }
361  
362   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines