TactivergeLite

Lite / Building Machines That Respond / 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 · Full interactive version

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.

Predict

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

  1. 20 °C
  2. 45 °C
  3. 60 °C
Guess first, then reveal

45 °C. At 20 and 60 °C both the specification and Version C agree. Only at exactly 45 does < give 'too hot' while the specification says charge.

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?

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

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

Intuitive

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.

Technical

Boundary value analysis partitions the input into ranges that should behave alike (equivalence classes) and tests at and around each partition edge, where off-by-one and comparison-operator faults concentrate.

Applied

Thermal cut-outs, over-current trips and speed limits are all thresholds. Certification testing of real products checks behaviour at each specified limit, for exactly this reason.

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

°C

Practice 2. A rule should pass readings from 10 to 60 inclusive, in steps of 1. Which set of tests is best?
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?

tests

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.

Reflect

Which boundary in a machine you use every day would you most want tested, and why?

Write your answer on paper or in your own notes. Lite never stores what you write.

← and, or, not: safety interlocks in codefor loops: summarise a sensor log →