Tutorial 8
- Consider the deceptively simple program below. Consider the three initial values of
a
,b
andc
to be inputs to the program. So the search space for test data generation is a vector of three values<a, b, c>
. In this exercise, we are concerned with branch and target coverage. There are four targets: 1, 2, 3, 4 in the program for which we wish to generate test data.
1int a, b, c
2
3if (a>b)
4 c = 1 /* target 1 */ ;
5else {
6 c = 2;
7
8 if (b==c)
9 /* target 2 */ ;
10}
11
12if (c==3)
13 do something; /* target 3 */ ;
14if (b==42)
15 do something else; /* target 4*/ ;
a). Consider the following 3 input vectors and show what each output each produces.
i).
<1, 2, 3>
The program will reach /* target 2 */
and end with <1, 2, 2>
.
ii).
<-1, -1, 4>
The program will end with <-1, -1, 2>
.
iii).
<24, 52, 1>
The program will end with <24, 52, 2>
.
iv). None of these cover target 1. Which comes closest?
ii). comes closest as it gets closest to a > b
.
b). Explain why it is not possible to cover target 3 in this program. Which input vector or vectors come closest to hitting target 3?
Because of the first if-else statement, c
is always set to either 1
or 2
. When a < b
, then c = 2
and that is the closest the program gets to hitting target 3.
c). Which input vectors hit target 4?
<1, 42, 2>
d). Is it possible to achieve 100% branch coverage? If yes, give the test suite with 100% coverage. If not, explain why not and construct a test suite that covers all the reachable branches.
It is not possible to attain 100% branch coverage as target 3 is unreachable. The following test suite covers the other branches:
- :
<3, 2, 1>
(target 1) - :
<1, 2, 1>
(target 2) - :
<1, 42, 1>
(target 4)
This could be done with 2 test cases, but then you are combining 2 targets into one test.
- Assume you have a part of a program:
1if ((a && b) || c) do X;
2else do Y;
Construct a test suite that gives 100% condition coverage; 100% branch coverage; 100% MC/DC.
Test Suite:
- :
TTT
- :
TTF
- :
TFF
- :
FFF
- :
FFT
- :
FTT
- :
FTF
- :
TFT
1a:
2 TTF = T x
3 FTF = F x
4b:
5 TTF = T
6 TFF = F x
7c:
8 TFT = T x
9 TFF = F
- Calculate the McCabe Number (cyclomatic complexity) for the following graphs:
- a).
- b).
- c).
- d).
a, b, c) are control flow graphs, whereas d is an arbitrary graph.