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

Comparing jsr166/src/test/tck/LinkedListTest.java (file contents):
Revision 1.38 by jsr166, Fri May 15 18:21:19 2015 UTC vs.
Revision 1.49 by jsr166, Fri Jun 22 00:04:58 2018 UTC

# Line 10 | Line 10 | import java.util.Arrays;
10   import java.util.Collection;
11   import java.util.Iterator;
12   import java.util.LinkedList;
13 + import java.util.List;
14   import java.util.NoSuchElementException;
15 + import java.util.concurrent.ThreadLocalRandom;
16  
17   import junit.framework.Test;
16 import junit.framework.TestSuite;
18  
19   public class LinkedListTest extends JSR166TestCase {
20      public static void main(String[] args) {
# Line 21 | Line 22 | public class LinkedListTest extends JSR1
22      }
23  
24      public static Test suite() {
25 <        return new TestSuite(LinkedListTest.class);
25 >        class Implementation implements CollectionImplementation {
26 >            public Class<?> klazz() { return LinkedList.class; }
27 >            public List emptyCollection() { return new LinkedList(); }
28 >            public Object makeElement(int i) { return i; }
29 >            public boolean isConcurrent() { return false; }
30 >            public boolean permitsNulls() { return true; }
31 >        }
32 >        class SubListImplementation extends Implementation {
33 >            public List emptyCollection() {
34 >                List list = super.emptyCollection();
35 >                ThreadLocalRandom rnd = ThreadLocalRandom.current();
36 >                if (rnd.nextBoolean())
37 >                    list.add(makeElement(rnd.nextInt()));
38 >                int i = rnd.nextInt(list.size() + 1);
39 >                return list.subList(i, i);
40 >            }
41 >        }
42 >        return newTestSuite(
43 >                LinkedListTest.class,
44 >                CollectionTest.testSuite(new Implementation()),
45 >                CollectionTest.testSuite(new SubListImplementation()));
46      }
47  
48      /**
49       * Returns a new queue of given size containing consecutive
50 <     * Integers 0 ... n.
50 >     * Integers 0 ... n - 1.
51       */
52 <    private LinkedList<Integer> populatedQueue(int n) {
53 <        LinkedList<Integer> q = new LinkedList<Integer>();
52 >    private static LinkedList<Integer> populatedQueue(int n) {
53 >        LinkedList<Integer> q = new LinkedList<>();
54          assertTrue(q.isEmpty());
55          for (int i = 0; i < n; ++i)
56              assertTrue(q.offer(new Integer(i)));
57          assertFalse(q.isEmpty());
58          assertEquals(n, q.size());
59 +        assertEquals((Integer) 0, q.peekFirst());
60 +        assertEquals((Integer) (n - 1), q.peekLast());
61          return q;
62      }
63  
# Line 87 | Line 110 | public class LinkedListTest extends JSR1
110      public void testSize() {
111          LinkedList q = populatedQueue(SIZE);
112          for (int i = 0; i < SIZE; ++i) {
113 <            assertEquals(SIZE-i, q.size());
113 >            assertEquals(SIZE - i, q.size());
114              q.remove();
115          }
116          for (int i = 0; i < SIZE; ++i) {
# Line 243 | Line 266 | public class LinkedListTest extends JSR1
266              assertTrue(q.contains(i));
267              assertTrue(q.remove((Integer)i));
268              assertFalse(q.contains(i));
269 <            assertTrue(q.contains(i-1));
269 >            assertTrue(q.contains(i - 1));
270          }
271          for (int i = 0; i < SIZE; i += 2) {
272              assertTrue(q.contains(i));
273              assertTrue(q.remove((Integer)i));
274              assertFalse(q.contains(i));
275 <            assertFalse(q.remove((Integer)(i+1)));
276 <            assertFalse(q.contains(i+1));
275 >            assertFalse(q.remove((Integer)(i + 1)));
276 >            assertFalse(q.contains(i + 1));
277          }
278          assertTrue(q.isEmpty());
279      }
# Line 309 | Line 332 | public class LinkedListTest extends JSR1
332                  assertTrue(changed);
333  
334              assertTrue(q.containsAll(p));
335 <            assertEquals(SIZE-i, q.size());
335 >            assertEquals(SIZE - i, q.size());
336              p.remove();
337          }
338      }
# Line 322 | Line 345 | public class LinkedListTest extends JSR1
345              LinkedList q = populatedQueue(SIZE);
346              LinkedList p = populatedQueue(i);
347              assertTrue(q.removeAll(p));
348 <            assertEquals(SIZE-i, q.size());
348 >            assertEquals(SIZE - i, q.size());
349              for (int j = 0; j < i; ++j) {
350                  Integer x = (Integer)(p.remove());
351                  assertFalse(q.contains(x));
# Line 335 | Line 358 | public class LinkedListTest extends JSR1
358       */
359      public void testToArray() {
360          LinkedList q = populatedQueue(SIZE);
361 <        Object[] o = q.toArray();
362 <        for (int i = 0; i < o.length; i++)
363 <            assertSame(o[i], q.poll());
361 >        Object[] a = q.toArray();
362 >        assertSame(Object[].class, a.getClass());
363 >        for (Object o : a)
364 >            assertSame(o, q.poll());
365 >        assertTrue(q.isEmpty());
366      }
367  
368      /**
# Line 348 | Line 373 | public class LinkedListTest extends JSR1
373          Integer[] ints = new Integer[SIZE];
374          Integer[] array = q.toArray(ints);
375          assertSame(ints, array);
376 <        for (int i = 0; i < ints.length; i++)
377 <            assertSame(ints[i], q.poll());
376 >        for (Integer o : ints)
377 >            assertSame(o, q.poll());
378 >        assertTrue(q.isEmpty());
379      }
380  
381      /**
# Line 359 | Line 385 | public class LinkedListTest extends JSR1
385          LinkedList l = new LinkedList();
386          l.add(new Object());
387          try {
388 <            l.toArray(null);
388 >            l.toArray((Object[])null);
389              shouldThrow();
390          } catch (NullPointerException success) {}
391      }
# Line 547 | Line 573 | public class LinkedListTest extends JSR1
573       */
574      public void testPollLast() {
575          LinkedList q = populatedQueue(SIZE);
576 <        for (int i = SIZE-1; i >= 0; --i) {
576 >        for (int i = SIZE - 1; i >= 0; --i) {
577              assertEquals(i, q.pollLast());
578          }
579          assertNull(q.pollLast());
# Line 572 | Line 598 | public class LinkedListTest extends JSR1
598       */
599      public void testPeekLast() {
600          LinkedList q = populatedQueue(SIZE);
601 <        for (int i = SIZE-1; i >= 0; --i) {
601 >        for (int i = SIZE - 1; i >= 0; --i) {
602              assertEquals(i, q.peekLast());
603              assertEquals(i, q.pollLast());
604              assertTrue(q.peekLast() == null ||
# Line 598 | Line 624 | public class LinkedListTest extends JSR1
624       */
625      public void testLastElement() {
626          LinkedList q = populatedQueue(SIZE);
627 <        for (int i = SIZE-1; i >= 0; --i) {
627 >        for (int i = SIZE - 1; i >= 0; --i) {
628              assertEquals(i, q.getLast());
629              assertEquals(i, q.pollLast());
630          }
# Line 619 | Line 645 | public class LinkedListTest extends JSR1
645          }
646          for (int i = 0; i < SIZE; i += 2) {
647              assertTrue(q.removeFirstOccurrence(new Integer(i)));
648 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
648 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
649          }
650          assertTrue(q.isEmpty());
651      }
# Line 634 | Line 660 | public class LinkedListTest extends JSR1
660          }
661          for (int i = 0; i < SIZE; i += 2) {
662              assertTrue(q.removeLastOccurrence(new Integer(i)));
663 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
663 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
664          }
665          assertTrue(q.isEmpty());
666      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines