Page 16 Exercises 1.2.3 -------Introduction to Software Testing (Paul Ammann and Jeff Offutt)

Below are four faulty programs. Each includes a test case that results in failure. Answer the following questions about each progrma.

(a) Indentify the fault.

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

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

(d) If possible, identify a test case that resutls in an error, but not a failure. Hint: Don't forget about the program counter.

(e) For the given test case, identify the first error state. Be sure to describe the complete state.

(f) Fix the fault and verify that the given test now produces the expected output.

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

(a) fault: in the for loop, it shoule be:

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

(b) test case: x = [1]; y = 2, it doesn't execute i > 0

(c) test case: x = [1, 2, 3]; y = 3, it executes x > 0 but doesn't go to i = 0

(d) test case: x = [1, 2, 3]; y = -1, it goes to i = 0 but the result is right

(e) first error state: x = [2, 3, 4], y = 2, i = 0, it should compare x[0] to y, but it doesn't

(f) see(a)

 

Program 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, 2]
       //    Excepted = 2;

(a) fault: in the for loop, it shoule be:

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

(b) test: x = []

(c) test: x = [1]

(d) test: x = [0, 1]

(e) first error state: x = [0, 1, 0], i = 0, after that i should decrease 1

(f) see (a)

 

Program 3:

public int countPositive(int[] x){
//Effects: If x==null throw NullPointerException
// else return the number of
// positive elements in x.
    int count = 0;
    for(int i = 0; i < x.length; i++)
    {
        if(x[i] >= 0)
        {
            count++;
        }
    }
    return count;   
}
    //test: x= [-4, 2, 0, 2]
    //    Excepted = 2

(a) fault: it shoule be:

if(x[i] > 0)

(b) test case: x = []

(c) test case: x = [1]

(d) if it goes to an error, it must results in failure

(e) first error state: x = [-4, 2, 0, 2], i = 2, after that it should continue the for loop instead of increasing the count

(f) see (a)

 

Program 4:

public static int oddOrPos(int[] x){
//Effects: if x==null throw NullPointerException
// else return the number of elements in x that
// are either odd or positive (or both)
    int count = 0;
    for(int i =0; i < x.length; i++)
    {
        if(x[i]%2 == 1 || x[i] > 0)
        {
            count++;
        }
    }
    return count;
}
    //test: x = [-3, -2, 0, 1, 4]
    //    Excepted = 3

(a) fault: it should be:

if(x[i] % 2 == -1 || x[i] > 0)

(b) test: x = []

(c) test: x = [1]

(d) test: the test case that results in an error, but not a failure does not exist

(e) first error state: x = [-3, -2, 0, 1, 4], i = 0, after that it should execute count++, but it doesn't

(f) see(a)

转载于:https://www.cnblogs.com/xuechengyun/p/5248030.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值