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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines