Test with a fake GPIO
Write a fake that records pin writes and delays, so code can be tested off target. An easy embedded C problem (Testing and code coverage), run and graded in…
easy · Testing and code coverage · testing · fakes · function-pointers
Code that talks to hardware through an interface can be tested on a PC by giving it a fake: an implementation that records what the code did, so a test can check it.
Write fake_write(pin, level) and fake_delay(ms). Keep a fake clock in milliseconds (delays advance it, starting at 0), and record each write in fake_log (provided): the time, pin and level. fake_count is the number of entries; the log holds at most 32, so ignore writes once it is full.
The tests run signal_pattern with your fake and check the recorded timeline.
Starter code
void fake_write(int pin, int level) {
fake_log[fake_count].pin = pin;
fake_log[fake_count].level = level;
fake_count++;
}
void fake_delay(int ms) {
(void)ms;
}Example tests
The pattern's timeline
run_pattern(13);Expected output:
t=0 pin 13 high
t=100 pin 13 low
t=200 pin 13 high
t=300 pin 13 low
t=400 pin 13 high
t=500 pin 13 low
t=600 pin 13 high
t=1000 pin 13 lowLearn it first
- Testing and code coverage · Unit tests on the host, mocks, HIL, coverage and static analysis.
More Testing and code coverage problems
- Write a CMock-style mock · A mock that checks a driver makes exactly the bus writes a test expects, in order, and reports what went wrong.
- A fake I2C device for driver tests · Write a fake I2C bus with one register-mapped device, so a sensor driver can be tested on a PC.
- Write tests that catch the bugs · Write a test suite for a scaling function. It is graded by running it against buggy versions it must reject.