TactivergeLite

Lite / Building Machines That Respond / Week 9

Build it, test it, break it

Your checker says OK for the real log. Will it still say the right thing when the logger glitches, the file is empty, or a reading is exactly 60.0?

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

Why should I care?

Building in small, tested steps is how engineers keep a growing program under control. Each function you finish and test becomes something you can trust, so when the whole program misbehaves you know where not to look.

Predict

Lines "41.2", "ERR", "54.0", "--". What mean should the checker report?

  1. 23.8 °C (dividing by all 4 lines)
  2. 47.6 °C (dividing by the 2 good readings)
  3. It should crash
Guess first, then reveal

47.6 °C: (41.2 + 54.0) / 2. The malformed lines are skipped and counted, and the mean uses only good readings. Dividing by 4 would be a bug that tests should catch.

Discover

In the Code Lab, start with decide() from this lesson and its four asserts: run them. Then add parse_reading, summarise and count_outside one at a time, each with its own asserts, and only then the main program. Paste each of your test logs into the lines list and compare with your expected reports.

What the interactive shows (text version)

The Code Lab exercise starts with decide(values, malformed): it returns INSPECT for an empty list, for any value below 10 or above 60, or when more than 2 lines are malformed; otherwise OK. Its tests: [20.0, 30.0] with 0 malformed → OK; [20.0, 61.0] → INSPECT; [10, 60] with 2 malformed → OK; [25.0] with 3 malformed → INSPECT; [] → INSPECT.

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

Understand

Build one function at a time

SAFE_LOW_C = 10
SAFE_HIGH_C = 60

def decide(values, malformed):
    """Return "INSPECT" or "OK" following the brief."""
    if len(values) == 0:
        return "INSPECT"
    outside = 0
    for v in values:
        if v < SAFE_LOW_C or v > SAFE_HIGH_C:
            outside += 1
    if outside > 0 or malformed > 2:
        return "INSPECT"
    return "OK"

assert decide([20.0, 30.0], 0) == "OK"
assert decide([20.0, 61.0], 0) == "INSPECT"
assert decide([10, 60], 2) == "OK"       # band edges are safe
assert decide([25.0], 3) == "INSPECT"
assert decide([], 0) == "INSPECT"

Reading the log

In the Code Lab, paste the log into a list: lines = ["41.2", "ERR", ...]. On your own computer, read a file instead:

with open("motor_log.txt") as f:
    lines = f.read().splitlines()

Either way the rest of the program receives the same list of strings, which is why parsing lives in its own function.

Keep a bug diary

When a test fails, don't just fix it: write the symptom (decide([10, 60], 2) said INSPECT), the hypothesis (< versus <= on the band), the test that confirmed it (a probe showed outside = 2) and the fix. This record is a deliverable of the project, and the habit is worth more than the program.

Explain it another way

Intuitive

Like building a wall one course of bricks at a time and checking it with a level each time, instead of building the whole wall and hoping it's straight.

Technical

Incremental development with unit tests: each function is verified against its contract before integration. Integration tests (whole test logs with expected reports) then check that the pieces fit together.

Applied

This is how professional software is built: small changes, each covered by tests, run automatically. Your sensor-log checker is a miniature of real condition-monitoring software.

Worked example 1

A tester's log is 60.0, 60.0, 60.1. What should the report be?

All three convert; mean = 180.1 / 3 ≈ 60.03 °C, min 60.0, max 60.1. 60.1 is above 60, so outside = 1 and the decision is INSPECT. If your checker says OK, look at the comparison against SAFE_HIGH_C.

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. In what order should you build the checker?
Practice 3. decide([10, 60], 0) returns INSPECT. Which line is most likely wrong?
Practice 4. Which is the best bug-diary entry?

Apply it

Complete the project: the full checker with at least four tested functions, your test logs with expected and actual reports, a bug-diary entry for a real bug, and a one-page note explaining the algorithm so another learner can run it from the note alone.

Reflect

Which test log found the most interesting bug, and what did it teach you about your own code?

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

Go further

← Plan the sensor-log checkerCharge on the move →