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.16 by jsr166, Sun Nov 22 18:57:17 2009 UTC vs.
Revision 1.46 by jsr166, Sat Mar 11 18:20:47 2017 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  
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 <     * Create a queue of given size containing consecutive
43 <     * Integers 0 ... n.
42 >     * Returns a new queue of given size containing consecutive
43 >     * Integers 0 ... n - 1.
44       */
45 <    private LinkedList populatedQueue(int n) {
46 <        LinkedList q = new LinkedList();
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 45 | 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 82 | 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 97 | 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 123 | Line 146 | public class LinkedListTest extends JSR1
146       * addAll(null) throws NPE
147       */
148      public void testAddAll1() {
149 +        LinkedList q = new LinkedList();
150          try {
127            LinkedList q = new LinkedList();
151              q.addAll(null);
152              shouldThrow();
153          } catch (NullPointerException success) {}
# Line 174 | Line 197 | public class LinkedListTest extends JSR1
197      }
198  
199      /**
200 <     *  poll succeeds unless empty
200 >     * poll succeeds unless empty
201       */
202      public void testPoll() {
203          LinkedList q = populatedQueue(SIZE);
# Line 185 | Line 208 | public class LinkedListTest extends JSR1
208      }
209  
210      /**
211 <     *  peek returns next element, or null if empty
211 >     * peek returns next element, or null if empty
212       */
213      public void testPeek() {
214          LinkedList q = populatedQueue(SIZE);
# Line 214 | Line 237 | public class LinkedListTest extends JSR1
237      }
238  
239      /**
240 <     *  remove removes next element, or throws NSEE if empty
240 >     * remove removes next element, or throws NSEE if empty
241       */
242      public void testRemove() {
243          LinkedList q = populatedQueue(SIZE);
# Line 232 | Line 255 | public class LinkedListTest extends JSR1
255       */
256      public void testRemoveElement() {
257          LinkedList q = populatedQueue(SIZE);
258 <        for (int i = 1; i < SIZE; i+=2) {
259 <            assertTrue(q.remove(new Integer(i)));
260 <        }
261 <        for (int i = 0; i < SIZE; i+=2) {
262 <            assertTrue(q.remove(new Integer(i)));
263 <            assertFalse(q.remove(new Integer(i+1)));
258 >        for (int i = 1; i < SIZE; i += 2) {
259 >            assertTrue(q.contains(i));
260 >            assertTrue(q.remove((Integer)i));
261 >            assertFalse(q.contains(i));
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));
270          }
271          assertTrue(q.isEmpty());
272      }
# Line 296 | 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 309 | 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 I = (Integer)(p.remove());
344 <                assertFalse(q.contains(I));
343 >                Integer x = (Integer)(p.remove());
344 >                assertFalse(q.contains(x));
345              }
346          }
347      }
348  
349      /**
350 <     *  toArray contains all elements
350 >     * toArray contains all elements in FIFO order
351       */
352      public void testToArray() {
353          LinkedList q = populatedQueue(SIZE);
354          Object[] o = q.toArray();
326        Arrays.sort(o);
355          for (int i = 0; i < o.length; i++)
356 <            assertEquals(o[i], q.poll());
356 >            assertSame(o[i], q.poll());
357      }
358  
359      /**
360 <     *  toArray(a) contains all elements
360 >     * toArray(a) contains all elements in FIFO order
361       */
362      public void testToArray2() {
363 <        LinkedList q = populatedQueue(SIZE);
363 >        LinkedList<Integer> q = populatedQueue(SIZE);
364          Integer[] ints = new Integer[SIZE];
365 <        ints = (Integer[])q.toArray(ints);
366 <        Arrays.sort(ints);
365 >        Integer[] array = q.toArray(ints);
366 >        assertSame(ints, array);
367          for (int i = 0; i < ints.length; i++)
368 <            assertEquals(ints[i], q.poll());
368 >            assertSame(ints[i], q.poll());
369      }
370  
371      /**
372 <     * toArray(null) throws NPE
372 >     * toArray(null) throws NullPointerException
373       */
374 <    public void testToArray_BadArg() {
374 >    public void testToArray_NullArg() {
375          LinkedList l = new LinkedList();
376          l.add(new Object());
377          try {
378 <            Object o[] = l.toArray(null);
378 >            l.toArray(null);
379              shouldThrow();
380          } catch (NullPointerException success) {}
381      }
382  
383      /**
384 <     * toArray with incompatable aray type throws CCE
384 >     * toArray(incompatible array type) throws ArrayStoreException
385       */
386      public void testToArray1_BadArg() {
387          LinkedList l = new LinkedList();
388          l.add(new Integer(5));
389          try {
390 <            Object o[] = l.toArray(new String[10]);
390 >            l.toArray(new String[10]);
391              shouldThrow();
392          } catch (ArrayStoreException success) {}
393      }
394  
395      /**
396 <     *  iterator iterates through all elements
396 >     * iterator iterates through all elements
397       */
398      public void testIterator() {
399          LinkedList q = populatedQueue(SIZE);
372        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()));
376            ++i;
377        }
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      /**
416 <     *  iterator ordering is FIFO
416 >     * iterator ordering is FIFO
417       */
418      public void testIteratorOrdering() {
419          final LinkedList q = new LinkedList();
# Line 397 | Line 431 | public class LinkedListTest extends JSR1
431      /**
432       * iterator.remove removes current element
433       */
434 <    public void testIteratorRemove () {
434 >    public void testIteratorRemove() {
435          final LinkedList q = new LinkedList();
436          q.add(new Integer(1));
437          q.add(new Integer(2));
438          q.add(new Integer(3));
439          Iterator it = q.iterator();
440 <        assertEquals(it.next(), 1);
440 >        assertEquals(1, it.next());
441          it.remove();
442          it = q.iterator();
443 <        assertEquals(it.next(), 2);
444 <        assertEquals(it.next(), 3);
443 >        assertEquals(2, it.next());
444 >        assertEquals(3, it.next());
445          assertFalse(it.hasNext());
446      }
447  
448      /**
449 <     *  Descending iterator iterates through all elements
449 >     * Descending iterator iterates through all elements
450       */
451      public void testDescendingIterator() {
452          LinkedList q = populatedQueue(SIZE);
# Line 431 | Line 465 | public class LinkedListTest extends JSR1
465      }
466  
467      /**
468 <     *  Descending iterator ordering is reverse FIFO
468 >     * Descending iterator ordering is reverse FIFO
469       */
470      public void testDescendingIteratorOrdering() {
471          final LinkedList q = new LinkedList();
# Line 449 | Line 483 | public class LinkedListTest extends JSR1
483      /**
484       * descendingIterator.remove removes current element
485       */
486 <    public void testDescendingIteratorRemove () {
486 >    public void testDescendingIteratorRemove() {
487          final LinkedList q = new LinkedList();
488 <        q.add(new Integer(3));
489 <        q.add(new Integer(2));
490 <        q.add(new Integer(1));
488 >        q.add(three);
489 >        q.add(two);
490 >        q.add(one);
491          Iterator it = q.descendingIterator();
492          it.next();
493          it.remove();
494          it = q.descendingIterator();
495 <        assertEquals(it.next(), 2);
496 <        assertEquals(it.next(), 3);
495 >        assertSame(it.next(), two);
496 >        assertSame(it.next(), three);
497          assertFalse(it.hasNext());
498      }
499  
466
500      /**
501       * toString contains toStrings of elements
502       */
# Line 471 | Line 504 | public class LinkedListTest extends JSR1
504          LinkedList q = populatedQueue(SIZE);
505          String s = q.toString();
506          for (int i = 0; i < SIZE; ++i) {
507 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
507 >            assertTrue(s.contains(String.valueOf(i)));
508          }
509      }
510  
# Line 481 | Line 514 | public class LinkedListTest extends JSR1
514      public void testAddFirst() {
515          LinkedList q = populatedQueue(3);
516          q.addFirst(four);
517 <        assertEquals(four,q.peek());
517 >        assertSame(four, q.peek());
518      }
519  
520      /**
# Line 489 | Line 522 | public class LinkedListTest extends JSR1
522       */
523      public void testPush() {
524          LinkedList q = populatedQueue(3);
492        q.pollLast();
525          q.push(four);
526 <        assertEquals(four,q.peekFirst());
526 >        assertSame(four, q.peekFirst());
527      }
528  
529      /**
530 <     *  pop removes next element, or throws NSEE if empty
530 >     * pop removes next element, or throws NSEE if empty
531       */
532      public void testPop() {
533          LinkedList q = populatedQueue(SIZE);
# Line 527 | Line 559 | public class LinkedListTest extends JSR1
559      }
560  
561      /**
562 <     *  pollLast succeeds unless empty
562 >     * pollLast succeeds unless empty
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());
570      }
571  
572      /**
573 <     *  peekFirst returns next element, or null if empty
573 >     * peekFirst returns next element, or null if empty
574       */
575      public void testPeekFirst() {
576          LinkedList q = populatedQueue(SIZE);
# Line 551 | Line 583 | public class LinkedListTest extends JSR1
583          assertNull(q.peekFirst());
584      }
585  
554
586      /**
587 <     *  peekLast returns next element, or null if empty
587 >     * peekLast returns next element, or null if empty
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 579 | Line 610 | public class LinkedListTest extends JSR1
610      }
611  
612      /**
613 <     *  getLast returns next element, or throws NSEE if empty
613 >     * getLast returns next element, or throws NSEE if empty
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 599 | Line 630 | public class LinkedListTest extends JSR1
630       */
631      public void testRemoveFirstOccurrence() {
632          LinkedList q = populatedQueue(SIZE);
633 <        for (int i = 1; i < SIZE; i+=2) {
633 >        for (int i = 1; i < SIZE; i += 2) {
634              assertTrue(q.removeFirstOccurrence(new Integer(i)));
635          }
636 <        for (int i = 0; i < SIZE; i+=2) {
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 614 | Line 645 | public class LinkedListTest extends JSR1
645       */
646      public void testRemoveLastOccurrence() {
647          LinkedList q = populatedQueue(SIZE);
648 <        for (int i = 1; i < SIZE; i+=2) {
648 >        for (int i = 1; i < SIZE; i += 2) {
649              assertTrue(q.removeLastOccurrence(new Integer(i)));
650          }
651 <        for (int i = 0; i < SIZE; i+=2) {
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