简体   繁体   English

测试排序函数正确性的最快方法是什么?

[英]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.下面是一些使用排序功能的示例。

  • An empty list一个空列表
  • A single element list单个元素列表
  • A list with all zeros一个全零的列表
  • An ordered list有序列表
  • A reversed list反向列表
  • A list of all the same elements所有相同元素的列表
  • A very large list一个非常大的清单
  • A list with strange elements (for example, Unicode, negative numbers, overloaded numbers)包含奇怪元素的列表(例如,Unicode、负数、重载数字)

Then there's erroneous inputs which should return an error rather than garbage.然后是错误的输入,应该返回错误而不是垃圾。 Garbage in, error out.垃圾进,错误出。

  • A null pointer空指针
  • A list of nulls空值列表
  • A list which is too large (if your function has size limits)一个太大的列表(如果你的函数有大小限制)

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.

相关问题 在不排序的情况下搜索数组中重复项的最快方法是什么? - What's the fastest way to search for duplicates in an array without sorting it? 将递归 function 的结果连接到 Javascript 中的数组中的最快方法是什么? - What is the fastest way to concatenate results of a recursive function into an array in Javascript? 使用 Python 创建和排序时间戳数据的最快方法? - Fastest way of creating and sorting the timestamp data with Python? 重新排列数组的最快方法是什么 - What is the Fastest way to reorder an array 测试一个项目数组与另一个项目数组然后在javascript中对匹配项进行排序的最快,最有效的方法是什么? - What is the quickest and most efficient way of testing one array of items against another and then sorting the matches in javascript? Java中收集符号出现的最快方法是什么 - What is the fastest way to gather symbol occurances in java 计算数组中元素的最快方法是什么? - What is the fastest way to count elements in an array? 在PHP中将对象转换为数组的最快方法是什么? - What is the fastest way to convert an object to an array in PHP? 删除数组元素的最快方法是什么? - What is the fastest way to delete an element of an array? 阈值numpy数组的最快方法是什么? - What's the fastest way to threshold a numpy array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM