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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.34 by jsr166, Sat May 23 00:53:08 2015 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.*;
12 < import java.io.*;
9 > import java.util.ArrayList;
10 > import java.util.Arrays;
11 > import java.util.Collection;
12 > import java.util.Collections;
13 > import java.util.Iterator;
14 > import java.util.NoSuchElementException;
15 > import java.util.Set;
16 > import java.util.concurrent.CopyOnWriteArraySet;
17 >
18 > import junit.framework.Test;
19 > import junit.framework.TestSuite;
20  
21   public class CopyOnWriteArraySetTest extends JSR166TestCase {
14    
22      public static void main(String[] args) {
23 <        junit.textui.TestRunner.run (suite());  
23 >        main(suite(), args);
24      }
18
25      public static Test suite() {
26 <        return new TestSuite(CopyOnWriteArraySetTest.class);
26 >        return new TestSuite(CopyOnWriteArraySetTest.class);
27      }
28  
29 <    static CopyOnWriteArraySet populatedSet(int n){
30 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
29 >    static CopyOnWriteArraySet<Integer> populatedSet(int n) {
30 >        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
31          assertTrue(a.isEmpty());
32 <        for (int i = 0; i < n; ++i)
33 <            a.add(new Integer(i));
34 <        assertFalse(a.isEmpty());
32 >        for (int i = 0; i < n; i++)
33 >            a.add(i);
34 >        assertEquals(n == 0, a.isEmpty());
35          assertEquals(n, a.size());
36          return a;
37      }
38  
39 +    static CopyOnWriteArraySet populatedSet(Integer[] elements) {
40 +        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
41 +        assertTrue(a.isEmpty());
42 +        for (int i = 0; i < elements.length; i++)
43 +            a.add(elements[i]);
44 +        assertFalse(a.isEmpty());
45 +        assertEquals(elements.length, a.size());
46 +        return a;
47 +    }
48 +
49 +    /**
50 +     * Default-constructed set is empty
51 +     */
52      public void testConstructor() {
53 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
53 >        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
54          assertTrue(a.isEmpty());
55      }
56  
57 +    /**
58 +     * Collection-constructed set holds all of its elements
59 +     */
60      public void testConstructor3() {
61          Integer[] ints = new Integer[SIZE];
62 <        for (int i = 0; i < SIZE-1; ++i)
62 >        for (int i = 0; i < SIZE - 1; ++i)
63              ints[i] = new Integer(i);
64 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
65 <        for (int i = 0; i < SIZE; ++i)
64 >        CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
65 >        for (int i = 0; i < SIZE; ++i)
66              assertTrue(a.contains(ints[i]));
67      }
46        
68  
69      /**
70 <     *   addAll correctly adds each element from the given collection
70 >     * addAll adds each non-duplicate element from the given collection
71       */
72 <    public void testAddAll(){
73 <        CopyOnWriteArraySet full = populatedSet(3);
74 <        Vector v = new Vector();
75 <        v.add(three);
76 <        v.add(four);
77 <        v.add(five);
57 <        full.addAll(v);
58 <        assertEquals(6, full.size());
72 >    public void testAddAll() {
73 >        Set full = populatedSet(3);
74 >        assertTrue(full.addAll(Arrays.asList(three, four, five)));
75 >        assertEquals(6, full.size());
76 >        assertFalse(full.addAll(Arrays.asList(three, four, five)));
77 >        assertEquals(6, full.size());
78      }
79  
80      /**
81 <     *   addAllAbsent adds each element from the given collection that did not
63 <     *  already exist in the List
81 >     * addAll adds each non-duplicate element from the given collection
82       */
83 <    public void testAddAll2(){
84 <        CopyOnWriteArraySet full = populatedSet(3);
85 <        Vector v = new Vector();
86 <        v.add(three);
87 <        v.add(four);
88 <        v.add(one); // will not add this element
89 <        full.addAll(v);
72 <        assertEquals(5, full.size());
83 >    public void testAddAll2() {
84 >        Set full = populatedSet(3);
85 >        // "one" is duplicate and will not be added
86 >        assertTrue(full.addAll(Arrays.asList(three, four, one)));
87 >        assertEquals(5, full.size());
88 >        assertFalse(full.addAll(Arrays.asList(three, four, one)));
89 >        assertEquals(5, full.size());
90      }
91  
92      /**
93 <     *   addIfAbsent will not add the element if it already exists in the list
93 >     * add will not add the element if it already exists in the set
94       */
95 <    public void testAdd2(){
96 <        CopyOnWriteArraySet full = populatedSet(3);
97 <        full.add(one);
98 <        assertEquals(3, full.size());
95 >    public void testAdd2() {
96 >        Set full = populatedSet(3);
97 >        full.add(one);
98 >        assertEquals(3, full.size());
99      }
100  
101      /**
102 <     *   addIfAbsent correctly adds the element when it does not exist in the list
102 >     * add adds the element when it does not exist in the set
103       */
104 <    public void testAdd3(){
105 <        CopyOnWriteArraySet full = populatedSet(3);
104 >    public void testAdd3() {
105 >        Set full = populatedSet(3);
106          full.add(three);
107          assertTrue(full.contains(three));
108      }
109  
110      /**
111 <     *   clear correctly removes all elements from the list
111 >     * clear removes all elements from the set
112       */
113 <    public void testClear(){
114 <        CopyOnWriteArraySet full = populatedSet(3);
115 <        full.clear();
116 <        assertEquals(0, full.size());
113 >    public void testClear() {
114 >        Collection full = populatedSet(3);
115 >        full.clear();
116 >        assertEquals(0, full.size());
117 >        assertTrue(full.isEmpty());
118      }
119  
120      /**
121 <     *   contains returns the correct values
121 >     * contains returns true for added elements
122       */
123 <    public void testContains(){
124 <        CopyOnWriteArraySet full = populatedSet(3);
125 <        assertTrue(full.contains(one));
126 <        assertFalse(full.contains(five));
123 >    public void testContains() {
124 >        Collection full = populatedSet(3);
125 >        assertTrue(full.contains(one));
126 >        assertFalse(full.contains(five));
127      }
128  
129 +    /**
130 +     * Sets with equal elements are equal
131 +     */
132      public void testEquals() {
133 <        CopyOnWriteArraySet a = populatedSet(3);
134 <        CopyOnWriteArraySet b = populatedSet(3);
133 >        CopyOnWriteArraySet a = populatedSet(3);
134 >        CopyOnWriteArraySet b = populatedSet(3);
135          assertTrue(a.equals(b));
136          assertTrue(b.equals(a));
137          assertEquals(a.hashCode(), b.hashCode());
# Line 123 | Line 144 | public class CopyOnWriteArraySetTest ext
144          assertEquals(a.hashCode(), b.hashCode());
145      }
146  
126    
147      /**
148 <     *   containsAll returns the correct values
148 >     * containsAll returns true for collections with subset of elements
149 >     */
150 >    public void testContainsAll() {
151 >        Collection full = populatedSet(3);
152 >        assertTrue(full.containsAll(Arrays.asList()));
153 >        assertTrue(full.containsAll(Arrays.asList(one)));
154 >        assertTrue(full.containsAll(Arrays.asList(one, two)));
155 >        assertFalse(full.containsAll(Arrays.asList(one, two, six)));
156 >        assertFalse(full.containsAll(Arrays.asList(six)));
157 >    }
158 >
159 >    /**
160 >     * isEmpty is true when empty, else false
161       */
162 <    public void testContainsAll(){
163 <        CopyOnWriteArraySet full = populatedSet(3);
164 <        Vector v = new Vector();
133 <        v.add(one);
134 <        v.add(two);
135 <        assertTrue(full.containsAll(v));
136 <        v.add(six);
137 <        assertFalse(full.containsAll(v));
162 >    public void testIsEmpty() {
163 >        assertTrue(populatedSet(0).isEmpty());
164 >        assertFalse(populatedSet(3).isEmpty());
165      }
166  
167      /**
168 <     *   isEmpty returns the correct values
168 >     * iterator() returns an iterator containing the elements of the
169 >     * set in insertion order
170       */
171 <    public void testIsEmpty(){
172 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
173 <        CopyOnWriteArraySet full = populatedSet(3);
174 <        assertTrue(empty.isEmpty());
175 <        assertFalse(full.isEmpty());
171 >    public void testIterator() {
172 >        Collection empty = new CopyOnWriteArraySet();
173 >        assertFalse(empty.iterator().hasNext());
174 >        try {
175 >            empty.iterator().next();
176 >            shouldThrow();
177 >        } catch (NoSuchElementException success) {}
178 >
179 >        Integer[] elements = new Integer[SIZE];
180 >        for (int i = 0; i < SIZE; i++)
181 >            elements[i] = i;
182 >        Collections.shuffle(Arrays.asList(elements));
183 >        Collection<Integer> full = populatedSet(elements);
184 >
185 >        Iterator it = full.iterator();
186 >        for (int j = 0; j < SIZE; j++) {
187 >            assertTrue(it.hasNext());
188 >            assertEquals(elements[j], it.next());
189 >        }
190 >        assertIteratorExhausted(it);
191      }
192  
193      /**
194 <     *   iterator() returns an iterator containing the elements of the list
194 >     * iterator of empty collection has no elements
195       */
196 <    public void testIterator(){
197 <        CopyOnWriteArraySet full = populatedSet(3);
155 <        Iterator i = full.iterator();
156 <        int j;
157 <        for(j = 0; i.hasNext(); j++)
158 <            assertEquals(j, ((Integer)i.next()).intValue());
159 <        assertEquals(3, j);
196 >    public void testEmptyIterator() {
197 >        assertIteratorExhausted(new CopyOnWriteArraySet().iterator());
198      }
199  
200 <    public void testIteratorRemove () {
201 <        CopyOnWriteArraySet full = populatedSet(3);
200 >    /**
201 >     * iterator remove is unsupported
202 >     */
203 >    public void testIteratorRemove() {
204 >        Collection full = populatedSet(3);
205          Iterator it = full.iterator();
206          it.next();
207          try {
208              it.remove();
209 <            fail("should throw");
210 <        }
170 <        catch (UnsupportedOperationException success) {}
209 >            shouldThrow();
210 >        } catch (UnsupportedOperationException success) {}
211      }
212  
213 <    public void testToString(){
214 <        CopyOnWriteArraySet full = populatedSet(3);
213 >    /**
214 >     * toString holds toString of elements
215 >     */
216 >    public void testToString() {
217 >        assertEquals("[]", new CopyOnWriteArraySet().toString());
218 >        Collection full = populatedSet(3);
219          String s = full.toString();
220 <        for (int i = 0; i < 3; ++i) {
221 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
222 <        }
223 <    }        
224 <
220 >        for (int i = 0; i < 3; ++i)
221 >            assertTrue(s.contains(String.valueOf(i)));
222 >        assertEquals(new ArrayList(full).toString(),
223 >                     full.toString());
224 >    }
225  
226      /**
227 <     *   removeAll correctly removes all elements from the given collection
227 >     * removeAll removes all elements from the given collection
228       */
229 <    public void testRemoveAll(){
230 <        CopyOnWriteArraySet full = populatedSet(3);
231 <        Vector v = new Vector();
232 <        v.add(one);
233 <        v.add(two);
234 <        full.removeAll(v);
191 <        assertEquals(1, full.size());
229 >    public void testRemoveAll() {
230 >        Set full = populatedSet(3);
231 >        assertTrue(full.removeAll(Arrays.asList(one, two)));
232 >        assertEquals(1, full.size());
233 >        assertFalse(full.removeAll(Arrays.asList(one, two)));
234 >        assertEquals(1, full.size());
235      }
236  
237 <
238 <    public void testRemove(){
239 <        CopyOnWriteArraySet full = populatedSet(3);
240 <        full.remove(one);
237 >    /**
238 >     * remove removes an element
239 >     */
240 >    public void testRemove() {
241 >        Collection full = populatedSet(3);
242 >        full.remove(one);
243          assertFalse(full.contains(one));
244 <        assertEquals(2, full.size());
244 >        assertEquals(2, full.size());
245      }
246  
247      /**
248 <     *   size returns the correct values
248 >     * size returns the number of elements
249       */
250 <    public void testSize(){
251 <        CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
252 <        CopyOnWriteArraySet full = populatedSet(3);
253 <        assertEquals(3, full.size());
254 <        assertEquals(0, empty.size());
250 >    public void testSize() {
251 >        Collection empty = new CopyOnWriteArraySet();
252 >        Collection full = populatedSet(3);
253 >        assertEquals(3, full.size());
254 >        assertEquals(0, empty.size());
255      }
256  
257      /**
258 <     *   toArray returns an Object array containing all elements from the list
258 >     * toArray() returns an Object array containing all elements from
259 >     * the set in insertion order
260       */
261 <    public void testToArray(){
262 <        CopyOnWriteArraySet full = populatedSet(3);
263 <        Object[] o = full.toArray();
264 <        assertEquals(3, o.length);
265 <        assertEquals(0, ((Integer)o[0]).intValue());
266 <        assertEquals(1, ((Integer)o[1]).intValue());
267 <        assertEquals(2, ((Integer)o[2]).intValue());
261 >    public void testToArray() {
262 >        Object[] a = new CopyOnWriteArraySet().toArray();
263 >        assertTrue(Arrays.equals(new Object[0], a));
264 >        assertSame(Object[].class, a.getClass());
265 >
266 >        Integer[] elements = new Integer[SIZE];
267 >        for (int i = 0; i < SIZE; i++)
268 >            elements[i] = i;
269 >        Collections.shuffle(Arrays.asList(elements));
270 >        Collection<Integer> full = populatedSet(elements);
271 >
272 >        assertTrue(Arrays.equals(elements, full.toArray()));
273 >        assertSame(Object[].class, full.toArray().getClass());
274      }
275  
276      /**
277 <     *   toArray returns an Integer array containing all elements from the list
277 >     * toArray(Integer array) returns an Integer array containing all
278 >     * elements from the set in insertion order
279       */
280 <    public void testToArray2(){
281 <        CopyOnWriteArraySet full = populatedSet(3);
282 <        Integer[] i = new Integer[3];
283 <        i = (Integer[])full.toArray(i);
284 <        assertEquals(3, i.length);
285 <        assertEquals(0, i[0].intValue());
233 <        assertEquals(1, i[1].intValue());
234 <        assertEquals(2, i[2].intValue());
235 <    }
280 >    public void testToArray2() {
281 >        Collection empty = new CopyOnWriteArraySet();
282 >        Integer[] a;
283 >
284 >        a = new Integer[0];
285 >        assertSame(a, empty.toArray(a));
286  
287 +        a = new Integer[SIZE / 2];
288 +        Arrays.fill(a, 42);
289 +        assertSame(a, empty.toArray(a));
290 +        assertNull(a[0]);
291 +        for (int i = 1; i < a.length; i++)
292 +            assertEquals(42, (int) a[i]);
293  
294 +        Integer[] elements = new Integer[SIZE];
295 +        for (int i = 0; i < SIZE; i++)
296 +            elements[i] = i;
297 +        Collections.shuffle(Arrays.asList(elements));
298 +        Collection<Integer> full = populatedSet(elements);
299  
300 <    // Exception tests
300 >        Arrays.fill(a, 42);
301 >        assertTrue(Arrays.equals(elements, full.toArray(a)));
302 >        for (int i = 0; i < a.length; i++)
303 >            assertEquals(42, (int) a[i]);
304 >        assertSame(Integer[].class, full.toArray(a).getClass());
305 >
306 >        a = new Integer[SIZE];
307 >        Arrays.fill(a, 42);
308 >        assertSame(a, full.toArray(a));
309 >        assertTrue(Arrays.equals(elements, a));
310 >
311 >        a = new Integer[2 * SIZE];
312 >        Arrays.fill(a, 42);
313 >        assertSame(a, full.toArray(a));
314 >        assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
315 >        assertNull(a[SIZE]);
316 >        for (int i = SIZE + 1; i < a.length; i++)
317 >            assertEquals(42, (int) a[i]);
318 >    }
319  
320      /**
321 <     *   toArray throws an ArrayStoreException when the given array
322 <     *  can not store the objects inside the list
321 >     * toArray throws an ArrayStoreException when the given array can
322 >     * not store the objects inside the set
323       */
324 <    public void testToArray_ArrayStoreException(){
325 <        try{
326 <            CopyOnWriteArraySet c = new CopyOnWriteArraySet();
327 <            c.add("zfasdfsdf");
328 <            c.add("asdadasd");
324 >    public void testToArray_ArrayStoreException() {
325 >        CopyOnWriteArraySet c = new CopyOnWriteArraySet();
326 >        c.add("zfasdfsdf");
327 >        c.add("asdadasd");
328 >        try {
329              c.toArray(new Long[5]);
330 <            fail("Object[] toArray(Object[]) should throw ArrayStoreException");
331 <        }catch(ArrayStoreException e){}
330 >            shouldThrow();
331 >        } catch (ArrayStoreException success) {}
332      }
333  
334 <    public void testSerialization() {
335 <        CopyOnWriteArraySet q = populatedSet(SIZE);
334 >    /**
335 >     * A deserialized serialized set is equal
336 >     */
337 >    public void testSerialization() throws Exception {
338 >        Set x = populatedSet(SIZE);
339 >        Set y = serialClone(x);
340 >
341 >        assertNotSame(y, x);
342 >        assertEquals(x.size(), y.size());
343 >        assertEquals(x.toString(), y.toString());
344 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
345 >        assertEquals(x, y);
346 >        assertEquals(y, x);
347 >    }
348  
349 <        try {
350 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
351 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
352 <            out.writeObject(q);
353 <            out.close();
354 <
355 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
356 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
357 <            CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
267 <            assertEquals(q.size(), r.size());
268 <            assertTrue(q.equals(r));
269 <            assertTrue(r.equals(q));
270 <        } catch(Exception e){
271 <            e.printStackTrace();
272 <            fail("unexpected exception");
273 <        }
349 >    /**
350 >     * addAll is idempotent
351 >     */
352 >    public void testAddAll_idempotent() throws Exception {
353 >        Set x = populatedSet(SIZE);
354 >        Set y = new CopyOnWriteArraySet(x);
355 >        y.addAll(x);
356 >        assertEquals(x, y);
357 >        assertEquals(y, x);
358      }
359  
360   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines