Imagine an API like this:
bool double_if_positive(float input, float* output) {
if (input > 0) {
*output = input * 2.0f;
return true;
} else {
return false;
}
}
And a unit test like this:
extern float var;
TEST(test) {
float output;
CHECK_EQUAL(true, double_if_positive(var, &output));
CHECK_EQUAL(var * 2.0f, output);
}
I believe an optimizing compiler would be able to elide both unit test checks, because accessing is output is only defined behavior if the return value is true, therefore the return value must be true.
EDIT: Actually, the second wouldn't get elided.
I'm assuming that CHECK_EQUAL expands to a function call if the condition is false; in this case, the compiler must assume that the function has side effects, for example it might throw - in which case you can't really elide the checks.