Skip to content
Katabench
Try free

Test the Version Comparer

Hard About 30 min Free, no account needed

You're given a correct dotted-version comparer. Write the tests that prove it compares numerically, not lexically.

The brief

Comparing version strings looks trivial until "1.10.0" has to rank above "1.9.0". The comparer 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 VersionComparer
{
    // Compares two dot-separated numeric versions. Returns -1 (a < b), 1 (a > b), 0 (equal).
    // Missing trailing components count as 0, so "1.2" equals "1.2.0".
    public int Compare(string a, string b)
    {
        string[] pa = a.Split('.');
        string[] pb = b.Split('.');
        int n = Math.Max(pa.Length, pb.Length);
        for (int i = 0; i < n; i++)
        {
            int va = i < pa.Length ? int.Parse(pa[i]) : 0;
            int vb = i < pb.Length ? int.Parse(pb[i]) : 0;
            if (va < vb) return -1;
            if (va > vb) return 1;
        }
        return 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>(() => ...);

How you're graded

Your suite is run twice over:

  1. Against the correct VersionComparer above, every one of your tests must pass.
  2. Against a set of planted-bug "mutants", broken comparers 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 compare components as text, ignore missing components, stop after the major, invert the result, or never report "greater". Cover both directions, the numeric-not-lexical case, and padded versions. Stuck? Reveal the model suite to see what thorough looks like.

Starter code

C#
// Write tests for the `VersionComparer` 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 headline bug is comparing "10" and "9" as text, where "10" wrongly sorts first.
public class Tests
{
    public void Equal_versions_return_zero()
    {
        Assert.Equal(0, new VersionComparer().Compare("1.2.3", "1.2.3"));
    }

    // TODO: cover both directions of inequality, the numeric-not-lexical case, and padded components.
}

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

Equivalence partitioning
Cover each class of input and every branch with one deliberate case.
Mutation testing
Write tests strong enough to catch planted bugs, not just to pass.

Related katas