Testing a Score Calculator: Boundary Values, Bands, and Off-by-One Bugs
Every score calculator starts as the friendliest ticket in the backlog: read a table, return a number. Nobody reviews it closely. The table is right there, the arithmetic is trivial, and the first few manual checks pass.
Every score calculator starts as the friendliest ticket in the backlog: read a
table, return a number. Nobody reviews it closely. The table is right there, the
arithmetic is trivial, and the first few manual checks pass. Then someone enters
a value that lands exactly on a line in the table, gets the wrong score, and the
bug surfaces weeks later in a support thread.
Score-lookup calculators are fragile for one structural reason: the interesting
cases are edges, and edges are exactly where comparisons, rounding and range
checks disagree with each other. Below are the three passes I now run against any
lookup-style calculator (the one I keep for this kind of work is at
aftptcalc.com), plus the small amounts of code that
caught every real bug so far.
Say out loud what a band includes
Most scoring tables are written as bands: a range of results maps to a number of
points. The function usually follows straight away.
// One event: run time in seconds -> points (faster is better).
const RUN_BANDS = [
{ min: 0, max: 900, points: 60 }, // 15:00 or better
{ min: 901, max: 960, points: 58 },
{ min: 961, max: 1020, points: 54 },
];
function runPoints(seconds) {
const band = RUN_BANDS.find(b => seconds >= b.min && seconds <= b.max);
return band ? band.points : null;
}
That looks fine until a value of 900.5 arrives, or a stored 15:00 becomes
900 while the band edge was meant to be exclusive. Before testing anything,
write down three sentences: are band edges included, are gaps between bands
allowed, and what happens to a value outside the table. Half the bugs I have
fixed were a missing answer to that third question, not a wrong comparison.
Test every edge, and the two values beside it
Random inputs almost never hit a boundary, so generate them instead. For each
band, the values worth testing are min, min - 1, max and max + 1.
const edges = RUN_BANDS.flatMap(b => [b.min - 1, b.min, b.max, b.max + 1]);
for (const seconds of edges) {
const band = RUN_BANDS.find(b => seconds >= b.min && seconds <= b.max);
const expected = band ? band.points : null;
const actual = runPoints(seconds);
if (actual !== expected) {
console.error(`FAIL ${seconds}s -> ${actual}, expected ${expected}`);
}
}
The loop is short enough to paste into a console and strict enough to earn its
place: it fails on a band that swallows its neighbour's last value, on a
one-second gap that silently returns nothing, and on a <= that quietly became
a <. Add one case per band for a value in the middle of the band too, so a
reversed range (min: 961, max: 900) cannot hide behind a passing edge test.
Then check that the table still behaves like a scale. A score should never
reward a worse result, so walk the input range in one-unit steps and assert
points never go up as the result goes down.
for (let seconds = 1; seconds <= 1020; seconds++) {
const better = runPoints(seconds);
const worse = runPoints(seconds + 1);
if (better !== null && worse !== null && worse > better) {
console.error(`FAIL ${seconds + 1}s scores higher than ${seconds}s`);
}
}
A monotonicity walk like this catches the bug class that table edits introduce:
someone re-types a column, one row is off by a point, and no individual lookup
looks wrong.
Off-by-one bugs usually arrive through rounding
The last family is not in the lookup at all. Raw results are often averaged or
weighted before they are shown, and that is where rounding sneaks in a score the
published table never contained. Decide whether a half point rounds up, down, or
to even, then pin the two values either side of the cut and assert both. Also
decide where rounding happens: round once, at the end. Rounding an intermediate
average and then looking it up in a band shifts every boundary by a fraction of
a unit, which is how a calculator ends up disagreeing with the printed chart.
The same care applies to unit conversion. Convert once, keep the source unit in
the variable name (seconds, not value), and reject non-integer or negative
input at the door rather than letting it fall through to a default of zero.
Make failures name the row
Finally, print enough to act on. Expected 58, got 54 tells you nothing;
901s -> 54, expected 58 (band 901-960) tells you which row of the table moved.
Table-driven tests plus edge-generated inputs keep a scoring calculator honest
with very little ceremony, and they make the next edit to a band table a review
instead of a gamble.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.