Test the RPN Calculator
Hard About 30 min Free, no account needed
You're given a correct reverse-Polish evaluator. Write the tests that pin operand order and the error cases.
The brief
This evaluator reads a reverse-Polish expression like 3 4 + and returns 7. It is already written
and correct. Your job is to write the tests that prove it, including the half of the behavior
that is error handling.
Here is the code under test:
public class RpnCalculator
{
// Integers and + - * /, space separated. Integer division.
// Throws InvalidOperationException on a malformed expression,
// DivideByZeroException on division by zero.
public int Evaluate(string expression)
{
var stack = new Stack<int>();
foreach (var token in expression.Split(' ', StringSplitOptions.RemoveEmptyEntries))
{
if (int.TryParse(token, out int number)) { stack.Push(number); continue; }
if (stack.Count < 2) throw new InvalidOperationException("Operator needs two operands.");
int b = stack.Pop();
int a = stack.Pop();
stack.Push(token switch
{
"+" => a + b, "-" => a - b, "*" => a * b,
"/" => b == 0 ? throw new DivideByZeroException() : a / b,
_ => throw new InvalidOperationException($"Unknown token '{token}'."),
});
}
if (stack.Count != 1) throw new InvalidOperationException("The expression did not reduce to a single value.");
return stack.Pop();
}
}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>(() => ...);Reference exceptions by their full names, for example Assert.Throws<System.DivideByZeroException>(...).
Your suite is run twice over:
- Against the correct
RpnCalculatorabove, every one of your tests must pass. - Against a set of planted-bug "mutants", broken evaluators 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 reverse the operands of a non-commutative operator, swap one operator for another, or
quietly skip an error case. Outputs alone cannot catch the error-path bugs, so reach for
Assert.Throws. Stuck? Reveal the model suite to see what thorough looks like.
Starter code
// Write tests for the `RpnCalculator` 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, order matters for - and /, and the error cases (divide by zero, malformed input) count.
public class Tests
{
public void Adds_two_numbers()
{
Assert.Equal(7, new RpnCalculator().Evaluate("3 4 +"));
}
// TODO: cover subtraction and division order, multiplication, and the two error cases.
}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
- Error path testing
- Prove exceptions, retries and rejected inputs behave as specified.
- Mutation testing
- Write tests strong enough to catch planted bugs, not just to pass.
Related katas
- Test the CSV Record Parser Hard Free You're given a correct CSV record parser. Write tests that pin quoting, empty fields, whitespace, and malformed input.
- Test the Retry Executor Hard Free You're given a correct retry executor. Write tests that prove its attempt budget, exception policy, and call sequence.
- Test the Bowling Scorer Hard Free You're given a correct ten-pin bowling scorer. Write the tests that exercise spares, strikes, and the bonus rolls.
- 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.