Test the Next-Day Calculator
Hard About 30 min Free, no account needed
You're given a correct next-day date calculator. Write the tests that pin month, year, and leap-February rollover.
The brief
"What's the date tomorrow?" hides month rollover, year rollover, and the leap-year rules all at once. The calculator here is already written and correct. Your job is to write the tests that prove it.
Here is the code under test:
public class DateMath
{
private static readonly int[] DaysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// Returns the date after the given one, formatted "YYYY-MM-DD".
public string NextDay(int year, int month, int day)
{
int last = DaysInMonth[month - 1];
if (month == 2 && IsLeap(year)) last = 29;
day++;
if (day > last) { day = 1; month++; if (month > 12) { month = 1; year++; } }
return $"{year:D4}-{month:D2}-{day:D2}";
}
private static bool IsLeap(int year) =>
year % 400 == 0 || (year % 100 != 0 && year % 4 == 0);
}Write a Tests class. Each public, parameterless method is one test: it passes if it runs
without throwing and fails when an assertion throws. A tiny assertion library is in scope (no
using needed):
Assert.True(condition); Assert.False(condition);
Assert.Equal(expected, actual); Assert.NotEqual(a, b);
Assert.Null(x); Assert.NotNull(x);
Assert.Throws<SomeException>(() => ...);The result is a "YYYY-MM-DD" string, zero-padded, so NextDay(2021, 3, 14) returns "2021-03-15".
Your suite is run twice over:
- Against the correct
DateMathabove, every one of your tests must pass. - Against a set of planted-bug "mutants", broken calculators you never see. A mutant is caught when at least one of your tests fails against it.
You pass when your suite is green on the correct code and catches at least 5 of the 6 mutants. The mutants skip month or year rollover, ignore leap February, use the naive every-four-years rule, roll one day early, or drop the zero-padding. A single mid-month test catches almost none of them. Aim at the boundaries, and at both a leap and a non-leap February. Stuck? Reveal the model suite to see what thorough looks like.
Starter code
// Write tests for the `DateMath` class. Each public, parameterless method is one test; it passes
// if it doesn't throw. Make your suite pass on the correct code AND catch the hidden buggy versions.
// The interesting days are the last of a month, the last of a year, and the end of February.
public class Tests
{
public void A_mid_month_day_advances_by_one()
{
Assert.Equal("2021-03-15", new DateMath().NextDay(2021, 3, 14));
}
// TODO: add the end of a month, the end of the year, and leap vs non-leap February.
}How it is graded
You write the tests. A suite passes only when it stays green on correct code and turns enough faulty implementations red.
What it practices
- Boundary value testing
- Pin behavior exactly at, just below and just above every boundary.
- Dates, times and ranges
- Time zones, local day boundaries, month arithmetic and half-open range overlap.
- Mutation testing
- Write tests strong enough to catch planted bugs, not just to pass.
Do these first
Related katas
- Test the Progressive Tax Hard Free You're given a correct marginal income-tax calculator. Write the tests that prove each band is taxed marginally, not flatly.
- Test the Interval Merger Hard Free You're given a correct interval merger. Write the tests that pin overlap, touching, containment, and sorting.
- Test the Sliding-Window Rate Limiter Hard Free You're given a correct sliding-window rate limiter. Write the call sequences that prove the window slides and the limit holds.
- Test the Caesar Cipher Medium Free You're given a correct Caesar cipher. Write the tests that pin wraparound, case, non-letters, and shift normalization.