Skip to content
Katabench
Try free

Test the Roman Numeral Converter

Medium About 15 min Free, no account needed

You're given a correct integer-to-Roman converter. Write the tests that pin the subtractive cases and the symbols.

The brief

The converter is already written and correct. Your job is the skill an interview really probes: proving it is right by writing the tests, including the cases that look like exceptions.

Here is the code under test:

C#
public class RomanNumerals
{
    // Converts a positive integer (1 to 3999) to its Roman numeral string.
    // Subtractive forms: IV=4, IX=9, XL=40, XC=90, CD=400, CM=900.
    public string ToRoman(int number) { /* greedy table of value/symbol pairs */ }
}

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 RomanNumerals above, every one of your tests must pass.
  2. Against a set of planted-bug "mutants", broken converters 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 4 of the 5 mutants. The mutants drop a subtractive rule, swap a symbol, reverse a pair, or use the wrong comparison. A suite of 1, 2, 3 catches none of them. Aim at the subtractive forms and a mixed number where several rules combine. Stuck? Reveal the model suite to see what thorough looks like.

Starter code

C#
// Write tests for the `RomanNumerals` 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 cases are the subtractive ones (4, 9, 40, 90, 400, 900), not 'III'.
public class Tests
{
    public void Three_is_III()
    {
        Assert.Equal("III", new RomanNumerals().ToRoman(3));
    }

    // TODO: add tests for the subtractive forms (4 -> IV, 40 -> XL, 900 -> CM) and a mixed number.
}

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