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()); } }
相关文章推荐
- 面试题36:数组中的逆序对
- 修改文件上传默认样式
- Nutz配置Jndi
- 文件编码格式转换
- javascript取当月最后一天
- debian软件源source.list文件格式说明
- 文件编码格式转换
- 用c# 将Access表修改复制到Sql server表
- plsql连接异常问题
- 求两个字符串去掉重复字母后所包含字符
- 安装好maven,在dos下输入mvn -version, 提示不是内部命令的问题
- 策略模式小练习
- datagrid 动态生成列
- Oracle_SQL_序列与groupby同时用
- Jeecms投票不准解决方案
- Jeecms投票不准解决方案
- 修改文件上传默认样式
- 求两个字符串去掉重复字母后所包含字符
- Jeecms之new标签实现
- itext生成只读pdf文档