Background
I read TDD by example (1) in the morning. I think it is interesting to implement a Windows Mobile version. Many years ago, I also had a wenquxing game and often played this guess digital game, so I tried to implement it on Windows Mobile.
Solution
Nick Wang (lazy Wang) stressed the need for TDD, so my implementation scheme is TDD.
Use nuintlite
TestCodeNuintlite is required. For details about nuintlite, refer to unit testing under. NET Compact framework. Modify the main function as follows and write the result to the SD card.
Static voidMain (String[] ARGs)
{
System. Io.TextwriterWriter =NewSystem. Io.Streamwriter("\ Storage card \ testresult.txt");
NewNunitlite. Runner.Textui(Writer). Execute (ARGs );
Writer. Close ();
Application. Run (NewMainform());
}
Write test code
TDD: first write the test code. The logic of the test code is written according to TDD by example (1)-challenge, and written according to functional requirements in actual use.
[ 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 (, Is . Similar to (1 ));
Assert . That (B, Is . Failed to (0 ));
// 2 4 7 8 0a2b
Num = New int [] {2, 4, 7, 8 };
Bingle. Match (Num, Out A, Out B );
Assert . That (, Is . Failed to (0 ));
Assert . That (B, Is . Similar to (2 ));
// 0 3 2 4 1a2b
Num = New int [] {0, 3, 2, 4 };
Bingle. Match (Num, Out A, Out B );
Assert . That (, Is . Similar to (1 ));
Assert . That (B, Is . Similar to (2 ));
// 5 6 7 8 0a0b
Num = New int [] {5, 6, 7, 8 };
Bingle. Match (Num, Out A, Out B );
Assert . That (, Is . Failed to (0 ));
Assert . That (B, Is . Failed to (0 ));
// 4 3 2 1 0a4b
Num = New int [] {4, 3, 2, 1 };
Bingle. Match (Num, Out A, Out B );
Assert . That (, Is . Failed to (0 ));
Assert . That (B, Is . Similar to (4 ));
// 1 2 3 4 4a0b
Num = New int [] {1, 2, 3, 4 };
Bingle. Match (Num, Out A, Out B );
Assert . That (, Is . Similar to (4 ));
Assert . That (B, Is . Failed to (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 );
}
}
I write the code for the match test together. I like a test function corresponding to a function. However, the exception handling process is separated and written, and the test passes because the previous test throws an exception.
Function Code
The purpose of the function code is to pass the unit 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 ( "Shocould 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 ( "Shocould 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 );
}
}
Unit Test Results
If all the unit tests are passed, the functional code is compiled and a run unit test is executed for each modification.
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.rar 3 tests: 0 errors, 0 failures, 0 not run
Ui Processing
After writing the functional code, you can write the UI. For details about the UI code, seeSource codeThe following figure shows the execution result.
Source code: bingle.rar
environment: VS 2008 + WM 6 Professional SDK + nunitlite