C#怎么对下面的代码进行单元测试
方法如下为测试的代码:
private void Sort(int[] list)
{
int temp;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 9 - i; j++)
{
if (list[j] > list[j + 1])
{
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
}
下面为实现:
private void Form1_Load(object sender, EventArgs e)
{
int[] s = new int[] { 2, 1, 3, 5, 4, 77, 7, 8, 69, 9 };
Sort(s);
for (int m = 0; m < 10; m++)
{
label1.Text = label1.Text + " " + s[m].ToString();
}
}
------解决方案--------------------
你可以直接在Form1_Load中写断言,例如
C# code
private void Form1_Load(object sender, EventArgs e)
{
int[] s = new int[] { 2, 1, 3, 5, 4, 77, 7, 8, 69, 9 };
Sort(s);
Debug.Assert(s.Length==10);
Debug.Assert(s[2]==3);
Debug.Assert(s[9]==77);
for (int m = 0; m < 10; m++)
{
label1.Text = label1.Text + " " + s[m].ToString();
}
}