主要内容

Function Exit Coverage

Percentage of function exit points that current test cases reach

Since R2023b

Description

A function exit point is any statement that pushes the control flow out of the function. For instance, a return statement is an exit point.

This metric indicates how many of the function exit points in your code you reach during testing. For instance, if the function exit coverage of your code is 50%, then the current test cases do not reach half of the function exit points in your code. To increase function exit coverage, add test cases that can reach the untested exit points.

Note that in the Polyspace Platform user interface, you do not see function exit coverage in a separate column on the Results List pane. If you select a function name on this pane, the Result Details pane shows both function coverage and function exit coverage.

Polyspace Implementation

To calculate the function exit coverage, Polyspace® counts the number of function exit points (FX_x) the test cases reach during execution and the total number of function exit points (FX_tot):

Function Exit Coverage = 100*FX_x/FX_tot
Consider this code:
int foo(int x)
{
    int y = (x >= 5 && x != 7); 
    if (x < 0)
        return 1; //Exit point 1
    else if (x > 0 && y)
        return 2;//Exit point 2
    else
        return -1;//Exit point 3
}
In this function, there are three exit points. During a test, Polyspace counts how many of exit points are reached. If you test foo() with x== -1, then the test case reaches first exit point after the if(x<0) statement, resulting in FX_x = 1 and a function exit coverage of 33%.

Examples

expand all

Consider the function foo(), which contains three function exit points.

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: (1/3)×100 or 33%

  • Coverage Details: This test case evaluates the return statement return 1;.

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

  • Assessment: Return value = -1

  • Coverage: (1/3)×100 or 33%

  • Coverage Details: This test case evaluates the return statement return -1;.

The two preceding test cases reach two of three function exit points in foo(). To reach all exit points in foo(), add a new test case that can reach return 2;.

Correction — Add Test Case to Complete Function Exit Coverage

To obtain 100% function exit coverage, add a third test case:

  • Input — Parameter value = (2,0)

  • Assessment — Return value = 2

  • Coverage — (1/3)×100 or 33%

  • Coverage Details — This test case evaluates the return statement return 2;.

With the addition of this test case, the test cases reach all exit points and function exit coverage reaches 100%.

Version History

Introduced in R2023b