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.7 by dl, Sat Dec 27 19:26:43 2003 UTC vs.
Revision 1.10 by jsr166, Mon Nov 2 20:28:31 2009 UTC

# Line 2 | Line 2
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
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 12 | Line 12 | import java.util.concurrent.*;
12  
13   public class LinkedListTest extends JSR166TestCase {
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run (suite());  
15 >        junit.textui.TestRunner.run (suite());
16      }
17  
18      public static Test suite() {
# Line 32 | Line 32 | public class LinkedListTest extends JSR1
32          assertEquals(n, q.size());
33          return q;
34      }
35 <
35 >
36      /**
37       * new queue is empty
38       */
# Line 103 | Line 103 | public class LinkedListTest extends JSR1
103          try {
104              LinkedList q = new LinkedList();
105              q.offer(null);
106 <        } catch (NullPointerException ie) {
106 >        } catch (NullPointerException ie) {
107              unexpectedException();
108 <        }  
108 >        }
109      }
110  
111      /**
112 <     * Offer succeeds
112 >     * Offer succeeds
113       */
114      public void testOffer() {
115          LinkedList q = new LinkedList();
# Line 239 | Line 239 | public class LinkedListTest extends JSR1
239              q.remove();
240              shouldThrow();
241          } catch (NoSuchElementException success){
242 <        }  
242 >        }
243      }
244  
245      /**
# Line 256 | Line 256 | public class LinkedListTest extends JSR1
256          }
257          assertTrue(q.isEmpty());
258      }
259 <        
259 >
260      /**
261       * contains(x) reports true when elements added but not yet removed
262       */
# Line 378 | Line 378 | public class LinkedListTest extends JSR1
378              shouldThrow();
379          } catch(ArrayStoreException  success){}
380      }
381 <    
381 >
382      /**
383       *  iterator iterates through all elements
384       */
# Line 427 | Line 427 | public class LinkedListTest extends JSR1
427          assertFalse(it.hasNext());
428      }
429  
430 +    /**
431 +     *  Descending iterator iterates through all elements
432 +     */
433 +    public void testDescendingIterator() {
434 +        LinkedList q = populatedQueue(SIZE);
435 +        int i = 0;
436 +        Iterator it = q.descendingIterator();
437 +        while(it.hasNext()) {
438 +            assertTrue(q.contains(it.next()));
439 +            ++i;
440 +        }
441 +        assertEquals(i, SIZE);
442 +        assertFalse(it.hasNext());
443 +        try {
444 +            it.next();
445 +        } catch(NoSuchElementException success) {
446 +        }
447 +    }
448 +
449 +    /**
450 +     *  Descending iterator ordering is reverse FIFO
451 +     */
452 +    public void testDescendingIteratorOrdering() {
453 +        final LinkedList q = new LinkedList();
454 +        q.add(new Integer(3));
455 +        q.add(new Integer(2));
456 +        q.add(new Integer(1));
457 +        int k = 0;
458 +        for (Iterator it = q.descendingIterator(); it.hasNext();) {
459 +            int i = ((Integer)(it.next())).intValue();
460 +            assertEquals(++k, i);
461 +        }
462 +
463 +        assertEquals(3, k);
464 +    }
465 +
466 +    /**
467 +     * descendingIterator.remove removes current element
468 +     */
469 +    public void testDescendingIteratorRemove () {
470 +        final LinkedList q = new LinkedList();
471 +        q.add(new Integer(3));
472 +        q.add(new Integer(2));
473 +        q.add(new Integer(1));
474 +        Iterator it = q.descendingIterator();
475 +        it.next();
476 +        it.remove();
477 +        it = q.descendingIterator();
478 +        assertEquals(it.next(), new Integer(2));
479 +        assertEquals(it.next(), new Integer(3));
480 +        assertFalse(it.hasNext());
481 +    }
482 +
483  
484      /**
485       * toString contains toStrings of elements
# Line 437 | Line 490 | public class LinkedListTest extends JSR1
490          for (int i = 0; i < SIZE; ++i) {
491              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
492          }
493 <    }        
493 >    }
494  
495      /**
496       * peek returns element inserted with addFirst
# Line 446 | Line 499 | public class LinkedListTest extends JSR1
499          LinkedList q = populatedQueue(3);
500          q.addFirst(four);
501          assertEquals(four,q.peek());
502 <    }  
502 >    }
503 >
504 >    /**
505 >     * peekFirst returns element inserted with push
506 >     */
507 >    public void testPush() {
508 >        LinkedList q = populatedQueue(3);
509 >        q.pollLast();
510 >        q.push(four);
511 >        assertEquals(four,q.peekFirst());
512 >    }
513 >
514 >    /**
515 >     *  pop removes next element, or throws NSEE if empty
516 >     */
517 >    public void testPop() {
518 >        LinkedList q = populatedQueue(SIZE);
519 >        for (int i = 0; i < SIZE; ++i) {
520 >            assertEquals(i, ((Integer)q.pop()).intValue());
521 >        }
522 >        try {
523 >            q.pop();
524 >            shouldThrow();
525 >        } catch (NoSuchElementException success){
526 >        }
527 >    }
528 >
529 >    /**
530 >     * OfferFirst succeeds
531 >     */
532 >    public void testOfferFirst() {
533 >        LinkedList q = new LinkedList();
534 >        assertTrue(q.offerFirst(new Integer(0)));
535 >        assertTrue(q.offerFirst(new Integer(1)));
536 >    }
537 >
538 >    /**
539 >     * OfferLast succeeds
540 >     */
541 >    public void testOfferLast() {
542 >        LinkedList q = new LinkedList();
543 >        assertTrue(q.offerLast(new Integer(0)));
544 >        assertTrue(q.offerLast(new Integer(1)));
545 >    }
546 >
547 >    /**
548 >     *  pollLast succeeds unless empty
549 >     */
550 >    public void testPollLast() {
551 >        LinkedList q = populatedQueue(SIZE);
552 >        for (int i = SIZE-1; i >= 0; --i) {
553 >            assertEquals(i, ((Integer)q.pollLast()).intValue());
554 >        }
555 >        assertNull(q.pollLast());
556 >    }
557 >
558 >    /**
559 >     *  peekFirst returns next element, or null if empty
560 >     */
561 >    public void testPeekFirst() {
562 >        LinkedList q = populatedQueue(SIZE);
563 >        for (int i = 0; i < SIZE; ++i) {
564 >            assertEquals(i, ((Integer)q.peekFirst()).intValue());
565 >            q.pollFirst();
566 >            assertTrue(q.peekFirst() == null ||
567 >                       i != ((Integer)q.peekFirst()).intValue());
568 >        }
569 >        assertNull(q.peekFirst());
570 >    }
571 >
572 >
573 >    /**
574 >     *  peekLast returns next element, or null if empty
575 >     */
576 >    public void testPeekLast() {
577 >        LinkedList q = populatedQueue(SIZE);
578 >        for (int i = SIZE-1; i >= 0; --i) {
579 >            assertEquals(i, ((Integer)q.peekLast()).intValue());
580 >            q.pollLast();
581 >            assertTrue(q.peekLast() == null ||
582 >                       i != ((Integer)q.peekLast()).intValue());
583 >        }
584 >        assertNull(q.peekLast());
585 >    }
586 >
587 >    public void testFirstElement() {
588 >        LinkedList q = populatedQueue(SIZE);
589 >        for (int i = 0; i < SIZE; ++i) {
590 >            assertEquals(i, ((Integer)q.getFirst()).intValue());
591 >            q.pollFirst();
592 >        }
593 >        try {
594 >            q.getFirst();
595 >            shouldThrow();
596 >        }
597 >        catch (NoSuchElementException success) {}
598 >    }
599 >
600 >    /**
601 >     *  getLast returns next element, or throws NSEE if empty
602 >     */
603 >    public void testLastElement() {
604 >        LinkedList q = populatedQueue(SIZE);
605 >        for (int i = SIZE-1; i >= 0; --i) {
606 >            assertEquals(i, ((Integer)q.getLast()).intValue());
607 >            q.pollLast();
608 >        }
609 >        try {
610 >            q.getLast();
611 >            shouldThrow();
612 >        }
613 >        catch (NoSuchElementException success) {}
614 >        assertNull(q.peekLast());
615 >    }
616 >
617 >    /**
618 >     * removeFirstOccurrence(x) removes x and returns true if present
619 >     */
620 >    public void testRemoveFirstOccurrence() {
621 >        LinkedList q = populatedQueue(SIZE);
622 >        for (int i = 1; i < SIZE; i+=2) {
623 >            assertTrue(q.removeFirstOccurrence(new Integer(i)));
624 >        }
625 >        for (int i = 0; i < SIZE; i+=2) {
626 >            assertTrue(q.removeFirstOccurrence(new Integer(i)));
627 >            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
628 >        }
629 >        assertTrue(q.isEmpty());
630 >    }
631 >
632 >    /**
633 >     * removeLastOccurrence(x) removes x and returns true if present
634 >     */
635 >    public void testRemoveLastOccurrence() {
636 >        LinkedList q = populatedQueue(SIZE);
637 >        for (int i = 1; i < SIZE; i+=2) {
638 >            assertTrue(q.removeLastOccurrence(new Integer(i)));
639 >        }
640 >        for (int i = 0; i < SIZE; i+=2) {
641 >            assertTrue(q.removeLastOccurrence(new Integer(i)));
642 >            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
643 >        }
644 >        assertTrue(q.isEmpty());
645 >    }
646  
647   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines