namespace NunitTest
{
//模拟测试堆栈,堆栈是数据结构里面的一种类型,后进先出,与之相反的是队列,先进后出
public class MyStack
{
private int nextIndex; //放置的是下一个下标在数组中的元素
private string[] elements; //存放字符串数组
public MyStack() //构造函数
{
elements = new string[100]; //假设数组只能存放100个元素
nextIndex = 0; //下一个元素的索引
}
//定义堆栈的操作
public void push(string element)
{
if (100 == this.nextIndex)
{
throw new Exception("数组越界异常");
}
elements[nextIndex++] = element; //往堆栈里面压的下一个元素
}
public string Pop() //取出数组中的元素
{
if (0 == this.nextIndex)
{
throw new Exception("数组越界异常");
}
return elements[--nextIndex];
}
public void Delete(int n)
{
if (this.nextIndex - n < 0)
{
throw new Exception("数组越界异常");
}
nextIndex -= n; //
}
public string Top()
{
if (0 == this.nextIndex)
{
throw new Exception("数组越界异常");
}
return elements[nextIndex - 1];
}
public int Size()
{
return this.nextIndex;
}
}
}
测试上面的方法是否正确在MystackTest类中,代码如下所示:
namespace NunitTest
{
[TestFixture]
public class MyStackTest
{
private MyStack mystack;
[SetUp]
public void Init()
{
mystack = new MyStack();
}
[Test]
public void TestPush()
{
mystack.push("Hello World");
string result = mystack.Pop();
Assert.AreEqual("Hello World", result);
}
[Test]
public void TestPush2()
{
for (int i = 0; i < 100; ++i)
{
mystack.push(i + "");
}
for (int i = 0; i < 100; ++i)
{
string result = mystack.Pop();
Assert.AreEqual((99 - i) + "", result);
}
}
[Test,ExpectedException]
public void TestPush3()
{
for (int i = 0; i < 101; ++i)
{
mystack.push(i + "");
}
for (int i = 0; i < 100; ++i)
{
string result = mystack.Pop();
Assert.AreEqual((100 - i) + "", result);
}
}
[Test,ExpectedException]
public void TestPop()
{
mystack.Pop();
}
[Test]
public void TestPop2()
{
mystack.push("Hello");
mystack.push("World");
mystack.Pop();
string result = mystack.Pop();
Assert.AreEqual("Hello", result);
}
[Test]
public void TestTop()
{
mystack.push("Hello World");
string result = mystack.Top();
Assert.AreEqual("Hello World", result);
}
[Test,ExpectedException]
public void TestTop2()
{
mystack.Top();
}
[Test]
public void TestDelete()
{
for (int i = 0; i < 10; i++)
{
mystack.push(i + "");
}
mystack.Delete(10);
Assert.AreEqual(0, mystack.Size());
}
[Test,ExpectedException]
public void TestDelete2()
{
for (int i = 0; i < 10; i++)
{
mystack.push(i + "");
}
mystack.Delete(11);
}
}
}
对数据库增删改查的一些测试
(1).判断数据库的链接,新建connection类代码如下:
public class connection
{
public static SqlConnection GetConnection() //连接数据库函数
{
string connString = "server=HYL-PC;database=Test;uid=sa;pwd=saa";
SqlConnection connection = new SqlConnection(connString);