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.18 by jsr166, Fri May 27 19:26:42 2011 UTC vs.
Revision 1.35 by jsr166, Tue May 26 19:22:09 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines