结对编程2

1.结对说明

         结对对象:陈高瑶   博客地址:http://www.cnblogs.com/ccgy/

         项目已同步至GitHub: https://github.com/JackChuahe/pair_program

         双方贡献:2:1

2.题目要求
        构造程序,分别是:
        •不能触发Fault。
        •触发Fault,但是不能触发Error。
        •触发Error,但是不能产生Failure。
3.程序描述
输入两个整数A B ,range in [-100 , 100],若A B都小于 0,则输出 All less than 0;否则就判断A B中的最大值y = max(A,B), y是否处于[25 ,65] 之间,若处于,则输出 25 <= max(A,B) <=65,否则输出 Illegal !
 
4.结对照片
 
5.源程序
package pair_program_2;

import java.util.Scanner;

/**
 * 
 * @author JackCai
 * @brief 输入两个整数A B ,range in [-100 ,- 100],若A B都小于 0,则输出 All less than 0
 *        ;否则就判断A B中的最大值y = max(A,B), y是否处于[25 ,65] 之间,若处于,则输出 25 <= max(A,B) <=
 *        65,否则输出 Illegal !
 *
 */
public class PairProgram2 {
    public static int opA;
    /** <opA number */
    public static int opB;
    /** <opB number */
    public static int maxValue;
    /** <maxValue */

    private final static int ZERO = 0;
    private final static int LEG_RANGE_LOW = 25;
    private final static int LEG_RANGE_HIGHT = 65;
    private final static int RANGE_LOW = -100;
    private final static int RANGE_HIGHT = 100;

    private static Scanner scanner = new Scanner(System.in);

    /**
     * @brief this is the main function of this program
     * @param [in] args
     */
    public static void main(String args[]) {
        opA = getUserInput(); // 获取操作数A
        opB = getUserInput(); // 获取操作数B

        if (opA < ZERO && opB < ZERO) {
            System.out.println("All less than 0 !");
            return;
        }
        // otherwise
        maxValue = max(opA, opB); // 获取两者的MAX值
        if (maxValue >= LEG_RANGE_LOW && maxValue <= LEG_RANGE_HIGHT) { // 满足在
                                                                        // [25,65]之间的话,则输出
            System.out.println(LEG_RANGE_LOW + " <= max(" + opA + " , " + opB
                    + ") <= " + LEG_RANGE_HIGHT);
        } else {
            System.out.println("Illegal !"); // 否则就输出 Illegal !
        }
    }

    /**
     * @brief this function to get the max value between A and B (PS: some fault
     *        in these code)
     * @param [in] opA
     * @param [in] opB
     * @return
     */
    public static int max(int opA, int opB) {
        int temp = opA;
        if (opB > opA) {
            temp = opA;
        }
        return temp;
    }

    /**
     * @brief this function to get input data from user
     * @return int
     */
    public static int getUserInput() {
        int value = -1;
        System.out
                .println("please enter a integer number,range in [-100,100]:");
        while (true) {
            if (scanner.hasNextInt()) {
                value = scanner.nextInt();
                if (value >= RANGE_LOW && value <= RANGE_HIGHT) {
                    break;
                } else {
                    System.out
                            .println("Illegal range, should be in [-100,100],try again: ");
                }
            } else {
                scanner.nextLine(); // flush cache
                System.out.println("please enter right number:");
            }
        }
        return value;
    }
}

 

6.存在fault的代码说明
 //获取两数中的最大值
public static int max(int opA, int opB) { int temp = opA; if (opB > opA) { temp = opA; } return temp; }
// 如上存在一个fault,该函数是得到两个数中的最大值,并返回,这里永远都只会返回第一个操作数的值。
 如上存在一个fault,该函数是得到两个数中的最大值,并返回。但是这里永远都只会返回第一个操作数的值。
7.测试用例
        •1. 存在fault,但是不能触发Fault。
             由于本程序存在两个条件分支,于是只要不进入有fault分支的条件就能达到该测试效果。
             input: -25 -25
             
if (opA < ZERO && opB < ZERO) {
            System.out.println("All less than 0 !");
            return;
        }

            如上,存在fault,但是不并没有触发fault。因为存在 opA < 0 && opB <0 的条件满足,所以直接程序结束,不会进入求max部分。

        •2.触发Fault,但是不能触发Error。
     input: 30 19
            
 
             
             如上,因为正好opA = 30 > opB = 19,所出发了fault,但是没有出现error。
 
 
         •3.触发Error,但是不能产生Failure。
             input: 5 20
             
             如上,实际上opA < opB ,但是调用max函数得到的maxValue = opA,于是触发fault,且产生了error,但是由于opA 与 opB都不在 [25,65]之间,所以输出的结果是正确。如上,测试用例满足了触发了error,但未触发failure。
8.总结
           现在已经进入软件测试方面的学习,通过该程序,对软件中fault,error,failure 有了很直观的认识和理解,也让我们明白了,测试有一定的局限性,如何高效的测试,如何科学的测试,如何能得到较高的测试回报率,这也是本门课程中最重要的核心学习的内容。这是本门课程的开头,也让我们认识到软件测试有有趣性,对软件测试领域有了不一样的认识。
9.Doxygen 生成的注释文档
posted on 2016-04-15 16:38  Compilers  阅读(282)  评论(2)    收藏  举报