engineer14 engineer14 - 11 months ago 84
C Question

Code Coverage for C

I've been doing a little bit of reviewing the different code coverage testing used on embedded systems. In particular, I'm looking at the MC/DC. From what I understand, one of the objectives is to make sure that each logical clause in a statement should affect the outcome of the statement.

Two questions:

  1. What is gained by independently verifying that each clause has an effect on the outcome?

  2. Why would
    (A||B) && (A||!C)
    not be able to achieve 100% MC/DC, while
    will achieve 100% MC/DC even if they have the exact same functionality?

Answer Source

To answer your questions

  1. You want as little of code as possible and as less complex code as possible. Having unreachable conditions lengthens your code and makes your code unnecessarily complex.

  2. (A||B) && (A||!C) won't achieve 100% because it requires A to be checked twice for no reason. In the condition where A is false and B is true, A's truthiness will be checked for a second time in the (A||!C) expression for no reason in this formulation whereas in the formula A||(B&&!C) has A's truthiness being checked only once.