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.17 by jsr166, Tue Dec 1 09:56:28 2009 UTC vs.
Revision 1.40 by jsr166, Sun May 24 01:42:14 2015 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() {
# Line 20 | Line 25 | public class LinkedListTest extends JSR1
25      }
26  
27      /**
28 <     * Create a queue of given size containing consecutive
28 >     * Returns a new queue of given size containing consecutive
29       * Integers 0 ... n.
30       */
31 <    private LinkedList populatedQueue(int n) {
32 <        LinkedList q = new LinkedList();
31 >    private LinkedList<Integer> populatedQueue(int n) {
32 >        LinkedList<Integer> q = new LinkedList<Integer>();
33          assertTrue(q.isEmpty());
34          for (int i = 0; i < n; ++i)
35              assertTrue(q.offer(new Integer(i)));
# Line 45 | Line 50 | public class LinkedListTest extends JSR1
50       */
51      public void testConstructor3() {
52          try {
53 <            LinkedList q = new LinkedList((Collection)null);
53 >            new LinkedList((Collection)null);
54              shouldThrow();
55          } catch (NullPointerException success) {}
56      }
# Line 82 | Line 87 | public class LinkedListTest extends JSR1
87      public void testSize() {
88          LinkedList q = populatedQueue(SIZE);
89          for (int i = 0; i < SIZE; ++i) {
90 <            assertEquals(SIZE-i, q.size());
90 >            assertEquals(SIZE - i, q.size());
91              q.remove();
92          }
93          for (int i = 0; i < SIZE; ++i) {
# Line 97 | Line 102 | public class LinkedListTest extends JSR1
102      public void testOfferNull() {
103          LinkedList q = new LinkedList();
104          q.offer(null);
105 +        assertNull(q.get(0));
106 +        assertTrue(q.contains(null));
107      }
108  
109      /**
# Line 123 | Line 130 | public class LinkedListTest extends JSR1
130       * addAll(null) throws NPE
131       */
132      public void testAddAll1() {
133 +        LinkedList q = new LinkedList();
134          try {
127            LinkedList q = new LinkedList();
135              q.addAll(null);
136              shouldThrow();
137          } catch (NullPointerException success) {}
# Line 174 | Line 181 | public class LinkedListTest extends JSR1
181      }
182  
183      /**
184 <     *  poll succeeds unless empty
184 >     * poll succeeds unless empty
185       */
186      public void testPoll() {
187          LinkedList q = populatedQueue(SIZE);
# Line 185 | Line 192 | public class LinkedListTest extends JSR1
192      }
193  
194      /**
195 <     *  peek returns next element, or null if empty
195 >     * peek returns next element, or null if empty
196       */
197      public void testPeek() {
198          LinkedList q = populatedQueue(SIZE);
# Line 214 | Line 221 | public class LinkedListTest extends JSR1
221      }
222  
223      /**
224 <     *  remove removes next element, or throws NSEE if empty
224 >     * remove removes next element, or throws NSEE if empty
225       */
226      public void testRemove() {
227          LinkedList q = populatedQueue(SIZE);
# Line 232 | Line 239 | public class LinkedListTest extends JSR1
239       */
240      public void testRemoveElement() {
241          LinkedList q = populatedQueue(SIZE);
242 <        for (int i = 1; i < SIZE; i+=2) {
243 <            assertTrue(q.remove(new Integer(i)));
244 <        }
245 <        for (int i = 0; i < SIZE; i+=2) {
246 <            assertTrue(q.remove(new Integer(i)));
247 <            assertFalse(q.remove(new Integer(i+1)));
242 >        for (int i = 1; i < SIZE; i += 2) {
243 >            assertTrue(q.contains(i));
244 >            assertTrue(q.remove((Integer)i));
245 >            assertFalse(q.contains(i));
246 >            assertTrue(q.contains(i - 1));
247 >        }
248 >        for (int i = 0; i < SIZE; i += 2) {
249 >            assertTrue(q.contains(i));
250 >            assertTrue(q.remove((Integer)i));
251 >            assertFalse(q.contains(i));
252 >            assertFalse(q.remove((Integer)(i + 1)));
253 >            assertFalse(q.contains(i + 1));
254          }
255          assertTrue(q.isEmpty());
256      }
# Line 296 | Line 309 | public class LinkedListTest extends JSR1
309                  assertTrue(changed);
310  
311              assertTrue(q.containsAll(p));
312 <            assertEquals(SIZE-i, q.size());
312 >            assertEquals(SIZE - i, q.size());
313              p.remove();
314          }
315      }
# Line 309 | Line 322 | public class LinkedListTest extends JSR1
322              LinkedList q = populatedQueue(SIZE);
323              LinkedList p = populatedQueue(i);
324              assertTrue(q.removeAll(p));
325 <            assertEquals(SIZE-i, q.size());
325 >            assertEquals(SIZE - i, q.size());
326              for (int j = 0; j < i; ++j) {
327 <                Integer I = (Integer)(p.remove());
328 <                assertFalse(q.contains(I));
327 >                Integer x = (Integer)(p.remove());
328 >                assertFalse(q.contains(x));
329              }
330          }
331      }
332  
333      /**
334 <     *  toArray contains all elements
334 >     * toArray contains all elements in FIFO order
335       */
336      public void testToArray() {
337          LinkedList q = populatedQueue(SIZE);
338          Object[] o = q.toArray();
326        Arrays.sort(o);
339          for (int i = 0; i < o.length; i++)
340 <            assertEquals(o[i], q.poll());
340 >            assertSame(o[i], q.poll());
341      }
342  
343      /**
344 <     *  toArray(a) contains all elements
344 >     * toArray(a) contains all elements in FIFO order
345       */
346      public void testToArray2() {
347 <        LinkedList q = populatedQueue(SIZE);
347 >        LinkedList<Integer> q = populatedQueue(SIZE);
348          Integer[] ints = new Integer[SIZE];
349 <        ints = (Integer[])q.toArray(ints);
350 <        Arrays.sort(ints);
349 >        Integer[] array = q.toArray(ints);
350 >        assertSame(ints, array);
351          for (int i = 0; i < ints.length; i++)
352 <            assertEquals(ints[i], q.poll());
352 >            assertSame(ints[i], q.poll());
353      }
354  
355      /**
356 <     * toArray(null) throws NPE
356 >     * toArray(null) throws NullPointerException
357       */
358 <    public void testToArray_BadArg() {
358 >    public void testToArray_NullArg() {
359          LinkedList l = new LinkedList();
360          l.add(new Object());
361          try {
362 <            Object o[] = l.toArray(null);
362 >            l.toArray(null);
363              shouldThrow();
364          } catch (NullPointerException success) {}
365      }
366  
367      /**
368 <     * toArray with incompatable aray type throws CCE
368 >     * toArray(incompatible array type) throws ArrayStoreException
369       */
370      public void testToArray1_BadArg() {
371          LinkedList l = new LinkedList();
372          l.add(new Integer(5));
373          try {
374 <            Object o[] = l.toArray(new String[10]);
374 >            l.toArray(new String[10]);
375              shouldThrow();
376          } catch (ArrayStoreException success) {}
377      }
378  
379      /**
380 <     *  iterator iterates through all elements
380 >     * iterator iterates through all elements
381       */
382      public void testIterator() {
383          LinkedList q = populatedQueue(SIZE);
372        int i = 0;
384          Iterator it = q.iterator();
385 <        while (it.hasNext()) {
385 >        int i;
386 >        for (i = 0; it.hasNext(); i++)
387              assertTrue(q.contains(it.next()));
376            ++i;
377        }
388          assertEquals(i, SIZE);
389 +        assertIteratorExhausted(it);
390 +    }
391 +
392 +    /**
393 +     * iterator of empty collection has no elements
394 +     */
395 +    public void testEmptyIterator() {
396 +        assertIteratorExhausted(new LinkedList().iterator());
397      }
398  
399      /**
400 <     *  iterator ordering is FIFO
400 >     * iterator ordering is FIFO
401       */
402      public void testIteratorOrdering() {
403          final LinkedList q = new LinkedList();
# Line 397 | Line 415 | public class LinkedListTest extends JSR1
415      /**
416       * iterator.remove removes current element
417       */
418 <    public void testIteratorRemove () {
418 >    public void testIteratorRemove() {
419          final LinkedList q = new LinkedList();
420          q.add(new Integer(1));
421          q.add(new Integer(2));
422          q.add(new Integer(3));
423          Iterator it = q.iterator();
424 <        assertEquals(it.next(), 1);
424 >        assertEquals(1, it.next());
425          it.remove();
426          it = q.iterator();
427 <        assertEquals(it.next(), 2);
428 <        assertEquals(it.next(), 3);
427 >        assertEquals(2, it.next());
428 >        assertEquals(3, it.next());
429          assertFalse(it.hasNext());
430      }
431  
432      /**
433 <     *  Descending iterator iterates through all elements
433 >     * Descending iterator iterates through all elements
434       */
435      public void testDescendingIterator() {
436          LinkedList q = populatedQueue(SIZE);
# Line 431 | Line 449 | public class LinkedListTest extends JSR1
449      }
450  
451      /**
452 <     *  Descending iterator ordering is reverse FIFO
452 >     * Descending iterator ordering is reverse FIFO
453       */
454      public void testDescendingIteratorOrdering() {
455          final LinkedList q = new LinkedList();
# Line 449 | Line 467 | public class LinkedListTest extends JSR1
467      /**
468       * descendingIterator.remove removes current element
469       */
470 <    public void testDescendingIteratorRemove () {
470 >    public void testDescendingIteratorRemove() {
471          final LinkedList q = new LinkedList();
472          q.add(three);
473          q.add(two);
# Line 463 | Line 481 | public class LinkedListTest extends JSR1
481          assertFalse(it.hasNext());
482      }
483  
466
484      /**
485       * toString contains toStrings of elements
486       */
# Line 471 | Line 488 | public class LinkedListTest extends JSR1
488          LinkedList q = populatedQueue(SIZE);
489          String s = q.toString();
490          for (int i = 0; i < SIZE; ++i) {
491 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
491 >            assertTrue(s.contains(String.valueOf(i)));
492          }
493      }
494  
# Line 494 | Line 511 | public class LinkedListTest extends JSR1
511      }
512  
513      /**
514 <     *  pop removes next element, or throws NSEE if empty
514 >     * pop removes next element, or throws NSEE if empty
515       */
516      public void testPop() {
517          LinkedList q = populatedQueue(SIZE);
# Line 526 | Line 543 | public class LinkedListTest extends JSR1
543      }
544  
545      /**
546 <     *  pollLast succeeds unless empty
546 >     * pollLast succeeds unless empty
547       */
548      public void testPollLast() {
549          LinkedList q = populatedQueue(SIZE);
550 <        for (int i = SIZE-1; i >= 0; --i) {
550 >        for (int i = SIZE - 1; i >= 0; --i) {
551              assertEquals(i, q.pollLast());
552          }
553          assertNull(q.pollLast());
554      }
555  
556      /**
557 <     *  peekFirst returns next element, or null if empty
557 >     * peekFirst returns next element, or null if empty
558       */
559      public void testPeekFirst() {
560          LinkedList q = populatedQueue(SIZE);
# Line 550 | Line 567 | public class LinkedListTest extends JSR1
567          assertNull(q.peekFirst());
568      }
569  
553
570      /**
571 <     *  peekLast returns next element, or null if empty
571 >     * peekLast returns next element, or null if empty
572       */
573      public void testPeekLast() {
574          LinkedList q = populatedQueue(SIZE);
575 <        for (int i = SIZE-1; i >= 0; --i) {
575 >        for (int i = SIZE - 1; i >= 0; --i) {
576              assertEquals(i, q.peekLast());
577              assertEquals(i, q.pollLast());
578              assertTrue(q.peekLast() == null ||
# Line 578 | Line 594 | public class LinkedListTest extends JSR1
594      }
595  
596      /**
597 <     *  getLast returns next element, or throws NSEE if empty
597 >     * getLast returns next element, or throws NSEE if empty
598       */
599      public void testLastElement() {
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.getLast());
603              assertEquals(i, q.pollLast());
604          }
# Line 598 | Line 614 | public class LinkedListTest extends JSR1
614       */
615      public void testRemoveFirstOccurrence() {
616          LinkedList q = populatedQueue(SIZE);
617 <        for (int i = 1; i < SIZE; i+=2) {
617 >        for (int i = 1; i < SIZE; i += 2) {
618              assertTrue(q.removeFirstOccurrence(new Integer(i)));
619          }
620 <        for (int i = 0; i < SIZE; i+=2) {
620 >        for (int i = 0; i < SIZE; i += 2) {
621              assertTrue(q.removeFirstOccurrence(new Integer(i)));
622 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
622 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
623          }
624          assertTrue(q.isEmpty());
625      }
# Line 613 | Line 629 | public class LinkedListTest extends JSR1
629       */
630      public void testRemoveLastOccurrence() {
631          LinkedList q = populatedQueue(SIZE);
632 <        for (int i = 1; i < SIZE; i+=2) {
632 >        for (int i = 1; i < SIZE; i += 2) {
633              assertTrue(q.removeLastOccurrence(new Integer(i)));
634          }
635 <        for (int i = 0; i < SIZE; i+=2) {
635 >        for (int i = 0; i < SIZE; i += 2) {
636              assertTrue(q.removeLastOccurrence(new Integer(i)));
637 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
637 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
638          }
639          assertTrue(q.isEmpty());
640      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines