Unit tests that run on the board
A ring buffer and a tiny test framework of three macros: ten tests run at start-up on the Uno, print PASS or FAIL with the line, and light a green or red LED.
Firmware · Intermediate
In a real product: Self-test at power-on (Medical): Medical and industrial devices test their own firmware modules at start-up before they treat or control anything.
Most firmware tests run on a PC, but some must run on the target. The code may depend on the compiler, the integer sizes or the hardware. And a device may have to prove at every power-on that its modules still work. You don't need a framework for that: three macros and a counter are enough.
This sketch tests a ring buffer, the queue between an interrupt and the main loop in almost every firmware. Ten tests run in setup(). Each failed check prints the file, line and expression, and the result lights the green LED (all passed) or the red one.
What you'll learn
- Writing small, independent tests: set up, act, check
- A
CHECKmacro that uses#expr,__FILE__and__LINE__to say exactly what failed - What to test in a ring buffer: empty, full, wrapping round, and rejecting a write when full
- Why the buffer keeps one slot free, and why
headandtailarevolatile
How it works
Ring has an 8-byte array and two indexes. put() writes at head, get() reads at tail, and both wrap with & (SIZE - 1), which works because 8 is a power of two. One slot always stays empty, so "full" (head + 1 == tail) and "empty" (head == tail) look different. That means 7 bytes fit.
CHECK(expr) evaluates expr. If it is false, the macro prints #expr, the expression turned into text by the preprocessor, with the line it is on, and marks the current test as failed. TEST(name) starts a test and END_TEST() counts it as passed or failed. The do { } while (0) around the macro body makes it act like a single statement, even after an if without braces.
Each test starts with a fresh buffer, so one test's leftovers can't make another pass or fail. A test that passes only because the previous one ran is a bug in the tests.
Try this
- Break
put()by removing the full check: two tests fail, and each says which line. - Make
SIZE10.& (SIZE - 1)is now wrong: which test catches it? - Add a test for
count()after the buffer has wrapped round twice.
sketch.ino
// A ring buffer and ten tests for it, run on the Uno at start-up: PASS/FAIL per test, green LED (pin 11) if all
// pass, red (pin 12) if any fail.
const uint8_t SIZE = 8; // a power of two: indexes wrap with & (SIZE - 1)
struct Ring {
uint8_t data[SIZE];
volatile uint8_t head, tail; // head: next write, tail: next read
};
void reset(Ring *r) { r->head = r->tail = 0; }
bool isEmpty(const Ring *r) { return r->head == r->tail; }
bool isFull(const Ring *r) { return ((r->head + 1) & (SIZE - 1)) == r->tail; }
uint8_t count(const Ring *r) { return (r->head - r->tail) & (SIZE - 1); }
bool put(Ring *r, uint8_t b) {
if (isFull(r)) return false;
r->data[r->head] = b;
r->head = (r->head + 1) & (SIZE - 1);
return true;
}
bool get(Ring *r, uint8_t *b) {
if (isEmpty(r)) return false;
*b = r->data[r->tail];
r->tail = (r->tail + 1) & (SIZE - 1);
return true;
}
// ---- the test framework: three macros ------------------------------------------------------------------------
int passed, failed;
bool ok;
#define TEST(name) do { ok = true; Serial.print(F(name ": ")); } while (0)
#define CHECK(expr) do { if (!(expr)) { ok = false; Serial.print(F("\r\n line ")); Serial.print(__LINE__); \
Serial.print(F(": CHECK(" #expr ") failed")); } } while (0)
#define END_TEST() do { if (ok) { passed++; Serial.println(F("PASS")); } else { failed++; Serial.println(F("\r\n FAIL")); } } while (0)
Ring ring;
void runTests() {
uint8_t b = 0;
TEST("a new buffer is empty");
reset(&ring);
CHECK(isEmpty(&ring));
CHECK(!isFull(&ring));
CHECK(count(&ring) == 0);
END_TEST();
TEST("get from an empty buffer fails");
reset(&ring);
CHECK(!get(&ring, &b));
END_TEST();
TEST("one byte in, the same byte out");
reset(&ring);
CHECK(put(&ring, 42));
CHECK(get(&ring, &b));
CHECK(b == 42);
CHECK(isEmpty(&ring));
END_TEST();
TEST("bytes come out in the order they went in");
reset(&ring);
for (uint8_t i = 1; i <= 5; i++) put(&ring, i * 10);
for (uint8_t i = 1; i <= 5; i++) { CHECK(get(&ring, &b)); CHECK(b == i * 10); }
END_TEST();
TEST("it holds SIZE - 1 bytes");
reset(&ring);
for (uint8_t i = 0; i < SIZE - 1; i++) CHECK(put(&ring, i));
CHECK(isFull(&ring));
CHECK(count(&ring) == SIZE - 1);
END_TEST();
TEST("put into a full buffer fails and changes nothing");
reset(&ring);
for (uint8_t i = 0; i < SIZE - 1; i++) put(&ring, i);
CHECK(!put(&ring, 99));
CHECK(get(&ring, &b));
CHECK(b == 0);
END_TEST();
TEST("it wraps round the end of the array");
reset(&ring);
for (uint8_t round = 0; round < 3; round++) {
for (uint8_t i = 0; i < 5; i++) CHECK(put(&ring, round * 10 + i));
for (uint8_t i = 0; i < 5; i++) { CHECK(get(&ring, &b)); CHECK(b == round * 10 + i); }
}
CHECK(isEmpty(&ring));
END_TEST();
TEST("count follows puts and gets");
reset(&ring);
put(&ring, 1); put(&ring, 2); put(&ring, 3);
CHECK(count(&ring) == 3);
get(&ring, &b);
CHECK(count(&ring) == 2);
END_TEST();
TEST("a full buffer empties again");
reset(&ring);
for (uint8_t i = 0; i < SIZE - 1; i++) put(&ring, i);
for (uint8_t i = 0; i < SIZE - 1; i++) CHECK(get(&ring, &b));
CHECK(isEmpty(&ring));
CHECK(!get(&ring, &b));
END_TEST();
TEST("the bytes 0 and 255 survive");
reset(&ring);
put(&ring, 0); put(&ring, 255);
CHECK(get(&ring, &b) && b == 0);
CHECK(get(&ring, &b) && b == 255);
END_TEST();
}
void setup() {
Serial.begin(9600);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
Serial.println(F("Ring buffer self-test"));
runTests();
Serial.print(passed);
Serial.print(F(" passed, "));
Serial.print(failed);
Serial.println(F(" failed"));
digitalWrite(failed ? 12 : 11, HIGH);
}
void loop() {}