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.23 by jsr166, Thu Nov 4 01:04:54 2010 UTC vs.
Revision 1.42 by jsr166, Mon Oct 17 00:56:11 2016 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.NoSuchElementException;
14 >
15 > import junit.framework.Test;
16 > import junit.framework.TestSuite;
17  
18   public class LinkedListTest extends JSR166TestCase {
19      public static void main(String[] args) {
20 <        junit.textui.TestRunner.run(suite());
20 >        main(suite(), args);
21      }
22  
23      public static Test suite() {
24 <        return new TestSuite(LinkedListTest.class);
24 >        class Implementation implements CollectionImplementation {
25 >            public Class<?> klazz() { return LinkedList.class; }
26 >            public Collection emptyCollection() { return new LinkedList(); }
27 >            public Object makeElement(int i) { return i; }
28 >            public boolean isConcurrent() { return false; }
29 >            public boolean permitsNulls() { return true; }
30 >        }
31 >        return newTestSuite(LinkedListTest.class,
32 >                            CollectionTest.testSuite(new Implementation()));
33      }
34  
35      /**
36 <     * Create a queue of given size containing consecutive
37 <     * Integers 0 ... n.
36 >     * Returns a new queue of given size containing consecutive
37 >     * Integers 0 ... n - 1.
38       */
39 <    private LinkedList populatedQueue(int n) {
40 <        LinkedList q = new LinkedList();
39 >    private LinkedList<Integer> populatedQueue(int n) {
40 >        LinkedList<Integer> q = new LinkedList<Integer>();
41          assertTrue(q.isEmpty());
42          for (int i = 0; i < n; ++i)
43              assertTrue(q.offer(new Integer(i)));
44          assertFalse(q.isEmpty());
45          assertEquals(n, q.size());
46 +        assertEquals((Integer) 0, q.peekFirst());
47 +        assertEquals((Integer) (n - 1), q.peekLast());
48          return q;
49      }
50  
# Line 45 | Line 60 | public class LinkedListTest extends JSR1
60       */
61      public void testConstructor3() {
62          try {
63 <            LinkedList q = new LinkedList((Collection)null);
63 >            new LinkedList((Collection)null);
64              shouldThrow();
65          } catch (NullPointerException success) {}
66      }
# Line 82 | Line 97 | public class LinkedListTest extends JSR1
97      public void testSize() {
98          LinkedList q = populatedQueue(SIZE);
99          for (int i = 0; i < SIZE; ++i) {
100 <            assertEquals(SIZE-i, q.size());
100 >            assertEquals(SIZE - i, q.size());
101              q.remove();
102          }
103          for (int i = 0; i < SIZE; ++i) {
# Line 97 | Line 112 | public class LinkedListTest extends JSR1
112      public void testOfferNull() {
113          LinkedList q = new LinkedList();
114          q.offer(null);
115 +        assertNull(q.get(0));
116 +        assertTrue(q.contains(null));
117      }
118  
119      /**
# Line 123 | Line 140 | public class LinkedListTest extends JSR1
140       * addAll(null) throws NPE
141       */
142      public void testAddAll1() {
143 +        LinkedList q = new LinkedList();
144          try {
127            LinkedList q = new LinkedList();
145              q.addAll(null);
146              shouldThrow();
147          } catch (NullPointerException success) {}
# Line 232 | Line 249 | public class LinkedListTest extends JSR1
249       */
250      public void testRemoveElement() {
251          LinkedList q = populatedQueue(SIZE);
252 <        for (int i = 1; i < SIZE; i+=2) {
253 <            assertTrue(q.remove(new Integer(i)));
254 <        }
255 <        for (int i = 0; i < SIZE; i+=2) {
256 <            assertTrue(q.remove(new Integer(i)));
257 <            assertFalse(q.remove(new Integer(i+1)));
252 >        for (int i = 1; i < SIZE; i += 2) {
253 >            assertTrue(q.contains(i));
254 >            assertTrue(q.remove((Integer)i));
255 >            assertFalse(q.contains(i));
256 >            assertTrue(q.contains(i - 1));
257 >        }
258 >        for (int i = 0; i < SIZE; i += 2) {
259 >            assertTrue(q.contains(i));
260 >            assertTrue(q.remove((Integer)i));
261 >            assertFalse(q.contains(i));
262 >            assertFalse(q.remove((Integer)(i + 1)));
263 >            assertFalse(q.contains(i + 1));
264          }
265          assertTrue(q.isEmpty());
266      }
# Line 296 | Line 319 | public class LinkedListTest extends JSR1
319                  assertTrue(changed);
320  
321              assertTrue(q.containsAll(p));
322 <            assertEquals(SIZE-i, q.size());
322 >            assertEquals(SIZE - i, q.size());
323              p.remove();
324          }
325      }
# Line 309 | Line 332 | public class LinkedListTest extends JSR1
332              LinkedList q = populatedQueue(SIZE);
333              LinkedList p = populatedQueue(i);
334              assertTrue(q.removeAll(p));
335 <            assertEquals(SIZE-i, q.size());
335 >            assertEquals(SIZE - i, q.size());
336              for (int j = 0; j < i; ++j) {
337 <                Integer I = (Integer)(p.remove());
338 <                assertFalse(q.contains(I));
337 >                Integer x = (Integer)(p.remove());
338 >                assertFalse(q.contains(x));
339              }
340          }
341      }
# Line 331 | Line 354 | public class LinkedListTest extends JSR1
354       * toArray(a) contains all elements in FIFO order
355       */
356      public void testToArray2() {
357 <        LinkedList q = populatedQueue(SIZE);
357 >        LinkedList<Integer> q = populatedQueue(SIZE);
358          Integer[] ints = new Integer[SIZE];
359 <        assertSame(ints, q.toArray(ints));
359 >        Integer[] array = q.toArray(ints);
360 >        assertSame(ints, array);
361          for (int i = 0; i < ints.length; i++)
362              assertSame(ints[i], q.poll());
363      }
# Line 367 | Line 391 | public class LinkedListTest extends JSR1
391       */
392      public void testIterator() {
393          LinkedList q = populatedQueue(SIZE);
370        int i = 0;
394          Iterator it = q.iterator();
395 <        while (it.hasNext()) {
395 >        int i;
396 >        for (i = 0; it.hasNext(); i++)
397              assertTrue(q.contains(it.next()));
374            ++i;
375        }
398          assertEquals(i, SIZE);
399 +        assertIteratorExhausted(it);
400 +    }
401 +
402 +    /**
403 +     * iterator of empty collection has no elements
404 +     */
405 +    public void testEmptyIterator() {
406 +        assertIteratorExhausted(new LinkedList().iterator());
407      }
408  
409      /**
# Line 401 | Line 431 | public class LinkedListTest extends JSR1
431          q.add(new Integer(2));
432          q.add(new Integer(3));
433          Iterator it = q.iterator();
434 <        assertEquals(it.next(), 1);
434 >        assertEquals(1, it.next());
435          it.remove();
436          it = q.iterator();
437 <        assertEquals(it.next(), 2);
438 <        assertEquals(it.next(), 3);
437 >        assertEquals(2, it.next());
438 >        assertEquals(3, it.next());
439          assertFalse(it.hasNext());
440      }
441  
# Line 461 | Line 491 | public class LinkedListTest extends JSR1
491          assertFalse(it.hasNext());
492      }
493  
464
494      /**
495       * toString contains toStrings of elements
496       */
# Line 469 | Line 498 | public class LinkedListTest extends JSR1
498          LinkedList q = populatedQueue(SIZE);
499          String s = q.toString();
500          for (int i = 0; i < SIZE; ++i) {
501 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
501 >            assertTrue(s.contains(String.valueOf(i)));
502          }
503      }
504  
# Line 528 | Line 557 | public class LinkedListTest extends JSR1
557       */
558      public void testPollLast() {
559          LinkedList q = populatedQueue(SIZE);
560 <        for (int i = SIZE-1; i >= 0; --i) {
560 >        for (int i = SIZE - 1; i >= 0; --i) {
561              assertEquals(i, q.pollLast());
562          }
563          assertNull(q.pollLast());
# Line 548 | Line 577 | public class LinkedListTest extends JSR1
577          assertNull(q.peekFirst());
578      }
579  
551
580      /**
581       * peekLast returns next element, or null if empty
582       */
583      public void testPeekLast() {
584          LinkedList q = populatedQueue(SIZE);
585 <        for (int i = SIZE-1; i >= 0; --i) {
585 >        for (int i = SIZE - 1; i >= 0; --i) {
586              assertEquals(i, q.peekLast());
587              assertEquals(i, q.pollLast());
588              assertTrue(q.peekLast() == null ||
# Line 580 | Line 608 | public class LinkedListTest extends JSR1
608       */
609      public void testLastElement() {
610          LinkedList q = populatedQueue(SIZE);
611 <        for (int i = SIZE-1; i >= 0; --i) {
611 >        for (int i = SIZE - 1; i >= 0; --i) {
612              assertEquals(i, q.getLast());
613              assertEquals(i, q.pollLast());
614          }
# Line 596 | Line 624 | public class LinkedListTest extends JSR1
624       */
625      public void testRemoveFirstOccurrence() {
626          LinkedList q = populatedQueue(SIZE);
627 <        for (int i = 1; i < SIZE; i+=2) {
627 >        for (int i = 1; i < SIZE; i += 2) {
628              assertTrue(q.removeFirstOccurrence(new Integer(i)));
629          }
630 <        for (int i = 0; i < SIZE; i+=2) {
630 >        for (int i = 0; i < SIZE; i += 2) {
631              assertTrue(q.removeFirstOccurrence(new Integer(i)));
632 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
632 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
633          }
634          assertTrue(q.isEmpty());
635      }
# Line 611 | Line 639 | public class LinkedListTest extends JSR1
639       */
640      public void testRemoveLastOccurrence() {
641          LinkedList q = populatedQueue(SIZE);
642 <        for (int i = 1; i < SIZE; i+=2) {
642 >        for (int i = 1; i < SIZE; i += 2) {
643              assertTrue(q.removeLastOccurrence(new Integer(i)));
644          }
645 <        for (int i = 0; i < SIZE; i+=2) {
645 >        for (int i = 0; i < SIZE; i += 2) {
646              assertTrue(q.removeLastOccurrence(new Integer(i)));
647 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
647 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
648          }
649          assertTrue(q.isEmpty());
650      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines