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.7 by dl, Mon Dec 29 19:05:40 2003 UTC vs.
Revision 1.37 by jsr166, Wed Aug 24 22:22:39 2016 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
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 >        return new TestSuite(CopyOnWriteArraySetTest.class);
26      }
27  
28 <    static CopyOnWriteArraySet populatedSet(int n){
29 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
28 >    static CopyOnWriteArraySet<Integer> populatedSet(int n) {
29 >        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
30          assertTrue(a.isEmpty());
31 <        for (int i = 0; i < n; ++i)
32 <            a.add(new Integer(i));
33 <        assertFalse(a.isEmpty());
31 >        for (int i = 0; i < n; i++)
32 >            a.add(i);
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>();
40 +        assertTrue(a.isEmpty());
41 +        for (int i = 0; i < elements.length; i++)
42 +            a.add(elements[i]);
43 +        assertFalse(a.isEmpty());
44 +        assertEquals(elements.length, a.size());
45 +        return a;
46 +    }
47 +
48      /**
49       * Default-constructed set is empty
50       */
51      public void testConstructor() {
52 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
52 >        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
53          assertTrue(a.isEmpty());
54      }
55  
# Line 42 | 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)
63 >        CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
64 >        for (int i = 0; i < SIZE; ++i)
65              assertTrue(a.contains(ints[i]));
66      }
51        
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);
76 <        v.add(five);
62 <        full.addAll(v);
63 <        assertEquals(6, full.size());
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
68 <     *  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
88 <        full.addAll(v);
77 <        assertEquals(5, full.size());
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  
91      /**
92 <     *   add will not add the element if it already exists in the set
92 >     * add will not add the element if it already exists in the set
93       */
94      public void testAdd2() {
95 <        CopyOnWriteArraySet full = populatedSet(3);
96 <        full.add(one);
97 <        assertEquals(3, full.size());
95 >        Set full = populatedSet(3);
96 >        full.add(one);
97 >        assertEquals(3, full.size());
98      }
99  
100      /**
101 <     *   add  adds the element when it does not exist
91 <     *   in the set
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      }
108  
109      /**
110 <     *   clear  removes all elements from the set
110 >     * clear removes all elements from the set
111       */
112      public void testClear() {
113 <        CopyOnWriteArraySet full = populatedSet(3);
114 <        full.clear();
115 <        assertEquals(0, full.size());
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
120 >     * contains returns true for added elements
121       */
122      public void testContains() {
123 <        CopyOnWriteArraySet full = populatedSet(3);
124 <        assertTrue(full.contains(one));
125 <        assertFalse(full.contains(five));
123 >        Collection full = populatedSet(3);
124 >        assertTrue(full.contains(one));
125 >        assertFalse(full.contains(five));
126      }
127  
128      /**
129       * Sets with equal elements are equal
130       */
131      public void testEquals() {
132 <        CopyOnWriteArraySet a = populatedSet(3);
133 <        CopyOnWriteArraySet b = populatedSet(3);
132 >        CopyOnWriteArraySet a = populatedSet(3);
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  
135    
178      /**
179 <     *   containsAll returns true for collections with subset of elements
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
204 >     * isEmpty is true when empty, else false
205       */
206      public void testIsEmpty() {
207 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
208 <        CopyOnWriteArraySet full = populatedSet(3);
155 <        assertTrue(empty.isEmpty());
156 <        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, ((Integer)i.next()).intValue());
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);
247 >    public void testIteratorRemove() {
248 >        Collection full = populatedSet(3);
249          Iterator it = full.iterator();
250          it.next();
251          try {
252              it.remove();
253              shouldThrow();
254 <        }
182 <        catch (UnsupportedOperationException success) {}
254 >        } catch (UnsupportedOperationException success) {}
255      }
256  
257      /**
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) {
265 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
266 <        }
267 <    }        
268 <
264 >        for (int i = 0; i < 3; ++i)
265 >            assertTrue(s.contains(String.valueOf(i)));
266 >        assertEquals(new ArrayList(full).toString(),
267 >                     full.toString());
268 >    }
269  
270      /**
271 <     *   removeAll  removes all elements from the given collection
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);
278 <        full.removeAll(v);
206 <        assertEquals(1, full.size());
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  
209
281      /**
282       * remove removes an element
283       */
284      public void testRemove() {
285 <        CopyOnWriteArraySet full = populatedSet(3);
286 <        full.remove(one);
285 >        Collection full = populatedSet(3);
286 >        full.remove(one);
287          assertFalse(full.contains(one));
288 <        assertEquals(2, full.size());
288 >        assertEquals(2, full.size());
289      }
290  
291      /**
292 <     *   size returns the number of elements
292 >     * size returns the number of elements
293       */
294      public void testSize() {
295 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
296 <        CopyOnWriteArraySet full = populatedSet(3);
297 <        assertEquals(3, full.size());
298 <        assertEquals(0, empty.size());
295 >        Collection empty = new CopyOnWriteArraySet();
296 >        Collection full = populatedSet(3);
297 >        assertEquals(3, full.size());
298 >        assertEquals(0, empty.size());
299      }
300  
301      /**
302 <     *   toArray returns an Object array containing all elements from the set
302 >     * toArray() returns an Object array containing all elements from
303 >     * the set in insertion order
304       */
305      public void testToArray() {
306 <        CopyOnWriteArraySet full = populatedSet(3);
307 <        Object[] o = full.toArray();
308 <        assertEquals(3, o.length);
309 <        assertEquals(0, ((Integer)o[0]).intValue());
310 <        assertEquals(1, ((Integer)o[1]).intValue());
311 <        assertEquals(2, ((Integer)o[2]).intValue());
306 >        Object[] a = new CopyOnWriteArraySet().toArray();
307 >        assertTrue(Arrays.equals(new Object[0], a));
308 >        assertSame(Object[].class, a.getClass());
309 >
310 >        Integer[] elements = new Integer[SIZE];
311 >        for (int i = 0; i < SIZE; i++)
312 >            elements[i] = i;
313 >        shuffle(elements);
314 >        Collection<Integer> full = populatedSet(elements);
315 >
316 >        assertTrue(Arrays.equals(elements, full.toArray()));
317 >        assertSame(Object[].class, full.toArray().getClass());
318      }
319  
320      /**
321 <     *   toArray returns an Integer array containing all elements from
322 <     *   the set
321 >     * toArray(Integer array) returns an Integer array containing all
322 >     * elements from the set in insertion order
323       */
324      public void testToArray2() {
325 <        CopyOnWriteArraySet full = populatedSet(3);
326 <        Integer[] i = new Integer[3];
327 <        i = (Integer[])full.toArray(i);
328 <        assertEquals(3, i.length);
329 <        assertEquals(0, i[0].intValue());
252 <        assertEquals(1, i[1].intValue());
253 <        assertEquals(2, i[2].intValue());
254 <    }
325 >        Collection empty = new CopyOnWriteArraySet();
326 >        Integer[] a;
327 >
328 >        a = new Integer[0];
329 >        assertSame(a, empty.toArray(a));
330  
331 +        a = new Integer[SIZE / 2];
332 +        Arrays.fill(a, 42);
333 +        assertSame(a, empty.toArray(a));
334 +        assertNull(a[0]);
335 +        for (int i = 1; i < a.length; i++)
336 +            assertEquals(42, (int) a[i]);
337 +
338 +        Integer[] elements = new Integer[SIZE];
339 +        for (int i = 0; i < SIZE; i++)
340 +            elements[i] = i;
341 +        shuffle(elements);
342 +        Collection<Integer> full = populatedSet(elements);
343 +
344 +        Arrays.fill(a, 42);
345 +        assertTrue(Arrays.equals(elements, full.toArray(a)));
346 +        for (int i = 0; i < a.length; i++)
347 +            assertEquals(42, (int) a[i]);
348 +        assertSame(Integer[].class, full.toArray(a).getClass());
349 +
350 +        a = new Integer[SIZE];
351 +        Arrays.fill(a, 42);
352 +        assertSame(a, full.toArray(a));
353 +        assertTrue(Arrays.equals(elements, a));
354 +
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)));
359 +        assertNull(a[SIZE]);
360 +        for (int i = SIZE + 1; i < a.length; i++)
361 +            assertEquals(42, (int) a[i]);
362 +    }
363  
364      /**
365 <     *  toArray throws an ArrayStoreException when the given array can
366 <     *  not store the objects inside the set
365 >     * toArray throws an ArrayStoreException when the given array can
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 {
263            CopyOnWriteArraySet c = new CopyOnWriteArraySet();
264            c.add("zfasdfsdf");
265            c.add("asdadasd");
373              c.toArray(new Long[5]);
374 <            shouldThrow();
375 <        } catch(ArrayStoreException e){}
374 >            shouldThrow();
375 >        } catch (ArrayStoreException success) {}
376      }
377  
378      /**
379       * A deserialized serialized set is equal
380       */
381 <    public void testSerialization() {
382 <        CopyOnWriteArraySet q = populatedSet(SIZE);
381 >    public void testSerialization() throws Exception {
382 >        Set x = populatedSet(SIZE);
383 >        Set y = serialClone(x);
384 >
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 <        try {
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();
286 <            assertEquals(q.size(), r.size());
287 <            assertTrue(q.equals(r));
288 <            assertTrue(r.equals(q));
289 <        } catch(Exception e){
290 <            unexpectedException();
291 <        }
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      }
403  
404   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines