Skip to content
Katabench
Try free

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:

C#
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):

C#
Assert.True(condition);          Assert.False(condition);
Assert.Equal(expected, actual);  Assert.NotEqual(a, b);
Assert.Null(x);                  Assert.NotNull(x);
Assert.Throws<SomeException>(() => ...);

How you're graded

Your suite is run twice over:

  1. Against the correct BillSplitter above, every one of your tests must pass.
  2. 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

C#
// 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.

C# unit testing exercises →

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