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.7 by dl, Mon Dec 29 19:05: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 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/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
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 {
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run (suite());  
19 >        junit.textui.TestRunner.run(suite());
20      }
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  
# Line 44 | Line 57 | public class CopyOnWriteArraySetTest ext
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      }
51        
64  
65      /**
66 <     *   addAll  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());
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 <     *   addAll adds each element from the given collection that did not
80 <     *  already exist in the set
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());
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 <     *   add will not add the element if it already exists in the set
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());
96 >        CopyOnWriteArraySet full = populatedSet(3);
97 >        full.add(one);
98 >        assertEquals(3, full.size());
99      }
100  
101      /**
102 <     *   add  adds the element when it does not exist
91 <     *   in the set
102 >     * add adds the element when it does not exist in the set
103       */
104      public void testAdd3() {
105 <        CopyOnWriteArraySet full = populatedSet(3);
105 >        CopyOnWriteArraySet full = populatedSet(3);
106          full.add(three);
107          assertTrue(full.contains(three));
108      }
109  
110      /**
111 <     *   clear  removes all elements from the set
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());
114 >        CopyOnWriteArraySet full = populatedSet(3);
115 >        full.clear();
116 >        assertEquals(0, full.size());
117      }
118  
119      /**
120 <     *   contains returns true for added elements
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));
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 132 | Line 143 | public class CopyOnWriteArraySetTest ext
143          assertEquals(a.hashCode(), b.hashCode());
144      }
145  
135    
146      /**
147 <     *   containsAll returns true for collections with subset of elements
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));
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 is true when empty, else false
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());
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 set
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);
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      /**
182       * iterator remove is unsupported
183       */
184 <    public void testIteratorRemove () {
185 <        CopyOnWriteArraySet full = populatedSet(3);
184 >    public void testIteratorRemove() {
185 >        CopyOnWriteArraySet full = populatedSet(3);
186          Iterator it = full.iterator();
187          it.next();
188          try {
189              it.remove();
190              shouldThrow();
191 <        }
182 <        catch (UnsupportedOperationException success) {}
191 >        } catch (UnsupportedOperationException success) {}
192      }
193  
194      /**
195       * toString holds toString of elements
196       */
197      public void testToString() {
198 <        CopyOnWriteArraySet full = populatedSet(3);
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 <    }        
195 <
203 >    }
204  
205      /**
206 <     *   removeAll  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());
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  
209
217      /**
218       * remove removes an element
219       */
220      public void testRemove() {
221 <        CopyOnWriteArraySet full = populatedSet(3);
222 <        full.remove(one);
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 number of elements
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());
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 set
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());
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
258 <     *   the set
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];
249 <        i = (Integer[])full.toArray(i);
250 <        assertEquals(3, i.length);
251 <        assertEquals(0, i[0].intValue());
252 <        assertEquals(1, i[1].intValue());
253 <        assertEquals(2, i[2].intValue());
254 <    }
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 +        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 can
302 <     *  not store the objects inside the set
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 {
# Line 264 | Line 307 | public class CopyOnWriteArraySetTest ext
307              c.add("zfasdfsdf");
308              c.add("asdadasd");
309              c.toArray(new Long[5]);
310 <            shouldThrow();
311 <        } catch(ArrayStoreException e){}
310 >            shouldThrow();
311 >        } catch (ArrayStoreException success) {}
312      }
313  
314      /**
315       * A deserialized serialized set is equal
316       */
317 <    public void testSerialization() {
318 <        CopyOnWriteArraySet q = populatedSet(SIZE);
319 <
320 <        try {
321 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
322 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
323 <            out.writeObject(q);
324 <            out.close();
282 <
283 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
284 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
285 <            CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
286 <            assertEquals(q.size(), r.size());
287 <            assertTrue(q.equals(r));
288 <            assertTrue(r.equals(q));
289 <        } catch(Exception e){
290 <            unexpectedException();
291 <        }
317 >    public void testSerialization() throws Exception {
318 >        Set x = populatedSet(SIZE);
319 >        Set y = serialClone(x);
320 >
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