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.28 by jsr166, Wed Dec 31 19:05:42 2014 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
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());
# Line 25 | Line 30 | public class CopyOnWriteArraySetTest ext
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)
33 >        for (int i = 0; i < n; i++)
34              a.add(i);
35          assertFalse(a.isEmpty());
36          assertEquals(n, a.size());
# Line 167 | 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, i.next());
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      /**
# Line 195 | 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) {
224 >        for (int i = 0; i < 3; ++i)
225              assertTrue(s.contains(String.valueOf(i)));
226 <        }
226 >        assertEquals(new ArrayList(full).toString(),
227 >                     full.toString());
228      }
229  
230      /**
# Line 247 | Line 272 | public class CopyOnWriteArraySetTest ext
272          for (int i = 0; i < SIZE; i++)
273              elements[i] = i;
274          Collections.shuffle(Arrays.asList(elements));
275 <        CopyOnWriteArraySet<Integer> full = populatedSet(elements);
275 >        Collection<Integer> full = populatedSet(elements);
276  
277          assertTrue(Arrays.equals(elements, full.toArray()));
278          assertSame(Object[].class, full.toArray().getClass());
# Line 258 | Line 283 | public class CopyOnWriteArraySetTest ext
283       * elements from the set in insertion order
284       */
285      public void testToArray2() {
286 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
286 >        Collection empty = new CopyOnWriteArraySet();
287          Integer[] a;
288  
289          a = new Integer[0];
# Line 275 | Line 300 | public class CopyOnWriteArraySetTest ext
300          for (int i = 0; i < SIZE; i++)
301              elements[i] = i;
302          Collections.shuffle(Arrays.asList(elements));
303 <        CopyOnWriteArraySet<Integer> full = populatedSet(elements);
303 >        Collection<Integer> full = populatedSet(elements);
304  
305          Arrays.fill(a, 42);
306          assertTrue(Arrays.equals(elements, full.toArray(a)));
# Line 318 | Line 343 | public class CopyOnWriteArraySetTest ext
343          Set x = populatedSet(SIZE);
344          Set y = serialClone(x);
345  
346 <        assertTrue(x != y);
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      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines