ST HW2 fault & error & failure

时间:2021-07-30 07:36:01

Software Testing

3014218128 牛菲菲

Below are two faulty programs. Each includes a test case that results in failure.Answer the following questions (in the next slide) about each program.

1.

public int findLast (int[] x, int y) {
//Effects: If x==null throw
NullPointerException
// else return the index of the last element
// in x that equals y.
// If no such element exists, return -1
for (int i=x.length-1; i > 0; i--)
{
if (x[i] == y)
{
return i;
}
}
return -1;
}
// test: x=[2, 3, 5]; y = 2
// Expected = 0

answer:

1) Identify the fault.

Fault: for (int i=x.length-1; i > 0; i--)

i should end with 0, but not with 1. The right should be" i>=0 "

Error: i is 1, not 0, on the last iteration
Failure: return the wrong result -1
2) If possible, identify a test case that does not execute the fault. (Reachability)

test: x = NULL, y=2

In this case, the fault isn't executed.

3) If possible, identify a test case that executes the fault, but does not result in an error state.

test: x=[3, 5, 2]; y=2

expected = 2, result is 2

In this case, it executes the fault but does not result in an error state.

4) If possible identify a test case that results in an error, but not a failure.

test: x=[3, 5, 2]; y=1

expected = -1, result is -1

In this case, it results in an error but not a failure.

2.
public static int lastZero (int[] x) {
//Effects: if x==null throw
NullPointerException
// else return the index of the LAST 0 in x.
// Return -1 if 0 does not occur in x
for (int i = 0; i < x.length; i++)
{
  if (x[i] == 0)
  {
    return i;
  }
}

  return -1;
}
// test: x=[0, 1, 0]
// Expected = 2

answer:

1) Identify the fault.

Fault: for (int i = 0; i < x.length; i++)

  It will return on the first iteration while it should continue iteration.

Failure: expected =2, result = 0
2) If possible, identify a test case that does not execute the
fault. (Reachability)

x=NULL

3) If possible, identify a test case that executes the fault, but
does not result in an error state.

test: x=[1, 2, 3]

4) If possible identify a test case that results in an error, but
not a failure.

test: x=[1, 0, 2].