TactivergeLite

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.)

About 35 min · 3 goals · Checked by automated tests · Full interactive version

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.

Predict

A guard says battery ≥ 30. Which battery values are the most valuable to test?

  1. 0 and 100
  2. 29 and 30
  3. 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.

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

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

Practice

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

%

Practice 2. A guard is speed < 0.5 m/s. Which two test values check its boundary best?

state number

Practice 4. Why store transitions in a table rather than nested if-statements?
Practice 5. All single-transition tests pass, but the robot fails after its second delivery. Which test would have caught it?

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.

← No state left behindWhy P control always falls short →