Given a function that needs to be inlined for performance reasons (because it's called in a loop and I don't want the call overhead). Simplified example:
void increment(int *single_value) {
*single_value++;
}
void increment_values(int *array, size_t length) {
for(size_t i=0;i<length;i++) {
increment(&A[i]);
}
}
But I want to unit test the function, for example
void test_increment() {
int value = 5;
increment(&value);
assert_equal(value, 6);
}
Is it possible to tell the compiler to inline the function and to export it, so I can link against my tests? I'm using gcc
, but methods working for all compilers (clang
, icc
, ...) would be preferred.
using inline
is a mere hint that the compiler might at will inline:
"[ . . . ] An inline definition does not provide an external definition for the function, and does not forbid an external definition in another translation unit. An inline definition provides an alternative to an external definition, which a translator may use to implement any call to the function in the same translation unit. It is unspecified whether a call to the function uses the inline definition or the external definition."
— ISO 9899:1999(E), the C99 standard, section 6.7.4
In fact, many compilers do that to functions that look like your example automatically when being called with optimization flags. Also, the compiler usually knows better than you do what kind of instruction code it's producing, so it is not necessarily to tell it to inline.
In your situation, I'd just let the compiler decide, and not use inline at all. However, if you choose to use inline
, you can still use that function in a unit test, so no problem at all.