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.41 by jsr166, Tue May 8 00:25:33 2018 UTC vs.
Revision 1.42 by dl, Tue Jan 26 13:33:05 2021 UTC

# Line 24 | Line 24 | public class CopyOnWriteArraySetTest ext
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 i; }
27 >            public Object makeElement(int i) { return JSR166TestCase.itemFor(i); }
28              public boolean isConcurrent() { return true; }
29              public boolean permitsNulls() { return true; }
30          }
# Line 33 | Line 33 | public class CopyOnWriteArraySetTest ext
33                  CollectionTest.testSuite(new Implementation()));
34      }
35  
36 <    static CopyOnWriteArraySet<Integer> populatedSet(int n) {
37 <        CopyOnWriteArraySet<Integer> a = new CopyOnWriteArraySet<>();
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 <        assertEquals(n == 0, a.isEmpty());
42 <        assertEquals(n, a.size());
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<>();
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 57 | 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<Item>();
61          assertTrue(a.isEmpty());
62      }
63  
# Line 65 | 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)
70 <            ints[i] = new Integer(i);
71 <        CopyOnWriteArraySet a = new CopyOnWriteArraySet(Arrays.asList(ints));
68 >        Item[] items = defaultItems;
69 >        CopyOnWriteArraySet<Item> a = new CopyOnWriteArraySet<Item>(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 non-duplicate element from the given collection
76       */
77      public void testAddAll() {
78 <        Set full = populatedSet(3);
78 >        Set<Item> full = populatedSet(3);
79          assertTrue(full.addAll(Arrays.asList(three, four, five)));
80 <        assertEquals(6, full.size());
80 >        mustEqual(6, full.size());
81          assertFalse(full.addAll(Arrays.asList(three, four, five)));
82 <        assertEquals(6, full.size());
82 >        mustEqual(6, full.size());
83      }
84  
85      /**
86       * addAll adds each non-duplicate element from the given collection
87       */
88      public void testAddAll2() {
89 <        Set full = populatedSet(3);
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 <        assertEquals(5, full.size());
92 >        mustEqual(5, full.size());
93          assertFalse(full.addAll(Arrays.asList(three, four, one)));
94 <        assertEquals(5, full.size());
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 <        Set 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 <        Set 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 <        Collection 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  
# Line 128 | Line 126 | public class CopyOnWriteArraySetTest ext
126       * contains returns true for added elements
127       */
128      public void testContains() {
129 <        Collection 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          assertTrue(a.containsAll(b));
143          assertTrue(b.containsAll(a));
144 <        assertEquals(a.hashCode(), b.hashCode());
145 <        assertEquals(a.size(), b.size());
144 >        mustEqual(a.hashCode(), b.hashCode());
145 >        mustEqual(a.size(), b.size());
146  
147 <        a.add(m1);
147 >        a.add(minusOne);
148          assertFalse(a.equals(b));
149          assertFalse(b.equals(a));
150          assertTrue(a.containsAll(b));
151          assertFalse(b.containsAll(a));
152 <        b.add(m1);
152 >        b.add(minusOne);
153          assertTrue(a.equals(b));
154          assertTrue(b.equals(a));
155          assertTrue(a.containsAll(b));
156          assertTrue(b.containsAll(a));
157 <        assertEquals(a.hashCode(), b.hashCode());
157 >        mustEqual(a.hashCode(), b.hashCode());
158  
159 <        Object x = a.iterator().next();
159 >        Item x = a.iterator().next();
160          a.remove(x);
161          assertFalse(a.equals(b));
162          assertFalse(b.equals(a));
# Line 169 | Line 167 | public class CopyOnWriteArraySetTest ext
167          assertTrue(b.equals(a));
168          assertTrue(a.containsAll(b));
169          assertTrue(b.containsAll(a));
170 <        assertEquals(a.hashCode(), b.hashCode());
171 <        assertEquals(a.size(), b.size());
170 >        mustEqual(a.hashCode(), b.hashCode());
171 >        mustEqual(a.size(), b.size());
172  
173 <        CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList());
174 <        CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList());
173 >        CopyOnWriteArraySet<Item> empty1 = new CopyOnWriteArraySet<Item>(Arrays.asList());
174 >        CopyOnWriteArraySet<Item> empty2 = new CopyOnWriteArraySet<Item>(Arrays.asList());
175          assertTrue(empty1.equals(empty1));
176          assertTrue(empty1.equals(empty2));
177  
# Line 187 | Line 185 | public class CopyOnWriteArraySetTest ext
185       * containsAll returns true for collections with subset of elements
186       */
187      public void testContainsAll() {
188 <        Collection full = populatedSet(3);
188 >        Collection<Item> full = populatedSet(3);
189          assertTrue(full.containsAll(full));
190          assertTrue(full.containsAll(Arrays.asList()));
191          assertTrue(full.containsAll(Arrays.asList(one)));
# Line 195 | Line 193 | public class CopyOnWriteArraySetTest ext
193          assertFalse(full.containsAll(Arrays.asList(one, two, six)));
194          assertFalse(full.containsAll(Arrays.asList(six)));
195  
196 <        CopyOnWriteArraySet empty1 = new CopyOnWriteArraySet(Arrays.asList());
197 <        CopyOnWriteArraySet empty2 = new CopyOnWriteArraySet(Arrays.asList());
196 >        CopyOnWriteArraySet<Item> empty1 = new CopyOnWriteArraySet<Item>(Arrays.asList());
197 >        CopyOnWriteArraySet<Item> empty2 = new CopyOnWriteArraySet<Item>(Arrays.asList());
198          assertTrue(empty1.containsAll(empty2));
199          assertTrue(empty1.containsAll(empty1));
200          assertFalse(empty1.containsAll(full));
# Line 221 | Line 219 | public class CopyOnWriteArraySetTest ext
219       * set in insertion order
220       */
221      public void testIterator() {
222 <        Collection empty = new CopyOnWriteArraySet();
222 >        Collection<Item> empty = new CopyOnWriteArraySet<Item>();
223          assertFalse(empty.iterator().hasNext());
224          try {
225              empty.iterator().next();
226              shouldThrow();
227          } catch (NoSuchElementException success) {}
228  
229 <        Integer[] elements = new Integer[SIZE];
232 <        for (int i = 0; i < SIZE; i++)
233 <            elements[i] = i;
229 >        Item[] elements = seqItems(SIZE);
230          shuffle(elements);
231 <        Collection<Integer> full = populatedSet(elements);
231 >        Collection<Item> full = populatedSet(elements);
232  
233 <        Iterator it = full.iterator();
233 >        Iterator<? extends Item> it = full.iterator();
234          for (int j = 0; j < SIZE; j++) {
235              assertTrue(it.hasNext());
236 <            assertEquals(elements[j], it.next());
236 >            mustEqual(elements[j], it.next());
237          }
238          assertIteratorExhausted(it);
239      }
# Line 246 | Line 242 | public class CopyOnWriteArraySetTest ext
242       * iterator of empty collection has no elements
243       */
244      public void testEmptyIterator() {
245 <        assertIteratorExhausted(new CopyOnWriteArraySet().iterator());
245 >        assertIteratorExhausted(new CopyOnWriteArraySet<Item>().iterator());
246      }
247  
248      /**
249       * iterator remove is unsupported
250       */
251      public void testIteratorRemove() {
252 <        Collection 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 266 | Line 262 | public class CopyOnWriteArraySetTest ext
262       * toString holds toString of elements
263       */
264      public void testToString() {
265 <        assertEquals("[]", new CopyOnWriteArraySet().toString());
266 <        Collection 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)
269              assertTrue(s.contains(String.valueOf(i)));
270 <        assertEquals(new ArrayList(full).toString(),
270 >        mustEqual(new ArrayList<Item>(full).toString(),
271                       full.toString());
272      }
273  
# Line 279 | Line 275 | public class CopyOnWriteArraySetTest ext
275       * removeAll removes all elements from the given collection
276       */
277      public void testRemoveAll() {
278 <        Set full = populatedSet(3);
278 >        Set<Item> full = populatedSet(3);
279          assertTrue(full.removeAll(Arrays.asList(one, two)));
280 <        assertEquals(1, full.size());
280 >        mustEqual(1, full.size());
281          assertFalse(full.removeAll(Arrays.asList(one, two)));
282 <        assertEquals(1, full.size());
282 >        mustEqual(1, full.size());
283      }
284  
285      /**
286       * remove removes an element
287       */
288      public void testRemove() {
289 <        Collection 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 <        Collection empty = new CopyOnWriteArraySet();
300 <        Collection full = populatedSet(3);
301 <        assertEquals(3, full.size());
302 <        assertEquals(0, empty.size());
299 >        Collection<Item> empty = new CopyOnWriteArraySet<Item>();
300 >        Collection<Item> full = populatedSet(3);
301 >        mustEqual(3, full.size());
302 >        mustEqual(0, empty.size());
303      }
304  
305      /**
# Line 311 | 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<Item>().toArray();
311          assertTrue(Arrays.equals(new Object[0], a));
312          assertSame(Object[].class, a.getClass());
313  
314 <        Integer[] elements = new Integer[SIZE];
319 <        for (int i = 0; i < SIZE; i++)
320 <            elements[i] = i;
314 >        Item[] elements = seqItems(SIZE);
315          shuffle(elements);
316 <        Collection<Integer> full = populatedSet(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 <        Collection empty = new CopyOnWriteArraySet();
328 <        Integer[] a;
327 >        Collection<Item> empty = new CopyOnWriteArraySet<Item>();
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];
347 <        for (int i = 0; i < SIZE; i++)
348 <            elements[i] = i;
340 >        Item[] elements = seqItems(SIZE);
341          shuffle(elements);
342 <        Collection<Integer> full = populatedSet(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 374 | Line 366 | public class CopyOnWriteArraySetTest ext
366       * not store the objects inside the set
367       */
368      public void testToArray_ArrayStoreException() {
369 <        CopyOnWriteArraySet c = new CopyOnWriteArraySet();
370 <        c.add("zfasdfsdf");
371 <        c.add("asdadasd");
369 >        CopyOnWriteArraySet<Item> c = new CopyOnWriteArraySet<Item>();
370 >        c.add(one);
371 >        c.add(two);
372          try {
373              c.toArray(new Long[5]);
374              shouldThrow();
# Line 387 | Line 379 | public class CopyOnWriteArraySetTest ext
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          assertNotSame(y, x);
386 <        assertEquals(x.size(), y.size());
387 <        assertEquals(x.toString(), y.toString());
386 >        mustEqual(x.size(), y.size());
387 >        mustEqual(x.toString(), y.toString());
388          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
389 <        assertEquals(x, y);
390 <        assertEquals(y, x);
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 x = populatedSet(SIZE);
398 <        Set y = new CopyOnWriteArraySet(x);
397 >        Set<Item> x = populatedSet(SIZE);
398 >        Set<Item> y = new CopyOnWriteArraySet<Item>(x);
399          y.addAll(x);
400 <        assertEquals(x, y);
401 <        assertEquals(y, x);
400 >        mustEqual(x, y);
401 >        mustEqual(y, x);
402      }
403  
404   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines