I am trying to improve my unit tests by inspecting my current code coverage percentage. I am using gcov and lcov to produce a HTML report of the coverage results. However, I am having problems understanding some of the output. I know that a +
indicates that a branch was taken and a -
that it was not taken.
131 : 8 : QString toString() const
132 : : {
133 [ + - ][ + - ]: 8 : return ((negative && !isZero()) ? "-" : "") + QString::number(sec) + "." + QString::number(nano).rightJustified(9, '0');
[ + - ][ + + ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
134 : : }
Variable negative
is of type bool as is the return value of method isZero
. So I would expect four branches here but I don't see how the output maps to this expectation. So how is the output to be interpreted? Tooltips when hovering over the +/- signs tell me that branches 3, 6, 9, 11, 12, 14, 17, 20, 23 and 26 were taken while branches 4, 7, 10, 15, 18, 21, 24 and 27 were never taken.
Please refer the answer [here] as below[1]:
the reason here is the same, QString::number() is a static function and it calls a inline function [QString::setNum][2] which had many branch inside.
Can check below as an example to illustrate this:
foo.h
foo.cpp
main.c
after compile and generate code coverage report(relate method refered [here][3]), can get below coverage report for function foo():
[![foo_coverage][4]][4]
we can find that foo have total 8 branchs(2 X 2 X 2), and the two foo(1), foo(2) hit 5 of the branchs. [1]: https://stackoverflow.com/a/69164983/4277805 [2]: https://github.com/radekp/qt/blob/master/src/corelib/tools/qstring.h [3]: https://shenxianpeng.github.io/2021/07/gcov-example/ [4]: https://i.stack.imgur.com/n6huJ.png