Test the Bracket Matcher
Medium About 20 min Free, no account needed
You're given a correct bracket matcher for (), [], and {}. Write the tests that pin type, order, and the unbalanced cases.
The brief
Checking that brackets are balanced is a classic stack problem, and it is easy to get almost right. The matcher here is already written and correct. Your job is to write the tests that prove it.
Here is the code under test:
public class Brackets
{
// True when every opening bracket has a matching close of the same type, correctly nested.
// Non-bracket characters are ignored. The empty string is balanced.
public bool IsBalanced(string s)
{
var stack = new Stack<char>();
foreach (char c in s)
{
if (c == '(' || c == '[' || c == '{') stack.Push(c);
else if (c == ')' || c == ']' || c == '}')
{
if (stack.Count == 0) return false;
char open = stack.Pop();
if (!Matches(open, c)) return false;
}
}
return stack.Count == 0;
}
// Matches pairs ( ), [ ], { } by type.
}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
Bracketsabove, every one of your tests must pass. - Against a set of planted-bug "mutants", broken matchers 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 ignore the bracket type, never check for a lone closing bracket, ignore leftover opens, forget to track opens, or drop a whole bracket type. A single matched pair catches almost none of them. Test type mismatches, unbalanced inputs, and all three bracket kinds. Stuck? Reveal the model suite to see what thorough looks like.
Starter code
// Write tests for the `Brackets` 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.
// A matched pair is the easy case. The bugs are in type, order, and the unbalanced inputs.
public class Tests
{
public void A_matched_pair_is_balanced()
{
Assert.True(new Brackets().IsBalanced("()"));
}
// TODO: add a mismatched type, an unclosed bracket, a lone closing bracket, and a curly mismatch.
}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 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.
- Test the Roman Numeral Converter Medium Free You're given a correct integer-to-Roman converter. Write the tests that pin the subtractive cases and the symbols.