主要内容

Statement Coverage

Percentage of statements that current test cases cover

Since R2023b

Description

A statement is a valid line of code according to the C/C++ standard.

This metric indicates the percentage of source code statements that execute in the current test cases. For instance, a value of 50% indicates that the current test cases invoke only half of the statements in your code at least once. To increase statement coverage, add test cases that can execute the untested statements.

Polyspace Implementation

Polyspace® counts the number of valid C or C++ statements in your code (n_total). Then, when running a test, Polyspace counts which lines are executed (n_exec) and computes the statement coverage as:

Statement coverage = (n_exec / n_total) *100
For instance, consider this code:
#include<stdio.h>
int foo(int x)  //1
{
    if (x > 0)  //2
        return -1;  //3
    else if (x < 0)  //4
        return 1;  //5
    else
        return 0;  //6
} 
Polyspace counts six statements in this code. Polyspace counts how many of these statements a test case executes before the function returns a value or terminates operation. If you test foo() with x == -1, lines 1,2,4, and 5 are executed, resulting in a statement coverage of 4/6×100 or 67%.

Examples

expand all

Consider the function foo(), which contains six statements, including one function entry and five executable statements.

#include<stdio.h>
int foo(int x)
{
    if (x > 0)
        return -1;
    else if (x < 0)
        return 1;
    else
        return 0;
}

Consider these test cases.

Test Case 1Test Case 2

  • Input: Parameter value = -1

  • Assessment: Return value = 1

  • Coverage: (4/6)×100 or 67%

  • Coverage Details: This test case executes four out of the possible six statements.

    #include<stdio.h>
    int foo(int x)  //Covered
    {
        if (x > 0)  //Covered
            return -1;
        else if (x < 0)  //Covered
            return 1;  //Covered
        else
            return 0;
    } 

  • Input: Parameter value = 1

  • Assessment: Return value = -1

  • Coverage: (3/6)×100 or 50%

  • Coverage Details: This test case executes three out of the possible six statements.

    #include<stdio.h>
    int foo(int x)  //Covered
    {
        if (x > 0)  //Covered
            return -1;  //Covered
        else if (x < 0)
            return 1;
        else
            return 0;
    } 

The two preceding tests, in combination, execute five of the six statements in foo():

#include<stdio.h>
int foo(int x)  //Covered
{
    if (x > 0)  //Covered
        return -1;  //Covered
    else if (x < 0)  //Covered
        return 1;  //Covered
    else  return 0;   //Not covered
}
The combined statement coverage of the preceding two tests is 83%. To test all statements in foo() , you require an additional test case that can execute the else branch.

Correction — Add Test Case to Obtain Complete Coverage

The preceding two test cases cannot cover the else statement because their input values match the other branches of the if-else statement. To obtain 100% statement coverage, add a third test case:

  • Input — Parameter value = 0

  • Assessment — Return value = 0

  • Coverage — (4/6)×100 or 67%

  • Coverage Details — This test case executes four out of the possible six statements, including the previously uncovered else statement:

    #include<stdio.h>
    int foo(int x)  //Covered
    {
        if (x > 0)  //Covered
            return -1;
        else if (x < 0)  //Covered
            return 1;
        else  return 0;  //Covered,termination point
    } 

All statements of the function execute at least once during the three tests, resulting in 100% statement coverage.

Version History

Introduced in R2023b