[英]What is the fastest way of testing correctness of a sorting function?
Using a genetic algorithm, I found this comparison list:使用遗传算法,我找到了这个比较列表:
compareAndSwap(x[0],x[2]);
compareAndSwap(x[3],x[4]);
compareAndSwap(x[2],x[4]);
compareAndSwap(x[0],x[3]);
compareAndSwap(x[2],x[3]);
compareAndSwap(x[1],x[3]);
compareAndSwap(x[1],x[2]);
compareAndSwap(x[0],x[1]);
compareAndSwap(x[3],x[4]);
but I need to test it if it works for all cases.但我需要测试它是否适用于所有情况。 Also number of array elements(currently 5) can grow up to 100 in some situations.
在某些情况下,数组元素的数量(当前为 5)可以增长到 100。 This would mean that number of cases to check against is growing fast like more than
pow(2,100)
.这意味着要检查的案例数量正在快速增长,超过
pow(2,100)
。
If I give an oppositely sorted array alone as a worst case, that doesn't check against any error about middle element x[2]
comparisons.如果我单独给出一个相反排序的数组作为最坏的情况,则不会检查关于中间元素
x[2]
比较的任何错误。 For example, 5,4,3,2,1 is sorted by some function into 1,2,3,4,5, by例如,5、4、3、2、1被某个函数排序为1、2、3、4、5,通过
compareAndSwap(x[0],x[4]);
compareAndSwap(x[1],x[3]);
alone and this certainly doesn't sort many cases of 5-element arrays.单独,这当然不会对 5 元素数组的许多情况进行排序。
Tried random number generators for sample arrays but not sure if its acceptable:为样本数组尝试了随机数生成器,但不确定它是否可以接受:
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_real_distribution<double> dist(0,1);
for(int k=0;k<500;k++)
{
std::vector<double> arraySorted;
for(int i=0;i<5;i++)
arraySorted.push_back(dist(rng));
//sortNetwork(arraySorted.data());
//if(!std::is_sorted(arraySorted.begin(),arraySorted.end()))
throw std::runtime_error("error");
}
even this can still miss some parts.即使这样仍然会遗漏某些部分。 Is there a fast way to test sorting algorithms?
有没有一种快速的方法来测试排序算法?
What if it was 1000 elements array?如果是 1000 个元素的数组呢? Are these tested using math, pen and paper within some theorems and known algorithms or using supercomputers?
这些是使用数学、笔和纸在某些定理和已知算法中还是使用超级计算机进行测试的?
Just some sample cases for 4 elements:只是 4 个元素的一些示例案例:
1 2 3 4
1 2 4 3
2 1 3 4
2 1 4 3
1 2 0 1
1 2 1 0
2 1 0 1
2 1 1 0
3 4 2 1
3 4 1 2
4 3 2 1
4 3 1 2
1 1 1 1
seems to have more than pow(2,n) cases.似乎不止 pow(2,n) 案例。
Can a sorting network be treated like a graph problem when generating test data, somehow?在生成测试数据时,排序网络是否可以被视为图形问题?
While you could check every single iteration of every single possible list, as you've pointed out this would be far too slow.虽然您可以检查每个可能列表的每次迭代,但正如您所指出的那样,这太慢了。 Testing is not about proving the algorithm correct , for that you'd need to do a proof.
测试不是证明算法正确,因为你需要做一个证明。 Testing is about reducing the possibility of a bug by testing all the places it might hide.
测试是通过测试它可能隐藏的所有位置来降低出现错误的可能性。 Testing rarely attempts to cover the entire possible space, but rather possible types of errors.
测试很少尝试覆盖整个可能的空间,而是覆盖可能的错误类型。
Here's some examples to exercise a sort function.下面是一些使用排序功能的示例。
Then there's erroneous inputs which should return an error rather than garbage.然后是错误的输入,应该返回错误而不是垃圾。 Garbage in, error out.
垃圾进,错误出。
And yes, randomize.是的,随机化。 Generate random valid lists of random valid sizes and then verify the result of the sort is in order.
生成随机有效大小的随机有效列表,然后验证排序结果是否有序。 This helps cover any cases you might have missed and avoids any bad assumptions you may have made.
这有助于涵盖您可能错过的任何情况,并避免您可能做出的任何错误假设。 This is particularly important when testing a function "black box" meaning the tester has no knowledge of its internals.
这在测试功能“黑匣子”时尤为重要,这意味着测试人员不了解其内部结构。 Every time you run more random lists against the function you further reduce the possibility there is a bug.
每次针对该函数运行更多随机列表时,都会进一步降低出现错误的可能性。
Be sure to output the random seed used so you can repeat the test if there is a failure.确保输出使用的随机种子,以便在失败时重复测试。
Finally, use test coverage to ensure your tests are hitting all lines and branches of the code.最后,使用测试覆盖率来确保您的测试符合代码的所有行和分支。 The code might be generated by an AI, but you can still do a coverage analysis on it to identify your testing gaps.
代码可能是由 AI 生成的,但您仍然可以对其进行覆盖分析,以确定您的测试差距。 Running a code beautifier over the probably unreadable AI generated code will help your understanding of where your need more tests.
在可能不可读的 AI 生成的代码上运行代码美化程序将有助于您了解需要更多测试的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.