背景
早上看了TDD by example (1) -- 挑戰,覺得有趣,實現一個Windows Mobile版本。很多年前我也有一台文曲星,也常常玩這個猜數字遊戲,所以嘗試在Windows Mobile下實現。
方案
Nick Wang (懶人王) 強調需要TDD,所以我的實現方案也是TDD。
實現使用NUintLite
測試代碼需要使用NUintLite,NUintLite具體可以參考 .NET Compact Framework下的單元測試。修改Main函數如下,把結果寫到SD卡上。
static void Main(string[] args)
{
System.IO.TextWriter writer = new System.IO.StreamWriter("\\Storage Card\\TestResult.txt");
new NUnitLite.Runner.TextUI(writer).Execute(args);
writer.Close();
Application.Run(new MainForm());
}
編寫測試代碼
TDD,先寫測試代碼。測試代碼的邏輯是按照TDD by example (1) -- 挑戰寫的,在實際使用中根據功能需求編寫。
[TestFixture]
class BingleTest
{
private Bingle bingle;
[SetUp]
public void SetUp()
{
bingle = new Bingle();
}
[TearDown]
public void TearDown()
{
}
[Test]
public void BuildAnswersTest()
{
bingle.BuildAnswers();
Assert.True(bingle.Answers[0] != bingle.Answers[1]
&& bingle.Answers[0] != bingle.Answers[2]
&& bingle.Answers[0] != bingle.Answers[3]
&& bingle.Answers[1] != bingle.Answers[2]
&& bingle.Answers[1] != bingle.Answers[3]
&& bingle.Answers[2] != bingle.Answers[3]);
}
[Test]
public void MatchTest()
{
bingle.Answers = new int[] { 1, 2, 3, 4 };
int a;
int b;
int[] num;
//1 5 6 7 1A0B
num = new int[] { 1, 5, 6, 7 };
bingle.Match(num, out a, out b);
Assert.That(a, Is.EqualTo(1));
Assert.That(b, Is.EqualTo(0));
//2 4 7 8 0A2B
num = new int[] { 2, 4, 7, 8 };
bingle.Match(num, out a, out b);
Assert.That(a, Is.EqualTo(0));
Assert.That(b, Is.EqualTo(2));
//0 3 2 4 1A2B
num = new int[] { 0, 3, 2, 4 };
bingle.Match(num, out a, out b);
Assert.That(a, Is.EqualTo(1));
Assert.That(b, Is.EqualTo(2));
//5 6 7 8 0A0B
num = new int[] { 5, 6, 7, 8 };
bingle.Match(num, out a, out b);
Assert.That(a, Is.EqualTo(0));
Assert.That(b, Is.EqualTo(0));
//4 3 2 1 0A4B
num = new int[] { 4, 3, 2, 1 };
bingle.Match(num, out a, out b);
Assert.That(a, Is.EqualTo(0));
Assert.That(b, Is.EqualTo(4));
//1 2 3 4 4A0B
num = new int[] { 1, 2, 3, 4 };
bingle.Match(num, out a, out b);
Assert.That(a, Is.EqualTo(4));
Assert.That(b, Is.EqualTo(0));
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void MatchTest2()
{
int a;
int b;
int[] num;
//1 1 2 3
num = new int[] { 1, 1, 2, 3 };
bingle.Match(num, out a, out b);
//1 2
num = new int[] { 1, 2 };
bingle.Match(num, out a, out b);
}
}
我把對Match測試的代碼寫在一起,我喜歡一個Test函數對應一個功能函數。而把異常處理分開出來寫了,怕前面的測試拋出異常,導致測試通過了。
功能代碼
功能代碼的目的就是通過所以單元測試。
public class Bingle
{
public int[] Answers { set; get; }
private int attemptTimes;
public int AttemptTimes
{
get
{
return attemptTimes;
}
}
public Bingle()
{
Answers = new int[4];
}
public void BuildAnswers()
{
Random r = new Random();
while(true)
{
int i = r.Next(9999);
Answers[0] = i / 1000;
Answers[1] = i % 1000 / 100;
Answers[2] = i % 100 / 10;
Answers[3] = i % 10;
if (Answers[0] != Answers[1]
&& Answers[0] != Answers[2]
&& Answers[0] != Answers[3]
&& Answers[1] != Answers[2]
&& Answers[1] != Answers[3]
&& Answers[2] != Answers[3])
{
return;
}
}
}
public bool Match(int[] attemptNum, out int bingle, out int match)
{
bingle = 0;
match = 0;
if (attemptNum.Length != 4)
{
throw new ArgumentException("Should be 4 digits.");
}
if(!(attemptNum[0] != attemptNum[1]
&& attemptNum[0] != attemptNum[2]
&& attemptNum[0] != attemptNum[3]
&& attemptNum[1] != attemptNum[2]
&& attemptNum[1] != attemptNum[3]
&& attemptNum[2] != attemptNum[3]))
{
throw new ArgumentException("Should be non repeating 4 digits.");
}
++attemptTimes;
for(int i=0; i<4; ++i)
{
if (attemptNum[i] == Answers[i])
{
++bingle;
}
else
{
for (int j = 0; j < 4; ++j)
{
if (attemptNum[i] == Answers[j])
{
++match;
}
}
}
}
return (bingle == 4);
}
}
單元測試結果
如果通過所有單元測試,說明功能代碼編寫完畢,每次修改都有run單元測試。
NUnitLite version 0.2.0
Copyright 2007, Charlie Poole
Runtime Environment -
OS Version: Microsoft Windows CE 5.2.21234
.NET Version: 2.0.7045.0
/Files/procoder/Bingle.rar3 Tests : 0 Errors, 0 Failures, 0 Not Run
UI 處理
功能代碼寫完以後,可以寫UI了,具體UI代碼見原始碼,下面是執行效果。
原始碼:Bingle.rar
環境: VS 2008 + WM 6 professional SDK + NUnitLite