0

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.

the busybee
  • 10,755
  • 3
  • 13
  • 30
  • The code you posted is *not* the code you tested. -- And you don't need the backslash in C source, only on preprocessor lines that have to be one line. – the busybee Apr 07 '20 at 11:37

1 Answers1

1

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.

the busybee
  • 10,755
  • 3
  • 13
  • 30
  • Thanks a lot. I wanted to know is there any way to make evaluate it forcefully ? Any compilation flags? – antromeda_12 Apr 15 '20 at 08:00
  • If you dare to read through the hundreds of GCC options, you might find one. -- Anyway, I don't know for sure, but you might like to try to compile without optimization, for GCC by the option `-O0`. This is always a good idea while testing, because optimized code may fold instruction sequences together, leading to "funny" observations. -- However, as the standard *defines* lazy evaluation, even unoptimized code might take the short-cut. – the busybee Apr 15 '20 at 09:13