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.21 by jsr166, Tue Nov 29 05:09:05 2011 UTC vs.
Revision 1.27 by jsr166, Thu May 30 03:28:55 2013 UTC

# Line 7 | Line 7
7   */
8  
9   import junit.framework.*;
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;
# Line 25 | Line 28 | public class CopyOnWriteArraySetTest ext
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)
31 >        for (int i = 0; i < n; i++)
32              a.add(i);
33          assertFalse(a.isEmpty());
34          assertEquals(n, a.size());
# Line 167 | Line 170 | public class CopyOnWriteArraySetTest ext
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, i.next());
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      /**
# Line 195 | 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 247 | Line 270 | public class CopyOnWriteArraySetTest ext
270          for (int i = 0; i < SIZE; i++)
271              elements[i] = i;
272          Collections.shuffle(Arrays.asList(elements));
273 <        CopyOnWriteArraySet<Integer> full = populatedSet(elements);
273 >        Collection<Integer> full = populatedSet(elements);
274  
275          assertTrue(Arrays.equals(elements, full.toArray()));
276          assertSame(Object[].class, full.toArray().getClass());
# Line 258 | Line 281 | public class CopyOnWriteArraySetTest ext
281       * elements from the set in insertion order
282       */
283      public void testToArray2() {
284 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
284 >        Collection empty = new CopyOnWriteArraySet();
285          Integer[] a;
286  
287          a = new Integer[0];
# Line 275 | Line 298 | public class CopyOnWriteArraySetTest ext
298          for (int i = 0; i < SIZE; i++)
299              elements[i] = i;
300          Collections.shuffle(Arrays.asList(elements));
301 <        CopyOnWriteArraySet<Integer> full = populatedSet(elements);
301 >        Collection<Integer> full = populatedSet(elements);
302  
303          Arrays.fill(a, 42);
304          assertTrue(Arrays.equals(elements, full.toArray(a)));
# Line 318 | Line 341 | public class CopyOnWriteArraySetTest ext
341          Set x = populatedSet(SIZE);
342          Set y = serialClone(x);
343  
344 <        assertTrue(x != y);
344 >        assertNotSame(y, x);
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 +    /**
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      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines