I like the functionality of the GNU linker to greatly wrap functions. I usually use this, for example, non-deterministic function calls like rand() . Consider the following example, where I would like to write a unit test for giveMeANumber :
//number.cpp int giveMeANumber() { return rand() % 6 + 1; }
I can enclose a call in rand using the GNU linker link function:
//test.cpp extern "C" int __wrap_rand(void) { return 4; } void unitTest() { assert giveMeANumber() == 5; } $ g++ test.cpp -o test number.o -Xlinker --wrap=rand
Is there a way to do the same with regular C ++ functions? The following does not work, I think this is due to the distortion of the name. But even when I try this with a distorted name, it does not work.
//number.cpp int foo() { //some complex calculations I would like to mock } int giveMeANumber() { return foo() % 6 + 1; } //test.cpp extern "C" int __wrap_foo(void) { return 4; } $ g++ test.cpp -o test number.o -Xlinker --wrap=foo
source share