主要内容

Function Coverage

Percentage of unique functions that current test cases execute at least once

Since R2023b

Description

This metric indicates how many unique functions in your code the current test cases execute at least once. For instance, if the function coverage of your code is 50%, then the test cases do not execute half of the functions in your code. To increase function coverage, add test cases that can execute the untested functions.

Polyspace Implementation

To calculate the function coverage, Polyspace® counts the number of functions that execute during a test case (func_x) and the number of functions that are in the scope of the test cases (func_tot):

Function Coverage = 100*func_x/func_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 this code, there are three functions. For a test case, Polyspace counts how many of the functions execute. If you test foo() with (x== -1,y==1), then the functions func1() and foo() execute, resulting in func_x = 2 and a function coverage of (2/3) or 67%.

Examples

expand all

Consider this code, which contains four functions.

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: (2/4)×100 or 50%

  • Coverage Details: This test case executes the functions func1() and foo().

  • Input: Parameter value = (1,0)

  • Assessment: Return value = 2

  • Coverage: (2/4)×100 or 50%

  • Coverage Details: This test case executes the functions func2() and foo().

The two preceding test cases execute three out of the four functions in the code. To reach all function calls, add a test that executes func3().

Correction — Add Test to Complete Function Coverage

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

  • Input — Parameter value = (0,2)

  • Assessment — Return value = -1

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

  • Coverage Details — This test case executes the functions func3() and foo().

With the addition of this test case, the test cases execute all functions and function coverage reaches 100%.

Version History

Introduced in R2023b