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.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.*;
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;
14 import java.util.Vector;
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) {
23          junit.textui.TestRunner.run(suite());
# Line 25 | Line 29 | public class CopyOnWriteArraySetTest ext
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)
32 >        for (int i = 0; i < n; i++)
33              a.add(i);
34          assertFalse(a.isEmpty());
35          assertEquals(n, a.size());
# Line 67 | 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);
73 <        v.add(five);
74 <        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 81 | 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
88 <        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 148 | 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);
156 <        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 167 | 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 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 207 | 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);
213 <        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 247 | Line 269 | public class CopyOnWriteArraySetTest ext
269          for (int i = 0; i < SIZE; i++)
270              elements[i] = i;
271          Collections.shuffle(Arrays.asList(elements));
272 <        CopyOnWriteArraySet<Integer> full = populatedSet(elements);
272 >        Collection<Integer> full = populatedSet(elements);
273  
274          assertTrue(Arrays.equals(elements, full.toArray()));
275          assertSame(Object[].class, full.toArray().getClass());
# Line 258 | Line 280 | public class CopyOnWriteArraySetTest ext
280       * elements from the set in insertion order
281       */
282      public void testToArray2() {
283 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
283 >        Collection empty = new CopyOnWriteArraySet();
284          Integer[] a;
285  
286          a = new Integer[0];
# Line 275 | Line 297 | public class CopyOnWriteArraySetTest ext
297          for (int i = 0; i < SIZE; i++)
298              elements[i] = i;
299          Collections.shuffle(Arrays.asList(elements));
300 <        CopyOnWriteArraySet<Integer> full = populatedSet(elements);
300 >        Collection<Integer> full = populatedSet(elements);
301  
302          Arrays.fill(a, 42);
303          assertTrue(Arrays.equals(elements, full.toArray(a)));
# Line 318 | Line 340 | public class CopyOnWriteArraySetTest ext
340          Set x = populatedSet(SIZE);
341          Set y = serialClone(x);
342  
343 <        assertTrue(x != y);
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      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines