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.8 by jsr166, Mon Nov 2 20:28:31 2009 UTC vs.
Revision 1.22 by jsr166, Tue Nov 29 05:23:56 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
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 {
19      public static void main(String[] args) {
20 <        junit.textui.TestRunner.run (suite());
20 >        junit.textui.TestRunner.run(suite());
21      }
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  
# Line 44 | Line 58 | public class CopyOnWriteArraySetTest ext
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));
61 >        CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
62          for (int i = 0; i < SIZE; ++i)
63              assertTrue(a.contains(ints[i]));
64      }
65  
52
66      /**
67 <     *   addAll  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());
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 <     *   addAll adds each element from the given collection that did not
81 <     *  already exist in the set
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());
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 <     *   add will not add the element if it already exists in the set
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());
97 >        CopyOnWriteArraySet full = populatedSet(3);
98 >        full.add(one);
99 >        assertEquals(3, full.size());
100      }
101  
102      /**
103 <     *   add  adds the element when it does not exist
91 <     *   in the set
103 >     * add adds the element when it does not exist in the set
104       */
105      public void testAdd3() {
106 <        CopyOnWriteArraySet full = populatedSet(3);
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
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());
115 >        CopyOnWriteArraySet full = populatedSet(3);
116 >        full.clear();
117 >        assertEquals(0, full.size());
118      }
119  
120      /**
121 <     *   contains returns true for added elements
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));
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 132 | Line 144 | public class CopyOnWriteArraySetTest ext
144          assertEquals(a.hashCode(), b.hashCode());
145      }
146  
135
147      /**
148 <     *   containsAll returns true for collections with subset of elements
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));
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 is true when empty, else false
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());
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 set
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);
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      /**
183       * iterator remove is unsupported
184       */
185 <    public void testIteratorRemove () {
186 <        CopyOnWriteArraySet full = populatedSet(3);
185 >    public void testIteratorRemove() {
186 >        CopyOnWriteArraySet full = populatedSet(3);
187          Iterator it = full.iterator();
188          it.next();
189          try {
190              it.remove();
191              shouldThrow();
192 <        }
182 <        catch (UnsupportedOperationException success) {}
192 >        } catch (UnsupportedOperationException success) {}
193      }
194  
195      /**
196       * toString holds toString of elements
197       */
198      public void testToString() {
199 <        CopyOnWriteArraySet full = populatedSet(3);
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      }
205  
196
206      /**
207 <     *   removeAll  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());
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  
209
218      /**
219       * remove removes an element
220       */
221      public void testRemove() {
222 <        CopyOnWriteArraySet full = populatedSet(3);
223 <        full.remove(one);
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 number of elements
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());
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 set
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());
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
259 <     *   the set
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];
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 <    }
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 +        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 can
303 <     *  not store the objects inside the set
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 {
# Line 264 | Line 308 | public class CopyOnWriteArraySetTest ext
308              c.add("zfasdfsdf");
309              c.add("asdadasd");
310              c.toArray(new Long[5]);
311 <            shouldThrow();
312 <        } catch(ArrayStoreException e){}
311 >            shouldThrow();
312 >        } catch (ArrayStoreException success) {}
313      }
314  
315      /**
316       * A deserialized serialized set is equal
317       */
318 <    public void testSerialization() {
319 <        CopyOnWriteArraySet q = populatedSet(SIZE);
320 <
321 <        try {
322 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
323 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
324 <            out.writeObject(q);
325 <            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 <        }
318 >    public void testSerialization() throws Exception {
319 >        Set x = populatedSet(SIZE);
320 >        Set y = serialClone(x);
321 >
322 >        assertTrue(x != y);
323 >        assertEquals(x.size(), y.size());
324 >        assertEquals(x, y);
325 >        assertEquals(y, x);
326      }
327  
328   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines