Simvik

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 low

Learn it first

More Testing and code coverage problems