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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines