Skip to Main Content
Test Driven Development for Embedded C
book

Test Driven Development for Embedded C

by James W. Grenning
April 2011
Intermediate to advanced content levelIntermediate to advanced
356 pages
8h 55m
English
Pragmatic Bookshelf
Content preview from Test Driven Development for Embedded C

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Design Patterns for Embedded Systems in C

Design Patterns for Embedded Systems in C

Bruce Powel Douglass

Publisher Resources

ISBN: 9781941222997Errata Page