Test the Caesar Cipher
Medium About 20 min Free, no account needed
You're given a correct Caesar cipher. Write the tests that pin wraparound, case, non-letters, and shift normalization.
The brief
A Caesar cipher shifts each letter along the alphabet. Simple, until you reach the end of the alphabet, a capital letter, a space, or a shift bigger than 26. The cipher here is already written and correct. Your job is to write the tests that prove it.
Here is the code under test:
public class Caesar
{
// Shifts each letter forward by `shift`, wrapping within its case, leaving non-letters alone.
// The shift is normalized, so 27 behaves like 1 and -1 like 25.
public string Encrypt(string text, int shift)
{
shift = ((shift % 26) + 26) % 26;
var sb = new StringBuilder(text.Length);
foreach (char c in text)
{
if (c >= 'a' && c <= 'z') sb.Append((char)('a' + (c - 'a' + shift) % 26));
else if (c >= 'A' && c <= 'Z') sb.Append((char)('A' + (c - 'A' + shift) % 26));
else sb.Append(c);
}
return sb.ToString();
}
}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
Caesarabove, every one of your tests must pass. - Against a set of planted-bug "mutants", broken ciphers 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 forget to wrap past z, skip uppercase, shift punctuation, drop the shift normalization, shift one too far, or shift backward. A single lowercase shift catches almost none of them. Test wraparound, case, non-letters, and odd shift values. Stuck? Reveal the model suite to see what thorough looks like.
Starter code
// Write tests for the `Caesar` 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 corners are wraparound past z, keeping case, leaving punctuation alone, and odd shift values.
public class Tests
{
public void Letters_shift_forward()
{
Assert.Equal("def", new Caesar().Encrypt("abc", 3));
}
// TODO: add wraparound, uppercase, non-letters, and negative/large shifts.
}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.
- Boundary value testing
- Pin behavior exactly at, just below and just above every boundary.
- Mutation testing
- Write tests strong enough to catch planted bugs, not just to pass.
Related katas
- 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 Triangle Classifier Medium Free You're given a correct triangle classifier. Write the tests that cover every shape and the invalid cases.
- 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 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.