@Test public void testMyStack() { // Test expected results of creating a new MyStack object MyStack<Integer> testStack = new MyStack<Integer>(); assertTrue(testStack.size() == 0); assertTrue(testStack.isEmpty()); }
@Test public void canPushAndPeekTag() { MyStack testStack = new MyStack(); HtmlTag testTag = new HtmlTag("b"); testStack.push(testTag); assertEquals(testStack.peek().getElement(), testTag.getElement()); }
@Test public void canPushAndPopTag() { MyStack testStack = new MyStack(); HtmlTag testTag = new HtmlTag("html"); testStack.push(testTag); assertEquals(testStack.pop().getElement(), testTag.getElement()); assertTrue(testStack.isEmpty()); }
@Test public void pushShouldAddToEmptyStack() { MyStack testStack = new MyStack(); HtmlTag testTag = new HtmlTag("html"); assertTrue(testStack.isEmpty()); testStack.push(testTag); assertFalse(testStack.isEmpty()); }
@Test public void testIsEmpty() { MyStack<Integer> test = new MyStack<Integer>(); test.push(2); test.push(230); test.pop(); test.pop(); assertTrue(test.isEmpty()); }
@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); }
@Test public void testPush() { MyStack<Integer> testStack = new MyStack<Integer>(); testStack.push(23); testStack.push(43); testStack.push(21); // Ensure the correct size after doing a number of pushes assertFalse(testStack.isEmpty()); assertTrue(testStack.size() == 3); }
@Test public void testClear() { MyStack<Integer> testStack = new MyStack<Integer>(); testStack.push(23); testStack.push(2039); testStack.push(220); assertTrue(testStack.size() == 3); // Ensure quality of the clear method. testStack.clear(); assertTrue(testStack.size() == 0); assertTrue(testStack.isEmpty()); }
@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 public void canCreateEmptyMyStack() { MyStack testStack = new MyStack(); assertTrue(testStack.isEmpty()); }
// 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(expected = NoSuchElementException.class) public void testPopFail() { MyStack<Integer> testStack = new MyStack<Integer>(); testStack.pop(); }
@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(); }