Test the Bill Splitter
Hard About 30 min Free, no account needed
You're given a correct bill splitter that spreads leftover cents fairly. Write the tests that prove every cent is accounted for.
The brief
Splitting a bill evenly is easy until the cents do not divide cleanly. This splitter is already written and correct: it hands each leftover cent to the first people in line. Your job is to write the tests that prove every cent is accounted for.
Here is the code under test:
public class BillSplitter
{
// Splits totalCents as evenly as possible among `people`, giving leftover cents one each to
// the first people. Returns the shares joined by commas, e.g. "334,333,333" for 1000 over 3.
public string Split(int totalCents, int people)
{
int baseShare = totalCents / people;
int remainder = totalCents % people;
var shares = new List<int>();
for (int i = 0; i < people; i++)
shares.Add(i < remainder ? baseShare + 1 : baseShare);
return string.Join(",", shares);
}
}Write a Tests class. Each public, parameterless method is one test: it passes if it runs
without throwing and fails when an assertion throws. The result is a comma-joined string, so
compare it directly. 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
BillSplitterabove, every one of your tests must pass. - Against a set of planted-bug "mutants", broken splitters 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 drop the leftover cents, hand them to the wrong people, over-round everyone, or return the wrong number of shares. An even split catches almost none of them. Use totals that do not divide evenly, and check both the amounts and who gets the extra. Stuck? Reveal the model suite to see what thorough looks like.
Starter code
// Write tests for the `BillSplitter` 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 hard part is the leftover cents: every one must be handed out, and to the right people.
public class Tests
{
public void An_even_split_is_equal()
{
Assert.Equal("300,300,300", new BillSplitter().Split(900, 3));
}
// TODO: add splits with a remainder, a single payer, and a total smaller than the group.
}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.
Related guides
What it practices
- Equivalence partitioning
- Cover each class of input and every branch with one deliberate case.
- Money and rounding
- Exact numeric arithmetic, rounding order and apportioning cents so totals reconcile.
- Mutation testing
- Write tests strong enough to catch planted bugs, not just to pass.
Related katas
- 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 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 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.
- Test the Interval Merger Hard Free You're given a correct interval merger. Write the tests that pin overlap, touching, containment, and sorting.