Files
Indiana 0966fa8cfc test: make firmware logic bugs catchable without hardware (Workstream F)
The firmware has never been flashed, and a real bug already reached the
repo because of it: RD03E_FRAME_LEN was 5 for a 6-byte frame, so the footer
check collided with the distance high byte and EVERY distance reading was
garbage — always `lo | 0x5500`, about 218 metres, regardless of what the
sensor saw. That was pure logic with no hardware dependency. It should have
been catchable on a laptop, and there was simply no way to run the code.

Extracted the hardware-free logic out of the three drivers — rd03e_parse,
bmp280_compensate, mems_level — as moves rather than rewrites, carrying the
explanatory comments along with the code they explain. The drivers now own
only their bus I/O and call into the pure units, so nothing changes for the
real device.

`./run_tests.sh` builds them with gcc -Wall -Wextra -Werror plus a
dependency-free assert harness: 175 checks, 0 failed, from a clean tree.

Proven to catch the actual bug rather than assumed to: reintroducing
FRAME_LEN 5 fails four checks, including one that reads "a simple-report
frame is 6 bytes, not 5", plus the truncated-frame and 5-byte-window cases.
Restored, green again.

This does NOT make the firmware verified, and the README says so plainly —
it is called a narrow exception and scoped to pure logic. Wiring, timing,
real register behaviour and the reconstructed RD-03E frame format all still
need the physical board.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-31 13:17:35 +00:00

57 lines
3.0 KiB
C

// Minimal test scaffolding. No frameworks, no dependencies — the whole
// point of this harness is that it runs anywhere gcc runs.
#pragma once
#include <stdio.h>
#include <stdlib.h>
extern int g_tests_run;
extern int g_tests_failed;
#define CHECK(cond, ...) \
do { \
g_tests_run++; \
if (!(cond)) { \
g_tests_failed++; \
printf(" FAIL %s:%d: ", __FILE__, __LINE__); \
printf(__VA_ARGS__); \
printf("\n condition: %s\n", #cond); \
} \
} while (0)
#define CHECK_EQ_U(actual, expected, ...) \
do { \
unsigned long long a_ = (unsigned long long)(actual); \
unsigned long long e_ = (unsigned long long)(expected); \
g_tests_run++; \
if (a_ != e_) { \
g_tests_failed++; \
printf(" FAIL %s:%d: ", __FILE__, __LINE__); \
printf(__VA_ARGS__); \
printf("\n expected %llu, got %llu\n", e_, a_); \
} \
} while (0)
#define CHECK_NEAR(actual, expected, tol, ...) \
do { \
double a_ = (double)(actual); \
double e_ = (double)(expected); \
double d_ = a_ - e_; \
if (d_ < 0) d_ = -d_; \
g_tests_run++; \
if (!(d_ <= (double)(tol))) { \
g_tests_failed++; \
printf(" FAIL %s:%d: ", __FILE__, __LINE__); \
printf(__VA_ARGS__); \
printf("\n expected %.6f +/- %.6f, got %.6f\n", \
e_, (double)(tol), a_); \
} \
} while (0)
#define SUITE(name) printf("[%s]\n", (name))
void test_rd03e_parse(void);
void test_bmp280_compensate(void);
void test_mems_level(void);