Lite / Intelligent Robotic Systems / Week 29
Test every arrow
Your state machine has 9 transitions. How many tests prove it works? (Hint: not 1, and not 1,000.)
Why should I care?
A state machine is unusually testable: each transition is one small, checkable fact. Writing one test per arrow, plus the guard boundaries, catches most mistakes before the robot ever moves.
- Write a transition table as data and a test for each transition.
- Test guard boundaries on both sides of the threshold.
- Measure transition coverage of a test suite.
Predict
A guard says battery ≥ 30. Which battery values are the most valuable to test?
- 0 and 100
- 29 and 30
- 50 only
Guess first, then reveal
The values either side of the threshold. Off-by-one mistakes (> instead of ≥) only show up exactly at the boundary.
Discover
In the Code Lab, write the delivery robot's rules as a list of (state, event, guard, next) tuples and a step() function. Then write one assert per rule plus asserts at battery 29 and 30. Break one rule on purpose: does a test catch it?
What the interactive shows (text version)
In the Code Lab, the rules are written as a list of tuples and a step function applies the first matching rule; tests assert the next state for each rule and for battery values 29 and 30.
Understand
Rules as data
Store transitions in a table (a list of tuples or a dictionary) instead of scattered if-statements. Then one generic step() function runs them, and the table itself can be printed, checked and tested.
Transition coverage
Transition coverage = transitions exercised by tests ÷ total transitions. Aim for 100 %: one test per arrow that starts in the source state, sends the event and checks the target. Add tests for important ignored events too, such as 'order while DRIVING does nothing'.
Boundaries and sequences
Test each guard on both sides of its threshold (29 and 30 for ≥ 30). Then add a few sequence tests that run realistic event chains, such as a full delivery, because some bugs only appear after the battery has changed.
Explain it another way
Intuitive
Checking a train timetable: for every station and every departure, does the train go where the board says?
Technical
Unit tests per transition give transition coverage; boundary-value analysis covers guards; scenario tests cover paths. Together they are a cheap, strong test strategy for discrete logic.
Applied
Safety-related controllers are often required to show that every transition was exercised by a test, and code coverage tools can report it automatically.
Worked example 1
A suite has tests for 6 of the 7 delivery-robot transitions. What is its transition coverage, and which test is most likely missing?
6/7 ≈ 86 %. Commonly the missing one is FAULT + reset → IDLE, because fault paths are the easiest to forget.
Common mix-ups
100 % transition coverage proves the machine is correct.
✓ It proves every arrow was exercised, not that the arrows are the right ones. Reviews and scenario tests still matter.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Write a test suite with 100 % transition coverage for the delivery robot, plus boundary tests at 29 % and 30 % and one two-delivery sequence test. Report the coverage figure and one bug the tests caught when you broke a rule.
Reflect
Which test would you be most embarrassed to have skipped if the robot failed in public?
Write your answer on paper or in your own notes. Lite never stores what you write.