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.1 by dl, Sun Aug 31 19:24:54 2003 UTC vs.
Revision 1.41 by jsr166, Tue May 8 00:25:33 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines