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);