主要内容

Function Call Coverage

Percentage of function calls that current test cases execute at least once

Since R2023b

Description

This metric indicates how many of the function calls in your code are executed by the current test cases. For instance, if the function call coverage of your code is 50%, then the current tests do not execute half the function calls in your code. To increase function call coverage, add test cases that execute the untested function calls.

Polyspace Implementation

To calculate the function call coverage, Polyspace® counts the number of function calls that the test cases execute (call_x) and the number of function calls that are in the scope of the test cases (call_tot):

Function Call Coverage = 100*call_x/call_tot
Consider this code:
int func1(){return 1;}
int func2(){return 2;}

int foo(int x, int y)
{
    if (x < 0 && y>0)
        return func1(); 
    else if (x > 0 && y==0)
        return func2();
    else
        return func1();
}
In the function foo(), there are three function calls. For a test case, Polyspace counts how many of the calls execute. If you test foo() with (x== -1,y==1), then only the call to func1() executes, resulting in call_x = 1 and a function call coverage of 33%.

Polyspace Test™ does not consider calls to class constructors as function calls. The function call coverage metrics exclude calls to class constructors.

Examples

expand all

Consider this code which contains a function with four function calls.

int func1(){return 1;}
int func2(){return 2;}
int func3(){return -1;}

int foo(int x, int y)
{
	if (x < 0 && y>0)
	return func1(); 
	else if (x > 0 && y==0)
	return func2();
	else if (x == 0 && y==2)
	return func3();
	else
	return func1();
}

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 first call to func1().

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

  • Assessment: Return value = 2

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

  • Coverage Details: This test case evaluates the function call func2().

The two preceding test cases execute two of the four function calls in foo(). To test all function calls in foo(), add new test cases that execute the untested function calls.

Correction — Add Test Cases to Complete Function Call Coverage

To obtain 100% function call coverage, add these new test cases.

Test Case 1Test Case 2

  • Input: Parameter values = (0,2)

  • Assessment: Return value = -1

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

  • Coverage Details: This test case evaluates the function call func3().

  • Input: Parameter value = (2,2)

  • Assessment: Return value = 1

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

  • Coverage Details: This test case evaluates the second call to func1().

With the addition of these test cases, all function calls execute and function call coverage reaches 100%.

Version History

Introduced in R2023b