About software Testing (4): Judgement of input for leap year and processing test of illegal input

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.