C1250 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C12 · Lesson 50 · Testing embedded code

Checking firmware without waiting for the hardware

Firmware is hard to test the obvious way: the hardware may not exist yet, a bug might take a week of field use to appear, and watching LEDs by eye catches only the crudest failures. Professional embedded teams test most of their code without any hardware at all. They keep the logic (debouncing, parsing, control math, state machines) apart from the few lines that touch registers, so the logic can be run thousands of times with chosen inputs, on a computer or on the board itself, and checked automatically. What's left for the real hardware is a thin layer, and a final test with the device in the loop.

  • The program is its own test suite: a tiny CHECK macro runs each test case and records failures, then the program prints the number of tests and the number that failed.
  • The functions under test never touch a register: clamp, a checksum, and a debouncer that takes one raw sample at a time and returns the accepted level. That's what makes them testable with made-up inputs.
  • Break one: change clamp's < to <=, or the debouncer's 3 to 2, and run again. The failing tests print their numbers.

Predict

A function has two independent if statements, each of which can go either way. How many different paths through it does a thorough test cover?

Name it

Separate logic from hardware

Write the decisions and calculations as plain functions that take values and return values. A thin hardware layer reads the pins and registers and calls them. The logic can then be tested:

  • on a computer, compiled for it, with a normal test framework, in milliseconds;
  • on the board, as the play does, with no peripherals involved;
  • with fakes: stand-ins for the hardware layer that return scripted values and record what was written.

Choosing test cases

  • Typical values, to show it works at all.
  • Boundaries: for a range lo..hi, test lo − 1, lo, hi and hi + 1, where off-by-one errors live.
  • Each decision both ways, and each state machine transition (every state × event pair, lesson 36).
  • Errors: empty input, overflow (the checksum's wrap), a missing device.
  • Regressions: each fixed bug gets a test, so it stays fixed.

The rest

Hardware-in-the-loop tests run the real firmware on a real board, driven by a test fixture that simulates the world (buttons, sensors, a serial terminal) and checks the outputs; this course's board and its scripted devices work exactly that way. And assert-style checks left in the firmware catch impossible conditions in the field, logging them before a watchdog reset.

Worked example

Choose test inputs for clamp(x, 0, 100).

Show the solution
  1. A typical value. 50.
  2. The boundaries. −1, 0, 100 and 101.
  3. Far outside. −30000 and 30000.
  4. Write down the expected results.
InputExpected
5050
−10
00
100100
101100
−300000
30000100
  1. What they catch. Seven tests, each a single line, catch both the off-by-one and the wrong-direction comparison.

Answer 50, −1, 0, 100, 101, −30000 and 30000, expecting 50, 0, 0, 100, 100, 0 and 100.

Worked example

A state machine has 5 states and 4 events. How many transition tests cover it fully, and how long do they take at 2 ms each?

Show the solution
  1. Count the pairs. 5×4=20 (state, event) pairs, including the ones that should do nothing.
  2. Time them. 20×2ms=40ms
  3. Compare with doing it by hand. Run on every build, that's free; done by hand on a board with buttons, it's a tedious afternoon.

Answer 20 tests, taking 40 ms.

Worked example

Choose boundary tests for a function that converts a 12-bit ADC code to millivolts.

Show the solution
  1. Pick the edges and their neighbors. 0, 1, 2047, 2048, 4094 and 4095, plus a value or two in between.
  2. Overflow at the top. Overflow bugs show up at 4095: does 4095 × 3300 fit in the arithmetic used?
  3. Off-by-one at the bottom. Off-by-one errors show up at 0 and 1.
  4. Sign in the middle. Sign mistakes show up around the middle if any signed arithmetic is involved.

Answer 0, 1, 2047, 2048, 4094 and 4095, plus a value or two in between.

Check

A function makes 4 independent yes-or-no decisions. How many paths does it have?

Check

A function checks 2 separate ranges, and each needs the four boundary tests (lo − 1, lo, hi, hi + 1). How many boundary tests is that?

Check

A state machine has 6 states and 3 events. How many tests cover every transition, including the ones that should do nothing?

Check

A test suite has 1000 tests taking 0.5 ms each. How long does it take to run?

Write units the way you normally would: 1.5 s and 1500 ms are the same answer.

Check

Tests exercise 45 of a program's branches; 40 branches are never reached. What's the branch coverage?

Check

Why does the play's debouncer take one sample at a time and return a level, instead of reading the pin itself?

Check

The tests below check an average of uint8_t values, and two fail. Fix average (not the tests) so all five pass; the program prints the number of tests and of failures.

C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.

Check

Write in_range(x, lo, hi), true when lo ≤ x ≤ hi, so that the six tests pass.

MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.

Check

A state machine has 5 states and 2 events; each test takes 10 ms.

(a)

How many tests cover every state-event pair?

(b)

How long do they take?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

A function takes a 10-bit unsigned input.

(a)

How many inputs would an exhaustive test try?

(b)

What is the largest input, the one that most often reveals an overflow?

Explain why

Why do embedded teams separate logic from hardware access, even though it adds a layer?

Practice

Keep practicing

10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.