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.24 by jsr166, Fri Nov 5 00:17:22 2010 UTC vs.
Revision 1.47 by jsr166, Tue Apr 3 05:49:43 2018 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
9 > 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;
18  
19   public class LinkedListTest extends JSR166TestCase {
20      public static void main(String[] args) {
21 <        junit.textui.TestRunner.run(suite());
21 >        main(suite(), args);
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 <     * Create a queue of given size containing consecutive
50 <     * Integers 0 ... n.
49 >     * Returns a new queue of given size containing consecutive
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 45 | Line 73 | public class LinkedListTest extends JSR1
73       */
74      public void testConstructor3() {
75          try {
76 <            LinkedList q = new LinkedList((Collection)null);
76 >            new LinkedList((Collection)null);
77              shouldThrow();
78          } catch (NullPointerException success) {}
79      }
# Line 82 | 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 97 | Line 125 | public class LinkedListTest extends JSR1
125      public void testOfferNull() {
126          LinkedList q = new LinkedList();
127          q.offer(null);
128 +        assertNull(q.get(0));
129 +        assertTrue(q.contains(null));
130      }
131  
132      /**
# Line 123 | Line 153 | public class LinkedListTest extends JSR1
153       * addAll(null) throws NPE
154       */
155      public void testAddAll1() {
156 +        LinkedList q = new LinkedList();
157          try {
127            LinkedList q = new LinkedList();
158              q.addAll(null);
159              shouldThrow();
160          } catch (NullPointerException success) {}
# Line 232 | Line 262 | public class LinkedListTest extends JSR1
262       */
263      public void testRemoveElement() {
264          LinkedList q = populatedQueue(SIZE);
265 <        for (int i = 1; i < SIZE; i+=2) {
266 <            assertTrue(q.remove(new Integer(i)));
267 <        }
268 <        for (int i = 0; i < SIZE; i+=2) {
269 <            assertTrue(q.remove(new Integer(i)));
270 <            assertFalse(q.remove(new Integer(i+1)));
265 >        for (int i = 1; i < SIZE; i += 2) {
266 >            assertTrue(q.contains(i));
267 >            assertTrue(q.remove((Integer)i));
268 >            assertFalse(q.contains(i));
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));
277          }
278          assertTrue(q.isEmpty());
279      }
# Line 296 | 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 309 | 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 I = (Integer)(p.remove());
351 <                assertFalse(q.contains(I));
350 >                Integer x = (Integer)(p.remove());
351 >                assertFalse(q.contains(x));
352              }
353          }
354      }
# Line 368 | Line 404 | public class LinkedListTest extends JSR1
404       */
405      public void testIterator() {
406          LinkedList q = populatedQueue(SIZE);
371        int i = 0;
407          Iterator it = q.iterator();
408 <        while (it.hasNext()) {
408 >        int i;
409 >        for (i = 0; it.hasNext(); i++)
410              assertTrue(q.contains(it.next()));
375            ++i;
376        }
411          assertEquals(i, SIZE);
412 +        assertIteratorExhausted(it);
413 +    }
414 +
415 +    /**
416 +     * iterator of empty collection has no elements
417 +     */
418 +    public void testEmptyIterator() {
419 +        assertIteratorExhausted(new LinkedList().iterator());
420      }
421  
422      /**
# Line 402 | Line 444 | public class LinkedListTest extends JSR1
444          q.add(new Integer(2));
445          q.add(new Integer(3));
446          Iterator it = q.iterator();
447 <        assertEquals(it.next(), 1);
447 >        assertEquals(1, it.next());
448          it.remove();
449          it = q.iterator();
450 <        assertEquals(it.next(), 2);
451 <        assertEquals(it.next(), 3);
450 >        assertEquals(2, it.next());
451 >        assertEquals(3, it.next());
452          assertFalse(it.hasNext());
453      }
454  
# Line 462 | Line 504 | public class LinkedListTest extends JSR1
504          assertFalse(it.hasNext());
505      }
506  
465
507      /**
508       * toString contains toStrings of elements
509       */
# Line 470 | Line 511 | public class LinkedListTest extends JSR1
511          LinkedList q = populatedQueue(SIZE);
512          String s = q.toString();
513          for (int i = 0; i < SIZE; ++i) {
514 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
514 >            assertTrue(s.contains(String.valueOf(i)));
515          }
516      }
517  
# Line 529 | Line 570 | public class LinkedListTest extends JSR1
570       */
571      public void testPollLast() {
572          LinkedList q = populatedQueue(SIZE);
573 <        for (int i = SIZE-1; i >= 0; --i) {
573 >        for (int i = SIZE - 1; i >= 0; --i) {
574              assertEquals(i, q.pollLast());
575          }
576          assertNull(q.pollLast());
# Line 549 | Line 590 | public class LinkedListTest extends JSR1
590          assertNull(q.peekFirst());
591      }
592  
552
593      /**
594       * peekLast returns next element, or null if empty
595       */
596      public void testPeekLast() {
597          LinkedList q = populatedQueue(SIZE);
598 <        for (int i = SIZE-1; i >= 0; --i) {
598 >        for (int i = SIZE - 1; i >= 0; --i) {
599              assertEquals(i, q.peekLast());
600              assertEquals(i, q.pollLast());
601              assertTrue(q.peekLast() == null ||
# Line 581 | Line 621 | public class LinkedListTest extends JSR1
621       */
622      public void testLastElement() {
623          LinkedList q = populatedQueue(SIZE);
624 <        for (int i = SIZE-1; i >= 0; --i) {
624 >        for (int i = SIZE - 1; i >= 0; --i) {
625              assertEquals(i, q.getLast());
626              assertEquals(i, q.pollLast());
627          }
# Line 597 | Line 637 | public class LinkedListTest extends JSR1
637       */
638      public void testRemoveFirstOccurrence() {
639          LinkedList q = populatedQueue(SIZE);
640 <        for (int i = 1; i < SIZE; i+=2) {
640 >        for (int i = 1; i < SIZE; i += 2) {
641              assertTrue(q.removeFirstOccurrence(new Integer(i)));
642          }
643 <        for (int i = 0; i < SIZE; i+=2) {
643 >        for (int i = 0; i < SIZE; i += 2) {
644              assertTrue(q.removeFirstOccurrence(new Integer(i)));
645 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
645 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
646          }
647          assertTrue(q.isEmpty());
648      }
# Line 612 | Line 652 | public class LinkedListTest extends JSR1
652       */
653      public void testRemoveLastOccurrence() {
654          LinkedList q = populatedQueue(SIZE);
655 <        for (int i = 1; i < SIZE; i+=2) {
655 >        for (int i = 1; i < SIZE; i += 2) {
656              assertTrue(q.removeLastOccurrence(new Integer(i)));
657          }
658 <        for (int i = 0; i < SIZE; i+=2) {
658 >        for (int i = 0; i < SIZE; i += 2) {
659              assertTrue(q.removeLastOccurrence(new Integer(i)));
660 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
660 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
661          }
662          assertTrue(q.isEmpty());
663      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines