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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines