ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CopyOnWriteArrayListTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CopyOnWriteArrayListTest.java (file contents):
Revision 1.31 by jsr166, Wed Dec 31 19:05:42 2014 UTC vs.
Revision 1.47 by jsr166, Mon Mar 26 21:28:22 2018 UTC

# Line 9 | Line 9
9   import java.util.ArrayList;
10   import java.util.Arrays;
11   import java.util.Collection;
12 import java.util.Collections;
12   import java.util.Iterator;
13   import java.util.LinkedList;
14   import java.util.List;
15   import java.util.ListIterator;
16   import java.util.NoSuchElementException;
18 import java.util.Vector;
17   import java.util.concurrent.CopyOnWriteArrayList;
18  
19   import junit.framework.Test;
22 import junit.framework.TestSuite;
20  
21   public class CopyOnWriteArrayListTest extends JSR166TestCase {
22  
23      public static void main(String[] args) {
24 <        junit.textui.TestRunner.run(suite());
24 >        main(suite(), args);
25      }
26  
27      public static Test suite() {
28 <        return new TestSuite(CopyOnWriteArrayListTest.class);
28 >        class Implementation implements CollectionImplementation {
29 >            public Class<?> klazz() { return CopyOnWriteArrayList.class; }
30 >            public List emptyCollection() { return new CopyOnWriteArrayList(); }
31 >            public Object makeElement(int i) { return i; }
32 >            public boolean isConcurrent() { return true; }
33 >            public boolean permitsNulls() { return true; }
34 >        }
35 >        class SubListImplementation extends Implementation {
36 >            public List emptyCollection() {
37 >                return super.emptyCollection().subList(0, 0);
38 >            }
39 >        }
40 >        return newTestSuite(
41 >                CopyOnWriteArrayListTest.class,
42 >                CollectionTest.testSuite(new Implementation()),
43 >                CollectionTest.testSuite(new SubListImplementation()));
44      }
45  
46      static CopyOnWriteArrayList<Integer> populatedArray(int n) {
47 <        CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>();
47 >        CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>();
48          assertTrue(a.isEmpty());
49          for (int i = 0; i < n; i++)
50              a.add(i);
# Line 42 | Line 54 | public class CopyOnWriteArrayListTest ex
54      }
55  
56      static CopyOnWriteArrayList<Integer> populatedArray(Integer[] elements) {
57 <        CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<Integer>();
57 >        CopyOnWriteArrayList<Integer> a = new CopyOnWriteArrayList<>();
58          assertTrue(a.isEmpty());
59 <        for (int i = 0; i < elements.length; i++)
60 <            a.add(elements[i]);
59 >        for (Integer element : elements)
60 >            a.add(element);
61          assertFalse(a.isEmpty());
62          assertEquals(elements.length, a.size());
63          return a;
# Line 64 | Line 76 | public class CopyOnWriteArrayListTest ex
76       */
77      public void testConstructor2() {
78          Integer[] ints = new Integer[SIZE];
79 <        for (int i = 0; i < SIZE-1; ++i)
79 >        for (int i = 0; i < SIZE - 1; ++i)
80              ints[i] = new Integer(i);
81          CopyOnWriteArrayList a = new CopyOnWriteArrayList(ints);
82          for (int i = 0; i < SIZE; ++i)
# Line 76 | Line 88 | public class CopyOnWriteArrayListTest ex
88       */
89      public void testConstructor3() {
90          Integer[] ints = new Integer[SIZE];
91 <        for (int i = 0; i < SIZE-1; ++i)
91 >        for (int i = 0; i < SIZE - 1; ++i)
92              ints[i] = new Integer(i);
93          CopyOnWriteArrayList a = new CopyOnWriteArrayList(Arrays.asList(ints));
94          for (int i = 0; i < SIZE; ++i)
# Line 84 | Line 96 | public class CopyOnWriteArrayListTest ex
96      }
97  
98      /**
99 <     * addAll adds each element from the given collection
99 >     * addAll adds each element from the given collection, including duplicates
100       */
101      public void testAddAll() {
102          CopyOnWriteArrayList full = populatedArray(3);
103 <        Vector v = new Vector();
92 <        v.add(three);
93 <        v.add(four);
94 <        v.add(five);
95 <        full.addAll(v);
103 >        assertTrue(full.addAll(Arrays.asList(three, four, five)));
104          assertEquals(6, full.size());
105 +        assertTrue(full.addAll(Arrays.asList(three, four, five)));
106 +        assertEquals(9, full.size());
107      }
108  
109      /**
# Line 102 | Line 112 | public class CopyOnWriteArrayListTest ex
112       */
113      public void testAddAllAbsent() {
114          CopyOnWriteArrayList full = populatedArray(3);
115 <        Vector v = new Vector();
116 <        v.add(three);
117 <        v.add(four);
118 <        v.add(one); // will not add this element
109 <        full.addAllAbsent(v);
115 >        // "one" is duplicate and will not be added
116 >        assertEquals(2, full.addAllAbsent(Arrays.asList(three, four, one)));
117 >        assertEquals(5, full.size());
118 >        assertEquals(0, full.addAllAbsent(Arrays.asList(three, four, one)));
119          assertEquals(5, full.size());
120      }
121  
# Line 181 | Line 190 | public class CopyOnWriteArrayListTest ex
190          CopyOnWriteArrayList b = populatedArray(3);
191          assertTrue(a.equals(b));
192          assertTrue(b.equals(a));
193 +        assertTrue(a.containsAll(b));
194 +        assertTrue(b.containsAll(a));
195          assertEquals(a.hashCode(), b.hashCode());
196          a.add(m1);
197          assertFalse(a.equals(b));
198          assertFalse(b.equals(a));
199 +        assertTrue(a.containsAll(b));
200 +        assertFalse(b.containsAll(a));
201          b.add(m1);
202          assertTrue(a.equals(b));
203          assertTrue(b.equals(a));
204 +        assertTrue(a.containsAll(b));
205 +        assertTrue(b.containsAll(a));
206          assertEquals(a.hashCode(), b.hashCode());
207 +
208 +        assertFalse(a.equals(null));
209      }
210  
211      /**
212 <     * containsAll returns true for collection with subset of elements
212 >     * containsAll returns true for collections with subset of elements
213       */
214      public void testContainsAll() {
215          CopyOnWriteArrayList full = populatedArray(3);
216 <        Vector v = new Vector();
217 <        v.add(one);
218 <        v.add(two);
219 <        assertTrue(full.containsAll(v));
220 <        v.add(six);
221 <        assertFalse(full.containsAll(v));
216 >        assertTrue(full.containsAll(Arrays.asList()));
217 >        assertTrue(full.containsAll(Arrays.asList(one)));
218 >        assertTrue(full.containsAll(Arrays.asList(one, two)));
219 >        assertFalse(full.containsAll(Arrays.asList(one, two, six)));
220 >        assertFalse(full.containsAll(Arrays.asList(six)));
221 >
222 >        try {
223 >            full.containsAll(null);
224 >            shouldThrow();
225 >        } catch (NullPointerException success) {}
226      }
227  
228      /**
# Line 213 | Line 234 | public class CopyOnWriteArrayListTest ex
234      }
235  
236      /**
237 <     * indexOf gives the index for the given object
237 >     * indexOf(Object) returns the index of the first occurrence of the
238 >     * specified element in this list, or -1 if this list does not
239 >     * contain the element
240       */
241      public void testIndexOf() {
242 <        CopyOnWriteArrayList full = populatedArray(3);
243 <        assertEquals(1, full.indexOf(one));
244 <        assertEquals(-1, full.indexOf("puppies"));
242 >        CopyOnWriteArrayList list = populatedArray(3);
243 >        assertEquals(-1, list.indexOf(-42));
244 >        int size = list.size();
245 >        for (int i = 0; i < size; i++) {
246 >            assertEquals(i, list.indexOf(i));
247 >            assertEquals(i, list.subList(0, size).indexOf(i));
248 >            assertEquals(i, list.subList(0, i + 1).indexOf(i));
249 >            assertEquals(-1, list.subList(0, i).indexOf(i));
250 >            assertEquals(0, list.subList(i, size).indexOf(i));
251 >            assertEquals(-1, list.subList(i + 1, size).indexOf(i));
252 >        }
253 >
254 >        list.add(1);
255 >        assertEquals(1, list.indexOf(1));
256 >        assertEquals(1, list.subList(0, size + 1).indexOf(1));
257 >        assertEquals(0, list.subList(1, size + 1).indexOf(1));
258 >        assertEquals(size - 2, list.subList(2, size + 1).indexOf(1));
259 >        assertEquals(0, list.subList(size, size + 1).indexOf(1));
260 >        assertEquals(-1, list.subList(size + 1, size + 1).indexOf(1));
261      }
262  
263      /**
264 <     * indexOf gives the index based on the given index
265 <     * at which to start searching
264 >     * indexOf(E, int) returns the index of the first occurrence of the
265 >     * specified element in this list, searching forwards from index,
266 >     * or returns -1 if the element is not found
267       */
268      public void testIndexOf2() {
269 <        CopyOnWriteArrayList full = populatedArray(3);
270 <        assertEquals(1, full.indexOf(one, 0));
271 <        assertEquals(-1, full.indexOf(one, 2));
269 >        CopyOnWriteArrayList list = populatedArray(3);
270 >        int size = list.size();
271 >        assertEquals(-1, list.indexOf(-42, 0));
272 >
273 >        // we might expect IOOBE, but spec says otherwise
274 >        assertEquals(-1, list.indexOf(0, size));
275 >        assertEquals(-1, list.indexOf(0, Integer.MAX_VALUE));
276 >
277 >        assertThrows(
278 >            IndexOutOfBoundsException.class,
279 >            () -> list.indexOf(0, -1),
280 >            () -> list.indexOf(0, Integer.MIN_VALUE));
281 >
282 >        for (int i = 0; i < size; i++) {
283 >            assertEquals(i, list.indexOf(i, 0));
284 >            assertEquals(i, list.indexOf(i, i));
285 >            assertEquals(-1, list.indexOf(i, i + 1));
286 >        }
287 >
288 >        list.add(1);
289 >        assertEquals(1, list.indexOf(1, 0));
290 >        assertEquals(1, list.indexOf(1, 1));
291 >        assertEquals(size, list.indexOf(1, 2));
292 >        assertEquals(size, list.indexOf(1, size));
293      }
294  
295      /**
# Line 256 | Line 317 | public class CopyOnWriteArrayListTest ex
317          Integer[] elements = new Integer[SIZE];
318          for (int i = 0; i < SIZE; i++)
319              elements[i] = i;
320 <        Collections.shuffle(Arrays.asList(elements));
320 >        shuffle(elements);
321          Collection<Integer> full = populatedArray(elements);
322  
323          Iterator it = full.iterator();
# Line 264 | Line 325 | public class CopyOnWriteArrayListTest ex
325              assertTrue(it.hasNext());
326              assertEquals(elements[j], it.next());
327          }
328 <        assertFalse(it.hasNext());
329 <        try {
330 <            it.next();
331 <            shouldThrow();
332 <        } catch (NoSuchElementException success) {}
328 >        assertIteratorExhausted(it);
329 >    }
330 >
331 >    /**
332 >     * iterator of empty collection has no elements
333 >     */
334 >    public void testEmptyIterator() {
335 >        Collection c = new CopyOnWriteArrayList();
336 >        assertIteratorExhausted(c.iterator());
337      }
338  
339      /**
# Line 298 | Line 363 | public class CopyOnWriteArrayListTest ex
363      }
364  
365      /**
366 <     * lastIndexOf returns the index for the given object
366 >     * lastIndexOf(Object) returns the index of the last occurrence of
367 >     * the specified element in this list, or -1 if this list does not
368 >     * contain the element
369       */
370      public void testLastIndexOf1() {
371 <        CopyOnWriteArrayList full = populatedArray(3);
372 <        full.add(one);
373 <        full.add(three);
374 <        assertEquals(3, full.lastIndexOf(one));
375 <        assertEquals(-1, full.lastIndexOf(six));
371 >        CopyOnWriteArrayList list = populatedArray(3);
372 >        assertEquals(-1, list.lastIndexOf(-42));
373 >        int size = list.size();
374 >        for (int i = 0; i < size; i++) {
375 >            assertEquals(i, list.lastIndexOf(i));
376 >            assertEquals(i, list.subList(0, size).lastIndexOf(i));
377 >            assertEquals(i, list.subList(0, i + 1).lastIndexOf(i));
378 >            assertEquals(-1, list.subList(0, i).lastIndexOf(i));
379 >            assertEquals(0, list.subList(i, size).lastIndexOf(i));
380 >            assertEquals(-1, list.subList(i + 1, size).lastIndexOf(i));
381 >        }
382 >
383 >        list.add(1);
384 >        assertEquals(size, list.lastIndexOf(1));
385 >        assertEquals(size, list.subList(0, size + 1).lastIndexOf(1));
386 >        assertEquals(1, list.subList(0, size).lastIndexOf(1));
387 >        assertEquals(0, list.subList(1, 2).lastIndexOf(1));
388 >        assertEquals(-1, list.subList(0, 1).indexOf(1));
389      }
390  
391      /**
392 <     * lastIndexOf returns the index from the given starting point
392 >     * lastIndexOf(E, int) returns the index of the last occurrence of the
393 >     * specified element in this list, searching backwards from index, or
394 >     * returns -1 if the element is not found
395       */
396      public void testLastIndexOf2() {
397 <        CopyOnWriteArrayList full = populatedArray(3);
398 <        full.add(one);
399 <        full.add(three);
400 <        assertEquals(3, full.lastIndexOf(one, 4));
401 <        assertEquals(-1, full.lastIndexOf(three, 3));
397 >        CopyOnWriteArrayList list = populatedArray(3);
398 >
399 >        // we might expect IOOBE, but spec says otherwise
400 >        assertEquals(-1, list.lastIndexOf(0, -1));
401 >
402 >        int size = list.size();
403 >        assertThrows(
404 >            IndexOutOfBoundsException.class,
405 >            () -> list.lastIndexOf(0, size),
406 >            () -> list.lastIndexOf(0, Integer.MAX_VALUE));
407 >
408 >        for (int i = 0; i < size; i++) {
409 >            assertEquals(i, list.lastIndexOf(i, i));
410 >            assertEquals(list.indexOf(i), list.lastIndexOf(i, i));
411 >            if (i > 0)
412 >                assertEquals(-1, list.lastIndexOf(i, i - 1));
413 >        }
414 >        list.add(one);
415 >        list.add(three);
416 >        assertEquals(1, list.lastIndexOf(one, 1));
417 >        assertEquals(1, list.lastIndexOf(one, 2));
418 >        assertEquals(3, list.lastIndexOf(one, 3));
419 >        assertEquals(3, list.lastIndexOf(one, 4));
420 >        assertEquals(-1, list.lastIndexOf(three, 3));
421      }
422  
423      /**
# Line 339 | Line 440 | public class CopyOnWriteArrayListTest ex
440          ListIterator i = full.listIterator(1);
441          int j;
442          for (j = 0; i.hasNext(); j++)
443 <            assertEquals(j+1, i.next());
443 >            assertEquals(j + 1, i.next());
444          assertEquals(2, j);
445      }
446  
# Line 383 | Line 484 | public class CopyOnWriteArrayListTest ex
484       */
485      public void testRemoveAll() {
486          CopyOnWriteArrayList full = populatedArray(3);
487 <        Vector v = new Vector();
488 <        v.add(one);
489 <        v.add(two);
389 <        full.removeAll(v);
487 >        assertTrue(full.removeAll(Arrays.asList(one, two)));
488 >        assertEquals(1, full.size());
489 >        assertFalse(full.removeAll(Arrays.asList(one, two)));
490          assertEquals(1, full.size());
491      }
492  
# Line 421 | Line 521 | public class CopyOnWriteArrayListTest ex
521          Integer[] elements = new Integer[SIZE];
522          for (int i = 0; i < SIZE; i++)
523              elements[i] = i;
524 <        Collections.shuffle(Arrays.asList(elements));
524 >        shuffle(elements);
525          Collection<Integer> full = populatedArray(elements);
526  
527          assertTrue(Arrays.equals(elements, full.toArray()));
# Line 439 | Line 539 | public class CopyOnWriteArrayListTest ex
539          a = new Integer[0];
540          assertSame(a, empty.toArray(a));
541  
542 <        a = new Integer[SIZE/2];
542 >        a = new Integer[SIZE / 2];
543          Arrays.fill(a, 42);
544          assertSame(a, empty.toArray(a));
545          assertNull(a[0]);
# Line 449 | Line 549 | public class CopyOnWriteArrayListTest ex
549          Integer[] elements = new Integer[SIZE];
550          for (int i = 0; i < SIZE; i++)
551              elements[i] = i;
552 <        Collections.shuffle(Arrays.asList(elements));
552 >        shuffle(elements);
553          Collection<Integer> full = populatedArray(elements);
554  
555          Arrays.fill(a, 42);
# Line 463 | Line 563 | public class CopyOnWriteArrayListTest ex
563          assertSame(a, full.toArray(a));
564          assertTrue(Arrays.equals(elements, a));
565  
566 <        a = new Integer[2*SIZE];
566 >        a = new Integer[2 * SIZE];
567          Arrays.fill(a, 42);
568          assertSame(a, full.toArray(a));
569          assertTrue(Arrays.equals(elements, Arrays.copyOf(a, SIZE)));
# Line 493 | Line 593 | public class CopyOnWriteArrayListTest ex
593          assertEquals(a.get(4), m1);
594          s.clear();
595          assertEquals(7, a.size());
596 +
597 +        assertThrows(
598 +            IndexOutOfBoundsException.class,
599 +            () -> s.get(0),
600 +            () -> s.set(0, 42));
601      }
602  
603      // Exception tests
# Line 723 | Line 828 | public class CopyOnWriteArrayListTest ex
828      }
829  
830      /**
831 <     * a deserialized serialized list is equal
831 >     * a deserialized/reserialized list equals original
832       */
833      public void testSerialization() throws Exception {
834          List x = populatedArray(SIZE);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines