Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
31 views

Experiment - 9 Control Flow Graph

Uploaded by

Ridhi Garg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Experiment - 9 Control Flow Graph

Uploaded by

Ridhi Garg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment - 9

Aim: Estimation of Test Coverage Metrics and Structural


Complexity
A control flow graph (CFG) is a directed graph where the nodes represent
different instructions of a program, and the edges define the sequence of
execution of such instructions. In other words, a control flow graph describes
how the control flows through the program.
● In order to draw the control flow graph of a program, all the statements of
a program must be numbered first.
● The different numbered statements serve as nodes of the control flow
graph.
● An edge from one node to another node exists if the execution of the
statement representing the first node can result in the transfer of control
to the other node.
In software testing, analyzing the control flow graph (CFG) paths is crucial to
ensure comprehensive test coverage. A linearly independent path is a path in the
CFG that includes at least one unique edge not present in any previously
identified paths. Testing all linearly independent paths is necessary as they
represent the different ways a program can execute under various conditions.
McCabe's Cyclomatic Complexity is a common metric used to measure
program complexity. It provides an upper limit on the number of linearly
independent paths in a program module. A higher cyclomatic complexity
indicates a more complex module, which can make testing, understanding, and
maintenance more challenging. Cyclomatic complexity can be calculated using
different methods.
1. One method uses the formula V(G) = E - N + 2, where E is the number of
edges, and N is the number of nodes in the CFG.
2. Another method involves counting the number of bounded regions in the
CFG and adding one.
The ideal range for cyclomatic complexity is considered to be between 1 and
10. Values above 10 suggest that a module is overly complex and may need to
be redesigned, as high complexity can make the module harder to maintain and
understand.
Cyclomatic Complexity Calculation for Health Management
System

Step 1: Identify Cyclomatic Complexity


Cyclomatic complexity (V(G)) is calculated using the formula:
V(G)=E−N+2P
where:
 E = Number of edges (transitions between nodes).
 N = Number of nodes.
 P = Number of connected components (usually 1 for a single flow).
Nodes:
 Start, Login, Select Doctor, Book Appointment, Payment, Verify
Payment, Pay Again, Generate Bill, End
Total Nodes (N) = 9
Edges:
 Connections: Start → Login, Login → Select Doctor, Select Doctor →
Book Appointment, Book Appointment → Payment, Payment → Verify
Payment, Verify Payment → (Pay Again or Generate Bill), Generate Bill
→ End
Total Edges (E) = 9

Connected Components (P): 1


Calculate V(G)
Using the formula:
V(G)=E−N+2P=9−9+2=2

Q1. Calculate the Cyclomatic Complexity


IF A = 354
THEN IF B > C
THEN A = B
ELSE A = C
END IF
END IF
PRINT A

Method 1:
N = 7, E = 8
Cyclomatic Complexity (V) = E - N + 2
V=E−N+2
8−7+2=3
Method 2:
V = Number of bounded areas + 1
There are 2 bounded areas:
1. The IF A = 354 block.
2. The IF B > C block inside it.
V= 2+1 = 3

Q2. Calculate the Cyclomatic Complexity


begin int x, y, power;
float z;
input (x, y);
if(y<0)
power = -y;
else power = y;
z=1;
while (power! =0)
{z=z*x;
power=power-1;
} if(y<0)
z=1/z;
output(z);
end

Method 1:
N = 10, E = 16
Cyclomatic Complexity (V) = E - N + 2
V=16−10+2=8
Method 2:
V = Number of Bounded Areas + 1
Bounded Areas:
1. if (y < 0) block
2. while (power! = 0) block
3. Inner if (y < 0) block
V=3+1=4

You might also like