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.18 by jsr166, Fri May 27 19:26:42 2011 UTC vs.
Revision 1.40 by jsr166, Thu Apr 5 02:20:21 2018 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.io.*;
9 > import java.util.ArrayList;
10 > import java.util.Arrays;
11 > import java.util.Collection;
12 > import java.util.Iterator;
13 > import java.util.NoSuchElementException;
14 > import java.util.Set;
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);
25 >        class Implementation implements CollectionImplementation {
26 >            public Class<?> klazz() { return CopyOnWriteArraySet.class; }
27 >            public Set emptyCollection() { return new CopyOnWriteArraySet(); }
28 >            public Object makeElement(int i) { return i; }
29 >            public boolean isConcurrent() { return true; }
30 >            public boolean permitsNulls() { return true; }
31 >        }
32 >        return newTestSuite(
33 >                CopyOnWriteArraySetTest.class,
34 >                CollectionTest.testSuite(new Implementation()));
35      }
36  
37 <    static CopyOnWriteArraySet populatedSet(int n) {
38 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
37 >    static CopyOnWriteArraySet<Integer> populatedSet(int n) {
38 >        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<>();
39          assertTrue(a.isEmpty());
40 <        for (int i = 0; i < n; ++i)
41 <            a.add(new Integer(i));
42 <        assertFalse(a.isEmpty());
40 >        for (int i = 0; i < n; i++)
41 >            a.add(i);
42 >        assertEquals(n == 0, a.isEmpty());
43          assertEquals(n, a.size());
44          return a;
45      }
46  
47 +    static CopyOnWriteArraySet populatedSet(Integer[] elements) {
48 +        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<>();
49 +        assertTrue(a.isEmpty());
50 +        for (int i = 0; i < elements.length; i++)
51 +            a.add(elements[i]);
52 +        assertFalse(a.isEmpty());
53 +        assertEquals(elements.length, a.size());
54 +        return a;
55 +    }
56 +
57      /**
58       * Default-constructed set is empty
59       */
# Line 42 | Line 67 | public class CopyOnWriteArraySetTest ext
67       */
68      public void testConstructor3() {
69          Integer[] ints = new Integer[SIZE];
70 <        for (int i = 0; i < SIZE-1; ++i)
70 >        for (int i = 0; i < SIZE - 1; ++i)
71              ints[i] = new Integer(i);
72          CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
73          for (int i = 0; i < SIZE; ++i)
# Line 50 | Line 75 | public class CopyOnWriteArraySetTest ext
75      }
76  
77      /**
78 <     * addAll adds each element from the given collection
78 >     * addAll adds each non-duplicate element from the given collection
79       */
80      public void testAddAll() {
81 <        CopyOnWriteArraySet full = populatedSet(3);
82 <        Vector v = new Vector();
83 <        v.add(three);
84 <        v.add(four);
60 <        v.add(five);
61 <        full.addAll(v);
81 >        Set full = populatedSet(3);
82 >        assertTrue(full.addAll(Arrays.asList(three, four, five)));
83 >        assertEquals(6, full.size());
84 >        assertFalse(full.addAll(Arrays.asList(three, four, five)));
85          assertEquals(6, full.size());
86      }
87  
88      /**
89 <     * addAll adds each element from the given collection that did not
67 <     * already exist in the set
89 >     * addAll adds each non-duplicate element from the given collection
90       */
91      public void testAddAll2() {
92 <        CopyOnWriteArraySet full = populatedSet(3);
93 <        Vector v = new Vector();
94 <        v.add(three);
95 <        v.add(four);
96 <        v.add(one); // will not add this element
75 <        full.addAll(v);
92 >        Set full = populatedSet(3);
93 >        // "one" is duplicate and will not be added
94 >        assertTrue(full.addAll(Arrays.asList(three, four, one)));
95 >        assertEquals(5, full.size());
96 >        assertFalse(full.addAll(Arrays.asList(three, four, one)));
97          assertEquals(5, full.size());
98      }
99  
# Line 80 | Line 101 | public class CopyOnWriteArraySetTest ext
101       * add will not add the element if it already exists in the set
102       */
103      public void testAdd2() {
104 <        CopyOnWriteArraySet full = populatedSet(3);
104 >        Set full = populatedSet(3);
105          full.add(one);
106          assertEquals(3, full.size());
107      }
# Line 89 | Line 110 | public class CopyOnWriteArraySetTest ext
110       * add adds the element when it does not exist in the set
111       */
112      public void testAdd3() {
113 <        CopyOnWriteArraySet full = populatedSet(3);
113 >        Set full = populatedSet(3);
114          full.add(three);
115          assertTrue(full.contains(three));
116      }
# Line 98 | Line 119 | public class CopyOnWriteArraySetTest ext
119       * clear removes all elements from the set
120       */
121      public void testClear() {
122 <        CopyOnWriteArraySet full = populatedSet(3);
122 >        Collection full = populatedSet(3);
123          full.clear();
124          assertEquals(0, full.size());
125 +        assertTrue(full.isEmpty());
126      }
127  
128      /**
129       * contains returns true for added elements
130       */
131      public void testContains() {
132 <        CopyOnWriteArraySet full = populatedSet(3);
132 >        Collection full = populatedSet(3);
133          assertTrue(full.contains(one));
134          assertFalse(full.contains(five));
135      }
# Line 120 | Line 142 | public class CopyOnWriteArraySetTest ext
142          CopyOnWriteArraySet b = populatedSet(3);
143          assertTrue(a.equals(b));
144          assertTrue(b.equals(a));
145 +        assertTrue(a.containsAll(b));
146 +        assertTrue(b.containsAll(a));
147          assertEquals(a.hashCode(), b.hashCode());
148 +        assertEquals(a.size(), b.size());
149 +
150          a.add(m1);
151          assertFalse(a.equals(b));
152          assertFalse(b.equals(a));
153 +        assertTrue(a.containsAll(b));
154 +        assertFalse(b.containsAll(a));
155          b.add(m1);
156          assertTrue(a.equals(b));
157          assertTrue(b.equals(a));
158 +        assertTrue(a.containsAll(b));
159 +        assertTrue(b.containsAll(a));
160          assertEquals(a.hashCode(), b.hashCode());
161 +
162 +        Object x = a.iterator().next();
163 +        a.remove(x);
164 +        assertFalse(a.equals(b));
165 +        assertFalse(b.equals(a));
166 +        assertFalse(a.containsAll(b));
167 +        assertTrue(b.containsAll(a));
168 +        a.add(x);
169 +        assertTrue(a.equals(b));
170 +        assertTrue(b.equals(a));
171 +        assertTrue(a.containsAll(b));
172 +        assertTrue(b.containsAll(a));
173 +        assertEquals(a.hashCode(), b.hashCode());
174 +        assertEquals(a.size(), b.size());
175 +
176 +        CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList());
177 +        CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList());
178 +        assertTrue(empty1.equals(empty1));
179 +        assertTrue(empty1.equals(empty2));
180 +
181 +        assertFalse(empty1.equals(a));
182 +        assertFalse(a.equals(empty1));
183 +
184 +        assertFalse(a.equals(null));
185      }
186  
187      /**
188       * containsAll returns true for collections with subset of elements
189       */
190      public void testContainsAll() {
191 <        CopyOnWriteArraySet full = populatedSet(3);
192 <        Vector v = new Vector();
193 <        v.add(one);
194 <        v.add(two);
195 <        assertTrue(full.containsAll(v));
196 <        v.add(six);
197 <        assertFalse(full.containsAll(v));
191 >        Collection full = populatedSet(3);
192 >        assertTrue(full.containsAll(full));
193 >        assertTrue(full.containsAll(Arrays.asList()));
194 >        assertTrue(full.containsAll(Arrays.asList(one)));
195 >        assertTrue(full.containsAll(Arrays.asList(one, two)));
196 >        assertFalse(full.containsAll(Arrays.asList(one, two, six)));
197 >        assertFalse(full.containsAll(Arrays.asList(six)));
198 >
199 >        CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList());
200 >        CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList());
201 >        assertTrue(empty1.containsAll(empty2));
202 >        assertTrue(empty1.containsAll(empty1));
203 >        assertFalse(empty1.containsAll(full));
204 >        assertTrue(full.containsAll(empty1));
205 >
206 >        try {
207 >            full.containsAll(null);
208 >            shouldThrow();
209 >        } catch (NullPointerException success) {}
210      }
211  
212      /**
213       * isEmpty is true when empty, else false
214       */
215      public void testIsEmpty() {
216 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
217 <        CopyOnWriteArraySet full = populatedSet(3);
152 <        assertTrue(empty.isEmpty());
153 <        assertFalse(full.isEmpty());
216 >        assertTrue(populatedSet(0).isEmpty());
217 >        assertFalse(populatedSet(3).isEmpty());
218      }
219  
220      /**
221 <     * iterator() returns an iterator containing the elements of the set
221 >     * iterator() returns an iterator containing the elements of the
222 >     * set in insertion order
223       */
224      public void testIterator() {
225 <        CopyOnWriteArraySet full = populatedSet(3);
226 <        Iterator i = full.iterator();
227 <        int j;
228 <        for (j = 0; i.hasNext(); j++)
229 <            assertEquals(j, i.next());
230 <        assertEquals(3, j);
225 >        Collection empty = new CopyOnWriteArraySet();
226 >        assertFalse(empty.iterator().hasNext());
227 >        try {
228 >            empty.iterator().next();
229 >            shouldThrow();
230 >        } catch (NoSuchElementException success) {}
231 >
232 >        Integer[] elements = new Integer[SIZE];
233 >        for (int i = 0; i < SIZE; i++)
234 >            elements[i] = i;
235 >        shuffle(elements);
236 >        Collection<Integer> full = populatedSet(elements);
237 >
238 >        Iterator it = full.iterator();
239 >        for (int j = 0; j < SIZE; j++) {
240 >            assertTrue(it.hasNext());
241 >            assertEquals(elements[j], it.next());
242 >        }
243 >        assertIteratorExhausted(it);
244 >    }
245 >
246 >    /**
247 >     * iterator of empty collection has no elements
248 >     */
249 >    public void testEmptyIterator() {
250 >        assertIteratorExhausted(new CopyOnWriteArraySet().iterator());
251      }
252  
253      /**
254       * iterator remove is unsupported
255       */
256      public void testIteratorRemove() {
257 <        CopyOnWriteArraySet full = populatedSet(3);
257 >        Collection full = populatedSet(3);
258          Iterator it = full.iterator();
259          it.next();
260          try {
# Line 182 | Line 267 | public class CopyOnWriteArraySetTest ext
267       * toString holds toString of elements
268       */
269      public void testToString() {
270 <        CopyOnWriteArraySet full = populatedSet(3);
270 >        assertEquals("[]", new CopyOnWriteArraySet().toString());
271 >        Collection full = populatedSet(3);
272          String s = full.toString();
273 <        for (int i = 0; i < 3; ++i) {
273 >        for (int i = 0; i < 3; ++i)
274              assertTrue(s.contains(String.valueOf(i)));
275 <        }
275 >        assertEquals(new ArrayList(full).toString(),
276 >                     full.toString());
277      }
278  
279      /**
280       * removeAll removes all elements from the given collection
281       */
282      public void testRemoveAll() {
283 <        CopyOnWriteArraySet full = populatedSet(3);
284 <        Vector v = new Vector();
285 <        v.add(one);
286 <        v.add(two);
200 <        full.removeAll(v);
283 >        Set full = populatedSet(3);
284 >        assertTrue(full.removeAll(Arrays.asList(one, two)));
285 >        assertEquals(1, full.size());
286 >        assertFalse(full.removeAll(Arrays.asList(one, two)));
287          assertEquals(1, full.size());
288      }
289  
# Line 205 | Line 291 | public class CopyOnWriteArraySetTest ext
291       * remove removes an element
292       */
293      public void testRemove() {
294 <        CopyOnWriteArraySet full = populatedSet(3);
294 >        Collection full = populatedSet(3);
295          full.remove(one);
296          assertFalse(full.contains(one));
297          assertEquals(2, full.size());
# Line 215 | Line 301 | public class CopyOnWriteArraySetTest ext
301       * size returns the number of elements
302       */
303      public void testSize() {
304 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
305 <        CopyOnWriteArraySet full = populatedSet(3);
304 >        Collection empty = new CopyOnWriteArraySet();
305 >        Collection full = populatedSet(3);
306          assertEquals(3, full.size());
307          assertEquals(0, empty.size());
308      }
309  
310      /**
311 <     * toArray returns an Object array containing all elements from the set
311 >     * toArray() returns an Object array containing all elements from
312 >     * the set in insertion order
313       */
314      public void testToArray() {
315 <        CopyOnWriteArraySet full = populatedSet(3);
316 <        Object[] o = full.toArray();
317 <        assertEquals(3, o.length);
318 <        assertEquals(0, o[0]);
319 <        assertEquals(1, o[1]);
320 <        assertEquals(2, o[2]);
315 >        Object[] a = new CopyOnWriteArraySet().toArray();
316 >        assertTrue(Arrays.equals(new Object[0], a));
317 >        assertSame(Object[].class, a.getClass());
318 >
319 >        Integer[] elements = new Integer[SIZE];
320 >        for (int i = 0; i < SIZE; i++)
321 >            elements[i] = i;
322 >        shuffle(elements);
323 >        Collection<Integer> full = populatedSet(elements);
324 >
325 >        assertTrue(Arrays.equals(elements, full.toArray()));
326 >        assertSame(Object[].class, full.toArray().getClass());
327      }
328  
329      /**
330 <     * toArray returns an Integer array containing all elements from
331 <     * the set
330 >     * toArray(Integer array) returns an Integer array containing all
331 >     * elements from the set in insertion order
332       */
333      public void testToArray2() {
334 <        CopyOnWriteArraySet full = populatedSet(3);
335 <        Integer[] i = new Integer[3];
336 <        i = (Integer[])full.toArray(i);
337 <        assertEquals(3, i.length);
338 <        assertEquals(0, (int) i[0]);
339 <        assertEquals(1, (int) i[1]);
340 <        assertEquals(2, (int) i[2]);
334 >        Collection empty = new CopyOnWriteArraySet();
335 >        Integer[] a;
336 >
337 >        a = new Integer[0];
338 >        assertSame(a, empty.toArray(a));
339 >
340 >        a = new Integer[SIZE / 2];
341 >        Arrays.fill(a, 42);
342 >        assertSame(a, empty.toArray(a));
343 >        assertNull(a[0]);
344 >        for (int i = 1; i < a.length; i++)
345 >            assertEquals(42, (int) a[i]);
346 >
347 >        Integer[] elements = new Integer[SIZE];
348 >        for (int i = 0; i < SIZE; i++)
349 >            elements[i] = i;
350 >        shuffle(elements);
351 >        Collection<Integer> full = populatedSet(elements);
352 >
353 >        Arrays.fill(a, 42);
354 >        assertTrue(Arrays.equals(elements, full.toArray(a)));
355 >        for (int i = 0; i < a.length; i++)
356 >            assertEquals(42, (int) a[i]);
357 >        assertSame(Integer[].class, full.toArray(a).getClass());
358 >
359 >        a = new Integer[SIZE];
360 >        Arrays.fill(a, 42);
361 >        assertSame(a, full.toArray(a));
362 >        assertTrue(Arrays.equals(elements, a));
363 >
364 >        a = new Integer[2 * SIZE];
365 >        Arrays.fill(a, 42);
366 >        assertSame(a, full.toArray(a));
367 >        assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
368 >        assertNull(a[SIZE]);
369 >        for (int i = SIZE + 1; i < a.length; i++)
370 >            assertEquals(42, (int) a[i]);
371      }
372  
373      /**
# Line 252 | Line 375 | public class CopyOnWriteArraySetTest ext
375       * not store the objects inside the set
376       */
377      public void testToArray_ArrayStoreException() {
378 +        CopyOnWriteArraySet c = new CopyOnWriteArraySet();
379 +        c.add("zfasdfsdf");
380 +        c.add("asdadasd");
381          try {
256            CopyOnWriteArraySet c = new CopyOnWriteArraySet();
257            c.add("zfasdfsdf");
258            c.add("asdadasd");
382              c.toArray(new Long[5]);
383              shouldThrow();
384          } catch (ArrayStoreException success) {}
385      }
386  
387      /**
388 <     * A deserialized serialized set is equal
388 >     * A deserialized/reserialized set equals original
389       */
390      public void testSerialization() throws Exception {
391 <        CopyOnWriteArraySet q = populatedSet(SIZE);
391 >        Set x = populatedSet(SIZE);
392 >        Set y = serialClone(x);
393  
394 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
395 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
396 <        out.writeObject(q);
397 <        out.close();
398 <
399 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
400 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
401 <        CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
402 <        assertEquals(q.size(), r.size());
403 <        assertTrue(q.equals(r));
404 <        assertTrue(r.equals(q));
394 >        assertNotSame(y, x);
395 >        assertEquals(x.size(), y.size());
396 >        assertEquals(x.toString(), y.toString());
397 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
398 >        assertEquals(x, y);
399 >        assertEquals(y, x);
400 >    }
401 >
402 >    /**
403 >     * addAll is idempotent
404 >     */
405 >    public void testAddAll_idempotent() throws Exception {
406 >        Set x = populatedSet(SIZE);
407 >        Set y = new CopyOnWriteArraySet(x);
408 >        y.addAll(x);
409 >        assertEquals(x, y);
410 >        assertEquals(y, x);
411      }
412  
413   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines