download

How do you pick boundary values to catch off-by-one bugs?

Testing~7 minReviewed 2 Dec 2026Free · No sign-in

Question

A checkout function shipping_fee(total_cents) prices integer cart totals in cents:

  • total_cents <= 0: raise ValueError
  • 1 to 4999 ($0.01 to $49.99): return 500
  • 5000 to 9999 ($50.00 to $99.99): return 200
  • 10000 ($100.00) and above: return 0

Tests checked 2500, 7500, and 15000 and passed, yet $50.00 carts still paid $5.00 in production.

  1. Why did those tests miss the bug?
  2. List the minimal 2-value boundary inputs, and say what 3-value BVA adds.
  3. Fix shipping_fee and write a compact boundary test table.
"CODE · PYTHON"
def shipping_fee(total_cents: int) -> int:
    if total_cents <= 0:
        raise ValueError("total must be positive")
    if total_cents <= 5000:
        return 500
    if total_cents < 10000:
        return 200
    return 0
Show hints (3)
  1. "CHECK PARTITION EDGES"

    2500, 7500, and 15000 sit in the middle of each price band. Where does < vs <= actually change the result?

  2. "PAIR EVERY EDGE"

    The four partitions meet at three edges: 0 | 1, 4999 | 5000, and 9999 | 10000. Test both sides of each edge.

  3. "ADD INNER NEIGHBORS"

    2-value BVA uses 6 inputs. 3-value BVA adds each edge's inner neighbor (-1, 2, 4998, 5001, 9998, 10001) to catch == mistakes.

Show checked explanation

Midpoints (2500, 7500, 15000) never touch the 4999 | 5000 edge where <= 5000 fails. Use 2-value boundary value analysis across all three edges (0, 1, 4999, 5000, 9999, 10000, 6 cases) to catch every < vs <= off-by-one bug, and add 3-value neighbors when == mistakes are a risk.

"SOLUTION · PYTHON"
def shipping_fee(total_cents: int) -> int:
    if total_cents <= 0:
        raise ValueError("total must be positive")
    if total_cents < 5000:
        return 500
    if total_cents < 10000:
        return 200
    return 0


VALID = [(1, 500), (4999, 500), (5000, 200), (9999, 200), (10000, 0)]
for cents, expected in VALID:
    assert shipping_fee(cents) == expected, cents
try:
    shipping_fee(0)
    raise AssertionError("0 must fail")
except ValueError:
    pass
print("6 boundary cases passed")
"OUTPUT"
6 boundary cases passed

Executed the solution on CPython 3.12.10, proved midpoint tests pass on the buggy <= 5000 starter, and mutated every boundary comparison against 2-value and 3-value BVA sets. Checked by an editor other than the author.

Walkthrough

Equivalence partitioning splits inputs into groups that the spec treats the same way. Here there are four integer partitions: invalid (<= 0), standard (1..4999), reduced (5000..9999), and free (>= 10000). Picking one midpoint per valid band (2500, 7500, 15000) covers all three valid partitions yet skips every comparison edge.

Off-by-one bugs live at partition edges, where a developer writes <= instead of < or shifts a constant by one cent. 2-value boundary value analysis (BVA) tests each boundary value and its immediate neighbor across the edge. For integer cents (step size 1), the three edges give 6 cases:

EdgeInvalid / lower sideUpper sideOff-by-one bug caught
0 | 10 -> ValueError1 -> 500< 0 instead of <= 0
4999 | 50004999 -> 5005000 -> 200<= 5000 or < 4999
9999 | 100009999 -> 20010000 -> 0<= 10000 or < 9999

On the starter code (if total_cents <= 5000), shipping_fee(5000) returns 500 instead of 200, so the 6-case table fails immediately. Replacing <= 5000 with < 5000 makes every boundary pass.

What 3-value BVA adds. Under ISTQB CTFL v4.0, 3-value BVA tests b - 1, b, and b + 1 for every boundary b. Merging overlapping neighbors across the six boundary values (0, 1, 4999, 5000, 9999, 10000) yields 12 unique inputs: -1, 0, 1, 2, 4998, 4999, 5000, 5001, 9998, 9999, 10000, 10001. Why bother? If someone mistakenly wrote if total_cents == 10000: return 0, 2-value BVA passes on 9999 and 10000, whereas 3-value BVA catches the bug at 10001.

Keep the test table data-driven (for example @pytest.mark.parametrize) so each boundary reads like the spec and fails with the exact input that broke.

Common mistakes

  • Testing only partition midpoints (2500, 7500, 15000). That confirms the happy bands exist while missing every < vs <= bug.
  • Using floating-point dollars (49.99, 50.00) instead of integer cents. Binary float rounding can hide or invent boundary errors.
  • Testing only 4999 and 5001, skipping 5000 itself. The exact threshold is the single value where < 5000 and <= 5000 diverge.
  • Skipping the invalid partition (0 and -1). Missing that edge lets free or negative carts slip through without ValueError.

Follow-up questions

  • "What changes if the cart total is a decimal dollar amount with two decimal places?" The step size shrinks from 1 cent to 0.01 dollars, so the 50.00 edge becomes 49.99 and 50.00 (with 50.01 for 3-value BVA). You also need a case for three decimal places like 49.995 to check rounding rules.
  • "Does 100% 2-value BVA replace domain edge cases like huge numbers or non-integers?" No. BVA covers the spec's numerical edges. You still add targeted checks for None, booleans (True == 1 in Python), strings, or very large integers if the caller is untrusted.
  • "When do you choose 3-value BVA over 2-value BVA?" Use 2-value BVA by default for fast unit suites. Step up to 3-value BVA on high-risk billing, tax, or safety rules where a stray == or != check could pass a 2-value table.

What a strong answer covers

Maps all four partitions, derives the exact 6-case 2-value boundary table, explains why midpoints miss <= 5000, and names the == defect that 3-value BVA (12 inputs) catches.

Open in interactive practice deck →

Original OfferVibe question. ISTQB CTFL v4.0 Syllabus · pytest parametrize.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.