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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines