Reputation: 73
The solution for this problem in my textbook is that the minimum number of test cases is 3. However, I can't go below 4 for the full edge and condition coverage.
int x=0;
int k=10;
while (x<=10 && z>0) {
if (z<=y && k>=x)
y=y-z;
k--;
if (y>0) x++;
else break;
}
My solution would be something like this: {z=1, y=0},{z=-1, y=0},{z=1, y=10},{z=1, y=3}
. Which of these (if any) is redundant?
What is the three test cases combination that solve this problem? Or is there an error in my textbook?
Upvotes: 0
Views: 2049
Reputation: 1
You indeed need 3 test cases, but some of the answers did not consider the conditions in the while loop: you need to cover the case when z>0 is false (as you did). So two test cases for covering edge and conditions in the body of the while, e.g.:
(1,0) covering the conditions z<=y false and y<0 false, together with the edge "false" of the first "if";
(1,10) covering in the first 5 iterations the conditions z<=y true, k>=x true, and y>0 false, together with the edge true of the first if; in the following iterations this covers also the remaining condition k>=x false. No need to cover again z<=y false since it was already covered by the first test case!
Just add (-1,any) to cover the missing condition z>0 false.
The case (1,3) of OP is not necessary.
Upvotes: 0
Reputation: 846
The minimum number of test cases to achieve 100% condition coverage is indeed 3.
Here are the test cases: {z=10,y=1},{z=1,y=1},{z=1,y=10}
To achieve full condition coverage, each of the Boolean expression in a condition must have been evaluated to each true
and false
respectively.
For if (z<=y && k>=x)
condition
z<=y : true
k>=x: true
z<=y : true
k>=x: false
z<=y : false
k>=x: false
z<=y : false
k>=x: true
For if(y>0)
condition
y>0: true
y>0:false
Note that since there is a while loop with modification of variable values, there can be multiple Boolean expressions covered in just a test case.
The first test case {z=10,y=1}
covers
z<=y : false
k>=x: false
z<=y : false
k>=x: true
y>0: true
The second test case {z=1,y=1}
covers
z<=y : true
k>=x: true
y>0:false
The third test case {z=1,y=10}
covers the remaining Boolean expression
z<=y : true
k>=x: false
With that every Boolean expression in every condition in the code is covered, therefore ensuring 100% condition coverage
You can print to console the Boolean values of the expression required just before the if statements to ensure every Boolean expression in a condition is evaluated to true
and false
respectively.
int x=0;
int k=10;
while (x<=10 && z>0) {
System.out.println((z<=y),(k>=x))
if (z<=y && k>=x)
y=y-z;
k--;
System.out.println((y>0))
if (y>0) x++;
else break;
}
Upvotes: 0
Reputation: 153456
Curious function.
As seen below, with just looking as x
, there are a number of interesting regions that each deserve a test vector.
I disagree that "minimum number of test cases is 3."
struct kx {
int k, x;
};
struct kx foo(int z, int y) {
int x = 0;
int k = 10;
while (x <= 10 && z > 0) {
if (z <= y && k >= x)
y = y - z;
k--;
if (y > 0)
x++;
else
break;
}
struct kx r = {k, x};
return r;
}
int main(void) {
for (int z = -2; z <= 12; z++) {
for (int y = -2; y <= 20; y++) {
struct kx newest = foo(z, y);
printf(" %2d", newest.x);
}
printf("\n");
}
}
Output
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 2 3 4 5 11 11 11 11 11 11 11 11 11 11 11 11 11 11
0 0 0 11 0 11 1 11 2 11 3 11 4 11 5 11 11 11 11 11 11 11 11
0 0 0 11 11 0 11 11 1 11 11 2 11 11 3 11 11 4 11 11 5 11 11
0 0 0 11 11 11 0 11 11 11 1 11 11 11 2 11 11 11 3 11 11 11 4
0 0 0 11 11 11 11 0 11 11 11 11 1 11 11 11 11 2 11 11 11 11 3
0 0 0 11 11 11 11 11 0 11 11 11 11 11 1 11 11 11 11 11 2 11 11
0 0 0 11 11 11 11 11 11 0 11 11 11 11 11 11 1 11 11 11 11 11 11
0 0 0 11 11 11 11 11 11 11 0 11 11 11 11 11 11 11 1 11 11 11 11
0 0 0 11 11 11 11 11 11 11 11 0 11 11 11 11 11 11 11 11 1 11 11
0 0 0 11 11 11 11 11 11 11 11 11 0 11 11 11 11 11 11 11 11 11 1
0 0 0 11 11 11 11 11 11 11 11 11 11 0 11 11 11 11 11 11 11 11 11
0 0 0 11 11 11 11 11 11 11 11 11 11 11 0 11 11 11 11 11 11 11 11
Upvotes: 0