AfterMidSlide2 Graph Coverage
AfterMidSlide2 Graph Coverage
Specs DNF
3
CFG : The if Statement
if (x < y)
{
y = 0; 1
x=x+ x<y x >= y
1; y=0
x=x+ 2 3 x=y
} 1
else
{ 4
x = y;
} 1
if (x < y)
{ x<y
y = 0; y=0 x >= y
x=x+ 2
x=x+ 1
1;
} 3
4
Covering Graphs (7.1)
Graphs are the most commonly used structure for testing
5
Definition of a Graph
Any Graph G(N, N0, Nf, E), can be defined as-
– A set N of nodes, N is not empty
– A set N0 of initial nodes, N0 is not empty and
– A set Nf of final nodes, Nf is not empty and
– A set E of edges, each edge from one node to another ( ni, nj ),
where i is predecessor, j is successor and
N0 = { 1 }
Is this a Yes
Nf = { 1 }
graph? 1
E={}
6
Example Graphs
1 1 2 3 1
Not a
valid
2 3 4 5 6 7 2 graph 3
4 8 9 10 4
N0 = { 1 } N0 = { 1, 2, 3 } N0 = { }
Write down the Write down the Write down the
initial ={4
Nfand }
final 8, 9,
Nf = { and
initial 10 }
final initialNand 4}
f = { final
nodes,
E = {and the
(1,2), nodes,
E = { (1,4), and the
(1,5), (2,5), (3,6), nodes,
E = and the
{ (1,2),
edges
(1,3), (2,4), (3, 7),edges
(4, 8), (5,8), (5,9), edges
(1,3), (2,4),
(3,4) } (6,2), (6,10), (7,10) (9,6) } (3,4) } 7
Paths in Graphs
Path : A sequence of nodes – [n1, n2, …, nM]
– Each pair of nodes is an edge
Length : The number of edges
– A single node is a path of length 0
Subpath : A subsequence of nodes in p is a subpath of p
Reach (n) : Subgraph that can be reached from n
1 2 3
A Few Paths
[ 1, 4, 8 ]
Write down
4 5 6 7 [three
2, 5,paths
9, 6, 2 ]
in
[ 3,this graph
7, 10 ]
8 9 10
8
Test Paths and SESEs
Test Path : A path that starts at an initial node and ends at a
final node
Test paths represent execution of test cases
– Some test paths can be executed by many tests
– Some test paths cannot be executed by any tests
SESE graphs : All test paths start at a single node and end
at another node
– Single-entry, single-exit
– N0 and Nf have exactly one node
Double-diamond graph
2 5 Four test paths
[1, 2, 4, 5, 7]
1 4 7
[1, 2,down
Write 4, 6, all
7]
3 6 [1,test
the 3, 4, 5, 7]
paths
in[1, 3, graph
this 4, 6, 7]
9
Visiting and Touring
Visit : A test path p visits node n if n is in p
A test path p visits edge e if e is in p
Tour : A test path p tours subpath q if q is a subpath of p
Test path [ 1, 2, 4, 5, 7 ]
Visits nodes ? 1, 2, 4, 5, 7
Visits edges ? (1,2), (2,4), (4, 5), (5, 7)
Tours subpaths ? [1,2,4], [2,4,5], [4,5,7], [1,2,4,5],
[2,4,5,7], [1,2,4,5,7]
10
Tests and Test Paths
path (t) : The test path executed by test t
path (T) : The set of test paths executed by the set of tests
T
Each test executes one and only one test path
– Complete execution from a start node to an final node
A location in a graph (node or edge) can be reached from
another location if there is a sequence of edges from the
first location to the second
– Syntactic reach : A subpath exists in the graph
– Semantic reach : A test exists that can execute that subpath
– This distinction becomes important in section 7.3
11
Tests and Test Paths
test 1 many-to-one
Test
test 2
Path
test 3
Deterministic software–test always executes the same test path
many-to-many
test 1 Test Path 1
13
Node and Edge Coverage
The first (and simplest) two criteria require that each node
and edge in a graph be executed
14
Node and Edge Coverage
Edge coverage is slightly stronger than node coverage
Edge Coverage (EC) : TR contains each reachable path of
length up to 1, inclusive, in G.
• The phrase “length up to 1” allows for graphs with one
node and no edges
• NC and EC are only different when there is an edge and
another subpath between a pair of nodes (as in an “if-else”
statement)
Node Coverage : ? TR = { 1, 2, 3 }
1 Test Path = [ 1, 2, 3 ]
16
Covering Multiple Edges
Edge-pair coverage requires pairs of edges, or subpaths of
length 2
Edge-Pair Coverage (EPC) : TR contains each reachable path
of length up to 2, inclusive, in G.
• The phrase “length up to 2” is used to include graphs that
have less than 2 edges
1 5
Edge-Pair Coverage : ?
2 4 TR = { [1,4,5], [1,4,6], [2,4,5],
[2,4,6], [3,4,5], [3,4,6] }
3 6
17
Covering Multiple Edges
18
Structural Coverage Example
Node Coverage
TR = { 1, 2, 3, 4, 5, 6, 7 }
Test Paths: [ 1, 2, 3, 4, 7 ] [ 1, 2, 3, 5, 6, 5, 7 ]
Write down
1 the TRs and
Edge Coverage
Test(5,
TR = { (1,2), (1, 3), (2, 3), (3, 4), (3, 5), (4, 7), Paths
6), (5, 7),
2 (6, 5) } for these
Test Paths: [ 1, 2, 3, 4, 7 ] [1, 3, 5, 6, 5, 7 ] criteria
3
Edge-Pair Coverage
TR = { [1,2,3], [1,3,4], [1,3,5], [2,3,4], [2,3,5], [3,4,7],
4 5 [3,5,6], [3,5,7], [5,6,5], [6,5,6], [6,5,7] }
Test Paths: [ 1, 2, 3, 4, 7 ] [ 1, 2, 3, 5, 7 ] [ 1, 3, 4, 7 ]
[ 1, 3, 5, 6, 5, 6, 5, 7 ]
6
7
Complete Path Coverage
Test Paths: [ 1, 2, 3, 4, 7 ] [ 1, 2, 3, 5, 7 ] [ 1, 2, 3, 5, 6, 5, 7 ]
[ 1, 2, 3, 5, 6, 5, 6, 5, 7 ] [ 1, 2, 3, 5, 6, 5, 6, 5, 6, 5, 7 ] …
19
Handling Loops in Graphs
If a graph contains a loop, it has an infinite number of paths
20
Simple Paths and Prime Paths
Simple Path : A path from node ni to nj is simple if no node
appears more than once, except possibly the first and last
nodes are the same
– No internal loops
– A loop is a simple path
Prime Path : A simple path that does not appear as a
proper subpath of any other simple path
Simple Paths : [1,2,4,1], [1,3,4,1], [2,4,1,2], [2,4,1,3],
1 [3,4,1,2], [3,4,1,3], [4,1,2,4], [4,1,3,4], [1,2,4], [1,3,4],
Write
[2,4,1], [3,4,1], [4,1,2], down[1,2],
[4,1,3], the [1,3], [2,4], [3,4],
2 3 [4,1], [1], [2], [3], [4]simple and
prime paths for
4
Prime Paths : [2,4,1,2], this[2,4,1,3],
graph [1,3,4,1], [1,2,4,1],
[3,4,1,2], [4,1,3,4], [4,1,2,4], [3,4,1,3]
21
Prime Path Coverage
A simple, elegant and finite criterion that requires loops to
be executed as well as skipped
22
PPC Does Not Subsume EPC
• If a node n has an edge to itself (self edge), EPC
requires [n, n, m] and [m, n, n]
• [n, n, m] is not prime
• Neither [n, n, m] nor [m, n, n] are simple paths (not
prime)
EPC Requirements : ?
1 TR = { [1,2,3], [1,2,2], [2,2,3], [2,2,2] }
2 PPC Requirements : ?
TR = { [1,2,3], [2,2] }
3
23
Prime Path Example
The previous example has 38 simple paths
Only nine prime paths
1
Prime Paths
2 [1, 2, 3, 4, 7]
[1, 2, down
Write 3, 5, 7] Execute
[1,92,prime
all 3, 5, 6] loop 0 times
3 [1, 3, 4, 7]
paths
[1, 3, 5, 7] Execute
4 5 [1, 3, 5, 6] loop once
[6, 5, 7]
6 [6, 5, 6] Execute loop
7 [5, 6, 5] more than once
24
Simple & Prime Path Example
‘!’ means path
Simple Len 0 Len 1 Len 2
terminates Len 3
paths [1] [1, 2] [1, 2, 3] [1, 2,‘*’
3, means
4] path
[2]
Write [1, 3] [1, 3, 4] [1, 2, 3, 5]cycles
1 [3] of [2, 3] [1, 3, 5] Write
[1, 3, 4, 7] !
paths Write
[4] 0 [3, 4]of Write
[2, 3, 4] paths
[1, 3, 5,of7] !
length paths
2 [5] [3, 5] 1 paths
[2, 3, 5]of length
[1, 3, 5, 36] !
length
[6] [4, 7] ! length
[3, 4, 7]2! [2, 3, 4, 7] !
[7] ! [5, 7] ! [3, 5, 7] ! [2, 3, 5, 6] !
3 [5, 6] [3, 5, 6] ! [2, 3, 5, 7] !
[6, 5] [5, 6, 5] *
4 5 [6, 5, 7] !
[6, 5, 6] *
6
7 Len 4
[1,Write
2, 3, 4, 7] !
[1,paths
2, 3, 5,
of 7] ! Prime Paths ?
[1,length
2, 3, 5,46] !
25
Touring, Sidetrips, and Detours
Prime paths do not have internal loops … test paths might
26
Sidetrips and Detours Example
1 2 3 4
1 2 3 5 6
1 2 5
1 2 3 5 6
3
Touring with a 4
4
detour
27
Infeasible Test Requirements
An infeasible test requirement cannot be satisfied
– Unreachable statement (dead code)
– Subpath that can only be executed with a contradiction (X > 0 and X < 0)
• Most test criteria have some infeasible test requirements
• It is usually undecidable whether all test requirements are
feasible
• When sidetrips are not allowed, many structural criteria
have more infeasible test requirements
• However, always allowing sidetrips weakens the test
criteria
Practical recommendation—Best Effort Touring
– Satisfy as many test requirements as possible without sidetrips
– Allow sidetrips to try to satisfy remaining test requirements
28
Round Trips
Round-Trip Path : A prime path that starts and ends at the
same node
Simple Round Trip Coverage (SRTC) : TR contains at least
one round-trip path for each reachable node in G that begins
and ends a round-trip path.
X = 42
2 5 def (5) = { Z } Fill
in
1 4 7 def (6) = { Z } these
3 6 Uses: use (5) = { X } sets
Z = X-8 use (6) = { X }
Three criteria
– Use every def
– Get to every use
– Follow all du-paths
32
Data Flow Test Criteria
• First, we make sure every def reaches a use
• Then we make sure that every def reaches all possible uses
34
Overview
35
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 …
36
CFG : The if Statement
if (x < y)
{
y = 0; 1
Draw the
x=x+ x < y Label
graph. x >= y
1; y=0 the
x=x+ 2 edges with
3 x=y
} 1 the Java
else statements.
{ 4
x = y;
} 1
if (x < y)
{ Drawxthe
<y
y = 0; y=0
graph and x >= y
x=x+ 2
x=x+ 1label the
1; edges.
} 3
37
CFG : The if-Return Statement
Draw the
if (x < y) graph and
{ label the 1
return; edges. x<y
} x >= y
return 2
print (x);
return;
print
3 (x)
return
38
Loops
39
CFG : while and for Loops
Draw the
graphxand
= 1
x = 0;
label the
0
while (x < dummy node
edges.
y)
2
{ x< x >= implicitly
y = f (x, x= 1
y y initializes loop
y); 3 4 0
x=x+ y
=f(x,y) 2
1; x=x+ Draw the x <
} 1 x >=
for (x = 0; x < y; graph and y y
ylabel
= f (x,
x++) y) the 3 5
{ edges.
y = f (x, y);
4 x=x+
} 1
implicitly
increments loop
40
CFG : do Loop, break and continue
x = 0; x = 0;
do while (x < y) 1 x=
{ { 0
y = f (x, y); y = f (x, y); Draw the
x = x + 1; if (y == 0) 2 graph and
} while (x < { label the
y); break; edges.y =f(x,y)
3
println (y) } else if (y < y == 0
0)
{ 4 break
x= 1Draw the y = y*2;
0 graph and continue; 5
label the y<
}
y = f (x, 0 y = y*2
2edges.
y) x = x + 1; 6 continu
x >= y x = x+1 } e
x<
y print (y); 7 x=x+1
3
print (y) 8
41
CFG : The case (switch) Structure
read ( c) ;
switch ( c )
{
Draw the
case ‘N’: 1 read ( c );
graph and
z = 25;
label the c == ‘N’
case ‘Y’: c == ‘Y’ default
edges.
x = 50;
break; 2 3 4 x = 0;
default: z = 25; x = 50;
x = 0; break; break;
break;
} 5
print (x); print (x);
43
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++) Draw the
{
sum += numbers [ i ]; graph and
} label the
med = numbers [ length / 2];
mean = sum / (double) length; edges.
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 );
sum = 0;
for (int i = 0; i < length; i++) 2 i=0
{
sum += numbers [ i ];
}
med = numbers [ length / 2]; i >=
mean = sum / (double) length; 3 length
varsum = 0; i < length
for (int i = 0; i < length; i++)
{ i++ 4
varsum = varsum + ((numbers [ I ] - mean) * (numbers [ 5
I ] - mean)); i=0
}
var = varsum / ( length - 1.0 );
sd = Math.sqrt ( var ); 6
System.out.println ("length: " + length); i < length
System.out.println ("mean: " + mean); i >= length
System.out.println ("median: " + med);
System.out.println ("variance: " + var); 7 8
System.out.println ("standard deviation: " + sd);
} © Ammann & Offutt
i++
45
Control Flow TRs and Test Paths—EC
1
Edge Coverage
2 TR Test Path
A. [ 1, 2 ] [ 1, 2, 3,Write
4, 3,down
5, 6, 7, 6, 8 ]
test paths that
3 B. [ down
Write 2, 3 ]
tour all edges.
the
C.TRs
[ 3,for
4]
EC.
D. [ 3, 5 ]
4
5 E. [ 4, 3 ]
F. [ 5, 6 ]
6
G. [ 6, 7 ]
H. [ 6, 8 ]
I. [ 7, 6 ]
7 8
46
Control Flow TRs and Test Paths—EPC
Edge-Pair Coverage
1
TR Test Paths
A. [ 1, 2, 3 ] i. [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8
2 B. [ 2,down
Write 3, 4 ] ] Write down test
TRs[ for
C. 2, EPC.
3, 5 ] ii. [ 1,paths
2, 3,that tour
5, 6, 8 ]all
edge pairs.
D. [ 3, 4, 3 ] iii. [ 1, 2, 3, 4, 3, 4, 3, 5, 6,
3
E. [ 3, 5, 6 ] 7,
TP
F. [ 4, 3, 5 ] 6, 7,TRs
6, 8toured
] sidetrips
4 i A, B, D, E, F, G, I, J C, H
5 G. [ 5, 6, 7 ]
H. [ 5, 6, 8 ] ii A, C, E, H
I. [ 6, 7, 6 ] iii A, B, D, E, F, G, I, J, C, H
K, L
6 J. [ 7, 6, 8 ]
K. [ 4, 3, 4 ] TP iii makes TP i
L. [ 7, 6, 7 ] redundant. A minimal
7 8 set of TPs is cheaper.
47
Control Flow TRs and Test Paths—PPC
Prime Path Coverage
1
TR Test Paths
A. [ 3, 4, 3 ] i. [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8
2 B. [ 4,Write
3, 4down
] ] Write down test
C. [ 7,TRs
6, for
7 ] PPC. ii. [ 1,paths
2, 3,that
4, tour
3, 4,all3,
D. [ 7, 6, 8 ] 5,prime
6, 7,paths.
6, 7, 6, 8 ]
3 E. [ 6, 7, 6 ] iii. [ 1, 2, 3, 4, 3, 5, 6, 8 ]
F. [ 1, 2, 3, 4 ] iv. [ 1, 2, 3, 5, 6, 7, 6, 8 ]
4 v. [ 1, 2, 3, 5, 6, 8 ]
5
G. [ 4, 3, 5, 6, 7 ]
TP TRs toured sidetrips
H. [ 4, 3, 5, 6, 8 ]
i A, D, E, F, G H, I, J
I. [ 1, 2, 3, 5, 6, 7
6 ] ii A, B, C, D, E, F, G, H, I, J
J. [ 1, 2, 3, 5, 6, 8 iii A, F, H J
] TP ii makes iv D, E, F, I J
7 8 TP i redundant. v J
48
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 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
49
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 );
3 i >= length
i < length
med = numbers [ length / 2 ]
4 5 mean = sum / (double) length
varsum = 0
sum += numbers [ i ] i=0
i++
6 i >= length
i < length
var = varsum / ( length - 1.0 )
7 8 sd = Math.sqrt ( var )
varsum = … print (length, mean, med, var, sd)
i++
51
CFG for Stats – With Defs & Uses
def (1) = { numbers, sum, length }
1 use (1) = { numbers}
53
DU Pairs for Stats
defs come before uses, do
variable DU Pairs not count as DU pairs
numbers (1,1)(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) defs after use in loop,
these are valid DU pairs
sd (8, 8)
mean (5, 7) (5, 8)
No def-clear path …
sum (1, 4) (1, 5) (4, 4) (4, 5) different scope for i
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))
No path through graph from
(5, 7) (5, (6,7)) (5, (6,8))
nodes 5 and 7 to 4 or 3
(7, 7) (7, (6,7)) (7, (6,8))
54
DU Paths for Stats
variable DU Pairs DU Paths variable DU Pairs DU Paths
numbers (1, 4) [ 1, 2, 3, 4 ] mean (5, 7) [ 5, 6, 7 ]
(1, 5) [ 1, 2, 3, 5 ] (5, 8) [ 5, 6, 8 ]
(1, 7) [ 1, 2, 3, 5, 6, 7 ]
varsum (5, 7) [ 5, 6, 7 ]
length (1, 5) [ 1, 2, 3, 5 ] (5, 8) [ 5, 6, 8 ]
(1, 8) [ 1, 2, 3, 5, 6, 8 ] (7, 7) [ 7, 6, 7 ]
(1, (3,4)) [ 1, 2, 3, 4 ] (7, 8) [ 7, 6, 8 ]
(1, (3,5)) [ 1, 2, 3, 5 ] i (2, 4) [ 2, 3, 4 ]
(1, (6,7)) [ 1, 2, 3, 5, 6, 7 ] (2, (3,4)) [ 2, 3, 4 ]
(1, (6,8)) [ 1, 2, 3, 5, 6, 8 ] (2, (3,5)) [ 2, 3, 5 ]
(4, 4) [ 4, 3, 4 ]
med (5, 8) [ 5, 6, 8 ] (4, (3,4)) [ 4, 3, 4 ]
(4, (3,5)) [ 4, 3, 5 ]
var (8, 8) No path needed (5, 7) [ 5, 6, 7 ]
sd (8, 8) No path needed (5, (6,7)) [ 5, 6, 7 ]
sum (1, 4) [ 1, 2, 3, 4 ] (5, (6,8)) [ 5, 6, 8 ]
(1, 5) [ 1, 2, 3, 5 ] (7, 7) [ 7, 6, 7 ]
(4, 4) [ 4, 3, 4 ] (7, (6,7)) [ 7, 6, 7 ]
(4, 5) [ 4, 3, 5 ] (7, (6,8)) [ 7, 6, 8 ]
55
DU Paths for Stats—No Duplicates
There are 34 DU paths for Stats, but only 12 unique
[ 1, 2, 3, 4 ] [ 4, 3, 4 ]
[ 1, 2, 3, 5 ] [ 4, 3, 5 ]
[ 1, 2, 3, 5, 6, 7 ] [ 5, 6, 7 ]
[ 1, 2, 3, 5, 6, 8 ] [ 5, 6, 8 ]
[ 2, 3, 4 ] [ 7, 6, 7 ]
[ 2, 3, 5 ] [ 7, 6, 8 ]
56
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
58