Skip to content
Katabench
Try free

Test the Luhn Checksum

Hard About 30 min Free, no account needed

You're given a correct Luhn card-number validator. Write the tests that pin the doubling, the digit reduction, and the rejects.

The brief

The Luhn algorithm is what catches a mistyped credit-card number. The validator 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 LuhnCheck
{
    // From the rightmost digit, double every second digit (subtract 9 if over 9),
    // sum all digits, accept when the total is a multiple of 10.
    // Rejects an empty string or any non-digit character.
    public bool IsValid(string number)
    {
        if (string.IsNullOrEmpty(number)) return false;
        int sum = 0;
        bool doubleIt = false;
        for (int i = number.Length - 1; i >= 0; i--)
        {
            char c = number[i];
            if (c < '0' || c > '9') return false;
            int digit = c - '0';
            if (doubleIt) { digit *= 2; if (digit > 9) digit -= 9; }
            sum += digit;
            doubleIt = !doubleIt;
        }
        return sum % 10 == 0;
    }
}

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>(() => ...);

A handy known-valid number is 79927398713. Change any single digit and it should fail.

How you're graded

Your suite is run twice over:

  1. Against the correct LuhnCheck above, every one of your tests must pass.
  2. Against a set of planted-bug "mutants", broken validators 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 the subtract-9 reduction, double the wrong digits, skip the input rejects, or use the wrong modulus. A single happy-path number catches only some of them. Force the subtract-9 step, and assert what should be rejected. Stuck? Reveal the model suite to see what thorough looks like.

Starter code

C#
// Write tests for the `LuhnCheck` 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 subtle parts are which digits get doubled, the subtract-9 step, and rejecting bad input.
public class Tests
{
    public void A_short_valid_number_is_accepted()
    {
        Assert.True(new LuhnCheck().IsValid("18"));
    }

    // TODO: add a longer valid number, an invalid one, a digit that doubles past 9, empty, and a non-digit.
}

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

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