Compute invoice discounts in integer cents
C++ · C++ · intermediate · modification
The order discount went through `double` (`std::round(subtotal * discount_bp / 10000.0)`), which can be off by a cent or more on large orders, once the values involved no longer fit in a double's 53-bit mantissa, and the line totals were summed with plain `*` and `+`, so a large enough order was signed overflow. This PR keeps the signature in `src/billing/invoice.hpp` (only its comment changes) and rewrites `compute_totals` in `src/billing/invoice.cpp`: line totals and the running subtotal go through two small helpers, `checked_mul` and `checked_add`, that return nothing instead of overflowing, and `discount_for` computes the half-up discount in integers by splitting the subtotal into whole multiples of 10000 and a remainder. `tests/invoice_test.cpp` is new: it prices small orders with half-cent and other fractional discounts, 0% and 100% discounts, an order at the largest subtotal, orders that overflow, and orders with invalid inputs.
C++20, standard library only, built with CMake (unchanged: it builds `src/billing/*.cpp` into the billing library and every `tests/*_test.cpp` together with `tests/check_main.cpp` into the test runner) by Apple clang 17 locally; CI builds and runs the tests with both Apple clang 17 and GCC 13, with `-Wall -Wextra -Wconversion -Werror`. `compute_totals` is a pure function with no shared state; the checkout service calls it from its request threads. Existing and unchanged: the only caller, `checkout::place_order`, looks up the promo code's discount (stored as basis points), calls `compute_totals` with the cart's lines, and on an empty result refuses the order with a generic "this order can't be priced" message and logs the order id. Amounts here are never negative: refunds, tax and currency conversion are separate modules. `tests/check.hpp` is the repo's tiny test helper: `TEST(name) { ... }` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure with file and line, and the `main` in `tests/check_main.cpp` runs every registered test and exits non-zero if any check failed.
Requirements
- `billing::compute_totals(items, discount_bp)` prices an order: the subtotal is the sum over the lines of `unit_price_cents * quantity`; the discount is `subtotal * discount_bp / 10000` rounded to the nearest cent, with an exact half cent rounded up (0.5 cent is 1, 1.5 cents is 2, 0.4999 cent is 0); the total is the subtotal minus the discount. Use integer arithmetic only, so the result is exact for every input in range.
- Ranges: `unit_price_cents` is a `std::int64_t` and any value from 0 to its maximum is valid; `quantity` is a `std::int32_t` and any value from 0 to its maximum is valid; `discount_bp` is a `std::int32_t` from 0 to 10000 inclusive (1250 is 12.5%, 10000 is 100%); an order may have any number of lines, including none. A negative price, a negative quantity or a discount outside 0 to 10000 returns nothing.
- When a line total or the subtotal does not fit in `std::int64_t`, return nothing; no arithmetic may wrap or overflow. Every order whose line totals and subtotal fit, up to a subtotal of `INT64_MAX`, gets its exact subtotal, discount and total.
- Unchanged: the signature, `LineItem`, `InvoiceTotals`, and the caller's handling of an empty result.
- Ship unit tests for half-cent rounding, 0% and 100% discounts, the top of the range, overflow in a line and in the subtotal, and rejected inputs.
Files touched
- src/billing/invoice.hpp
- src/billing/invoice.cpp
- tests/invoice_test.cpp