Test the Palindrome Check
Easy About 12 min Free, no account needed
You're given a correct palindrome check that ignores case and punctuation. Write the tests that cover every input class.
The brief
The check is already written and correct. Your job is the harder half of the work, the half interviews and code reviews really probe: proving it handles every kind of input by writing the tests.
Here is the code under test:
public class Palindromes
{
// Same forwards and backwards, ignoring case and non-alphanumeric characters.
// Null and the empty string count as palindromes.
public bool IsPalindrome(string text)
{
if (text is null) return true;
int i = 0, j = text.Length - 1;
while (i < j)
{
if (!char.IsLetterOrDigit(text[i])) { i++; continue; }
if (!char.IsLetterOrDigit(text[j])) { j--; continue; }
if (char.ToLowerInvariant(text[i]) != char.ToLowerInvariant(text[j])) return false;
i++;
j--;
}
return true;
}
}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
Palindromesabove, every one of your tests must pass. - Against a set of planted-bug "mutants", broken versions 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. A suite that only checks 'racecar' passes step 1 and catches almost nothing, because 'racecar' has no uppercase letters, no punctuation, and no central mismatch to expose. Cover the input classes the normalization is meant to handle. Stuck? Reveal the model suite to see what thorough looks like.
Starter code
// Write tests for the `Palindromes` 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.
// Cover the input classes: mixed case, punctuation and spaces, non-palindromes, and the empty string.
public class Tests
{
public void A_simple_word_palindrome_is_true()
{
Assert.True(new Palindromes().IsPalindrome("racecar"));
}
// TODO: add tests for mixed case, punctuation, a non-palindrome, and the empty string.
}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 Clamp Function Easy Free You're given a correct clamp function. Write the tests that cover below, above, inside, and the boundaries.
- Test the Vowel Counter Easy Free You're given a correct vowel counter. Write the tests that cover case, every vowel, and the letters that are not vowels.
- Test the FizzBuzz Rule Easy Free You're given a correct FizzBuzz. Write the tests that prove every branch, and that would catch a broken one.
- Test the Max-of-Three Easy Free You're given a correct max-of-three function. Write the tests that prove it considers all three arguments.