Wikis & learning

Test one calculation against boundary cases

A small deterministic function can be checked by writing down inputs and expected outputs. Include the boundary where a new page starts rather than testing only a comfortable middle value.

A small deterministic function can be checked by writing down inputs and expected outputs. Include the boundary where a new page starts rather than testing only a comfortable middle value.

Walkthrough

The examples run the same function repeatedly and compare actual and expected values strictly. A failing case throws with its input, making the first discrepancy easy to locate.

PHP example

<?php
function numberOfPages(int $rows): int { return (int) ceil($rows / 10); }
$cases = [0 => 0, 1 => 1, 10 => 1, 11 => 2];
foreach ($cases as $input => $expected) {
    if (numberOfPages($input) !== $expected) {
        throw new RuntimeException('Mismatch for ' . $input);
    }
}
echo count($cases), ' checks passed';

Expected output

4 checks passed

Change and check

Change the formula to round down and run the cases again. Which case catches the change first? Add a separate test for a full second page.