软件测试第二次作业

  题目:

  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) {
 2 //Effects: If x==null throw
 3 NullPointerException
 4 // else return the index of the last element
 5 // in x that equals y.
 6 // If no such element exists, return -1
 7     for (int i=x.length-1; i > 0; i--)
 8     {
 9     if (x[i] == y)
10         {
11         return i;
12         }
13     }
14     return -1;
15 }
16 // test: x=[2, 3, 5]; y = 2
17 // Expected = 0
18                     

1. Identify the fault. 

  在遍历数组x的时候,循环条件应该是“i >= 0”,否则遍历不到数组x的第一个数字

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

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

  Expected = 1

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

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

  Expected = -1

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

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

  Expected = 0

 

 

 

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

1. Identify the fault. 

  因为是寻找最后一个0,所以要从后往前找,遍历条件改为“int i = x.length-1; i >=0 ; i--”

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

  test:x = [0,1,3]

  Expected = 0

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

  test:x = [1,3,5]

  Expected = -1

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

  test:x = [0,1,0] 

  Expected = 2

 

posted on 2017-03-02 20:37  fy2014  阅读(72)  评论(0编辑  收藏  举报