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.21 by jsr166, Tue Nov 29 05:09:05 2011 UTC vs.
Revision 1.43 by jsr166, Wed Jan 27 01:57:24 2021 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines