如何使用集合的组合作为测试数据 [英] How to use combinations of sets as test data

查看:68
本文介绍了如何使用集合的组合作为测试数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一组附带情况和正常值的元组测试一个函数.例如,在测试一个函数时,只要给定三个构成有效三角形的长度,它就会返回true,我会遇到一些特殊情况,例如负数/小数/大数,接近溢出的值等.而且,主要目的是生成这些值的组合, with 不重复,以便获得一组测试数据.

I would like to test a function with a tuple from a set of fringe cases and normal values. For example, while testing a function which returns true whenever given three lengths that form a valid triangle, I would have specific cases, negative / small / large numbers, values close-to being overflowed, etc.; what is more, main aim is to generate combinations of these values, with or without repetition, in order to get a set of test data.

(inf,0,-1), (5,10,1000), (10,5,5), (0,-1,5), (1000,inf,inf),
...

请注意:我实际上知道此问题的答案,但这可能对其他人有帮助,对这里的人也是一个挑战! -稍后会发布我的答案.

推荐答案

当然,尤其是处理许多这样的排列/组合时,我绝对可以确定第一遍是一个问题.

Absolutely, especially dealing with lots of these permutations/combinations I can definitely see that the first pass would be an issue.

有趣的python实现,尽管我基于"Algorithm 515"在C和Ocaml中编写了一个不错的实现(请参见下文).他在Fortran中写了他的著作,因为那是所有算法XX"论文(当时是汇编语言或c语言)的普遍用法.我不得不重新编写它,并做了一些小的改进以使用数组而不是数字范围.这是一个随机访问,我仍在努力获得Knuth第4卷第2卷中提到的方法的一些不错的实现.我将向读者解释这种方法的工作原理.尽管如果有人好奇,我不会反对写些东西.

Interesting implementation in python, though I wrote a nice one in C and Ocaml based on "Algorithm 515" (see below). He wrote his in Fortran as it was common back then for all the "Algorithm XX" papers, well, that assembly or c. I had to re-write it and make some small improvements to work with arrays not ranges of numbers. This one does random access, I'm still working on getting some nice implementations of the ones mentioned in Knuth 4th volume fascicle 2. I'll an explanation of how this works to the reader. Though if someone is curious, I wouldn't object to writing something up.

/** [combination c n p x]
 * get the [x]th lexicographically ordered set of [p] elements in [n]
 * output is in [c], and should be sizeof(int)*[p] */
void combination(int* c,int n,int p, int x){
    int i,r,k = 0;
    for(i=0;i<p-1;i++){
        c[i] = (i != 0) ? c[i-1] : 0;
        do {
            c[i]++;
            r = choose(n-c[i],p-(i+1));
            k = k + r;
        } while(k < x);
        k = k - r;
    }
    c[p-1] = c[p-2] + x - k;
}

〜算法515:根据词典索引生成向量"; Buckles,B. P.和Lybanon,M. ACM在数学软件上的交易,第1卷. 1977年6月,第2号,第3号.

~"Algorithm 515: Generation of a Vector from the Lexicographical Index"; Buckles, B. P., and Lybanon, M. ACM Transactions on Mathematical Software, Vol. 3, No. 2, June 1977.

这篇关于如何使用集合的组合作为测试数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆