【ST】hw2——find the error in the follow case

public int findLast (int[] x, int y) {

  //Effects: If x==null throw

    NullPointerException

  // 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

 

l   Identity the fault

这样的话丢掉了x[0]=y的情况

应该变成【for (int i=x.length-1; i >= 0; i--)】

l   If possible, identity a test case that does not execute the fault. (Reachability)

一个到达了错误代码行,却没有执行的例子

比如:x = null,y = 2

返回:期望值——-1

                实际值——-1

l   If possible, identity a test case that execute the fault, but dose not result in an error state.

到达了错误的代码行,却没有导致错误的结果

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

返回:期望值——1/2

                 实际值——1/2

l   If possible, identity a test case that results in an error, but not a failure.

产生了error,而不是failure

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

返回:期望值——0

                实际值——-1

 

 

 

 

 

 

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

 

l   Identity the fault

这样显示的是第一个0而不是最后一个0

应该改为for (int i = x.length – 1 ; i >= 0; i--)

l   If possible, identity a test case that does not execute the fault. (Reachability)

一个数组中不含0的数组,例如test: x = [3, 1, 3]

返回:期望值——-1

                实际值——-1

l   If possible, identity a test case that execute the fault, but dose not result in an error state.

大概是一个回文结构的数组,0在最中间

例如test: x = [3, 0, 3]

返回:期望值——1

                实际值——1

l   If possible, identity a test case that results in an error, but not a failure.

test: x = [0, 1, 0]

返回:期望值——2

                实际值——0

原文地址:https://www.cnblogs.com/eVonneDing/p/5247618.html