Basic vs. compound condition coverage

后端 未结 2 1578
情话喂你
情话喂你 2021-01-16 12:56

I\'m trying to get my head around the differences between these 2 coverage criteria and I can\'t work out how they differ. I think I\'m failing to understand exactly what de

2条回答
  •  甜味超标
    2021-01-16 13:34

    First, the difference between Decision and Condition.

    A Condition is an atomar boolean expression that can not be broken down into simpler boolean expression. For example: a (if a is boolean).

    A Decision is a compound of Conditions with zero or more Boolean operators. A Decision without an operator is also a condition. For example: (a or b) and c but also a and b or just a.

    Lets take a simple example

    if(decision)  {
      //branch 1
    } else {
      //branch 2
    }
    

    You need two tests to cover both branches. Thats the decision coverage or branch coverage. In case the decision is a condition (i.e. just a), that is also called basic condition coverage, which is the coverage of the two branches of a single condition.

    The decision can be broken down into conditions.

    Lets take for example

    decision = (a or b) and c
    

    The decision coverage would be achieved with

    • a,b,c = 0
    • a,b,c = 1

    But the permutation of all the combinations of its boolean sub expressions is the full condition coverage or multiple condition coverage), which is the compound of the basic condition coverage :

    | a | b | c |
    | 0 | 0 | 1 |
    | 0 | 0 | 0 |
    | 0 | 1 | 1 |
    | 0 | 1 | 0 |
    | 1 | 0 | 1 |
    | 1 | 0 | 0 |
    | 1 | 1 | 1 |
    | 1 | 1 | 0 |
    

    That would be quite a lot of tests, but some of those are redundant as some conditions are covered by others. This is reflected in the Modified Condition/Decision Coverage (MC/DC) which is a combination of condition coverage and function coverage.

    For MC/DC it is required, that each condition has to affect the outcome independently. With the above test (all are 0 or all are 1), we ignore the fact, that c-value doesn't matter if a and b are 0, or, that b-value doesnt matter if a and c are 1.

    So you should sit down, use your brain, and think about for which combinations the overall result R is 1 or 0.

      | a | b | c | a or b | c | R |  eq
    1 | 0 | 0 | 0 |    0   | 0 | 0 |  A
    2 | 0 | 0 | 1 |    0   | 1 | 0 |  B
    3 | 0 | 1 | 0 |    1   | 0 | 0 |  A
    4 | 0 | 1 | 1 |    1   | 1 | 1 |  C
    5 | 1 | 0 | 0 |    1   | 0 | 0 |  A
    6 | 1 | 0 | 1 |    1   | 1 | 1 |  D
    7 | 1 | 1 | 0 |    1   | 0 | 0 |  A
    8 | 1 | 1 | 1 |    1   | 1 | 1 |  D
    

    The last column shows the equivalence class:

    • A: c = 0, result is 0, neither a nor b have an influence
    • B: a,b = 0, result is 0, c has no influence
    • C: b,c = 1, result is 1, a has no influence
    • D: a,c = 1, result is 1, b has no influence

    For B and C it's quite obvious which to pick, not so for A and D. For each you have to check, what would happen, if I replace the operators, i.e. or -> and, and -> or, how will this affect the result of the (sub)decision. If the result will be affected, you got a candidate - If not, you don't.

    • A : (0 and/or 0) and/or 0 -> doesn't matter
    • A : (0 and 1) vs (0 or 1) -> does matter! -> Candidate
    • A : (1 and 0) vs (1 or 0) -> does matter! -> Candidate
    • A : (1 and/or 1) -> doesn't matter
    • D : (1 and 0) vs (1 or 0) -> does matter -> Candidate
    • D : (1 and 1) -> doesn't matter

    So you get the final test set as mentioned above:

    • a = 0, b = 1, c = 0 -> false branch (A) OR a = 1, b = 0, c = 0
    • a = 0, b = 0, c = 1 -> false branch (B)
    • a = 0, b = 1, c = 1 -> true branch (C)
    • a = 1, b = 0, c = 1 -> true branch (D)

    Especially the latter test - changing the operators - can be done with tools like mutation testing, that do not just replaing operators, but can do quite some more, i.e. flipping operands, removing statements, change order of execution, replace return values etc. And for each alteration of your code, it verifies if the test actually fails. This is good indicator of the quality of your test suite and ensures that code is not just covered but your tests for the code are actually valid.

    Regarding the terminology, I couldn't find the term "Compound Decision Coverage" somewhere. In my view a "compound decision" would be a compound of compounds of conditions, in other words: a compound of conditions.

提交回复
热议问题