首页 > 编程语言 > 详细

PairWise算法 JAVA实现

时间:2021-04-05 00:53:12      阅读:35      评论:0      收藏:0      [点我收藏+]

PairWise算法,基本思想是每一个测试用例至少会出现一个新的二元组(我瞎编的名词,懂得起我的意思即可),举个栗子:

技术分享图片

上图中A,B,C列表示他们的取值,每一行表示一个测试用例,深色标记的测试用例即为不符合PairWise的用例。以第24个测试用例举例,它有“23”,“24”,“34"共三个二元组,但第21、第16、第12个个测试用例中都已经出现过,所以没有出现新的二元组,不符合PairWise方法。

JAVA实现代码如下:

import java.util.HashMap;

/**
 * PairWise(成对)测试方法
 * author: likeqc
 * date: 2021-4-4 11:06:59
 */
public class PairWise {
    /**
     * PairWise方法
     * @param str String[][],二维数组,一维数组 str[i] 中存放第 i 个因素的因子
     */
    private static void solution(String[][] str) {
        if (str == null) {
            return;
        }
        // 传统方式测试用例数量
        int sum = 1;
        // 符合要求的测试用例数量
        int count = 0;
        HashMap hashMap = new HashMap();
        for (int i = 0; i < str.length; i++) {
            if (str[i].length < 1) {
                System.out.println("输出的数据错误!");
                return;
            }
            sum *= str[i].length;
        }
        int[] one = new int[str.length];
        for (int i = 0; i < sum; i++) {
            // 创造一个新的测试用例(传统方法,暴力所有可能的测试用例)
            int carry = 1;
            for (int j = str.length - 1; j >= 0; j--) {
                if (i == 0) {
                    continue;
                }
                one[j] = (one[j] + carry) % str[j].length;
                if (carry == 1 && one[j] == 0) {
                    carry = 1;
                } else {
                    carry = 0;
                }
            }
            // 测试该测试用例是否能够产生新的配对组
            boolean flag = false;
            for (int j = 0; j < str.length; j++) {
                for (int k = j + 1; k < str.length; k++) {
                    String key = j + "_" + str[j][one[j]] + "," + k + "_" + str[k][one[k]];
                    // System.out.println(key);
                    if (hashMap.get(key) == null) {
                        flag = true;
                        // System.out.println("新的key:" + key);
                        hashMap.put(key, 1);
                    }
                }
            }

            // 产生了新的配对组,说明该用例符合 PairWise 规则,输出
            if (flag) {
                count++;
                System.out.print("测试用例" + count + ":" + str[0][one[0]]);
                for (int j = 1; j < str.length; j++) {
                    System.out.print("," + str[j][one[j]]);
                }
                System.out.println();
            }
        }
        System.out.println("PairWise方法测试用用例:" + count);
        System.out.println("传统方法测试用用例:" + sum);
    }

    public static void main(String[] args) {
        solution(new String[][]{{"T", "F"}, {"1", "2", "3"}, {"a", "b", "c", "d"}});
    }
}

PairWise算法 JAVA实现

原文:https://www.cnblogs.com/likeqc/p/14617054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!