Skip to content
Katabench
Try free

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:

C#
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):

C#
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".

How you're graded

Your suite is run twice over:

  1. Against the correct DateMath above, every one of your tests must pass.
  2. 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

C#
// 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.

C# unit testing exercises →

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