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.13 by jsr166, Sat Nov 21 17:38:05 2009 UTC vs.
Revision 1.24 by jsr166, Tue Nov 29 20:59:24 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.NoSuchElementException;
15 > import java.util.Set;
16 > import java.util.Vector;
17 > import java.util.concurrent.CopyOnWriteArraySet;
18  
19   public class CopyOnWriteArraySetTest extends JSR166TestCase {
20      public static void main(String[] args) {
21 <        junit.textui.TestRunner.run (suite());
21 >        junit.textui.TestRunner.run(suite());
22      }
23      public static Test suite() {
24          return new TestSuite(CopyOnWriteArraySetTest.class);
25      }
26  
27 <    static CopyOnWriteArraySet populatedSet(int n) {
28 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet();
27 >    static CopyOnWriteArraySet<Integer> populatedSet(int n) {
28 >        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
29          assertTrue(a.isEmpty());
30 <        for (int i = 0; i < n; ++i)
31 <            a.add(new Integer(i));
30 >        for (int i = 0; i < n; i++)
31 >            a.add(i);
32          assertFalse(a.isEmpty());
33          assertEquals(n, a.size());
34          return a;
35      }
36  
37 +    static CopyOnWriteArraySet populatedSet(Integer[] elements) {
38 +        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<Integer>();
39 +        assertTrue(a.isEmpty());
40 +        for (int i = 0; i < elements.length; i++)
41 +            a.add(elements[i]);
42 +        assertFalse(a.isEmpty());
43 +        assertEquals(elements.length, a.size());
44 +        return a;
45 +    }
46 +
47      /**
48       * Default-constructed set is empty
49       */
# Line 49 | Line 64 | public class CopyOnWriteArraySetTest ext
64              assertTrue(a.contains(ints[i]));
65      }
66  
52
67      /**
68 <     *  addAll adds each element from the given collection
68 >     * addAll adds each element from the given collection
69       */
70      public void testAddAll() {
71          CopyOnWriteArraySet full = populatedSet(3);
# Line 64 | Line 78 | public class CopyOnWriteArraySetTest ext
78      }
79  
80      /**
81 <     *  addAll adds each element from the given collection that did not
82 <     *  already exist in the set
81 >     * addAll adds each element from the given collection that did not
82 >     * already exist in the set
83       */
84      public void testAddAll2() {
85          CopyOnWriteArraySet full = populatedSet(3);
# Line 78 | Line 92 | public class CopyOnWriteArraySetTest ext
92      }
93  
94      /**
95 <     *  add will not add the element if it already exists in the set
95 >     * add will not add the element if it already exists in the set
96       */
97      public void testAdd2() {
98          CopyOnWriteArraySet full = populatedSet(3);
# Line 87 | Line 101 | public class CopyOnWriteArraySetTest ext
101      }
102  
103      /**
104 <     *  add  adds the element when it does not exist
91 <     *  in the set
104 >     * add adds the element when it does not exist in the set
105       */
106      public void testAdd3() {
107          CopyOnWriteArraySet full = populatedSet(3);
# Line 97 | Line 110 | public class CopyOnWriteArraySetTest ext
110      }
111  
112      /**
113 <     *  clear removes all elements from the set
113 >     * clear removes all elements from the set
114       */
115      public void testClear() {
116          CopyOnWriteArraySet full = populatedSet(3);
# Line 106 | Line 119 | public class CopyOnWriteArraySetTest ext
119      }
120  
121      /**
122 <     *   contains returns true for added elements
122 >     * contains returns true for added elements
123       */
124      public void testContains() {
125          CopyOnWriteArraySet full = populatedSet(3);
# Line 132 | Line 145 | public class CopyOnWriteArraySetTest ext
145          assertEquals(a.hashCode(), b.hashCode());
146      }
147  
135
148      /**
149 <     *  containsAll returns true for collections with subset of elements
149 >     * containsAll returns true for collections with subset of elements
150       */
151      public void testContainsAll() {
152          CopyOnWriteArraySet full = populatedSet(3);
# Line 147 | Line 159 | public class CopyOnWriteArraySetTest ext
159      }
160  
161      /**
162 <     *  isEmpty is true when empty, else false
162 >     * isEmpty is true when empty, else false
163       */
164      public void testIsEmpty() {
165          CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
# Line 157 | Line 169 | public class CopyOnWriteArraySetTest ext
169      }
170  
171      /**
172 <     *  iterator() returns an iterator containing the elements of the set
172 >     * iterator() returns an iterator containing the elements of the
173 >     * set in insertion order
174       */
175      public void testIterator() {
176 <        CopyOnWriteArraySet full = populatedSet(3);
177 <        Iterator i = full.iterator();
178 <        int j;
179 <        for (j = 0; i.hasNext(); j++)
180 <            assertEquals(j, ((Integer)i.next()).intValue());
181 <        assertEquals(3, j);
176 >        Set empty = new CopyOnWriteArraySet();
177 >        assertFalse(empty.iterator().hasNext());
178 >        try {
179 >            empty.iterator().next();
180 >            shouldThrow();
181 >        } catch (NoSuchElementException success) {}
182 >
183 >        Integer[] elements = new Integer[SIZE];
184 >        for (int i = 0; i < SIZE; i++)
185 >            elements[i] = i;
186 >        Collections.shuffle(Arrays.asList(elements));
187 >        Collection<Integer> full = populatedSet(elements);
188 >
189 >        Iterator it = full.iterator();
190 >        for (int j = 0; j < SIZE; j++) {
191 >            assertTrue(it.hasNext());
192 >            assertEquals(elements[j], it.next());
193 >        }
194 >        assertFalse(it.hasNext());
195 >        try {
196 >            it.next();
197 >            shouldThrow();
198 >        } catch (NoSuchElementException success) {}
199      }
200  
201      /**
202       * iterator remove is unsupported
203       */
204 <    public void testIteratorRemove () {
204 >    public void testIteratorRemove() {
205          CopyOnWriteArraySet full = populatedSet(3);
206          Iterator it = full.iterator();
207          it.next();
# Line 188 | Line 218 | public class CopyOnWriteArraySetTest ext
218          CopyOnWriteArraySet full = populatedSet(3);
219          String s = full.toString();
220          for (int i = 0; i < 3; ++i) {
221 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
221 >            assertTrue(s.contains(String.valueOf(i)));
222          }
223      }
224  
195
225      /**
226 <     *  removeAll  removes all elements from the given collection
226 >     * removeAll removes all elements from the given collection
227       */
228      public void testRemoveAll() {
229          CopyOnWriteArraySet full = populatedSet(3);
# Line 205 | Line 234 | public class CopyOnWriteArraySetTest ext
234          assertEquals(1, full.size());
235      }
236  
208
237      /**
238       * remove removes an element
239       */
# Line 217 | Line 245 | public class CopyOnWriteArraySetTest ext
245      }
246  
247      /**
248 <     *  size returns the number of elements
248 >     * size returns the number of elements
249       */
250      public void testSize() {
251          CopyOnWriteArraySet empty = new CopyOnWriteArraySet();
# Line 227 | Line 255 | public class CopyOnWriteArraySetTest ext
255      }
256  
257      /**
258 <     *  toArray returns an Object array containing all elements from the set
258 >     * toArray() returns an Object array containing all elements from
259 >     * the set in insertion order
260       */
261      public void testToArray() {
262 <        CopyOnWriteArraySet full = populatedSet(3);
263 <        Object[] o = full.toArray();
264 <        assertEquals(3, o.length);
265 <        assertEquals(0, ((Integer)o[0]).intValue());
266 <        assertEquals(1, ((Integer)o[1]).intValue());
267 <        assertEquals(2, ((Integer)o[2]).intValue());
262 >        Object[] a = new CopyOnWriteArraySet().toArray();
263 >        assertTrue(Arrays.equals(new Object[0], a));
264 >        assertSame(Object[].class, a.getClass());
265 >
266 >        Integer[] elements = new Integer[SIZE];
267 >        for (int i = 0; i < SIZE; i++)
268 >            elements[i] = i;
269 >        Collections.shuffle(Arrays.asList(elements));
270 >        Collection<Integer> full = populatedSet(elements);
271 >
272 >        assertTrue(Arrays.equals(elements, full.toArray()));
273 >        assertSame(Object[].class, full.toArray().getClass());
274      }
275  
276      /**
277 <     *  toArray returns an Integer array containing all elements from
278 <     *  the set
277 >     * toArray(Integer array) returns an Integer array containing all
278 >     * elements from the set in insertion order
279       */
280      public void testToArray2() {
281 <        CopyOnWriteArraySet full = populatedSet(3);
282 <        Integer[] i = new Integer[3];
283 <        i = (Integer[])full.toArray(i);
284 <        assertEquals(3, i.length);
285 <        assertEquals(0, i[0].intValue());
251 <        assertEquals(1, i[1].intValue());
252 <        assertEquals(2, i[2].intValue());
253 <    }
281 >        Collection empty = new CopyOnWriteArraySet();
282 >        Integer[] a;
283 >
284 >        a = new Integer[0];
285 >        assertSame(a, empty.toArray(a));
286  
287 +        a = new Integer[SIZE/2];
288 +        Arrays.fill(a, 42);
289 +        assertSame(a, empty.toArray(a));
290 +        assertNull(a[0]);
291 +        for (int i = 1; i < a.length; i++)
292 +            assertEquals(42, (int) a[i]);
293 +
294 +        Integer[] elements = new Integer[SIZE];
295 +        for (int i = 0; i < SIZE; i++)
296 +            elements[i] = i;
297 +        Collections.shuffle(Arrays.asList(elements));
298 +        Collection<Integer> full = populatedSet(elements);
299 +
300 +        Arrays.fill(a, 42);
301 +        assertTrue(Arrays.equals(elements, full.toArray(a)));
302 +        for (int i = 0; i < a.length; i++)
303 +            assertEquals(42, (int) a[i]);
304 +        assertSame(Integer[].class, full.toArray(a).getClass());
305 +
306 +        a = new Integer[SIZE];
307 +        Arrays.fill(a, 42);
308 +        assertSame(a, full.toArray(a));
309 +        assertTrue(Arrays.equals(elements, a));
310 +
311 +        a = new Integer[2*SIZE];
312 +        Arrays.fill(a, 42);
313 +        assertSame(a, full.toArray(a));
314 +        assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
315 +        assertNull(a[SIZE]);
316 +        for (int i = SIZE + 1; i < a.length; i++)
317 +            assertEquals(42, (int) a[i]);
318 +    }
319  
320      /**
321 <     *  toArray throws an ArrayStoreException when the given array can
322 <     *  not store the objects inside the set
321 >     * toArray throws an ArrayStoreException when the given array can
322 >     * not store the objects inside the set
323       */
324      public void testToArray_ArrayStoreException() {
325          try {
# Line 271 | Line 335 | public class CopyOnWriteArraySetTest ext
335       * A deserialized serialized set is equal
336       */
337      public void testSerialization() throws Exception {
338 <        CopyOnWriteArraySet q = populatedSet(SIZE);
338 >        Set x = populatedSet(SIZE);
339 >        Set y = serialClone(x);
340  
341 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
342 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
343 <        out.writeObject(q);
344 <        out.close();
345 <
346 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
282 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
283 <        CopyOnWriteArraySet r = (CopyOnWriteArraySet)in.readObject();
284 <        assertEquals(q.size(), r.size());
285 <        assertTrue(q.equals(r));
286 <        assertTrue(r.equals(q));
341 >        assertTrue(x != y);
342 >        assertEquals(x.size(), y.size());
343 >        assertEquals(x.toString(), y.toString());
344 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
345 >        assertEquals(x, y);
346 >        assertEquals(y, x);
347      }
348  
349   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines