English 中文(简体)
调试/测试两个阵列的调试代码
原标题:Debugging/Testing code of two arrays

我做了一个合并试验,将数组合并为3个部分,而不是2个部分,以便进行分类,我还写了一些代码来测试我的合并试验是否正常。这是测试的代码:

while (true){

    Random rand = new Random();

    int[] randArray = new int[rand.nextInt(2000)];

    for (int i = 0; i < randArray.length; i++) {
        randArray[i] = rand.nextInt();
    }

    int[] temporary = new int[randArray.length];

    System.arraycopy(randArray, 0, temporary, 0, randArray.length);

    mergesort(randArray);

    Arrays.sort(temporary);

    if (Arrays.equals(randArray, temporary)){
        System.out.println("YES");
    } else {
       break;
    }

    }       
}

我所做的是每次创建最多为 2000 整数的数组,用随机整数填充数组,然后在临时数组中存储该数组,然后在使用原始数组中的合并数组之后,对临时数组使用 Arrays.sort () 方法,如果两个数组等于打印 " 是 ",或者从循环时断开。

现在它还没有从循环中打破 并且表明我的算法是正确的 所以我问我是否在这里 写了正确的测试代码

它在不断打印 "是",没有打破环环,所以我相信它有效,但我请求,这样我才能确定

最佳回答

您的测试代码对我来说似乎不错, 这意味着您的算法也不错。 另一方面, 请不要误解我, 但这种测试只能被视为快速和肮脏的检查。 如果您想做适当的测试, 您应该测试以下内容:

  • happy path with simple examples
  • boundary conditions
  • error conditions

您也应该考虑使用JUM 这样的自动测试框架。

最后,在测试中使用随机输入不是一个好的做法, 因为测试的结果可以从运行到运行。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签