Tutorial 3 , Q1 Answers. Draw the control for this program and (if it helps) the structural tree-form of this program. Calculate: number of nodes number of edges number of simple paths all paths with k=2.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Consider the following simple program (in no particular language) to find the smallest prime factor of a number:
smallest(int p) /* precondition p > 1 */
{
int q = 2;
while(p mod q > 0 AND q < sqrt(p) )
q := q+1
;
if (p mod q = 0)
then
print(q,''is the smallest factor'')
else
print(p, ``is prime'')
;
}
Tree
CFG
Program
smallest(int p) (*p>1*)
{
int q = 2;
while(p mod q > 0
AND
q < sqrt p)
do
q := q+1
;
if (p mod q = 0)
then
print(q,’is factor’)
else
print(p,’is prime’)
;
}
1
2
3
4
5
6
7
8
S
A1
S
A2
S
L3
C5
A4
A6
A7
S8
Number of Nodes:
A2
S7
A2
S6
L3
C4
A2
A2
A2
S9
Number of Edges:
A1
S8
A1
S7
L3
C4
A1
A1
A1
Number of simple paths
A simple path is one in which no edge is traversed more than once
S4
A1
S4
A1
S4
L2
C2
A1
A1
A1
Number of All Paths (k=2)
S6
A1
S6
A1
S6
L3
C2
A1
A1
A1
Consider the following simple program (in no particular language) to find the smallest prime factor of a number:
smallest(int p) /* precondition p > 1 */
{
int q = 2;
while(p mod q > 0 AND q < sqrt(p) )
q := q+1
;
if (p mod q = 0)
then
print(q,''is the smallest factor'')
else
print(p, ``is prime'')
;
}
All DU
paths
Usage
p q
CFG
Program
For p
123
12343
1235
123435
12357
1234357
For q
23
234
235
2356
43
434
435
4356
smallest(int p) (*p>1*)
{
int q = 2;
while(p mod q > 0
AND
q < sqrt p)
do
q := q+1
;
if (p mod q = 0)
then
print(q,’is factor’)
else
print(p,’is prime’)
;
}
1
2
3
4
5
6
7
8
d
up
up
uc
d
up
uc,d
up
uc
Required
paths
p
123
12343
1235
123435
12357
1234357
q
23
234
235
2356
43
434
435
4356
Test
Input
p=3
p=5
P=2
P=11
p=9
Actual path
123578
12343578
123568
1234343578
12343568
Test Output
3 is prime
5 is prime
2 is factor
11 is prime
3 is factor
Badly drawn wiggly line indicates paths which subsume other paths
So to cover all du paths we need to have 5 test cases: p=3 and p=5 and p = 2, p=9 and p=11.
Now to cover AU, we need only execute a single du path between each pair (d,u) that starts at a definition, d and ends at a use, u. However, we need to ensure that we have done this for both variables in the program. Let’s consider the variable p first. Of the du paths, we have 123 and 12343, but both these exercise the same pair (1,3) so just 123 will do. Also we don’t need both 1235 and 123435, just 1235 would do. Finally, we don’t need 12357 and 1234357, just 12357 will do. In conclusion, we see that we only need 12357 to cover all uses of the variable p. There is only one definition of the variable p (at node 1) and the case which passes through 12357 hits all three uses of the variable p at 3, 5, and 7. Therefore, to cover all uses of the variable p, we need only execute 123578 (p=3).
Notice how all du paths requires us (in this case) to go through the loop, whereas (for the variable p) AU does not. That is because there was a choice of du paths for each du pair, where one went through the loop and one did not. However, AU will force us to go through loops if the only du path from a definition to a use goes through a loop. This is the case with the variable q in this example, as we shall see.
Let’s look at the AU criterion for the variable q. To understand the difference between All du paths coverage and AU coverage, we need to be clear in our mind about the distinction between a du path and a du pair. The start and end node of a DU path is a Definition and a Use respectively. The definition and use, together, form a du pair. We notice that the 8 du paths identified for q all have different du pairs. That is, there are 8 distinct du pairs for q, each of which corresponds to one of the du paths. This means that covering all uses of q will be the same as covering all du paths for q. Therefore we need the same three test inputs to achieve this.
All predicate uses (as defined in the lecture) is not sufficient for branch coverage as it only requires each predicate to be tested in one direction, whereas branch coverage requires each predicate to be taken in both directions.
For this program, as there are no predicates inside the body of the loop, 100% APU coverage is possible without entering the loop, whilst branch cover requires loop body to be executed.