@Test
  public void testPeek() {
    MyStack<Integer> testStack = new MyStack<Integer>();

    testStack.push(23);
    assertTrue(testStack.peek() == 23);
    // Ensure the next pushed item is on top
    testStack.push(2);
    assertTrue(testStack.peek() == 2);
  }
Пример #2
0
  @Test
  public void canPushAndPeekTag() {
    MyStack testStack = new MyStack();
    HtmlTag testTag = new HtmlTag("b");

    testStack.push(testTag);
    assertEquals(testStack.peek().getElement(), testTag.getElement());
  }
 // Testing Stack
 @Test
 public void testStack() {
   MyStack<Integer> testStack = new MyStack<Integer>();
   testStack.push(5);
   testStack.push(2);
   testStack.push(3);
   assertEquals(3, testStack.size());
   assertEquals(3, testStack.peek(), 0);
   testStack.pop();
   assertEquals(2, testStack.size());
   assertEquals(2, testStack.peek(), 0);
   testStack.pop();
   assertEquals(1, testStack.size());
   assertEquals(5, testStack.peek(), 0);
   testStack.pop();
   assertEquals(0, testStack.size());
 }
  @Test
  public void testPop() {
    MyStack<String> testStack = new MyStack<String>();
    testStack.push("hello");
    testStack.push("dog");
    testStack.push("cat");

    assertEquals("cat", testStack.pop());
    assertTrue(testStack.size() == 2);
    // Ensure the popped object is gone
    assertEquals("dog", testStack.peek());
  }
 @Test(expected = NoSuchElementException.class)
 public void testPeekFail() {
   // Ensure the thrown exception of peeking at an empty stack.
   MyStack<Integer> testStack = new MyStack<Integer>();
   testStack.peek();
 }