Skip to content
Tactiverge

School/△ Builder/Week 5

Test the edges: boundary values

Two versions of a charger rule pass every test at 20 °C. One damages the battery at exactly 45 °C. Which test would have caught it?

About 35 min · 3 goals · Checked by automated tests

1.Why should I care?

Off-by-one and < versus <= mistakes are among the most common bugs in control code, and they only show up at exactly the boundary. Typical values never find them. Boundary testing is cheap, systematic and catches them every time.

  • ◇ Choose test values on, just below and just above each boundary.
  • ◇ Use a specification, not the code, to decide what each test should expect.
  • ◇ Find a < versus <= bug with boundary tests and fix it.

2.Predict

Version C uses elif temp_c < 45. The specification allows charging up to and including 45 °C. Which test reveals the bug?

Guess first. Being wrong here is how the surprise works.

3.Understand

Where the bugs live

A rule with thresholds at 0 and 45 °C has two boundaries. For each one, test on it (0, 45), just inside (0.5, 44.5) and just outside (−0.5, 45.5), using the smallest step the sensor can report. Six tests check both edges completely.

Expected values come from the specification

Write down what each test should give before looking at the code, by reading the specification: 'charge when 0 °C ≤ T ≤ 45 °C'. If you work out the expected value by running the code, the test can only confirm the bug.

Boundary tests as asserts

def charge_mode(temp_c):
    if temp_c < 0:
        return "BLOCK: too cold"
    elif temp_c <= 45:
        return "CHARGE"
    else:
        return "BLOCK: too hot"


assert charge_mode(-0.5) == "BLOCK: too cold"
assert charge_mode(0) == "CHARGE"
assert charge_mode(45) == "CHARGE"
assert charge_mode(45.5) == "BLOCK: too hot"
print("boundary tests passed")

Explain it another way

If a fence is supposed to stop at the edge of the garden, you check right at the edge, not in the middle of the lawn.

Worked example 1

A conveyor stops if the load is over 25.0 kg. The scale reads in 0.1 kg steps. Choose boundary tests and their expected results.

24.9 kg → run, 25.0 kg → run ('over 25' means strictly greater), 25.1 kg → stop. Add a typical value (10 kg → run) and an extreme one (0 kg → run). If the specification had said 'at or over 25 kg', 25.0 would be 'stop'.

Common mix-ups

  • If the code works for normal values, it works.
    ✓ Comparison mistakes only show at the boundary. Normal values cannot find them.
  • Testing 0 and 45 is enough.
    ✓ Also test one sensor step either side, so you know the boundary is in exactly the right place.

4.Discover

For each version, first add a few typical tests (10, 20, 30 °C). Do they find any bug? Then press 'Add the boundary tests' and record which version fails where. Which single version matches the specification?

Loading the interactive…

Can't use the interactive? Read a description instead

The boundary tester keeps a table of test temperatures with the specification's answer, the selected version's answer and pass or fail. Adding the boundary tests (−0.5, 0, 0.5, 44.5, 45, 45.5) shows: Version A fails only at 0; Version B passes all; Version C fails only at 45. Typical values such as 20 °C pass for all three versions.

5.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A thermometer reports in steps of 0.5 °C. The alarm rule is if temp_c >= 30: alarm(). As the temperature rises from 28 °C one step at a time, what is the first reading that sets off the alarm, in °C?

°C

Practice 2

A rule should pass readings from 10 to 60 inclusive, in steps of 1. Which set of tests is best?

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 3

Where should a test's expected value come from?

Practice 4

Version A uses if temp_c <= 0: return 'BLOCK: too cold'. Which input reveals its bug?

Practice 5

A rule has three thresholds. Using on, just-below and just-above tests for each, how many boundary tests is that?

tests

7.Apply it

Write boundary asserts for your tank classify(level_pct) function from the if/elif lesson (LOW below 20 %, OK 20–90 %, HIGH above 90 %), in 0.5 % steps. Deliberately change one <= to <, run the tests, and record which assert catches it.

8.Reflect

Concepts: Testing and debugging · Decisions and loops