Skip to content
Katabench
Try free

Test the Cart Discount Rules

Hard About 45 min Free, no account needed

You're given a correct cart pricer that stacks a volume discount, a coupon, and free shipping. Build the carts where a wrong rule order or a wrong rounding step changes the total.

The brief

Every discount rule is simple on its own. Pricing bugs come from how the rules stack: a coupon taken from the wrong total, or a free-shipping line checked before a discount instead of after it. The pricer here is already written and correct. Your job is to write the tests that prove it, with carts built so that each rule, and the order of the rules, shows up in the total.

Here is the code under test:

C#
public sealed record CartLine(string Sku, int Quantity, long UnitPriceCents);

public class CartPricer
{
    public const int VolumeThreshold = 10;
    public const int VolumeDiscountPercent = 10;
    public const int MaxCouponPercent = 50;
    public const long ShippingCents = 599;
    public const long FreeShippingFromCents = 5000;

    public long TotalCents(IReadOnlyList<CartLine> lines, int couponPercent) { ... }
}

All money is in whole cents. TotalCents applies three rules, always in this order:

  1. Volume discount. When the cart holds 10 or more units of one SKU, that SKU's merchandise costs 10% less. Lines with the same SKU are added together first, so 6 pens on one line and 4 on another make 10. Other SKUs in the cart are not discounted.
  2. Coupon. couponPercent (0 to 50) comes off the merchandise total left after step 1.
  3. Shipping. 599 cents is added, unless the merchandise total after step 2 is 5000 cents or more, in which case shipping is free.

Each discount rounds down to a whole cent on its own: the volume discount for each SKU first, then the coupon on what is left. An empty cart costs 0 and pays no shipping. A couponPercent below 0 or above 50 throws ArgumentOutOfRangeException.

C#
var cart = new[] { new CartLine("MUG", 2, 1500) };
Assert.Equal(3599L, new CartPricer().TotalCents(cart, 0));   // 3000 + 599 shipping

Write a Tests class. Every public, parameterless method is a test; a test passes if it does not throw. Work each expected total out by hand; a comment with the arithmetic above an assertion makes a failure easy to read. Use Assert.Equal, True, False, and Throws<TException> as needed (no using needed for Assert). Your suite must pass on the correct CartPricer and catch every hidden mutant, a planted-bug version you never see.

Starter code

C#
public class Tests
{
    public void A_small_cart_pays_list_price_plus_shipping()
    {
        var pricer = new CartPricer();
        var cart = new[] { new CartLine("MUG", 2, 1500) };

        Assert.Equal(3599L, pricer.TotalCents(cart, 0));
    }
}

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.
Money and rounding
Exact numeric arithmetic, rounding order and apportioning cents so totals reconcile.

Related katas