0% found this document useful (0 votes)
17 views

AfterMidSlide2 Graph Coverage

This document discusses graphs and their use in modeling software for testing purposes. It defines key graph concepts like nodes, edges, paths, and covers topics like: - Four common structures used to model software as graphs: control flow graphs, design structure, finite state machines, and use cases. - Definitions of graphs, paths in graphs, and visiting/touring paths. - How tests execute test paths in graphs and the relationship between tests and test paths. - Common criteria for testing graphs like node coverage and edge coverage, which require each node and edge to be executed by a test path.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

AfterMidSlide2 Graph Coverage

This document discusses graphs and their use in modeling software for testing purposes. It defines key graph concepts like nodes, edges, paths, and covers topics like: - Four common structures used to model software as graphs: control flow graphs, design structure, finite state machines, and use cases. - Definitions of graphs, paths in graphs, and visiting/touring paths. - How tests execute test paths in graphs and the relationship between tests and test paths. - Common criteria for testing graphs like node coverage and edge coverage, which require each node and edge to be executed by a test path.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

Graph Coverage

Ch. 7 : Graph Coverage


Four Structures for
Modeling Software

Input Space Graphs Logic Syntax


Applied Applied to
to
Applied
Source FSMs
to

Specs DNF

Source Specs Source Models

Design Use cases Integ Input


2
Small Illustrative Example

Control Flow Graph


Software Artifact : Java Method
/**
* Return index of node n at the 1 i = 0
* first position it appears,
* -1 if it is not present
*/ 2 i < path.size()
public int indexOf (Node n)
{
for (int i=0; i < path.size(); i++) 3 if
if (path.get(i).equals(n))
return i;
return -1; 5 4
} return -1 return i

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

 Graphs can come from many sources


– Control flow graphs
– Design structure
– FSMs and statecharts
– Use cases

 Tests usually are intended to “cover” the graph in some


way

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]

(Also, each edge is technically a subpath)

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

test 2 Test Path 2

test 3 Test Path 3


Non-deterministic software–the same test can execute different test
paths
12
Testing and Covering Graphs (7.2)
 We use graphs in testing as follows :
– Develop a model of the software as a graph
– Require tests to visit or tour specific sets of nodes, edges or
subpaths
• Test Requirements (TR) : Describe properties of test paths
• Test Criterion : Rules that define test requirements
• Satisfaction : Given a set TR of test requirements for a criterion C, a
set of tests T satisfies C on a graph if and only if for every test
requirement in TR, there is a test path in path(T) that meets the test
• requirement tr
Structural Coverage Criteria : Defined on a graph just in terms of
nodes and edges
• Data Flow Coverage Criteria : Requires a graph to be annotated
with references to variables

13
Node and Edge Coverage
 The first (and simplest) two criteria require that each node
and edge in a graph be executed

Node Coverage (NC) : Test set T satisfies node coverage on


graph G iff for every syntactically reachable node n in N,
there is some path p in path(T) such that p visits n.

• This statement is a bit cumbersome, so we abbreviate it in terms of


the set of test requirements

Node Coverage (NC) : TR contains each reachable node in G.

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 ]

2 Edge Coverage : ? TR = { (1, 2), (1, 3), (2, 3) }


Test Paths = [ 1, 2, 3 ]
3
[ 1, 3 ]
15
Paths of Length 1 and 0
 A graph with only one node will not have any edges

• It may seem trivial, but formally, Edge Coverage needs to


require Node Coverage on this graph
• Otherwise, Edge Coverage will not subsume Node
Coverage
– So we define “length up to 1” instead of simply “length 1”
• We have the same issue with graphs that
1
only have one edge – for Edge-Pair
Coverage …
2

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

• The logical extension is to require all paths …

17
Covering Multiple Edges

Complete Path Coverage (CPC) : TR contains all paths in G.

Unfortunately, this is impossible if the graph has a loop, so a


weak compromise makes the tester decide which paths:

Specified Path Coverage (SPC) : TR contains a set S of test


paths, where S is supplied as a parameter.

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

 Thus, CPC is not feasible

 SPC is not satisfactory because the results are subjective


and vary with the tester

 Attempts to “deal with” loops:


– 1970s : Execute cycles once ([4, 5, 4] in previous example, informal)
– 1980s : Execute each loop, exactly once (formalized)
– 1990s : Execute loops 0 times, once, more than once (informal description)
– 2000s : Prime paths (touring, sidetrips, and detours)

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

Prime Path Coverage (PPC) : TR contains each prime path in G.

• Will tour all paths of length 0, 1, …


• That is, it subsumes node and edge coverage
• PPC almost, but not quite, subsumes EPC …

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

• Tour : A test path p tours subpath q if q is a subpath of p


• Tour With Sidetrips : A test path p tours subpath q with
sidetrips iff every edge in q is also in p in the same order
• The tour can include a sidetrip, as long as it comes back to the
same node
• Tour With Detours : A test path p tours subpath q with
detours iff every node in q is also in p in the same order
• The tour can include a detour from node ni, as long as it comes
back to the prime path at a successor of ni

26
Sidetrips and Detours Example
1 2 3 4
1 2 3 5 6

Touring the prime path


[1, 2, 3, 5, 6] without 4
sidetrips or detours
1 2 5 6
1 2 3 5 6
3 4
Touring with a
sidetrip 4

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.

Complete Round Trip Coverage (CRTC) : TR contains all


round-trip paths for each reachable node in G.
• These criteria omit nodes and edges that are not in round
trips
• Thus, they do not subsume edge-pair, edge, or node
coverage
29
Data Flow Criteria
Goal : Ensure that values are computed and used correctly

 Definition (def) : A location where a value for a variable is


stored into memory
 Use : A location where a variable’s value is accessed
Z = X*2 Defs: def (1) = { X }

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 }

The values given in defs should reach at least one, some, or


all possible uses
30
DU Pairs and DU Paths
• def (n) or def (e) : The set of variables that are defined by node n
or edge e
• use (n) or use (e) : The set of variables that are used by node n or
edge e

• DU pair : A pair of locations (li, lj) such that a variable v is defined


at li and used at lj

• Def-clear : A path from li to lj is def-clear with respect to variable v


if v is not given another value on any of the nodes or edges in
the path
• Reach : If there is a def-clear path from li to lj with respect to v,
the def of v at li reaches the use at lj

• du-path : A simple subpath that is def-clear with respect to v


from a def of v to a use of v
• du (ni, nj, v) – the set of du-paths from ni to nj
• du (ni, v) – the set of du-paths that start at ni
31
Touring DU-Paths

 A test path p du-tours subpath d with respect to v if p tours


d and the subpath taken is def-clear with respect to v

 Sidetrips can be used, just as with previous touring

 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

All-defs coverage (ADC) : For each set of du-paths S = du (n,


v), TR contains at least one path d in S.

• Then we make sure that every def reaches all possible uses

All-uses coverage (AUC) : For each set of du-paths to uses S =


du (ni, nj, v), TR contains at least one path d in S.

• Finally, we cover all the paths between defs and uses

All-du-paths coverage (ADUPC) : For each set S = du (ni, nj,


v), TR contains every path d in S.
33
Data Flow Testing Example
Z = X*2
2 5
X = 42
1 4 7
3 6
Z = X-8

All-defs for X All-uses for X All-du-paths for X


[Write
1, 2,down
4, 5 ] [ 1, 2, 4, 5 ] [ 1, 2, 4, 5 ]
paths to [Write
1, 2,down
4, 6 ] [Write
1, 3,down
4, 5 ]
paths to
satisfy ADC paths to
satisfy AUC
[satisfy
1, 2, 4, 6 ]
ADUPC
[ 1, 3, 4, 6 ]

34
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

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

No edge from node 2 to 3.


The return nodes must be distinct.

38
Loops

 Loops require “extra” nodes to be added

 Nodes that do not represent statements or basic blocks

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);

Cases without breaks fall


through to the next case
42
CFG : Exceptions (try-catch)
try
{
1 s = br.readLine()
s = br.readLine();
if (s.length() > 96) IOException
Draw the
throw new
graph 2and 3
Exception length <= 96
label the
e.printStackTrace()
(“too long”); length > 96
edges.
if (s.length() == 0)
throw 4 5
throw new length != 0
Exception length == 0
(“too short”);
7
} (catch IOException e) throw
{
e.printStackTrace();
6
} (catch Exception e) { e.getMessage()
e.getMessage();
}
8
return (s); return (s)

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 );

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);
} © Ammann & Offutt
44
Control Flow Graph for Stats
public static void computeStats (int [ ] numbers)
{
int length = numbers.length; 1
double med, var, sd, mean, sum, varsum;

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 );

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);
}
50
Control Flow Graph for Stats
( numbers )
1 sum = 0
length = numbers.length

i=0 Annotate with the


2
statements …

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}

Turn the annotations


2 def (2) = { i } into def and use sets …

3 use (3, 5) = { i, length }


use (3, 4) = { i, length }
def (5) = { med, mean, varsum, i }
4 5 use (5) = { numbers, length, sum }
def (4) = { sum, i }
use (4) = { sum, numbers, i }

6 use (6, 8) = { i, length }


use (6, 7) = { i, length }
def (8) = { var, sd }
def (7) = { varsum, i } 7 8 use (8) = { varsum, length, mean,
use (7) = { varsum, numbers, i, mean } med, var, sd }
52
Defs and Uses Tables for Stats
Node Def Use Edge Use
1 { numbers, sum, { numbers } (1, 2)
length }
(2, 3)
2 {i}
(3, 4) { i, length }
3
(4, 3)
4 { sum, i } { numbers, i, sum }
(3, 5) { i, length }
5 { med, mean, { numbers, length, sum }
varsum, i } (5, 6)
6 (6, 7) { i, length }
7 { varsum, i } { varsum, numbers, i, (7, 6)
mean }
(6, 8) { i, length }
8 { var, sd } { varsum, length, var,
mean, med, var, sd }

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 ]

4 expect a loop not to be “entered”

6 require at least one iteration of a loop

2 require at least two iterations of a loop

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

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
57
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

58

You might also like