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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines