gpt4 book ai didi

java - 如何在 JUnit 测试中测试两个数组是否相等

转载 作者:行者123 更新时间:2023-11-30 06:14:10 25 4
gpt4 key购买 nike

我有一个名为 MySet 的类,该类对数组执行各种操作,解释一下,它对数组与其他数组进行加、减、乘和除。我想测试一下数组添加方法,看看它是否正确运行。

这是 MySet 类:

封装实验室;

import java.util.Arrays;

public class MySet implements MyMath<MySet> {
int count = 0;
int[] firstArray = new int[count];

public MySet(int[] x) {

int count = 0;
Arrays.sort(x);
for (int index = 0; index < x.length - 1; index++) {
if (x[index] != x[index + 1]) {
count++;
}
}

firstArray = addToArray(x, count + 1);
}
public int[] addToArray(int[] x, int y) {
int counter = 0;
int[] changedArray = new int[y];
for (int index = 0; index < x.length - 1; index++) {
if (x[index] != x[index + 1]) {
changedArray[counter] = x[index];
counter++;
}

}
changedArray[changedArray.length - 1] = x[x.length - 1];
return changedArray;
}

public Boolean equals(MySet a) {
if (a.firstArray.length != firstArray.length) {
return false;
}

int test1 = firstArray.length - 1;
int testValue = 0;
for (int index = 0; index < a.firstArray.length; index++) {
if (firstArray[index] == a.firstArray[index]) {
testValue++;
}
}
if (test1 == testValue) {
return true;
} else {
return false;
}
}

@Override
public MySet add(MySet o) {
int[] addedSet = new int[firstArray.length + o.firstArray.length ];
for (int index = 0; index < firstArray.length; index++) {
addedSet[index] = firstArray[index];
}
int counter = 0;
for (int index = firstArray.length; index <= firstArray.length - 1 +
o.firstArray.length; index++) {
addedSet[index] = o.firstArray[counter];
counter++;
}
Arrays.sort(addedSet);
addedSet = addToArray(addedSet, addedSet.length - 1);
MySet finalSetAdd = new MySet(addedSet);
return finalSetAdd;
}

@Override
public MySet subtract(MySet o) {
Arrays.sort(firstArray);
Arrays.sort(o.firstArray);
int count = 0;
int finalCount = 0;
int[] tester = addToArray(firstArray, firstArray.length - 1);
int[] testing = addToArray(o.firstArray, o.firstArray.length - 1);

for (int index = 0; index < testing.length; index++) {
for (int counter = 0; counter < tester.length; counter++) {
if (tester[index] != testing[counter]) {
count++;
}
}
if (count == tester.length) {
finalCount++;
}
count = 0;
}
int size = 0;
int[] finalOne = new int[finalCount];
for (int index = 0; index < o.firstArray.length; index++) {
for (int counter = 0; counter < firstArray.length; counter++) {
if (testing[index] != tester[counter]) {
finalOne[size] = testing[index];
size++;
finalOne = addToArray(finalOne, finalOne.length - 1);
}
}
}
MySet finalSetSub = new MySet(finalOne);
return finalSetSub;

}

@Override
public MySet divide(MySet o) {
Arrays.sort(firstArray);
Arrays.sort(o.firstArray);
int count = 0;
int finalCount = 0;
int[] tester = addToArray(firstArray, firstArray.length - 1);
int[] testing = addToArray(o.firstArray, o.firstArray.length - 1);

for (int index = 0; index < o.firstArray.length; index++) {
for (int counter = 0; counter < firstArray.length; counter++) {
if (testing[index] == tester[counter]) {
count++;
}
}
if (count == tester.length) {
finalCount++;
}
count = 0;
}
int size = 0;
int[] finalOne = new int[finalCount];
for (int index = 0; index < o.firstArray.length; index++) {
for (int counter = 0; counter < firstArray.length; counter++) {
if (testing[index] == tester[counter]) {
finalOne[size] = testing[index];
size++;
finalOne = addToArray(finalOne, finalOne.length - 1);

}
}
}
MySet finalSetSub = new MySet(finalOne);
return finalSetSub;
}

@Override
public MySet multiply(MySet o) {
Arrays.sort(firstArray);
Arrays.sort(o.firstArray);
int count = 0;
int finalCount = 0;
int[] tester = addToArray(firstArray, firstArray.length - 1);
int[] testing = addToArray(o.firstArray, o.firstArray.length - 1);

for (int index = 0; index < o.firstArray.length; index++) {
for (int counter = 0; counter < firstArray.length; counter++) {
if (testing[index] != tester[counter]) {
count++;
}
}
if (count == firstArray.length - 1) {
finalCount++;
}
count = 0;
}
int counter1 = 0;
int size = 0;
int[] finalOne = new int[finalCount];
for (int index = 0; index < o.firstArray.length; index++) {
for (int counter = 0; counter < firstArray.length; counter++) {
if (testing[index] != tester[counter]) {
counter1++;
}
}
if (counter1 == firstArray.length - 1) {
finalOne[size] = o.firstArray[index];
size++;
}

}
MySet finalSetSub = new MySet(finalOne);
return finalSetSub;
}

public String toString(int x) {
return "Value: " + firstArray[x];
}

}

这是我对 add 方法的测试:

package test;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

import lab.MyMath;
import lab.MySet;

class MySetTest {
@Test
public void testMysetAdd() {
int[] set1Arr = {1,2,6};
MyMath<MySet> set1 = new MySet(set1Arr);
int[] set2Arr = {4,5,6};
MyMath<MySet> set2 = new MySet(set2Arr);
int[] s = {1,2,4,5,6};
MyMath<MySet> sum = new MySet(s);
assertEquals(set1.add((MySet)set2).equals(sum), sum.equals(sum));
}
}

运行测试后,测试失败,显示“预期 但为: ”,因此 MySet 类中的 add 方法中存在错误,其中两个数组可能不是等于它们的实际总和,MySet 类中的 equals 方法,该方法不能正确检查两个数组是否相等,或者在测试本身中(我认为问题出在测试中)。我只是想知道我做错了什么以及如何解决它?

最佳答案

您实际上并没有检查数组,而是检查自定义对象MySet。不幸的是,您没有正确实现 equals()/hashCode()/toString()

  1. 修复您的 MySet.equals 签名。它必须重写 Object 方法 boolean equals(Object) 而不是 Boolean equals(MySet a)。您可以应用@Override注释来确保此方法被正确覆盖。如果没有这个,大多数 JUnit 方法都会失败。

  2. 添加相应的int hashCode()方法。

  3. 添加一个将由断言消息使用的String toString()

  4. 更改assertEquals(set1.add((MySet)set2).equals(sum), sum.equals(sum));应写为:assertEquals(sum, set1.add((MySet)set2));

  5. 您可以尝试使用 assertArrayEquals() 而不是 assertEquals() 来改进断言消息。您还可以尝试更现代的断言库,例如AssertJ .

关于java - 如何在 JUnit 测试中测试两个数组是否相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49593192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com