/** Test method for popping negative index. */ @Test(expected = ArrayIndexOutOfBoundsException.class) public void testPopNegativeIndex() { pushElements(); my_stack.pop(); my_stack.pop(); my_stack.pop(); }
/** Test method for Push. */ @Test public void testPush() { pushElements(); // test size was incremented correctly after two pushes; size should be 2 assertTrue("push() failed to increment size", my_stack.size() == 2); // test top was incremented correctly after two pushes; top should be 1 assertTrue("push() failed to increment top", my_stack.size() - 1 == 1); final String return_value1 = my_stack.pop(); // test if top element is in correct position; element should be "World" assertTrue("push() failed to put element in correctly!", "World".equals(return_value1)); final String return_value2 = my_stack.pop(); // test if the last element is in correct position; element should be "Hello" assertTrue("push() failed to put element in correctly!", "Hello".equals(return_value2)); }
/** Test method for Pop. */ @Test public void testPop() { pushElements(); final String return_value = my_stack.pop(); // test if the right element is returned; popped element should be "World" assertTrue("pop() returned the wrong element!", "World".equals(return_value)); // test size was decremented; size should be 1 assertTrue("pop() didn't decrement size correctly!", my_stack.size() == 1); // test top was decremented; top should be 0 assertTrue("pop() didn't decrement top correctly!", my_stack.size() - 1 == 0); }
/** Test method for isEmpty. */ @Test public void testIsEmpty() { // test if the new stack is empty; boolean should be true assertTrue("isEmpty() failed to return true", my_stack.isEmpty()); pushElements(); // test if the stack is not empty; boolean should be false assertFalse("isEmpty() failed to return false", my_stack.isEmpty()); my_stack.pop(); my_stack.pop(); // test if elements were pushed and popped from stack // that the stack is empty; boolean should be true assertTrue("isEmpty() failed to return true", my_stack.isEmpty()); }