Faking with a Function Pointer

Let’s use our random minute generator for the randomized light schedule feature. The scheduler will have unpredictable results because of its unpredictable indirect input from RandomMinute_Get. When production code depends on something unpredictable, it’s time to substitute in a test double. We have a problem; the binding of the LightScheduler to RandomMinute_Get is done by the linker, as you can see by looking at the interface of RandomMinute:

t0/include/HomeAutomation/RandomMinute.h
 
void​ RandomMinute_Create(​int​ bound);
 
int​ RandomMinute_Get(​void​);

Before we refactor the interface to allow function pointer substitution, let’s design the test for the randomized schedule.

t0/tests/HomeAutomation/LightSchedulerRandomizeTest.cpp ...

Get Test Driven Development for Embedded C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.