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:
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):
Assert.True(condition); Assert.False(condition);
Assert.Equal(expected, actual); Assert.NotEqual(a, b);
Assert.Null(x); Assert.NotNull(x);
Assert.Throws<SomeException>(() => ...);Your suite is run twice over:
- Against the correct
RomanNumeralsabove, every one of your tests must pass. - 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
// 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.
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
- Test the Bracket Matcher Medium Free You're given a correct bracket matcher for (), [], and {}. Write the tests that pin type, order, and the unbalanced cases.
- 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.
- Test the Leap-Year Rule Medium Free You're given a correct leap-year check. Write the tests that prove it, and that would catch a buggy one.
- Test the Password Policy Medium Free You're given a correct password validator with four rules. Write tests that isolate each rule so a dropped one is caught.