Thinking of "Software testing" leap year judgment and input problem

Source: Internet
Author: User

Leap year is set up to compensate for the time difference between the number of years due to the human calendar and the actual Earth cycle. The year in which the time difference is offset is leap years. A leap year has a total of 366 days. of which February is 29 days, leap year, every four years, every 100 years is not a leap, every 400 years is a leap year.

Test, we will write the following logical structure of the leap Year test code:

1 Get year information

2 test whether the common multiple is 4 if not, it is not a leap year

3 If yes, continue to test whether it is 100 common multiple if it is not a leap year

4 If yes, continue to test whether the common multiple is 400, if it is a leap year, if not it is not a leap year

So, the simplest code implementation

1 public static bool Year (int. year) {2   return (year% 4 = = 0 && Year% 100! = 0) | | (Year% 400 = = 0)) 3}

But here it is difficult to handle errors (all in one line), so we use another way

1           Public Static BOOLIsleapyear (intYear )2          {3              if(Year% -==0)4                 return true;5              if(Year% -==0)6                  return false;7              if(Year%4==0)8              return true;9              return false;Ten}

Considering the input problem, we want to judge year, which is a number greater than 0, because year is int at the time of declaration, so the program determines its own non-int

1             Try{2              intYear =int. Parse (TextBox1.Text);3              if(Isleapyear (year))4Form2.label1.Text = year+"is a leap year"; 5              Else6Form2.label1.Text = year+"not a leap year";7}Catch(Exception ex)8             {9Form2.label1.Text ="Input Error";Ten}

Make a year judgment

1             Try{2             intYear =int. Parse (TextBox1.Text);3            4             if(Isleapyear (year) >=0)5             6}Catch(Exception ex)7             {8Form2.label1.Text ="Please enter a year greater than 0";9}

So we can get the right results.

It is worth mentioning that because the year is positive infinity, the basis of the design test case is the known leap years Judgment rules, the analysis of each possible situation input

As a result, the following may be the case

1 not 4 common multiple year (1931)

2 is 4 of the Common multiple year (2004)

3 100 Common multiple year (100 1900 2000)

4 400 of common multiple year (400 2400)

Perform a black box test.

Thinking of "Software testing" leap year judgment and input problem

Related Article

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.