日期:2014-05-20 浏览次数:20875 次
import static org.junit.Assert.*;
import junit.framework.TestCase;
import org.junit.Test;
public class StackTest extends TestCase {
private SelfDefinedStack stack_test;
public void Test()
{
stack_test = new SelfDefinedStack();
testsize();
testclear();
testpush();
testtop();
testpop();
}
/**
* Test of size() method of class SelfDefinedStack
* This method will push an element into the stack and verify that the stack size will
* increase by 1
*/
public void testsize()
{
int count = 0;
count = stack_test.size();
System.out.println("size of the stack is "+count);
stack_test.push("test_stack1");
int newcount = stack_test.size();
System.out.println("size of the stack is "+newcount);
if(newcount != count+1)
{
System.out.println("sizetest failed");
}
else
{
System.out.println("sizetest passed");
}
}
/**
* Test of push() method of class SelfDefinedStack
* This method will push an element into the stack and verify that the stack size will
* increase by 1
*/
public void testpush()
{
stack_test.clear();
if(stack_test.size() != 0)
{
System.out.println("testpush() failed on size");
}
else
{
stack_test.push("test_stack2");
if(stack_test.size() != 1)
{
System.out.println("testpush() failed");
}
else
{
System.out.println("testpush() passed");
}
}
}
/**
* Test of pop() method of class SelfDefinedStack
* This method will pop out the element on the top of the stack and verify the returned item
*/
public void testpop()
{
stack_test.clear();
if(stack_test.size() != 0)
{
System.out.println("testpop() failed on size");
}
else
{
stack_test.push("test_stack3");
if(stack_test.size() != 1)
{
System.out.println("testpush() failed");
}
String popElem = stack_test.pop().toString();
if(popElem == null)
{
System.out.println("testtop() failed: return element is null");
}
else if (popElem != "test_stack3")
{
System.out.println("testpop() failed wrong element returned ");
}
else if(stack_test.size() != 0)
{
System.out.println("testpop() failed: size is not 0");
}
System.out.println("testpop() passed");
}
}
/**
* Test of top() method of class SelfDefinedStack
* This method will verify that the element on the top of the stack will be retrieved and the
* stack won't change
*/
public void testtop()
{
stack_test.clear();
if(stack_test.size() != 0)
{
System.out.println("testtop() failed on size");
}
else
{
stack_test.push("test_stack4");
stack_test.push("test_stack5");
String topElem = stack_test.top().toString();
if(stack_test.size() != 2)
{
System.out.println("testtop() failed on size");
}
else if(topElem == null)
{
System.out.println("testtop() failed: Element topped from stack is null");