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.22 by jsr166, Tue Nov 29 05:23:56 2011 UTC vs.
Revision 1.38 by jsr166, Wed Jan 4 06:09:58 2017 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;
12   import java.util.Iterator;
13 + import java.util.NoSuchElementException;
14   import java.util.Set;
15 import java.util.Vector;
15   import java.util.concurrent.CopyOnWriteArraySet;
16  
17 + import junit.framework.Test;
18 + import junit.framework.TestSuite;
19 +
20   public class CopyOnWriteArraySetTest extends JSR166TestCase {
21      public static void main(String[] args) {
22 <        junit.textui.TestRunner.run(suite());
22 >        main(suite(), args);
23      }
24      public static Test suite() {
25          return new TestSuite(CopyOnWriteArraySetTest.class);
26      }
27  
28      static CopyOnWriteArraySet<Integer> populatedSet(int n) {
29 <        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
29 >        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<>();
30          assertTrue(a.isEmpty());
31          for (int i = 0; i < n; i++)
32              a.add(i);
33 <        assertFalse(a.isEmpty());
33 >        assertEquals(n == 0, a.isEmpty());
34          assertEquals(n, a.size());
35          return a;
36      }
37  
38      static CopyOnWriteArraySet populatedSet(Integer[] elements) {
39 <        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
39 >        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<>();
40          assertTrue(a.isEmpty());
41          for (int i = 0; i < elements.length; i++)
42              a.add(elements[i]);
# Line 56 | Line 58 | public class CopyOnWriteArraySetTest ext
58       */
59      public void testConstructor3() {
60          Integer[] ints = new Integer[SIZE];
61 <        for (int i = 0; i < SIZE-1; ++i)
61 >        for (int i = 0; i < SIZE - 1; ++i)
62              ints[i] = new Integer(i);
63          CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
64          for (int i = 0; i < SIZE; ++i)
# Line 64 | Line 66 | public class CopyOnWriteArraySetTest ext
66      }
67  
68      /**
69 <     * addAll adds each element from the given collection
69 >     * addAll adds each non-duplicate element from the given collection
70       */
71      public void testAddAll() {
72 <        CopyOnWriteArraySet full = populatedSet(3);
73 <        Vector v = new Vector();
74 <        v.add(three);
75 <        v.add(four);
74 <        v.add(five);
75 <        full.addAll(v);
72 >        Set full = populatedSet(3);
73 >        assertTrue(full.addAll(Arrays.asList(three, four, five)));
74 >        assertEquals(6, full.size());
75 >        assertFalse(full.addAll(Arrays.asList(three, four, five)));
76          assertEquals(6, full.size());
77      }
78  
79      /**
80 <     * addAll adds each element from the given collection that did not
81 <     * already exist in the set
80 >     * addAll adds each non-duplicate element from the given collection
81       */
82      public void testAddAll2() {
83 <        CopyOnWriteArraySet full = populatedSet(3);
84 <        Vector v = new Vector();
85 <        v.add(three);
86 <        v.add(four);
87 <        v.add(one); // will not add this element
89 <        full.addAll(v);
83 >        Set full = populatedSet(3);
84 >        // "one" is duplicate and will not be added
85 >        assertTrue(full.addAll(Arrays.asList(three, four, one)));
86 >        assertEquals(5, full.size());
87 >        assertFalse(full.addAll(Arrays.asList(three, four, one)));
88          assertEquals(5, full.size());
89      }
90  
# Line 94 | Line 92 | public class CopyOnWriteArraySetTest ext
92       * add will not add the element if it already exists in the set
93       */
94      public void testAdd2() {
95 <        CopyOnWriteArraySet full = populatedSet(3);
95 >        Set full = populatedSet(3);
96          full.add(one);
97          assertEquals(3, full.size());
98      }
# Line 103 | Line 101 | public class CopyOnWriteArraySetTest ext
101       * add adds the element when it does not exist in the set
102       */
103      public void testAdd3() {
104 <        CopyOnWriteArraySet full = populatedSet(3);
104 >        Set full = populatedSet(3);
105          full.add(three);
106          assertTrue(full.contains(three));
107      }
# Line 112 | Line 110 | public class CopyOnWriteArraySetTest ext
110       * clear removes all elements from the set
111       */
112      public void testClear() {
113 <        CopyOnWriteArraySet full = populatedSet(3);
113 >        Collection full = populatedSet(3);
114          full.clear();
115          assertEquals(0, full.size());
116 +        assertTrue(full.isEmpty());
117      }
118  
119      /**
120       * contains returns true for added elements
121       */
122      public void testContains() {
123 <        CopyOnWriteArraySet full = populatedSet(3);
123 >        Collection full = populatedSet(3);
124          assertTrue(full.contains(one));
125          assertFalse(full.contains(five));
126      }
# Line 134 | Line 133 | public class CopyOnWriteArraySetTest ext
133          CopyOnWriteArraySet b = populatedSet(3);
134          assertTrue(a.equals(b));
135          assertTrue(b.equals(a));
136 +        assertTrue(a.containsAll(b));
137 +        assertTrue(b.containsAll(a));
138          assertEquals(a.hashCode(), b.hashCode());
139 +        assertEquals(a.size(), b.size());
140 +
141          a.add(m1);
142          assertFalse(a.equals(b));
143          assertFalse(b.equals(a));
144 +        assertTrue(a.containsAll(b));
145 +        assertFalse(b.containsAll(a));
146          b.add(m1);
147          assertTrue(a.equals(b));
148          assertTrue(b.equals(a));
149 +        assertTrue(a.containsAll(b));
150 +        assertTrue(b.containsAll(a));
151 +        assertEquals(a.hashCode(), b.hashCode());
152 +
153 +        Object x = a.iterator().next();
154 +        a.remove(x);
155 +        assertFalse(a.equals(b));
156 +        assertFalse(b.equals(a));
157 +        assertFalse(a.containsAll(b));
158 +        assertTrue(b.containsAll(a));
159 +        a.add(x);
160 +        assertTrue(a.equals(b));
161 +        assertTrue(b.equals(a));
162 +        assertTrue(a.containsAll(b));
163 +        assertTrue(b.containsAll(a));
164          assertEquals(a.hashCode(), b.hashCode());
165 +        assertEquals(a.size(), b.size());
166 +
167 +        CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList());
168 +        CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList());
169 +        assertTrue(empty1.equals(empty1));
170 +        assertTrue(empty1.equals(empty2));
171 +
172 +        assertFalse(empty1.equals(a));
173 +        assertFalse(a.equals(empty1));
174 +
175 +        assertFalse(a.equals(null));
176      }
177  
178      /**
179       * containsAll returns true for collections with subset of elements
180       */
181      public void testContainsAll() {
182 <        CopyOnWriteArraySet full = populatedSet(3);
183 <        Vector v = new Vector();
184 <        v.add(one);
185 <        v.add(two);
186 <        assertTrue(full.containsAll(v));
187 <        v.add(six);
188 <        assertFalse(full.containsAll(v));
182 >        Collection full = populatedSet(3);
183 >        assertTrue(full.containsAll(full));
184 >        assertTrue(full.containsAll(Arrays.asList()));
185 >        assertTrue(full.containsAll(Arrays.asList(one)));
186 >        assertTrue(full.containsAll(Arrays.asList(one, two)));
187 >        assertFalse(full.containsAll(Arrays.asList(one, two, six)));
188 >        assertFalse(full.containsAll(Arrays.asList(six)));
189 >
190 >        CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList());
191 >        CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList());
192 >        assertTrue(empty1.containsAll(empty2));
193 >        assertTrue(empty1.containsAll(empty1));
194 >        assertFalse(empty1.containsAll(full));
195 >        assertTrue(full.containsAll(empty1));
196 >
197 >        try {
198 >            full.containsAll(null);
199 >            shouldThrow();
200 >        } catch (NullPointerException success) {}
201      }
202  
203      /**
204       * isEmpty is true when empty, else false
205       */
206      public void testIsEmpty() {
207 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
208 <        CopyOnWriteArraySet full = populatedSet(3);
166 <        assertTrue(empty.isEmpty());
167 <        assertFalse(full.isEmpty());
207 >        assertTrue(populatedSet(0).isEmpty());
208 >        assertFalse(populatedSet(3).isEmpty());
209      }
210  
211      /**
212 <     * iterator() returns an iterator containing the elements of the set
212 >     * iterator() returns an iterator containing the elements of the
213 >     * set in insertion order
214       */
215      public void testIterator() {
216 <        CopyOnWriteArraySet full = populatedSet(3);
217 <        Iterator i = full.iterator();
218 <        int j;
219 <        for (j = 0; i.hasNext(); j++)
220 <            assertEquals(j, i.next());
221 <        assertEquals(3, j);
216 >        Collection empty = new CopyOnWriteArraySet();
217 >        assertFalse(empty.iterator().hasNext());
218 >        try {
219 >            empty.iterator().next();
220 >            shouldThrow();
221 >        } catch (NoSuchElementException success) {}
222 >
223 >        Integer[] elements = new Integer[SIZE];
224 >        for (int i = 0; i < SIZE; i++)
225 >            elements[i] = i;
226 >        shuffle(elements);
227 >        Collection<Integer> full = populatedSet(elements);
228 >
229 >        Iterator it = full.iterator();
230 >        for (int j = 0; j < SIZE; j++) {
231 >            assertTrue(it.hasNext());
232 >            assertEquals(elements[j], it.next());
233 >        }
234 >        assertIteratorExhausted(it);
235 >    }
236 >
237 >    /**
238 >     * iterator of empty collection has no elements
239 >     */
240 >    public void testEmptyIterator() {
241 >        assertIteratorExhausted(new CopyOnWriteArraySet().iterator());
242      }
243  
244      /**
245       * iterator remove is unsupported
246       */
247      public void testIteratorRemove() {
248 <        CopyOnWriteArraySet full = populatedSet(3);
248 >        Collection full = populatedSet(3);
249          Iterator it = full.iterator();
250          it.next();
251          try {
# Line 196 | Line 258 | public class CopyOnWriteArraySetTest ext
258       * toString holds toString of elements
259       */
260      public void testToString() {
261 <        CopyOnWriteArraySet full = populatedSet(3);
261 >        assertEquals("[]", new CopyOnWriteArraySet().toString());
262 >        Collection full = populatedSet(3);
263          String s = full.toString();
264 <        for (int i = 0; i < 3; ++i) {
264 >        for (int i = 0; i < 3; ++i)
265              assertTrue(s.contains(String.valueOf(i)));
266 <        }
266 >        assertEquals(new ArrayList(full).toString(),
267 >                     full.toString());
268      }
269  
270      /**
271       * removeAll removes all elements from the given collection
272       */
273      public void testRemoveAll() {
274 <        CopyOnWriteArraySet full = populatedSet(3);
275 <        Vector v = new Vector();
276 <        v.add(one);
277 <        v.add(two);
214 <        full.removeAll(v);
274 >        Set full = populatedSet(3);
275 >        assertTrue(full.removeAll(Arrays.asList(one, two)));
276 >        assertEquals(1, full.size());
277 >        assertFalse(full.removeAll(Arrays.asList(one, two)));
278          assertEquals(1, full.size());
279      }
280  
# Line 219 | Line 282 | public class CopyOnWriteArraySetTest ext
282       * remove removes an element
283       */
284      public void testRemove() {
285 <        CopyOnWriteArraySet full = populatedSet(3);
285 >        Collection full = populatedSet(3);
286          full.remove(one);
287          assertFalse(full.contains(one));
288          assertEquals(2, full.size());
# Line 229 | Line 292 | public class CopyOnWriteArraySetTest ext
292       * size returns the number of elements
293       */
294      public void testSize() {
295 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
296 <        CopyOnWriteArraySet full = populatedSet(3);
295 >        Collection empty = new CopyOnWriteArraySet();
296 >        Collection full = populatedSet(3);
297          assertEquals(3, full.size());
298          assertEquals(0, empty.size());
299      }
# Line 247 | Line 310 | public class CopyOnWriteArraySetTest ext
310          Integer[] elements = new Integer[SIZE];
311          for (int i = 0; i < SIZE; i++)
312              elements[i] = i;
313 <        Collections.shuffle(Arrays.asList(elements));
313 >        shuffle(elements);
314          Collection<Integer> full = populatedSet(elements);
315  
316          assertTrue(Arrays.equals(elements, full.toArray()));
# Line 265 | Line 328 | public class CopyOnWriteArraySetTest ext
328          a = new Integer[0];
329          assertSame(a, empty.toArray(a));
330  
331 <        a = new Integer[SIZE/2];
331 >        a = new Integer[SIZE / 2];
332          Arrays.fill(a, 42);
333          assertSame(a, empty.toArray(a));
334          assertNull(a[0]);
# Line 275 | Line 338 | public class CopyOnWriteArraySetTest ext
338          Integer[] elements = new Integer[SIZE];
339          for (int i = 0; i < SIZE; i++)
340              elements[i] = i;
341 <        Collections.shuffle(Arrays.asList(elements));
341 >        shuffle(elements);
342          Collection<Integer> full = populatedSet(elements);
343  
344          Arrays.fill(a, 42);
# Line 289 | Line 352 | public class CopyOnWriteArraySetTest ext
352          assertSame(a, full.toArray(a));
353          assertTrue(Arrays.equals(elements, a));
354  
355 <        a = new Integer[2*SIZE];
355 >        a = new Integer[2 * SIZE];
356          Arrays.fill(a, 42);
357          assertSame(a, full.toArray(a));
358          assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
# Line 303 | Line 366 | public class CopyOnWriteArraySetTest ext
366       * not store the objects inside the set
367       */
368      public void testToArray_ArrayStoreException() {
369 +        CopyOnWriteArraySet c = new CopyOnWriteArraySet();
370 +        c.add("zfasdfsdf");
371 +        c.add("asdadasd");
372          try {
307            CopyOnWriteArraySet c = new CopyOnWriteArraySet();
308            c.add("zfasdfsdf");
309            c.add("asdadasd");
373              c.toArray(new Long[5]);
374              shouldThrow();
375          } catch (ArrayStoreException success) {}
# Line 319 | Line 382 | public class CopyOnWriteArraySetTest ext
382          Set x = populatedSet(SIZE);
383          Set y = serialClone(x);
384  
385 <        assertTrue(x != y);
385 >        assertNotSame(y, x);
386          assertEquals(x.size(), y.size());
387 +        assertEquals(x.toString(), y.toString());
388 +        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
389 +        assertEquals(x, y);
390 +        assertEquals(y, x);
391 +    }
392 +
393 +    /**
394 +     * addAll is idempotent
395 +     */
396 +    public void testAddAll_idempotent() throws Exception {
397 +        Set x = populatedSet(SIZE);
398 +        Set y = new CopyOnWriteArraySet(x);
399 +        y.addAll(x);
400          assertEquals(x, y);
401          assertEquals(y, x);
402      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines