I have a test code like this:
int diff21(int n)
{
if (n <= 21)
{
return 21 - n;
}
else
{
return (n - 21) * 2;
}
}
and I added the compile flags: -fprofile-arcs -ftest-coverage
to provide coverage data.
I wrote the test code like this:
CU_ASSERT(diff21(19) == 2);
and I used lcov to generate a html report, and it said the else
branch's return (n - 21) * 2
is excuted, so I got a 100% line coverage. This is not true
I googled and found that is because of this --- the gcc optimization can eliminate some simple code lines by combining them with other lines, but how should I generate the coverage data if I'm not supposed to use the -fprofile-arcs -ftest-coverage
flag?
btw I'm using netbeans and cygwin to do unit tests, I don't think manually calling gcov
in a makefile or script is good for me.
just for your information, I'm using lcov like this:
#!/bin/bash
rm -rf coverage_report
rm -f app.info
lcov -b . -d . -c -o app.info
mkdir coverage_report
genhtml --legend --highlight -o coverage_report app.info
rm -f app.info
lcov -d . -z