Skip to content
Katabench
Try free

Test the Progressive Tax

Hard About 30 min Free, no account needed

You're given a correct marginal income-tax calculator. Write the tests that prove each band is taxed marginally, not flatly.

The brief

Progressive tax is the textbook example people get wrong: only the income within each band is taxed at that band's rate, never the whole amount. The calculator here is already written and correct. Your job is to write the tests that prove it.

Here is the code under test:

C#
public class IncomeTax
{
    // Marginal tax: first 10,000 tax-free; 10,000 to 40,000 at 10%; above 40,000 at 20%.
    public int Compute(int income)
    {
        int tax = 0;
        int inSecondBand = Math.Min(income, 40000) - 10000;
        if (inSecondBand > 0) tax += inSecondBand / 10;   // 10%
        int inThirdBand = income - 40000;
        if (inThirdBand > 0) tax += inThirdBand / 5;       // 20%
        return tax;
    }
}

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

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 IncomeTax above, every one of your tests must pass.
  2. Against a set of planted-bug "mutants", broken calculators 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 tax the whole income flatly, drop the top band, double-count the overlap, swap the rates, or move the allowance. A single low-income test catches almost none of them. Test each band on its own, the band boundaries, and a high income that stacks both. Stuck? Reveal the model suite to see what thorough looks like.

Starter code

C#
// Write tests for the `IncomeTax` 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 trap is "marginal": only the slice inside each band is taxed at that band's rate.
public class Tests
{
    public void Low_income_owes_no_tax()
    {
        Assert.Equal(0, new IncomeTax().Compute(5000));
    }

    // TODO: test income inside the 10% band, income in the 20% band, and the band boundaries.
}

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 →

What it practices

Boundary value testing
Pin behavior exactly at, just below and just above every boundary.
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