Testing for leap years

Source: Internet
Author: User

Many software implementations date this function, the calendar system is a human legacy systems. In the gradual evolution of this system, some problems are also derived, such as "Millennium bug" and "Leap Year Worm". Among them, leap year worm is people for some computers in the design without taking into account the leap-years factor, the February are specified of all vintages considered to have a 29-day or 28-day operation error of an image name, because in the English bug has bugs, defects and other meanings, so this flaw is called "Leap year Worm."

At the end of February 2012, the Guangzhou "Leap Year Worm" outbreak, thousands of taxis due to the problem of the meter, the outage:

Even the famous Microsoft will appear "Leap year Worm" error, Microsoft Windows Azure Cloud Platform several sub-regions affected by "leap year bug" caused many customers 12-24 hours to use the service. According to the Windows Azure Services Dashboard, a large number of sub-regional services and global services have been disrupted for more than 24 hours from the early morning of February 29, 2012 to March 1 in UTC.

So how to correctly judge a leap year is a very important issue, here is the code snippet for C #:

1  Public Static BOOLIsleapyear (intYear )2 {3System.Diagnostics.Debug.Assert (Year >=1900); 4     if(Year% -==0)5         return true;6     if(Year% -==0)7         return false;8     if(Year%4==0)9         return true;Ten     return false;  One}

If you were to write unit tests for this program, how many test cases would you list? A "stupid" method is to carry out the poor, but also face the following problems:

      1. The poor lift not finish
      2. Even though the poor cite many examples, they may not help to identify unique problems and are not representative

Therefore, we should introduce the concept of "equivalence class (equivalence)". The so-called equivalence class refers to a disjoint subset of the input fields, and the aggregate of all equivalence classes is the entire input domain, which is designed to test the non-redundancy of the case. A superficial approach is to:

If a function can return true | False, you have to have at least two classes of test sets, so that it returns true separately | False

If you know the workings of this function, or understand the real world The program is supposed to reflect, you can cite more detailed equivalence classes, such as test cases for isleapyear () as follows:

Year that is divisible by 400

Year that is divisible by 100 but not divisible by 400

A year that is divisible by 100 and divisible by 400.

Year that is divisible by 4 but not divisible by 100

A year that is divisible by 4 and divisible by 100.

An even number of years that are not divisible by 4

Odd years

Other illegally entered years

In addition, programmers know that programs often make mistakes near boundary conditions, and for isleapyear (), you can draw the following two test cases:

Minimum year allowed for design

Maximum year allowed for design

In summary, choosing the right and appropriate test case is a prerequisite for the test program to have bugs. In addition, the code to determine whether a leap year can also be simplified as follows:

1 BOOL int Year ) 2 {3     return  - 0 4 0  - 0 ); 4 }

Testing for leap years

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.