您的位置:首页 > 其它

Junit3.8 Stack测试

2015-07-30 10:14 267 查看
package test;

public class MyStack
{
private String[] elements;

private int nextIndex;

public MyStack()
{
elements = new String[100];
nextIndex = 0;
}

public void push(String element) throws Exception
{
if(100 == nextIndex)
{
throw new Exception("数组越界异常!");
}

elements[nextIndex++] = element;
}

public String pop() throws Exception
{
if(0 == nextIndex)
{
throw new Exception("数组越界异常!");
}

return elements[--nextIndex];
}

public void delete(int n) throws Exception
{
if(nextIndex - n < 0)
{
throw new Exception("数组越界异常!");
}

nextIndex -= n;
}

public String top() throws Exception
{
if(0 == nextIndex)
{
throw new Exception("数组越界异常!");
}

return elements[nextIndex - 1];
}

}

package junit;

import junit.framework.Assert;
import junit.framework.TestCase;

@SuppressWarnings("deprecation")
public class MyStackTest extends TestCase
{
private MyStack myStack;

@Override
public void setUp() throws Exception
{
myStack = new MyStack();
}

//测试正常情况的一般情况,向栈内放入一个元素,然后取出。
public void testPush()
{
try
{
myStack.push("hello world");
}
catch (Exception e)
{
Assert.fail();
}

String result = null;

try
{
result = myStack.pop();
}
catch (Exception ex)
{

}

Assert.assertEquals("hello world", result);
}

//测试正常情况的临界情况,即放入100个元素栈是否会出错
public void testPush2()
{
for (int i = 0; i < 100; i++)
{
try
{
myStack.push(i + "");
}
catch (Exception ex)
{
Assert.fail();
}
}

for (int i = 0; i < 100; i++)
{
String result = null;

try
{
result = myStack.pop();
}
catch (Exception ex)
{

}

Assert.assertEquals((99 - i) + "", result);
}
}

//测试异常情况,101个元素输入
public void testPush3()
{
Exception tx = null;

try
{
for (int i = 0; i <= 100; i++)
{
myStack.push(i + "");
}

Assert.fail();
}
catch (Exception ex)
{
tx = ex;
}

assertData(tx);
}

public void testPop()
{
try
{
myStack.push("hello world");
}
catch (Exception e)
{

}

String result = null;

try
{
result = myStack.pop();
}
catch (Exception ex)
{
Assert.fail();
}

Assert.assertEquals("hello world", result);
}

public void testPop2()
{
Throwable tx = null;

try
{
myStack.pop();

Assert.fail();
}
catch (Exception ex)
{
tx = ex;
}

assertData(tx);
}

public void testPop3()
{
Throwable tx = null;

try
{
myStack.push("hello");
}
catch (Exception ex)
{

}

try
{
myStack.pop();
myStack.pop();

Assert.fail();
}
catch (Exception ex)
{
tx = ex;
}

assertData(tx);
}

public void testTop()
{
try
{
myStack.push("hello");
}
catch (Exception ex)
{

}

String result = null;

try
{
result = myStack.top();
}
catch (Exception ex)
{
Assert.fail();
}

Assert.assertEquals("hello", result);
}

public void testTop2()
{
Throwable tx = null;

try
{
myStack.top();

Assert.fail();
}
catch (Exception ex)
{
tx = ex;
}

assertData(tx);
}

public void testDelete()
{
try
{
for (int i = 0; i < 10; i++)
{
myStack.push(i + "");
}

myStack.delete(10);
}
catch (Exception ex)
{
Assert.fail();
}
}

public void testDelete2()
{
Throwable tx = null;

try
{
for (int i = 0; i < 10; i++)
{
myStack.push(i + "");
}

myStack.delete(11);

Assert.fail();
}
catch (Exception ex)
{
tx = ex;
}

assertData(tx);
}

private void assertData(Throwable tx)
{
Assert.assertNotNull(tx);
Assert.assertEquals(Exception.class, tx.getClass());
Assert.assertEquals("数组越界异常!", tx.getMessage());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: