Skip to content
Katabench
Try free

Test the Transfer Ledger

Hard About 40 min Free, no account needed

You're given a correct transfer ledger. Write stateful tests that prove transfers are atomic and idempotent.

The brief

A transfer ledger has two account balances and remembers the transfer IDs it has already applied. The ledger here is already written and correct. Your job is to write the tests that prove its state transitions are atomic and idempotent.

Here is the public API:

C#
public enum TransferStatus
{
    Applied,
    Duplicate,
    Conflict,
    Rejected
}

public class TransferLedger
{
    public TransferLedger(int sourceBalance, int destinationBalance) { ... }

    public int SourceBalance { get; }
    public int DestinationBalance { get; }

    // Adds money to the source account.
    public void Deposit(int cents) { ... }

    // Attempts to move cents from source to destination.
    public TransferStatus Transfer(string transferId, int cents) { ... }
}

All balances and amounts in this puzzle are whole cents. Transfer IDs are non-empty, amounts are positive, and initial balances are non-negative.

A new transfer returns Applied when the source has enough money. It subtracts the amount from the source and adds exactly that amount to the destination. Spending the source's exact remaining balance is allowed.

Once a transfer has been applied, its ID is remembered:

  • Repeating the same ID and amount returns Duplicate and does not move money again.
  • Reusing the same ID with a different amount returns Conflict and changes neither balance.

An insufficient transfer returns Rejected. Rejection is atomic: neither balance changes, and the ID is not remembered. After a deposit, retrying that same ID and amount may therefore succeed.

Write a Tests class. Each public, parameterless void method is one test: it passes if it runs without throwing and fails when an assertion throws. A tiny assertion library is in scope, with 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 TransferLedger above, every one of your tests must pass.
  2. Against a set of planted-bug "mutants", broken ledgers 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 break one side of a successful transfer, mutate state during rejection, reserve a rejected ID, mishandle the exact-balance boundary, reapply a duplicate, or collapse a conflict into a duplicate. Snapshot both balances around multi-call sequences. Stuck? Reveal the model suite to see what thorough looks like.

Starter code

C#
// Write tests for TransferLedger. Each public, parameterless void method is one test. Make your
// suite pass on the correct ledger and catch the hidden buggy versions. A deposit check alone does
// not exercise transfer atomicity, balance boundaries, or the remembered transfer IDs.
public class Tests
{
    public void A_deposit_adds_to_the_source_balance()
    {
        var ledger = new TransferLedger(100, 20);
        ledger.Deposit(25);
        Assert.Equal(125, ledger.SourceBalance);
    }

    // TODO: test balance movement, rejection, retries, duplicates, and conflicting transfer IDs.
}

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

Stateful testing
Drive call sequences and state-machine transitions, not just single calls.
Idempotency and deduplication
Make retries and redeliveries safe with idempotency keys and canonical identity.
Mutation testing
Write tests strong enough to catch planted bugs, not just to pass.

Related katas