主要内容

Condition Coverage

Percentage of condition outcomes that current test cases evaluate

Since R2023b

Description

Conditions are simple C/C++ Boolean expressions that contain:

  • Relation operators, such as <, >, <=, or >=

  • Equation operators, such as != or ==

  • The logical negation operator (!)

Conditions do not contain logical operators such as && or ||.

Each condition in your code has two outcomes: true and false. This metric indicates the percentage of condition outcomes in your source code that the current test cases evaluate. A condition coverage of 50% indicates that the current test cases evaluate only half of the condition outcomes in your code. To increase condition coverage, add test cases that evaluate the unevaluated condition outcomes.

Polyspace Implementation

To calculate condition coverage, Polyspace® Test™ counts the total number of possible condition outcomes (n_outcome) and the condition outcomes that the current test cases evaluate (n_evaluated):

Condition coverage = (n_evaluated / n_outcomes) *100
When evaluating a Boolean statement containing multiple conditions, Polyspace Test skips the subsequent conditions in the statement if the first condition is false. If a simple Boolean expression is the only condition in a branching or looping statement, Polyspace Test considers the expression a decision rather than a condition.

Consider this code:

int foo(int x)
{
    int y = (x >= 5 && x != 7); //1,2

    if (x < 0)//Not a condition, but a decision
        return 1;
    else if (x > 0 && y)//3,4
        return 2;
    else
        return -1;
}
The conditions in this code are:

  • (x >= 5)

  • (x != 7)

  • (x > 0)

  • (y == 1)

The Boolean expression (x < 0) is the only one in the if statement and Polyspace Test considers the expression a decision. Each of the preceding four conditions can be either true or false, resulting in n_outcomes = 8. Polyspace calculates how many of these outcomes the test cases evaluate. If you test foo() with x == 0, then Polyspace Test evaluates these condition outcomes:

  • (x >= 5) == false

  • (x > 0) == false

Resulting in n_outcomes = 2 and a condition coverage of 2/8 × 100 or 25%.

Examples

expand all

Consider the function foo(), which contains four conditions with eight possible condition outcomes

int foo(int x, int y)
{
    if (x < 0 && y>0)
        return 1; 
    else if (x > 0 && y==0)
        return 2;
    else
        return -1;
}

Consider these test cases.

Test Case 1Test Case 2

  • Input: Parameter values = (-1,1)

  • Assessment: Return value = 1

  • Coverage: (2/8)×100 or 25%

  • Coverage Details: This test case evaluates these conditions.

    • x < 0 ==true

    • y > 0 ==true

  • Input: Parameter value = (1,-1)

  • Assessment: Return value = -1

  • Coverage: (3/8)×100 or 38%

  • Coverage Details: This test case evaluates these conditions.

    • x < 0 ==false

    • x > 0 ==true

    • (y == 0) == false

The two preceding test cases evaluate five of the eight decisions in foo().

ConditionOutcomeCoverage
(x < 0)trueCovered by test 1
falseCovered by test 2
(x > 0)trueCovered by test 2
falseNot covered
(y > 0)trueCovered by test 1
falseNot covered
(y==0)trueNot covered
falseCovered by test 2

The combined condition coverage of the preceding two test cases is 5/8×100 or 63%. To test all the conditions in foo(), add new test cases.

Correction — Add Test Cases to Complete Condition Coverage

To obtain 100% condition coverage, add two new test cases.

Test Case 3Test Case 4

  • Input: Parameter values = (2,0)

  • Assessment: Return value = 2

  • Coverage: (3/8)×100 or 38%

  • Coverage Details: This test case evaluates these conditions.

    • x < 0 == false

    • x > 0 == true

    • y == 0 == true

  • Input: Parameter value = (-1,-1)

  • Assessment: Return value = -1

  • Coverage: (3/8)×100 or 38%

  • Coverage Details: This test case evaluates these conditions.

    • x < 0 == true

    • y > 0 == false

    • x > 0 == false

With the addition of these two test cases, condition coverage reaches 100%.

Version History

Introduced in R2023b