`

用JUNIT测试堆栈的例子

阅读更多

package com.test.junit;

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 com.test.junit;

import com.test.junit.MyStack;

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

public class MyStackTest extends TestCase
{
	private MyStack myStack;

	public void setUp()
	{
		myStack = new MyStack();
	}

	public void testPush()
	{
		try
		{
			myStack.push("hello world");
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String result = null;
		try
		{
			result = myStack.pop();
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

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

	public void testPush2()
	{
		for (int i = 0; i < 100; ++i)
		{
			try
			{
				myStack.push(i + "");
			}
			catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		for (int i = 0; i < 100; ++i)
		{
			String result = null;
			try
			{
				result = myStack.pop();
			}
			catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

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

	public void testPush3()
	{
		Throwable tx = null;

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

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

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

	public void testPop()
	{
		Throwable tx = null;

		try
		{
			myStack.pop();
			Assert.fail();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			tx = e;
		}
		Assert.assertNotNull(tx);
		Assert.assertEquals(Exception.class, tx.getClass());
		Assert.assertEquals("数组越界异常!", tx.getMessage());
	}

	public void testTop()
	{
		try
		{
			myStack.push("hello world");

			String result = myStack.top();

			Assert.assertEquals("hello world", result);
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}

	public void testTop2()
	{
		Throwable tx = null;

		try
		{
			myStack.top();

			Assert.fail();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			tx = e;
		}
		Assert.assertNotNull(tx);
		Assert.assertEquals(Exception.class, tx.getClass());
		Assert.assertEquals("数组越界异常!", tx.getMessage());
	}

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

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

	public void testDelete2()
	{
		Throwable tx = null;

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

			myStack.delete(11);

			myStack.push("hello world");
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			tx = ex;
		}

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

}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics