我有一个名为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));
}
}
在我进行测试后,测试失败了,它说它是&#34;预期&lt; false&gt;但是:&lt; true&gt; &#34;所以我的MySet类中的add方法中的两个数组可能不等于它们的实际总和,MySet类中的equals方法,如果两个数组相等,方法没有正确检查,或者测试本身(我认为问题出在测试中)。我只是想知道我做错了什么,我该如何解决?
答案 0 :(得分:1)
您并未真正检查数组,而是自定义对象MySet
。很遗憾,您尚未正确实施equals()
/ hashCode()
/ toString()
。
修复您的MySet.equals
签名。它必须覆盖Object
方法boolean equals(Object)
而不是Boolean equals(MySet a)
。您可以应用@Override
注释以确保正确覆盖此方法。如果没有这个,大多数JUnit
方法都会失败。
添加相应的int hashCode()
方法。
添加将由断言消息使用的String toString()
。
更改assertEquals(set1.add((MySet)set2).equals(sum), sum.equals(sum));
应写成:assertEquals(sum, set1.add((MySet)set2));
。
您可以尝试使用assertArrayEquals()
而不是assertEquals()
来改进断言消息。您还可以尝试更现代的断言库,例如AssertJ