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.25 by jsr166, Wed Nov 30 05:42:38 2011 UTC vs.
Revision 1.34 by jsr166, Sat May 23 00:53:08 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;
# Line 14 | Line 13 | import java.util.Collections;
13   import java.util.Iterator;
14   import java.util.NoSuchElementException;
15   import java.util.Set;
17 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());
23 >        main(suite(), args);
24      }
25      public static Test suite() {
26          return new TestSuite(CopyOnWriteArraySetTest.class);
# Line 30 | Line 31 | public class CopyOnWriteArraySetTest ext
31          assertTrue(a.isEmpty());
32          for (int i = 0; i < n; i++)
33              a.add(i);
34 <        assertFalse(a.isEmpty());
34 >        assertEquals(n == 0, a.isEmpty());
35          assertEquals(n, a.size());
36          return a;
37      }
# Line 58 | Line 59 | public class CopyOnWriteArraySetTest ext
59       */
60      public void testConstructor3() {
61          Integer[] ints = new Integer[SIZE];
62 <        for (int i = 0; i < SIZE-1; ++i)
62 >        for (int i = 0; i < SIZE - 1; ++i)
63              ints[i] = new Integer(i);
64          CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
65          for (int i = 0; i < SIZE; ++i)
# Line 66 | Line 67 | public class CopyOnWriteArraySetTest ext
67      }
68  
69      /**
70 <     * addAll adds each element from the given collection
70 >     * addAll adds each non-duplicate element from the given collection
71       */
72      public void testAddAll() {
73 <        CopyOnWriteArraySet full = populatedSet(3);
74 <        Vector v = new Vector();
75 <        v.add(three);
76 <        v.add(four);
76 <        v.add(five);
77 <        full.addAll(v);
73 >        Set full = populatedSet(3);
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  
80      /**
81 <     * addAll adds each element from the given collection that did not
83 <     * already exist in the set
81 >     * addAll adds each non-duplicate element from the given collection
82       */
83      public void testAddAll2() {
84 <        CopyOnWriteArraySet full = populatedSet(3);
85 <        Vector v = new Vector();
86 <        v.add(three);
87 <        v.add(four);
88 <        v.add(one); // will not add this element
91 <        full.addAll(v);
84 >        Set full = populatedSet(3);
85 >        // "one" is duplicate and will not be added
86 >        assertTrue(full.addAll(Arrays.asList(three, four, one)));
87 >        assertEquals(5, full.size());
88 >        assertFalse(full.addAll(Arrays.asList(three, four, one)));
89          assertEquals(5, full.size());
90      }
91  
# Line 96 | Line 93 | public class CopyOnWriteArraySetTest ext
93       * add will not add the element if it already exists in the set
94       */
95      public void testAdd2() {
96 <        CopyOnWriteArraySet full = populatedSet(3);
96 >        Set full = populatedSet(3);
97          full.add(one);
98          assertEquals(3, full.size());
99      }
# Line 105 | Line 102 | public class CopyOnWriteArraySetTest ext
102       * add adds the element when it does not exist in the set
103       */
104      public void testAdd3() {
105 <        CopyOnWriteArraySet full = populatedSet(3);
105 >        Set full = populatedSet(3);
106          full.add(three);
107          assertTrue(full.contains(three));
108      }
# Line 114 | Line 111 | public class CopyOnWriteArraySetTest ext
111       * clear removes all elements from the set
112       */
113      public void testClear() {
114 <        CopyOnWriteArraySet full = populatedSet(3);
114 >        Collection full = populatedSet(3);
115          full.clear();
116          assertEquals(0, full.size());
117 +        assertTrue(full.isEmpty());
118      }
119  
120      /**
121       * contains returns true for added elements
122       */
123      public void testContains() {
124 <        CopyOnWriteArraySet full = populatedSet(3);
124 >        Collection full = populatedSet(3);
125          assertTrue(full.contains(one));
126          assertFalse(full.contains(five));
127      }
# Line 150 | Line 148 | public class CopyOnWriteArraySetTest ext
148       * containsAll returns true for collections with subset of elements
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);
159 <        assertFalse(full.containsAll(v));
151 >        Collection full = populatedSet(3);
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      /**
160       * isEmpty is true when empty, else false
161       */
162      public void testIsEmpty() {
163 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
164 <        CopyOnWriteArraySet full = populatedSet(3);
168 <        assertTrue(empty.isEmpty());
169 <        assertFalse(full.isEmpty());
163 >        assertTrue(populatedSet(0).isEmpty());
164 >        assertFalse(populatedSet(3).isEmpty());
165      }
166  
167      /**
# Line 192 | Line 187 | public class CopyOnWriteArraySetTest ext
187              assertTrue(it.hasNext());
188              assertEquals(elements[j], it.next());
189          }
190 <        assertFalse(it.hasNext());
191 <        try {
192 <            it.next();
193 <            shouldThrow();
194 <        } catch (NoSuchElementException success) {}
190 >        assertIteratorExhausted(it);
191 >    }
192 >
193 >    /**
194 >     * iterator of empty collection has no elements
195 >     */
196 >    public void testEmptyIterator() {
197 >        assertIteratorExhausted(new CopyOnWriteArraySet().iterator());
198      }
199  
200      /**
201       * iterator remove is unsupported
202       */
203      public void testIteratorRemove() {
204 <        CopyOnWriteArraySet full = populatedSet(3);
204 >        Collection full = populatedSet(3);
205          Iterator it = full.iterator();
206          it.next();
207          try {
# Line 217 | Line 215 | public class CopyOnWriteArraySetTest ext
215       */
216      public void testToString() {
217          assertEquals("[]", new CopyOnWriteArraySet().toString());
218 <        CopyOnWriteArraySet full = populatedSet(3);
218 >        Collection full = populatedSet(3);
219          String s = full.toString();
220          for (int i = 0; i < 3; ++i)
221              assertTrue(s.contains(String.valueOf(i)));
# Line 229 | Line 227 | public class CopyOnWriteArraySetTest ext
227       * removeAll removes all elements from the given collection
228       */
229      public void testRemoveAll() {
230 <        CopyOnWriteArraySet full = populatedSet(3);
231 <        Vector v = new Vector();
232 <        v.add(one);
233 <        v.add(two);
236 <        full.removeAll(v);
230 >        Set full = populatedSet(3);
231 >        assertTrue(full.removeAll(Arrays.asList(one, two)));
232 >        assertEquals(1, full.size());
233 >        assertFalse(full.removeAll(Arrays.asList(one, two)));
234          assertEquals(1, full.size());
235      }
236  
# Line 241 | Line 238 | public class CopyOnWriteArraySetTest ext
238       * remove removes an element
239       */
240      public void testRemove() {
241 <        CopyOnWriteArraySet full = populatedSet(3);
241 >        Collection full = populatedSet(3);
242          full.remove(one);
243          assertFalse(full.contains(one));
244          assertEquals(2, full.size());
# Line 251 | Line 248 | public class CopyOnWriteArraySetTest ext
248       * size returns the number of elements
249       */
250      public void testSize() {
251 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
252 <        CopyOnWriteArraySet full = populatedSet(3);
251 >        Collection empty = new CopyOnWriteArraySet();
252 >        Collection full = populatedSet(3);
253          assertEquals(3, full.size());
254          assertEquals(0, empty.size());
255      }
# Line 287 | Line 284 | public class CopyOnWriteArraySetTest ext
284          a = new Integer[0];
285          assertSame(a, empty.toArray(a));
286  
287 <        a = new Integer[SIZE/2];
287 >        a = new Integer[SIZE / 2];
288          Arrays.fill(a, 42);
289          assertSame(a, empty.toArray(a));
290          assertNull(a[0]);
# Line 311 | Line 308 | public class CopyOnWriteArraySetTest ext
308          assertSame(a, full.toArray(a));
309          assertTrue(Arrays.equals(elements, a));
310  
311 <        a = new Integer[2*SIZE];
311 >        a = new Integer[2 * SIZE];
312          Arrays.fill(a, 42);
313          assertSame(a, full.toArray(a));
314          assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
# Line 325 | Line 322 | public class CopyOnWriteArraySetTest ext
322       * not store the objects inside the set
323       */
324      public void testToArray_ArrayStoreException() {
325 +        CopyOnWriteArraySet c = new CopyOnWriteArraySet();
326 +        c.add("zfasdfsdf");
327 +        c.add("asdadasd");
328          try {
329            CopyOnWriteArraySet c = new CopyOnWriteArraySet();
330            c.add("zfasdfsdf");
331            c.add("asdadasd");
329              c.toArray(new Long[5]);
330              shouldThrow();
331          } catch (ArrayStoreException success) {}
# Line 341 | Line 338 | public class CopyOnWriteArraySetTest ext
338          Set x = populatedSet(SIZE);
339          Set y = serialClone(x);
340  
341 <        assertTrue(x != y);
341 >        assertNotSame(y, x);
342          assertEquals(x.size(), y.size());
343          assertEquals(x.toString(), y.toString());
344          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
345          assertEquals(x, y);
346          assertEquals(y, x);
347      }
348 +
349 +    /**
350 +     * addAll is idempotent
351 +     */
352 +    public void testAddAll_idempotent() throws Exception {
353 +        Set x = populatedSet(SIZE);
354 +        Set y = new CopyOnWriteArraySet(x);
355 +        y.addAll(x);
356 +        assertEquals(x, y);
357 +        assertEquals(y, x);
358 +    }
359  
360   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines