SlideShare a Scribd company logo
Introduction to Software
Testing
Chapter 7.3
Graph Coverage for Source
Code
Paul Ammann & Jeff Offutt
http://www.cs.gmu.edu/~offutt/softwaretest/
Update March 2016
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 2
Overview
• A common application of graph criteria is to program
source
• Graph : Usually the control flow graph (CFG)
• Node coverage : Execute every statement
• Edge coverage : Execute every branch
• Loops : Looping structures such as for loops, while loops,
etc.
• Data flow coverage :Augment the CFG
– defs are statements that assign values to variables
– uses are statements that use variables
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 3
Control Flow Graphs
• A CFG models all executions of a method by describing
control structures
• Nodes : Statements or sequences of statements (basic
blocks)
• Edges :Transfers of control
• Basic Block :A sequence of statements such that if the first
statement is executed, all statements will be (no branches)
• CFGs are sometimes annotated with extra information
– branch predicates
– defs
– uses
• Rules for translating statements into graphs …
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 4
CFG : The if Statement
if (x < y)
{
y = 0;
x = x + 1;
}
else
{
x = y;
}
4
1
2 3
x >= y
x < y
x = y
y = 0
x = x + 1
if (x < y)
{
y = 0;
x = x + 1;
}
3
1
2
x >= y
x < y
y = 0
x = x + 1
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 5
CFG : The if-Return Statement
if (x < y)
{
return;
}
print (x);
return;
3
1
2
x >= y
x < y
return
print (x)
return
No edge from node 2 to 3.
The return nodes must be distinct.
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 6
Loops
• Loops require “extra” nodes to be added
• Nodes that do not represent statements or basic blocks
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 7
CFG : while and for Loops
x = 0;
while (x < y)
{
y = f (x, y);
x = x + 1;
}
return (x);
1
x = 0
4
3
y =f(x,y)
x = x + 1
x >= y
x < y
for (x = 0; x < y; x++)
{
y = f (x, y);
}
return (x);
1
x = x +
1
2
3 5
x >= y
x < y
y = f (x, y)
4
2
dummy node
x = 0
implicitly
initializes loop
implicitly
increments loop
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 8
CFG : do Loop, break and continue
x = 0;
do
{
y = f (x, y);
x = x + 1;
} while (x < y);
return (y);
1
x = 0
3
2
x >= y
x < y
y = f (x, y)
x = x+1
1 x = 0
8
3
x = x + 1
break
y < 0
2
4
5
6
7
y =f(x,y)
y == 0
y = y*2
continue
x = 0;
while (x < y)
{
y = f (x, y);
if (y == 0)
{
break;
} else if (y < 0)
{
y = y*2;
continue;
}
x = x + 1;
}
return (y);
return (y)
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 9
CFG : The case (switch) Structure
read ( c) ;
switch ( c )
{
case ‘N’:
z = 25;
case ‘Y’:
x = 50;
break;
default:
x = 0;
break;
}
print (x);
5
1 read ( c );
c == ‘N’
x = 0;
break;
2 4
3
c == ‘Y’ default
x = 50;
break;
z = 25;
print (x);
Cases without breaks fall
through to the next case
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 10
CFG : Exceptions (try-catch)
1 s = br.readLine()
8
try
{
s = br.readLine();
if (s.length() > 96)
throw new Exception
(“too long”);
if (s.length() == 0)
throw new Exception
(“too short”);
} (catch IOException e) {
e.printStackTrace();
} (catch Exception e) {
e.getMessage();
}
return (s);
2 3
4 5
6
7
IOException
e.printStackTrace(
)
length > 96
length <= 96
return (s)
throw
length == 0
length != 0
throw
e.getMessage()
© Ammann & Offutt
Example Control Flow – Stats
public static void computeStats (int [ ] numbers)
{
int length = numbers.length;
double med, var, sd, mean, sum, varsum;
sum = 0;
for (int i = 0; i < length; i++)
{
sum += numbers [ i ];
}
med = numbers [ length / 2];
mean = sum / (double) length;
varsum = 0;
for (int i = 0; i < length; i++)
{
varsum = varsum + ((numbers [ i ] - mean) * (numbers [ i ] - mean));
}
var = varsum / ( length - 1.0 );
sd = Math.sqrt ( var );
System.out.println ("length: " + length);
System.out.println ("mean: " + mean);
System.out.println ("median: " + med);
System.out.println ("variance: " + var);
System.out.println ("standard deviation: " + sd);
}
Introduction to Software Testing, Edition 2 (Ch 7) 11
© Ammann & Offutt
© Ammann & Offutt
Control Flow Graph for Stats
public static void computeStats (int [ ] numbers)
{
int length = numbers.length;
double med, var, sd, mean, sum, varsum;
sum = 0;
for (int i = 0; i < length; i++)
{
sum += numbers [ i ];
}
med = numbers [ length / 2];
mean = sum / (double) length;
varsum = 0;
for (int i = 0; i < length; i++)
{
varsum = varsum + ((numbers [ I ] - mean) * (numbers [ I ] - mean));
}
var = varsum / ( length - 1.0 );
sd = Math.sqrt ( var );
System.out.println ("length: " + length);
System.out.println ("mean: " + mean);
System.out.println ("median: " + med);
System.out.println ("variance: " + var);
System.out.println ("standard deviation: " + sd);
}
i = 0
i >=
length
i < length
i++
i >= length
i < length
i = 0
i++
1
2
3
5
4
6
8
7
Introduction to Software Testing, Edition 2 (Ch 7) 12
© Ammann & Offutt
Control Flow TRs and Test Paths—EC
1
2
3
5
4
6
8
7
TR Test Path
Edge Coverage
Introduction to Software Testing, Edition 2 (Ch 7) 13
© Ammann & Offutt
A. [ 1, 2 ]
B. [ 2, 3 ]
C. [ 3, 4 ]
D. [ 3, 5 ]
E. [ 4, 3 ]
F. [ 5, 6 ]
G. [ 6, 7 ]
H. [ 6, 8 ]
I. [ 7, 6 ]
[ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ]
i A, B, D, E, F, G, I, J C, H
Control Flow TRs and Test Paths—EPC
1
2
3
5
4
6
8
7
TR Test Paths
Edge-Pair Coverage
TP TRs toured sidetrips
ii A, C, E, H
iii A, B, D, E, F, G, I, J, K,
L
C, H
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 14
A. [ 1, 2, 3 ]
B. [ 2, 3, 4 ]
C. [ 2, 3, 5 ]
D. [ 3, 4, 3 ]
E. [ 3, 5, 6 ]
F. [ 4, 3, 5 ]
G. [ 5, 6, 7 ]
H. [ 5, 6, 8 ]
I. [ 6, 7, 6 ]
J. [ 7, 6, 8 ]
K. [ 4, 3, 4 ]
L. [ 7, 6, 7 ]
i. [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ]
ii. [ 1, 2, 3, 5, 6, 8 ]
iii. [ 1, 2, 3, 4, 3, 4, 3, 5, 6, 7,
6, 7, 6, 8 ]
TP iii makesTP i
redundant. A minimal
set ofTPs is cheaper.
Control Flow TRs and Test Paths—PPC
1
2
3
5
4
6
8
7
TR Test Paths
Prime Path Coverage
i A, D, E, F, G H, I, J
TP TRs toured sidetrips
ii A, B, C, D, E, F, G, H, I, J
iii A, F, H J
iv D, E, F, I J
v J
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 15
A. [ 3, 4, 3 ]
B. [ 4, 3, 4 ]
C. [ 7, 6, 7 ]
D. [ 7, 6, 8 ]
E. [ 6, 7, 6 ]
F. [ 1, 2, 3, 4 ]
G. [ 4, 3, 5, 6, 7 ]
H. [ 4, 3, 5, 6, 8 ]
I. [ 1, 2, 3, 5, 6, 7 ]
J. [ 1, 2, 3, 5, 6, 8 ]
i. [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ]
ii. [ 1, 2, 3, 4, 3, 4, 3,
5, 6, 7, 6, 7, 6, 8 ]
iii. [ 1, 2, 3, 4, 3, 5, 6, 8 ]
iv. [ 1, 2, 3, 5, 6, 7, 6, 8 ]
v. [ 1, 2, 3, 5, 6, 8 ]
TP ii makes
TP i redundant.
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 16
Data Flow Coverage for Source
• def : a location where a value is stored into memory
– x appears on the left side of an assignment (x = 44;)
– x is an actual parameter in a call and the method changes its
value
– x is a formal parameter of a method (implicit def when method
starts)
– x is an input to a program
• use : a location where variable’s value is accessed
– x appears on the right side of an assignment
– x appears in a conditional test
– x is an actual parameter to a method
– x is an output of the program
– x is an output of a method in a return statement
• If a def and a use appear on the same node, then it is only a
DU-pair if the def occurs after the use and the node is in a
loop
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 17
Example Data Flow – Stats
public static void computeStats (int [ ] numbers)
{
int length = numbers.length;
double med, var, sd, mean, sum, varsum;
sum = 0.0;
for (int i = 0; i < length; i++)
{
sum += numbers [ i ];
}
med = numbers [ length / 2 ];
mean = sum / (double) length;
varsum = 0.o;
for (int i = 0; i < length; i++)
{
varsum = varsum + ((numbers [ i ] - mean) * (numbers [ i ] - mean));
}
var = varsum / ( length - 1 );
sd = Math.sqrt ( var );
System.out.println ("length: " + length);
System.out.println ("mean: " + mean);
System.out.println ("median: " + med);
System.out.println ("variance: " + var);
System.out.println ("standard deviation: " + sd);
}
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 18
8
1
2
4
3
5
6
7
Control Flow Graph for Stats
( numbers )
sum = 0
length = numbers.length
i = 0
i >= length
i < length
sum += numbers
[ i ]
i++
med = numbers [ length / 2 ]
mean = sum / (double)
length
varsum = 0
i = 0
i >= length
i < length
varsum = …
i++
var = varsum / ( length - 1.0 )
sd = Math.sqrt ( var )
print (length, mean, med, var,
sd)
Annotate with the
statements …
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 19
8
1
2
4
3
5
6
7
CFG for Stats – With Defs & Uses
def (1) = { numbers, sum, length }
def (2) = { i }
def (5) = { med, mean, varsum, i }
use (5) = { numbers, length, sum }
def (8) = { var, sd }
use (8) = { varsum, length, mean,
med, var, sd }
use (3, 5) = { i, length }
use (3, 4) = { i, length }
def (4) = { sum, i }
use (4) = { sum, numbers, i }
use (6, 8) = { i, length }
use (6, 7) = { i, length }
def (7) = { varsum, i }
use (7) = { varsum, numbers, i, mean }
Turn the
annotations into
def and use sets …
use (1) = { numbers}
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 20
Defs and Uses Tables for Stats
Node Def Use
1 { numbers, sum,
length }
{ numbers }
2 { i }
3
4 { sum, i } { numbers, i, sum }
5 { med, mean,
varsum, i }
{ numbers, length, sum }
6
7 { varsum, i } { varsum, numbers, i,
mean }
8 { var, sd } { varsum, length, var,
mean, med, var, sd }
Edge Use
(1, 2)
(2, 3)
(3, 4) { i, length }
(4, 3)
(3, 5) { i, length }
(5, 6)
(6, 7) { i, length }
(7, 6)
(6, 8) { i, length }
DU Pairs for Stats
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 21
variable DU Pairs
numbers (1, 4) (1, 5) (1, 7)
length (1, 5) (1, 8) (1, (3,4)) (1, (3,5)) (1, (6,7)) (1, (6,8))
med (5, 8)
var (8, 8)
sd (8, 8)
mean (5, 7) (5, 8)
sum (1, 4) (1, 5) (4, 4) (4, 5)
varsum (5, 7) (5, 8) (7, 7) (7, 8)
i (2, 4) (2, (3,4)) (2, (3,5)) (2, 7) (2, (6,7)) (2, (6,8))
(4, 4) (4, (3,4)) (4, (3,5)) (4, 7) (4, (6,7)) (4, (6,8))
(5, 7) (5, (6,7)) (5, (6,8))
(7, 7) (7, (6,7)) (7, (6,8))
No def-clear path …
different scope for i
No path through graph
from nodes 5 and 7 to 4 or 3
defs come before uses,
do not count as DU pairs
defs after use in loop,
these are valid DU pairs
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 22
DU Paths for Stats
variable DU Pairs DU Paths
numbers (1, 4)
(1, 5)
(1, 7)
[ 1, 2, 3, 4 ]
[ 1, 2, 3, 5 ]
[ 1, 2, 3, 5, 6, 7 ]
length (1, 5)
(1, 8)
(1, (3,4))
(1, (3,5))
(1, (6,7))
(1, (6,8))
[ 1, 2, 3, 5 ]
[ 1, 2, 3, 5, 6, 8 ]
[ 1, 2, 3, 4 ]
[ 1, 2, 3, 5 ]
[ 1, 2, 3, 5, 6, 7 ]
[ 1, 2, 3, 5, 6, 8 ]
med (5, 8) [ 5, 6, 8 ]
var (8, 8) No path needed
sd (8, 8) No path needed
sum (1, 4)
(1, 5)
(4, 4)
(4, 5)
[ 1, 2, 3, 4 ]
[ 1, 2, 3, 5 ]
[ 4, 3, 4 ]
[ 4, 3, 5 ]
variable DU Pairs DU Paths
mean (5, 7)
(5, 8)
[ 5, 6, 7 ]
[ 5, 6, 8 ]
varsum (5, 7)
(5, 8)
(7, 7)
(7, 8)
[ 5, 6, 7 ]
[ 5, 6, 8 ]
[ 7, 6, 7 ]
[ 7, 6, 8 ]
i (2, 4)
(2, (3,4))
(2, (3,5))
(4, 4)
(4, (3,4))
(4, (3,5))
(5, 7)
(5, (6,7))
(5, (6,8))
(7, 7)
(7, (6,7))
(7, (6,8))
[ 2, 3, 4 ]
[ 2, 3, 4 ]
[ 2, 3, 5 ]
[ 4, 3, 4 ]
[ 4, 3, 4 ]
[ 4, 3, 5 ]
[ 5, 6, 7 ]
[ 5, 6, 7 ]
[ 5, 6, 8 ]
[ 7, 6, 7 ]
[ 7, 6, 7 ]
[ 7, 6, 8 ]
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 23
DU Paths for Stats—No Duplicates
There are 38 DU paths for Stats, but only 12 unique
[ 1, 2, 3, 4 ]
[ 1, 2, 3, 5 ]
[ 1, 2, 3, 5, 6, 7 ]
[ 1, 2, 3, 5, 6, 8 ]
[ 2, 3, 4 ]
[ 2, 3, 5 ]
[ 4, 3, 4 ]
[ 4, 3, 5 ]
[ 5, 6, 7 ]
[ 5, 6, 8 ]
[ 7, 6, 7 ]
[ 7, 6, 8 ]
4 expect a loop not to be “entered”
2 require at least two iterations of a loop
6 require at least one iteration of a loop
Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 24
Test Cases and Test Paths
Test Case : numbers = (44) ; length = 1
Test Path : [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ]
Additional DU Paths covered (no sidetrips)
[ 1, 2, 3, 4 ] [ 2, 3, 4 ] [ 4, 3, 5 ] [ 5, 6, 7 ] [ 7, 6, 8 ]
The five stars that require at least one iteration of a loop
Test Case : numbers = (2, 10, 15) ; length = 3
Test Path : [ 1, 2, 3, 4, 3, 4, 3, 4, 3, 5, 6, 7, 6, 7, 6, 7, 6, 8 ]
DU Paths covered (no sidetrips)
[ 4, 3, 4 ] [ 7, 6, 7 ]
The two stars that require at least two iterations of a loop
Other DU paths require arrays with length 0 to skip loops
But the method fails with index out of bounds exception…
med = numbers [length / 2]; A fault was
found
Summary
• Applying the graph test criteria to control flow graphs is
relatively straightforward
– Most of the developmental research work was done with CFGs
• A few subtle decisions must be made to translate control
structures into the graph
• Some tools will assign each statement to a unique node
– These slides and the book uses basic blocks
– Coverage is the same, although the bookkeeping will differ
Introduction to Software Testing, Edition 2 (Ch 7) 25
© Ammann & Offutt

More Related Content

Similar to Ch07-3-sourceCode.pptxhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh (20)

PDF
ST Module 3 vtu prescribed syllabus and scheme
Shivakumar M
 
PPT
Path testing, data flow testing
priyasoundar
 
PDF
Test Automation Day 2018
Maurício Aniche
 
PPT
11 whiteboxtesting
asifusman1998
 
PPTX
Building Test Cases and Plans.pptx in computer science
AmitSingh395981
 
PPTX
SE%200-Testing%20(2).pptx
200723KarthikeyanD
 
PDF
Testing computer software
Jose Vergara Veas
 
PPTX
Software quality assurance,eTesting.pptx
singbling
 
PDF
White Box Testing (Introduction to)
Henry Muccini
 
DOC
White box-sol
Nguyen Duc Nhieu
 
PPT
2.2_Dynamic Testing: White Box Testing Techniques.ppt
tecaviw979
 
PPTX
Structural Testing: When Quality Really Matters
TechWell
 
PPTX
Test Coverage: An Art and a Science
TeamQualityPro
 
PPT
testing(2).pptjjsieieo2i33kejjskskosowwiwk
mhuzaifasultan8
 
PPT
white box testing.ppt
VISHNUSHANKARSINGH3
 
PDF
Introduction to Software Testing.pdf
MirzaNazimuddin
 
PPTX
seng301-10-testing-breaking-code.pptx
APuffinLekvr
 
PPT
Testing
nazeer pasha
 
PDF
Lecture 06 - 07 - 08 - Test Techniques - Whitebox Testing.pdf
mkhawar5
 
PDF
Code Coverage [9] - Software Testing Techniques (CIS640)
Venkatesh Prasad Ranganath
 
ST Module 3 vtu prescribed syllabus and scheme
Shivakumar M
 
Path testing, data flow testing
priyasoundar
 
Test Automation Day 2018
Maurício Aniche
 
11 whiteboxtesting
asifusman1998
 
Building Test Cases and Plans.pptx in computer science
AmitSingh395981
 
SE%200-Testing%20(2).pptx
200723KarthikeyanD
 
Testing computer software
Jose Vergara Veas
 
Software quality assurance,eTesting.pptx
singbling
 
White Box Testing (Introduction to)
Henry Muccini
 
White box-sol
Nguyen Duc Nhieu
 
2.2_Dynamic Testing: White Box Testing Techniques.ppt
tecaviw979
 
Structural Testing: When Quality Really Matters
TechWell
 
Test Coverage: An Art and a Science
TeamQualityPro
 
testing(2).pptjjsieieo2i33kejjskskosowwiwk
mhuzaifasultan8
 
white box testing.ppt
VISHNUSHANKARSINGH3
 
Introduction to Software Testing.pdf
MirzaNazimuddin
 
seng301-10-testing-breaking-code.pptx
APuffinLekvr
 
Testing
nazeer pasha
 
Lecture 06 - 07 - 08 - Test Techniques - Whitebox Testing.pdf
mkhawar5
 
Code Coverage [9] - Software Testing Techniques (CIS640)
Venkatesh Prasad Ranganath
 

More from VaibhavSrivastav52 (18)

PPTX
PPT-3.pptxppppppppppppppppppppppppppppppppppppppppp
VaibhavSrivastav52
 
PPTX
PPT-1.pptxpppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
VaibhavSrivastav52
 
PPTX
PPT-2.pptxpppppppppppppppppppppppppppppppppppp
VaibhavSrivastav52
 
PPTX
S-VYASA STQA-1.pptx00000000000000000000000000000000
VaibhavSrivastav52
 
PPTX
S-VYASA dbms111111111111111111111111111111111111111
VaibhavSrivastav52
 
PPTX
rdbms1-191014080818000000000000000000000000000000000
VaibhavSrivastav52
 
PPTX
rdbms3, dbms,dbms,rdbmssssssssssssssssssssssssssssssssss
VaibhavSrivastav52
 
PPTX
Ch09-4-modelBased.pptxhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
VaibhavSrivastav52
 
PPTX
Ch09-5-inputs.pptx ccccccccccccccccccccccccccccccccccccccccc
VaibhavSrivastav52
 
PPTX
advanced database management system by uni
VaibhavSrivastav52
 
PPTX
advanced database management system by uni
VaibhavSrivastav52
 
PPTX
python_module_4....................................
VaibhavSrivastav52
 
PPTX
python_module_3....................................
VaibhavSrivastav52
 
PPTX
python_module_........................................
VaibhavSrivastav52
 
PPTX
python_module_.................................................................
VaibhavSrivastav52
 
PPTX
rdbms parul university oracle dbms bca mca
VaibhavSrivastav52
 
PPTX
RDBMS PARUL UNIVERSITY VADODARA BTECH CSE
VaibhavSrivastav52
 
PPTX
dbms ppt parul university dbms course for
VaibhavSrivastav52
 
PPT-3.pptxppppppppppppppppppppppppppppppppppppppppp
VaibhavSrivastav52
 
PPT-1.pptxpppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
VaibhavSrivastav52
 
PPT-2.pptxpppppppppppppppppppppppppppppppppppp
VaibhavSrivastav52
 
S-VYASA STQA-1.pptx00000000000000000000000000000000
VaibhavSrivastav52
 
S-VYASA dbms111111111111111111111111111111111111111
VaibhavSrivastav52
 
rdbms1-191014080818000000000000000000000000000000000
VaibhavSrivastav52
 
rdbms3, dbms,dbms,rdbmssssssssssssssssssssssssssssssssss
VaibhavSrivastav52
 
Ch09-4-modelBased.pptxhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
VaibhavSrivastav52
 
Ch09-5-inputs.pptx ccccccccccccccccccccccccccccccccccccccccc
VaibhavSrivastav52
 
advanced database management system by uni
VaibhavSrivastav52
 
advanced database management system by uni
VaibhavSrivastav52
 
python_module_4....................................
VaibhavSrivastav52
 
python_module_3....................................
VaibhavSrivastav52
 
python_module_........................................
VaibhavSrivastav52
 
python_module_.................................................................
VaibhavSrivastav52
 
rdbms parul university oracle dbms bca mca
VaibhavSrivastav52
 
RDBMS PARUL UNIVERSITY VADODARA BTECH CSE
VaibhavSrivastav52
 
dbms ppt parul university dbms course for
VaibhavSrivastav52
 
Ad

Recently uploaded (20)

PPTX
1155 Teri Okoro.pptx APM People Interest Network Presents: A People-Centric A...
Association for Project Management
 
PDF
Job Card - Developer.pdf is for the a new srping platform
sashko9
 
DOCX
The Overlooked Impact of Growth Regulators on Fruit Quality.docx
jaydas201616
 
DOCX
Precision-Engineered Stainless Steel Tank for Biogas Fermentation Systems.docx
AllenLin596164
 
PPTX
一比一原版(UEM毕业证)马德里欧洲大学毕业证如何办理
Taqyea
 
PPTX
ATLANTIC ACTION MODE PRESENTATION FOR BEGINER
MrchemMasterMindtric
 
PPTX
一比一原版(Brown毕业证书)布朗大学毕业证如何办理
Taqyea
 
PPTX
Role of bacteria in wastewater treatment, an overview.pptx
Rajesh Kumar Dudi
 
PDF
Day Porter Cleaning Service for Your Business Needs
anchormpl1
 
PPTX
43_Sustainable Development Goals_GYAN-LIS-CLASSES
devkumar940802
 
PPTX
GEOGRAPHY PROJECT INDUSTRIES 2 25 06 2025
balar3168
 
PPTX
People, Development and Environment.pptx
Anandakumar Natarajan
 
PPTX
1155 Helen Clayton_Tasha Clark - ESG Slides - Final - APM Conference 25.pptx
Association for Project Management
 
DOCX
Ensuring Product Purity Stainless Steel Storage Tanks for Food Processing Pla...
AllenLin596164
 
PPTX
Biological composting.pptx (principle, benefits and applications)
murtazashehabi0
 
DOCX
Highly Corrosion-Resistant Stainless Steel Tanks for Chemical Solutions Stora...
AllenLin596164
 
PDF
8448-Article Text-40833-5-10-20250227.pdf
chudunguyen
 
PDF
Oscar Elizondo_ Building Lasting Impact through Sustainable Community Outreac...
Oscar Elizondo
 
PPTX
El dilemma zuhey. dfsdfdsfsdfdfsdfsdfpptx
blackfire25
 
PDF
2021 Population and Housing Census - SLUM REPORT
Kweku Zurek
 
1155 Teri Okoro.pptx APM People Interest Network Presents: A People-Centric A...
Association for Project Management
 
Job Card - Developer.pdf is for the a new srping platform
sashko9
 
The Overlooked Impact of Growth Regulators on Fruit Quality.docx
jaydas201616
 
Precision-Engineered Stainless Steel Tank for Biogas Fermentation Systems.docx
AllenLin596164
 
一比一原版(UEM毕业证)马德里欧洲大学毕业证如何办理
Taqyea
 
ATLANTIC ACTION MODE PRESENTATION FOR BEGINER
MrchemMasterMindtric
 
一比一原版(Brown毕业证书)布朗大学毕业证如何办理
Taqyea
 
Role of bacteria in wastewater treatment, an overview.pptx
Rajesh Kumar Dudi
 
Day Porter Cleaning Service for Your Business Needs
anchormpl1
 
43_Sustainable Development Goals_GYAN-LIS-CLASSES
devkumar940802
 
GEOGRAPHY PROJECT INDUSTRIES 2 25 06 2025
balar3168
 
People, Development and Environment.pptx
Anandakumar Natarajan
 
1155 Helen Clayton_Tasha Clark - ESG Slides - Final - APM Conference 25.pptx
Association for Project Management
 
Ensuring Product Purity Stainless Steel Storage Tanks for Food Processing Pla...
AllenLin596164
 
Biological composting.pptx (principle, benefits and applications)
murtazashehabi0
 
Highly Corrosion-Resistant Stainless Steel Tanks for Chemical Solutions Stora...
AllenLin596164
 
8448-Article Text-40833-5-10-20250227.pdf
chudunguyen
 
Oscar Elizondo_ Building Lasting Impact through Sustainable Community Outreac...
Oscar Elizondo
 
El dilemma zuhey. dfsdfdsfsdfdfsdfsdfpptx
blackfire25
 
2021 Population and Housing Census - SLUM REPORT
Kweku Zurek
 
Ad

Ch07-3-sourceCode.pptxhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

  • 1. Introduction to Software Testing Chapter 7.3 Graph Coverage for Source Code Paul Ammann & Jeff Offutt http://www.cs.gmu.edu/~offutt/softwaretest/ Update March 2016
  • 2. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 2 Overview • A common application of graph criteria is to program source • Graph : Usually the control flow graph (CFG) • Node coverage : Execute every statement • Edge coverage : Execute every branch • Loops : Looping structures such as for loops, while loops, etc. • Data flow coverage :Augment the CFG – defs are statements that assign values to variables – uses are statements that use variables
  • 3. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 3 Control Flow Graphs • A CFG models all executions of a method by describing control structures • Nodes : Statements or sequences of statements (basic blocks) • Edges :Transfers of control • Basic Block :A sequence of statements such that if the first statement is executed, all statements will be (no branches) • CFGs are sometimes annotated with extra information – branch predicates – defs – uses • Rules for translating statements into graphs …
  • 4. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 4 CFG : The if Statement if (x < y) { y = 0; x = x + 1; } else { x = y; } 4 1 2 3 x >= y x < y x = y y = 0 x = x + 1 if (x < y) { y = 0; x = x + 1; } 3 1 2 x >= y x < y y = 0 x = x + 1
  • 5. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 5 CFG : The if-Return Statement if (x < y) { return; } print (x); return; 3 1 2 x >= y x < y return print (x) return No edge from node 2 to 3. The return nodes must be distinct.
  • 6. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 6 Loops • Loops require “extra” nodes to be added • Nodes that do not represent statements or basic blocks
  • 7. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 7 CFG : while and for Loops x = 0; while (x < y) { y = f (x, y); x = x + 1; } return (x); 1 x = 0 4 3 y =f(x,y) x = x + 1 x >= y x < y for (x = 0; x < y; x++) { y = f (x, y); } return (x); 1 x = x + 1 2 3 5 x >= y x < y y = f (x, y) 4 2 dummy node x = 0 implicitly initializes loop implicitly increments loop
  • 8. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 8 CFG : do Loop, break and continue x = 0; do { y = f (x, y); x = x + 1; } while (x < y); return (y); 1 x = 0 3 2 x >= y x < y y = f (x, y) x = x+1 1 x = 0 8 3 x = x + 1 break y < 0 2 4 5 6 7 y =f(x,y) y == 0 y = y*2 continue x = 0; while (x < y) { y = f (x, y); if (y == 0) { break; } else if (y < 0) { y = y*2; continue; } x = x + 1; } return (y); return (y)
  • 9. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 9 CFG : The case (switch) Structure read ( c) ; switch ( c ) { case ‘N’: z = 25; case ‘Y’: x = 50; break; default: x = 0; break; } print (x); 5 1 read ( c ); c == ‘N’ x = 0; break; 2 4 3 c == ‘Y’ default x = 50; break; z = 25; print (x); Cases without breaks fall through to the next case
  • 10. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 10 CFG : Exceptions (try-catch) 1 s = br.readLine() 8 try { s = br.readLine(); if (s.length() > 96) throw new Exception (“too long”); if (s.length() == 0) throw new Exception (“too short”); } (catch IOException e) { e.printStackTrace(); } (catch Exception e) { e.getMessage(); } return (s); 2 3 4 5 6 7 IOException e.printStackTrace( ) length > 96 length <= 96 return (s) throw length == 0 length != 0 throw e.getMessage()
  • 11. © Ammann & Offutt Example Control Flow – Stats public static void computeStats (int [ ] numbers) { int length = numbers.length; double med, var, sd, mean, sum, varsum; sum = 0; for (int i = 0; i < length; i++) { sum += numbers [ i ]; } med = numbers [ length / 2]; mean = sum / (double) length; varsum = 0; for (int i = 0; i < length; i++) { varsum = varsum + ((numbers [ i ] - mean) * (numbers [ i ] - mean)); } var = varsum / ( length - 1.0 ); sd = Math.sqrt ( var ); System.out.println ("length: " + length); System.out.println ("mean: " + mean); System.out.println ("median: " + med); System.out.println ("variance: " + var); System.out.println ("standard deviation: " + sd); } Introduction to Software Testing, Edition 2 (Ch 7) 11 © Ammann & Offutt
  • 12. © Ammann & Offutt Control Flow Graph for Stats public static void computeStats (int [ ] numbers) { int length = numbers.length; double med, var, sd, mean, sum, varsum; sum = 0; for (int i = 0; i < length; i++) { sum += numbers [ i ]; } med = numbers [ length / 2]; mean = sum / (double) length; varsum = 0; for (int i = 0; i < length; i++) { varsum = varsum + ((numbers [ I ] - mean) * (numbers [ I ] - mean)); } var = varsum / ( length - 1.0 ); sd = Math.sqrt ( var ); System.out.println ("length: " + length); System.out.println ("mean: " + mean); System.out.println ("median: " + med); System.out.println ("variance: " + var); System.out.println ("standard deviation: " + sd); } i = 0 i >= length i < length i++ i >= length i < length i = 0 i++ 1 2 3 5 4 6 8 7 Introduction to Software Testing, Edition 2 (Ch 7) 12 © Ammann & Offutt
  • 13. Control Flow TRs and Test Paths—EC 1 2 3 5 4 6 8 7 TR Test Path Edge Coverage Introduction to Software Testing, Edition 2 (Ch 7) 13 © Ammann & Offutt A. [ 1, 2 ] B. [ 2, 3 ] C. [ 3, 4 ] D. [ 3, 5 ] E. [ 4, 3 ] F. [ 5, 6 ] G. [ 6, 7 ] H. [ 6, 8 ] I. [ 7, 6 ] [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ]
  • 14. i A, B, D, E, F, G, I, J C, H Control Flow TRs and Test Paths—EPC 1 2 3 5 4 6 8 7 TR Test Paths Edge-Pair Coverage TP TRs toured sidetrips ii A, C, E, H iii A, B, D, E, F, G, I, J, K, L C, H Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 14 A. [ 1, 2, 3 ] B. [ 2, 3, 4 ] C. [ 2, 3, 5 ] D. [ 3, 4, 3 ] E. [ 3, 5, 6 ] F. [ 4, 3, 5 ] G. [ 5, 6, 7 ] H. [ 5, 6, 8 ] I. [ 6, 7, 6 ] J. [ 7, 6, 8 ] K. [ 4, 3, 4 ] L. [ 7, 6, 7 ] i. [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ] ii. [ 1, 2, 3, 5, 6, 8 ] iii. [ 1, 2, 3, 4, 3, 4, 3, 5, 6, 7, 6, 7, 6, 8 ] TP iii makesTP i redundant. A minimal set ofTPs is cheaper.
  • 15. Control Flow TRs and Test Paths—PPC 1 2 3 5 4 6 8 7 TR Test Paths Prime Path Coverage i A, D, E, F, G H, I, J TP TRs toured sidetrips ii A, B, C, D, E, F, G, H, I, J iii A, F, H J iv D, E, F, I J v J Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 15 A. [ 3, 4, 3 ] B. [ 4, 3, 4 ] C. [ 7, 6, 7 ] D. [ 7, 6, 8 ] E. [ 6, 7, 6 ] F. [ 1, 2, 3, 4 ] G. [ 4, 3, 5, 6, 7 ] H. [ 4, 3, 5, 6, 8 ] I. [ 1, 2, 3, 5, 6, 7 ] J. [ 1, 2, 3, 5, 6, 8 ] i. [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ] ii. [ 1, 2, 3, 4, 3, 4, 3, 5, 6, 7, 6, 7, 6, 8 ] iii. [ 1, 2, 3, 4, 3, 5, 6, 8 ] iv. [ 1, 2, 3, 5, 6, 7, 6, 8 ] v. [ 1, 2, 3, 5, 6, 8 ] TP ii makes TP i redundant.
  • 16. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 16 Data Flow Coverage for Source • def : a location where a value is stored into memory – x appears on the left side of an assignment (x = 44;) – x is an actual parameter in a call and the method changes its value – x is a formal parameter of a method (implicit def when method starts) – x is an input to a program • use : a location where variable’s value is accessed – x appears on the right side of an assignment – x appears in a conditional test – x is an actual parameter to a method – x is an output of the program – x is an output of a method in a return statement • If a def and a use appear on the same node, then it is only a DU-pair if the def occurs after the use and the node is in a loop
  • 17. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 17 Example Data Flow – Stats public static void computeStats (int [ ] numbers) { int length = numbers.length; double med, var, sd, mean, sum, varsum; sum = 0.0; for (int i = 0; i < length; i++) { sum += numbers [ i ]; } med = numbers [ length / 2 ]; mean = sum / (double) length; varsum = 0.o; for (int i = 0; i < length; i++) { varsum = varsum + ((numbers [ i ] - mean) * (numbers [ i ] - mean)); } var = varsum / ( length - 1 ); sd = Math.sqrt ( var ); System.out.println ("length: " + length); System.out.println ("mean: " + mean); System.out.println ("median: " + med); System.out.println ("variance: " + var); System.out.println ("standard deviation: " + sd); }
  • 18. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 18 8 1 2 4 3 5 6 7 Control Flow Graph for Stats ( numbers ) sum = 0 length = numbers.length i = 0 i >= length i < length sum += numbers [ i ] i++ med = numbers [ length / 2 ] mean = sum / (double) length varsum = 0 i = 0 i >= length i < length varsum = … i++ var = varsum / ( length - 1.0 ) sd = Math.sqrt ( var ) print (length, mean, med, var, sd) Annotate with the statements …
  • 19. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 19 8 1 2 4 3 5 6 7 CFG for Stats – With Defs & Uses def (1) = { numbers, sum, length } def (2) = { i } def (5) = { med, mean, varsum, i } use (5) = { numbers, length, sum } def (8) = { var, sd } use (8) = { varsum, length, mean, med, var, sd } use (3, 5) = { i, length } use (3, 4) = { i, length } def (4) = { sum, i } use (4) = { sum, numbers, i } use (6, 8) = { i, length } use (6, 7) = { i, length } def (7) = { varsum, i } use (7) = { varsum, numbers, i, mean } Turn the annotations into def and use sets … use (1) = { numbers}
  • 20. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 20 Defs and Uses Tables for Stats Node Def Use 1 { numbers, sum, length } { numbers } 2 { i } 3 4 { sum, i } { numbers, i, sum } 5 { med, mean, varsum, i } { numbers, length, sum } 6 7 { varsum, i } { varsum, numbers, i, mean } 8 { var, sd } { varsum, length, var, mean, med, var, sd } Edge Use (1, 2) (2, 3) (3, 4) { i, length } (4, 3) (3, 5) { i, length } (5, 6) (6, 7) { i, length } (7, 6) (6, 8) { i, length }
  • 21. DU Pairs for Stats Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 21 variable DU Pairs numbers (1, 4) (1, 5) (1, 7) length (1, 5) (1, 8) (1, (3,4)) (1, (3,5)) (1, (6,7)) (1, (6,8)) med (5, 8) var (8, 8) sd (8, 8) mean (5, 7) (5, 8) sum (1, 4) (1, 5) (4, 4) (4, 5) varsum (5, 7) (5, 8) (7, 7) (7, 8) i (2, 4) (2, (3,4)) (2, (3,5)) (2, 7) (2, (6,7)) (2, (6,8)) (4, 4) (4, (3,4)) (4, (3,5)) (4, 7) (4, (6,7)) (4, (6,8)) (5, 7) (5, (6,7)) (5, (6,8)) (7, 7) (7, (6,7)) (7, (6,8)) No def-clear path … different scope for i No path through graph from nodes 5 and 7 to 4 or 3 defs come before uses, do not count as DU pairs defs after use in loop, these are valid DU pairs
  • 22. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 22 DU Paths for Stats variable DU Pairs DU Paths numbers (1, 4) (1, 5) (1, 7) [ 1, 2, 3, 4 ] [ 1, 2, 3, 5 ] [ 1, 2, 3, 5, 6, 7 ] length (1, 5) (1, 8) (1, (3,4)) (1, (3,5)) (1, (6,7)) (1, (6,8)) [ 1, 2, 3, 5 ] [ 1, 2, 3, 5, 6, 8 ] [ 1, 2, 3, 4 ] [ 1, 2, 3, 5 ] [ 1, 2, 3, 5, 6, 7 ] [ 1, 2, 3, 5, 6, 8 ] med (5, 8) [ 5, 6, 8 ] var (8, 8) No path needed sd (8, 8) No path needed sum (1, 4) (1, 5) (4, 4) (4, 5) [ 1, 2, 3, 4 ] [ 1, 2, 3, 5 ] [ 4, 3, 4 ] [ 4, 3, 5 ] variable DU Pairs DU Paths mean (5, 7) (5, 8) [ 5, 6, 7 ] [ 5, 6, 8 ] varsum (5, 7) (5, 8) (7, 7) (7, 8) [ 5, 6, 7 ] [ 5, 6, 8 ] [ 7, 6, 7 ] [ 7, 6, 8 ] i (2, 4) (2, (3,4)) (2, (3,5)) (4, 4) (4, (3,4)) (4, (3,5)) (5, 7) (5, (6,7)) (5, (6,8)) (7, 7) (7, (6,7)) (7, (6,8)) [ 2, 3, 4 ] [ 2, 3, 4 ] [ 2, 3, 5 ] [ 4, 3, 4 ] [ 4, 3, 4 ] [ 4, 3, 5 ] [ 5, 6, 7 ] [ 5, 6, 7 ] [ 5, 6, 8 ] [ 7, 6, 7 ] [ 7, 6, 7 ] [ 7, 6, 8 ]
  • 23. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 23 DU Paths for Stats—No Duplicates There are 38 DU paths for Stats, but only 12 unique [ 1, 2, 3, 4 ] [ 1, 2, 3, 5 ] [ 1, 2, 3, 5, 6, 7 ] [ 1, 2, 3, 5, 6, 8 ] [ 2, 3, 4 ] [ 2, 3, 5 ] [ 4, 3, 4 ] [ 4, 3, 5 ] [ 5, 6, 7 ] [ 5, 6, 8 ] [ 7, 6, 7 ] [ 7, 6, 8 ] 4 expect a loop not to be “entered” 2 require at least two iterations of a loop 6 require at least one iteration of a loop
  • 24. Introduction to Software Testing, Edition 2 (Ch 7) © Ammann & Offutt 24 Test Cases and Test Paths Test Case : numbers = (44) ; length = 1 Test Path : [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ] Additional DU Paths covered (no sidetrips) [ 1, 2, 3, 4 ] [ 2, 3, 4 ] [ 4, 3, 5 ] [ 5, 6, 7 ] [ 7, 6, 8 ] The five stars that require at least one iteration of a loop Test Case : numbers = (2, 10, 15) ; length = 3 Test Path : [ 1, 2, 3, 4, 3, 4, 3, 4, 3, 5, 6, 7, 6, 7, 6, 7, 6, 8 ] DU Paths covered (no sidetrips) [ 4, 3, 4 ] [ 7, 6, 7 ] The two stars that require at least two iterations of a loop Other DU paths require arrays with length 0 to skip loops But the method fails with index out of bounds exception… med = numbers [length / 2]; A fault was found
  • 25. Summary • Applying the graph test criteria to control flow graphs is relatively straightforward – Most of the developmental research work was done with CFGs • A few subtle decisions must be made to translate control structures into the graph • Some tools will assign each statement to a unique node – These slides and the book uses basic blocks – Coverage is the same, although the bookkeeping will differ Introduction to Software Testing, Edition 2 (Ch 7) 25 © Ammann & Offutt

Editor's Notes

  • #1: The animation allows the instructor to show a few programming statements, then let the students try to draw the graph. The graphs in the slides are there to verify their answers. If they all read the book and got the basics, you can jump to the Stats example (slide 10). See the notes on slides 10-14.
  • #11: We suggest stopping here and having the students draw the graph themselves. Then show the graph on the next slide to compare their answers.
  • #12: Nodes 1 and 2 could certainly be combined. We just separated them to emphasize two points: Initializations have to be included in the graph. They are also defs in data flow. In Java, primitive types get default values, so even declarations have implicit definitions. 2) The for loop control variable (i) is initialized before the test.
  • #13: The animation shows empty boxes. Students can fill these in, then show the answers. Edge coverage is very easy, of course …
  • #14: We show the empty boxes, let the students write down the TRs and Test Paths, then show the solution. Emphasize that it is VERY EASY to miss one, even if you understand it well.
  • #15: We also pause to let the students finish this example. Students often have trouble remembering all the PPs around loops.
  • #18: The rest of the example is easier to follow if the students can refer to this figure. We usually draw it on the board.