Lect 05
Lect 05
Graph-based testing
1 / 25
Overview
2 / 25
Graph definition
3 / 25
Graph-based testing
4 / 25
Constructing the graph
5 / 25
Sequence control flow graphs
s1
s2
6 / 25
if-then control flow graphs
given pseudocode like
if c then:
s1
s2
c true
false
s1
s2
7 / 25
if-then-else control flow graphs
if c then:
s1
else:
s2
s3
true c false
s1 s2
s3
8 / 25
What about loops?
9 / 25
while-do control flow graphs
while c do:
s1
s2
c false
true
s1
s2
10 / 25
other structures
11 / 25
other structures – example
A “case” statement:
case x of:
val1: s1; break
val2: s2; break
default: s3
s4
12 / 25
other structures – example (2)
13 / 25
Using the graph
To find a new test, examine the graph edges that haven’t been
exercised yet, and try to devise a test that exercises it
In general, we’d actually like to find a test that exercises as few
edges as possible
why?
14 / 25
Using the graph
To find a new test, examine the graph edges that haven’t been
exercised yet, and try to devise a test that exercises it
In general, we’d actually like to find a test that exercises as few
edges as possible
why?
Tests that exercise a large number of edges usually represent
“common” scenarios – we’d actually like to find less common
cases (i.e. get more “value” out of the test)
Ideally, we want tests to be small and independent, so that
when something goes wrong, we can localize the fault.
15 / 25
Example – sorting algorithm
S1 i = 2
C1 while (i <= n):
S2 j = i - 1
C2 while j >= 1 and A[j] >= A[j+1]:
S3 temp = A[j]
S4 A[j] = A[j+1]
S5 A[j+1] = temp
S6 j = j-1
S7 i = i + 1
16 / 25
Example – sorting algorithm (2)
17 / 25
Example – binary search
Inputs
n, the length of the following array.
A, an integer array with entries A[1], ..., A[n] such that
A[i] < A[i+1] for i between 1 and n-1
(i.e., it’s sorted in ascending order, and 1-based)
key, an integer to search for (the “needle”)
Outputs
index, an integer between 0 and n such that:
if index = 0 then key does not equal any entry of the array A
if index is between 1 and n then A[index] = key
18 / 25
Example – binary search (2)
found = false
low = 1
high = n
while ((low <= high) and not found):
medium = floor((low + high)/2)
if A[medium] == key:
index = medium
found = true
else:
if A[medium] < key then
low = medium + 1
else:
high = medium - 1
if not found:
index := 0
19 / 25
Graph-based testing criteria
20 / 25
Graph-based testing criteria
21 / 25
Graph-based testing criteria
Prime paths
Definitions:
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 in our path
A loop is a simple path
22 / 25
Graph-based testing criteria
Prime paths
Definitions:
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 in our path
A loop is a simple path
23 / 25
Graph-based testing criteria
24 / 25
Graph-based testing criteria
25 / 25