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.34 by jsr166, Wed Dec 31 20:17:39 2014 UTC vs.
Revision 1.46 by jsr166, Sat Mar 11 18:20:47 2017 UTC

# Line 13 | Line 13 | import java.util.LinkedList;
13   import java.util.NoSuchElementException;
14  
15   import junit.framework.Test;
16 import junit.framework.TestSuite;
16  
17   public class LinkedListTest extends JSR166TestCase {
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run(suite());
19 >        main(suite(), args);
20      }
21  
22      public static Test suite() {
23 <        return new TestSuite(LinkedListTest.class);
23 >        class Implementation implements CollectionImplementation {
24 >            public Class<?> klazz() { return LinkedList.class; }
25 >            public Collection emptyCollection() { return new LinkedList(); }
26 >            public Object makeElement(int i) { return i; }
27 >            public boolean isConcurrent() { return false; }
28 >            public boolean permitsNulls() { return true; }
29 >        }
30 >        class SubListImplementation extends Implementation {
31 >            public Collection emptyCollection() {
32 >                return new LinkedList().subList(0, 0);
33 >            }
34 >        }
35 >        return newTestSuite(
36 >                LinkedListTest.class,
37 >                CollectionTest.testSuite(new Implementation()),
38 >                CollectionTest.testSuite(new SubListImplementation()));
39      }
40  
41      /**
42       * Returns a new queue of given size containing consecutive
43 <     * Integers 0 ... n.
43 >     * Integers 0 ... n - 1.
44       */
45 <    private LinkedList<Integer> populatedQueue(int n) {
46 <        LinkedList<Integer> q = new LinkedList<Integer>();
45 >    private static LinkedList<Integer> populatedQueue(int n) {
46 >        LinkedList<Integer> q = new LinkedList<>();
47          assertTrue(q.isEmpty());
48          for (int i = 0; i < n; ++i)
49              assertTrue(q.offer(new Integer(i)));
50          assertFalse(q.isEmpty());
51          assertEquals(n, q.size());
52 +        assertEquals((Integer) 0, q.peekFirst());
53 +        assertEquals((Integer) (n - 1), q.peekLast());
54          return q;
55      }
56  
# Line 50 | Line 66 | public class LinkedListTest extends JSR1
66       */
67      public void testConstructor3() {
68          try {
69 <            LinkedList q = new LinkedList((Collection)null);
69 >            new LinkedList((Collection)null);
70              shouldThrow();
71          } catch (NullPointerException success) {}
72      }
# Line 87 | Line 103 | public class LinkedListTest extends JSR1
103      public void testSize() {
104          LinkedList q = populatedQueue(SIZE);
105          for (int i = 0; i < SIZE; ++i) {
106 <            assertEquals(SIZE-i, q.size());
106 >            assertEquals(SIZE - i, q.size());
107              q.remove();
108          }
109          for (int i = 0; i < SIZE; ++i) {
# Line 102 | Line 118 | public class LinkedListTest extends JSR1
118      public void testOfferNull() {
119          LinkedList q = new LinkedList();
120          q.offer(null);
121 +        assertNull(q.get(0));
122 +        assertTrue(q.contains(null));
123      }
124  
125      /**
# Line 128 | Line 146 | public class LinkedListTest extends JSR1
146       * addAll(null) throws NPE
147       */
148      public void testAddAll1() {
149 +        LinkedList q = new LinkedList();
150          try {
132            LinkedList q = new LinkedList();
151              q.addAll(null);
152              shouldThrow();
153          } catch (NullPointerException success) {}
# Line 241 | Line 259 | public class LinkedListTest extends JSR1
259              assertTrue(q.contains(i));
260              assertTrue(q.remove((Integer)i));
261              assertFalse(q.contains(i));
262 <            assertTrue(q.contains(i-1));
262 >            assertTrue(q.contains(i - 1));
263          }
264          for (int i = 0; i < SIZE; i += 2) {
265              assertTrue(q.contains(i));
266              assertTrue(q.remove((Integer)i));
267              assertFalse(q.contains(i));
268 <            assertFalse(q.remove((Integer)(i+1)));
269 <            assertFalse(q.contains(i+1));
268 >            assertFalse(q.remove((Integer)(i + 1)));
269 >            assertFalse(q.contains(i + 1));
270          }
271          assertTrue(q.isEmpty());
272      }
# Line 307 | Line 325 | public class LinkedListTest extends JSR1
325                  assertTrue(changed);
326  
327              assertTrue(q.containsAll(p));
328 <            assertEquals(SIZE-i, q.size());
328 >            assertEquals(SIZE - i, q.size());
329              p.remove();
330          }
331      }
# Line 320 | Line 338 | public class LinkedListTest extends JSR1
338              LinkedList q = populatedQueue(SIZE);
339              LinkedList p = populatedQueue(i);
340              assertTrue(q.removeAll(p));
341 <            assertEquals(SIZE-i, q.size());
341 >            assertEquals(SIZE - i, q.size());
342              for (int j = 0; j < i; ++j) {
343                  Integer x = (Integer)(p.remove());
344                  assertFalse(q.contains(x));
# Line 379 | Line 397 | public class LinkedListTest extends JSR1
397       */
398      public void testIterator() {
399          LinkedList q = populatedQueue(SIZE);
382        int i = 0;
400          Iterator it = q.iterator();
401 <        while (it.hasNext()) {
401 >        int i;
402 >        for (i = 0; it.hasNext(); i++)
403              assertTrue(q.contains(it.next()));
386            ++i;
387        }
404          assertEquals(i, SIZE);
405 +        assertIteratorExhausted(it);
406 +    }
407 +
408 +    /**
409 +     * iterator of empty collection has no elements
410 +     */
411 +    public void testEmptyIterator() {
412 +        assertIteratorExhausted(new LinkedList().iterator());
413      }
414  
415      /**
# Line 539 | Line 563 | public class LinkedListTest extends JSR1
563       */
564      public void testPollLast() {
565          LinkedList q = populatedQueue(SIZE);
566 <        for (int i = SIZE-1; i >= 0; --i) {
566 >        for (int i = SIZE - 1; i >= 0; --i) {
567              assertEquals(i, q.pollLast());
568          }
569          assertNull(q.pollLast());
# Line 564 | Line 588 | public class LinkedListTest extends JSR1
588       */
589      public void testPeekLast() {
590          LinkedList q = populatedQueue(SIZE);
591 <        for (int i = SIZE-1; i >= 0; --i) {
591 >        for (int i = SIZE - 1; i >= 0; --i) {
592              assertEquals(i, q.peekLast());
593              assertEquals(i, q.pollLast());
594              assertTrue(q.peekLast() == null ||
# Line 590 | Line 614 | public class LinkedListTest extends JSR1
614       */
615      public void testLastElement() {
616          LinkedList q = populatedQueue(SIZE);
617 <        for (int i = SIZE-1; i >= 0; --i) {
617 >        for (int i = SIZE - 1; i >= 0; --i) {
618              assertEquals(i, q.getLast());
619              assertEquals(i, q.pollLast());
620          }
# Line 611 | Line 635 | public class LinkedListTest extends JSR1
635          }
636          for (int i = 0; i < SIZE; i += 2) {
637              assertTrue(q.removeFirstOccurrence(new Integer(i)));
638 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
638 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
639          }
640          assertTrue(q.isEmpty());
641      }
# Line 626 | Line 650 | public class LinkedListTest extends JSR1
650          }
651          for (int i = 0; i < SIZE; i += 2) {
652              assertTrue(q.removeLastOccurrence(new Integer(i)));
653 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
653 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
654          }
655          assertTrue(q.isEmpty());
656      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines