cunit-testinggcovgcovrceedling

Why gcov is not covering logical && operator?


My piece of code which I am doing unit testing on is something like this:

     if(((State !=TCPIP_IPADDR_STATE_ASSIGNED)&& (State !=TCPIP_IPADDR_STATE_ONHOLD) && (State !=TCPIP_IPADDR_STATE_UNASSIGNED)) ||(SoConId==DOIP_INVALID_16))
       {
        }

And my unit test case includes following :

`DoIP_LocalIpAddrAssignmentChg(12,0xFF);`

Where DoIP_LocalIpAddrAssignmentChg is the function name in which if is located and 0xFF is for invalid state which is obviously not equal to all 3 : TCPIP_IPADDR_STATE_ASSIGNED, TCPIP_IPADDR_STATE_ONHOLD, TCPIP_IPADDR_STATE_UNASSIGNED. The value of SoConId is 12. The value of DOIP_INVALID_16 = 0xFF.

So when I check my unit test report, its gives this result: code coverage for if condition My question is why it's not covering condition for TCPIP_IPADDR_STATE_UNASSIGNED as value for state I am passing is 0xFF which is invalid value.


Solution

  • You're a "victim" of lazy evaluation.

    Chapter 6.5.14 "Logical OR opeartor":

    If the first operand compares unequal to 0, the second operand is not evaluated.

    All three parts of the multiple-AND are true, and so unequal to 0.

    Both logical && are covered, but you can't see it because of the || in the same line not being fully executed.