The following function in Figure 2 shows a function to calculate a factorial for any input number:
int factorial(int n) { /* calculate a factorial using a loop. A factorial of a number is the product of the number itself times the factorial of the number minus i; i.e n! = n x (n-1) x ... x 1 */
int f = -1;
if (n < 0) {
cout << "Error, Factorial: Negative argument." << endl;
} else {
f = 1;
for (int i = 2; i <= n; i++) {
f = f * i;
}
}
return f;
}
Figure 2: Fragment of factorial function code
Draw the control flow graph of the code. List down the possible test paths in order to achieve 100% test coverage for statement coverage. (Test path should be written using this format: Sn-Cn-)
List down the possible test paths in order to achieve 100% test coverage for decision coverage. (Test path should be written using this format: Sn-Cn-..)
List down the possible test paths in order to achieve 100% test coverage for branch coverage. (Test path should be written using this format: Sn-Cn-.)
Calculate the Cyclomatic Complexity value for path coverage. You may use any of the cyclomatic metric formulas.