简体   繁体   中英

What is the minimum number of test cases for edge and condition coverage in this example?

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM