For this week's software testing course, it was explained that improper handling of illegal entries would cause unnecessary damage to the program, so a simple leap year detection program was adopted to process the illegal input of the leap year test.
First, demand analysis
The input box is required to enter the year, click the button, a prompt appears. and can handle any input effectively.
Second, test cases
| Content |
Take value |
Expected results |
| Divisible by 400. |
2000 |
is a leap year |
| Divisible by 100 but not divisible by 400. |
1900 |
Not a leap year |
| Divisible by 4 and not divisible by 100 |
1904 |
is a leap year |
| Not divisible by 4. |
1901 |
Not a leap year |
| Non-numeric characters |
Abc |
Illegal input |
| Negative |
-1234 |
Illegal input |
| Decimal |
2000.5 |
Illegal input |
| Empty string |
Null |
Illegal input |
Third, the code implementation
The following is a program that runs in Visual Studio with only the key button code posted.
Private voidButton1_Click (Objectsender, EventArgs e) {String Test=TB. Text; //TB. Text = a + "#"; stringRegexstring =@"^[0-9]\d*$"; if(Regex.IsMatch (test, regexstring)) {Label2. Text="OK"; intNian =int. Parse (test); if(Nian% -==0) Label2. Text= Test +"years is a leap year"; Else if(nian% -==0) Label2. Text= Test +"years is not a leap year"; Else if(nian%4==0) Label2. Text= Test +"years is a leap year"; ElseLabel2. Text= Test +"years is not a leap year"; } ElseLabel2. Text="Please enter a valid year"; }
Iv. Output Results
V. Problem-solving Solutions
I use regular expressions to avoid input errors caused by illegal input when converting a string into a number, and only a non-negative integer can enter the conversion step.
You are welcome to criticize and correct me.
About software Testing (4): Judgement of input for leap year and processing test of illegal input