There are several ways to do this, but not as beautiful as in OOP languages. Here is the list I'm using:
Using Function Pointers
This approach is my favorite, because the most flexible, in my opinion.
In the header file (* .h)
int (*_fun) (int); int fun (int a); int fun_mock (int a);
In the sample test file (* .C)
_fun = fun_mock;
In the normal case file (* .C)
_fun = fun;
Function call (main.C)
... _fun (); ...
Compilation
If you want to make TDD, you need to compile the test file and the main (or other files). Otherwise, do not compile the test file.
Using macros to replace a function name
In the header file (* .h)
If you want to call fun
In the header file (* .h)
If you want to call the breadboard version
Function call (main.C)
... FUN (); ...
Using this method, we need to establish the correct definition before compiling any of the modules.
Using a structure with a function pointer
Honestly, I never used this method, but read in other places. The main idea is to have pointers to a structure for all the various functions of the module, and whenever you want to change a function, you simply change the address of this function pointer on that structure. In the end, a similar strategy to the first method, but implemented differently.
According to Timothy Jones
test-dept is a relatively recent C module testing framework that allows you to execute functions at runtime. I found it very easy to use - here is an example from their docs:
void test_stringify_cannot_malloc_returns_sane_result() { replace_function(&malloc, &always_failing_malloc); char *h = stringify('h'); assert_string_equals("cannot_stringify", h); }
Despite the fact that the download section is a little outdated, it looks quite actively developed - the author fixed the problem, which I did very quickly. You can get the latest version (which I used without problems) with:
svn checkout http://test-dept.googlecode.com/svn/trunk/ test-dept-read-only version was updated in October 2011.
However, since trimming is achieved using assembler, it may take some effort to get it to support ARM.
The last point I copied from fooobar.com/questions/589598 / ....