448 |
|
assertEquals(four,q.peek()); |
449 |
|
} |
450 |
|
|
451 |
+ |
/** |
452 |
+ |
* peekFirst returns element inserted with push |
453 |
+ |
*/ |
454 |
+ |
public void testPush() { |
455 |
+ |
LinkedList q = populatedQueue(3); |
456 |
+ |
q.pollLast(); |
457 |
+ |
q.push(four); |
458 |
+ |
assertEquals(four,q.peekFirst()); |
459 |
+ |
} |
460 |
+ |
|
461 |
+ |
/** |
462 |
+ |
* pop removes next element, or throws NSEE if empty |
463 |
+ |
*/ |
464 |
+ |
public void testPop() { |
465 |
+ |
LinkedList q = populatedQueue(SIZE); |
466 |
+ |
for (int i = 0; i < SIZE; ++i) { |
467 |
+ |
assertEquals(i, ((Integer)q.pop()).intValue()); |
468 |
+ |
} |
469 |
+ |
try { |
470 |
+ |
q.pop(); |
471 |
+ |
shouldThrow(); |
472 |
+ |
} catch (NoSuchElementException success){ |
473 |
+ |
} |
474 |
+ |
} |
475 |
+ |
|
476 |
+ |
/** |
477 |
+ |
* OfferFirst succeeds |
478 |
+ |
*/ |
479 |
+ |
public void testOfferFirst() { |
480 |
+ |
LinkedList q = new LinkedList(); |
481 |
+ |
assertTrue(q.offerFirst(new Integer(0))); |
482 |
+ |
assertTrue(q.offerFirst(new Integer(1))); |
483 |
+ |
} |
484 |
+ |
|
485 |
+ |
/** |
486 |
+ |
* OfferLast succeeds |
487 |
+ |
*/ |
488 |
+ |
public void testOfferLast() { |
489 |
+ |
LinkedList q = new LinkedList(); |
490 |
+ |
assertTrue(q.offerLast(new Integer(0))); |
491 |
+ |
assertTrue(q.offerLast(new Integer(1))); |
492 |
+ |
} |
493 |
+ |
|
494 |
+ |
/** |
495 |
+ |
* pollLast succeeds unless empty |
496 |
+ |
*/ |
497 |
+ |
public void testPollLast() { |
498 |
+ |
LinkedList q = populatedQueue(SIZE); |
499 |
+ |
for (int i = SIZE-1; i >= 0; --i) { |
500 |
+ |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
501 |
+ |
} |
502 |
+ |
assertNull(q.pollLast()); |
503 |
+ |
} |
504 |
+ |
|
505 |
+ |
/** |
506 |
+ |
* peekFirst returns next element, or null if empty |
507 |
+ |
*/ |
508 |
+ |
public void testPeekFirst() { |
509 |
+ |
LinkedList q = populatedQueue(SIZE); |
510 |
+ |
for (int i = 0; i < SIZE; ++i) { |
511 |
+ |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
512 |
+ |
q.pollFirst(); |
513 |
+ |
assertTrue(q.peekFirst() == null || |
514 |
+ |
i != ((Integer)q.peekFirst()).intValue()); |
515 |
+ |
} |
516 |
+ |
assertNull(q.peekFirst()); |
517 |
+ |
} |
518 |
+ |
|
519 |
+ |
|
520 |
+ |
/** |
521 |
+ |
* peekLast returns next element, or null if empty |
522 |
+ |
*/ |
523 |
+ |
public void testPeekLast() { |
524 |
+ |
LinkedList q = populatedQueue(SIZE); |
525 |
+ |
for (int i = SIZE-1; i >= 0; --i) { |
526 |
+ |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
527 |
+ |
q.pollLast(); |
528 |
+ |
assertTrue(q.peekLast() == null || |
529 |
+ |
i != ((Integer)q.peekLast()).intValue()); |
530 |
+ |
} |
531 |
+ |
assertNull(q.peekLast()); |
532 |
+ |
} |
533 |
+ |
|
534 |
+ |
public void testFirstElement() { |
535 |
+ |
LinkedList q = populatedQueue(SIZE); |
536 |
+ |
for (int i = 0; i < SIZE; ++i) { |
537 |
+ |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
538 |
+ |
q.pollFirst(); |
539 |
+ |
} |
540 |
+ |
try { |
541 |
+ |
q.getFirst(); |
542 |
+ |
shouldThrow(); |
543 |
+ |
} |
544 |
+ |
catch (NoSuchElementException success) {} |
545 |
+ |
} |
546 |
+ |
|
547 |
+ |
/** |
548 |
+ |
* getLast returns next element, or throws NSEE if empty |
549 |
+ |
*/ |
550 |
+ |
public void testLastElement() { |
551 |
+ |
LinkedList q = populatedQueue(SIZE); |
552 |
+ |
for (int i = SIZE-1; i >= 0; --i) { |
553 |
+ |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
554 |
+ |
q.pollLast(); |
555 |
+ |
} |
556 |
+ |
try { |
557 |
+ |
q.getLast(); |
558 |
+ |
shouldThrow(); |
559 |
+ |
} |
560 |
+ |
catch (NoSuchElementException success) {} |
561 |
+ |
assertNull(q.peekLast()); |
562 |
+ |
} |
563 |
+ |
|
564 |
+ |
/** |
565 |
+ |
* removeFirstOccurrence(x) removes x and returns true if present |
566 |
+ |
*/ |
567 |
+ |
public void testRemoveFirstOccurrence() { |
568 |
+ |
LinkedList q = populatedQueue(SIZE); |
569 |
+ |
for (int i = 1; i < SIZE; i+=2) { |
570 |
+ |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
571 |
+ |
} |
572 |
+ |
for (int i = 0; i < SIZE; i+=2) { |
573 |
+ |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
574 |
+ |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
575 |
+ |
} |
576 |
+ |
assertTrue(q.isEmpty()); |
577 |
+ |
} |
578 |
+ |
|
579 |
+ |
/** |
580 |
+ |
* removeLastOccurrence(x) removes x and returns true if present |
581 |
+ |
*/ |
582 |
+ |
public void testRemoveLastOccurrence() { |
583 |
+ |
LinkedList q = populatedQueue(SIZE); |
584 |
+ |
for (int i = 1; i < SIZE; i+=2) { |
585 |
+ |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
586 |
+ |
} |
587 |
+ |
for (int i = 0; i < SIZE; i+=2) { |
588 |
+ |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
589 |
+ |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
590 |
+ |
} |
591 |
+ |
assertTrue(q.isEmpty()); |
592 |
+ |
} |
593 |
+ |
|
594 |
|
} |