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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines