我的方法无法通过单元测试。我徒劳地盯着它看了5个小时。有人可以帮我看看它有什么问题吗?
PS:我下面代码中的 getAllRelations() 方法是将格式化的输入分隔为字符串 ArrayList 的数组列表,例如,如果我有这样的格式化输入(我在无法通过的测试用例中使用它)
String format = "John Doe , Mary Smith" + "\n" + "Brian William , John Doe" + "\n" + "Brian William ,Robert Andrew" + "\n" + "Mary Smith , Max Jackson";
在每一行中,第一个人是第二个人的父母。getAllRelations() 方法会将这些格式化的字符串拆分为数组列表,其中每个列表仅包含每行中的两个名称字符串(名称前后没有空格)作为其元素。例如 arraylist1 将是一个包含“John”和“Mary Smith”的列表。
这是我无法弄清楚出了什么问题的方法,我想用这种方法检查两个人是否共享同一个祖先。
private boolean hasSameAncestor(String person1, String person2){
ArrayList<ArrayList<String>> allRelations = allRelations();
int i = 0;
int j = 0;
String name1 = person1;
String name2 = person2;
String parent1;
String parent2;
for(i = 0, parent1 = ""; i < allRelations.size(); i++){
if(name1.equals(allRelations.get(i).get(1))){
parent1 = allRelations.get(i).get(0);
for(j = 0, name2 = person2, parent2 = ""; j < allRelations.size(); j++){
if(name2.equals(allRelations.get(j).get(1))){
parent2 = allRelations.get(j).get(0);
if(parent2.equals(parent1)){
return true;
}
else{
name2 = parent2;
j = 0;
}
}
}
name1 = parent1;
i = 0;
}
}
return false;
}
我不能通过的测试用例是这样的。
@Test
public void testHasSameAncestor()
FamilyTree familyTree4 = new FamilyTree("John Doe , Mary Smith" + "\n" + "Brian William , John Doe" + "\n" + "Brian William ,Robert Andrew" + "\n" + "Mary Smith , Max Jackson");
assertEquals(true, format.hasSameAncestor("Max Jackson", "Robert Andrew"));
}
我无法弄清楚我的功能有什么问题,有人可以帮助我吗?非常感谢。
可以粘贴到 Eclipse 以获取调试帮助的代码
package test;
import java.util.ArrayList;
import java.util.Arrays;
public class Test1 {
String test;
public Test1(String test){
this.test = test;
}
private ArrayList<String> lineRelations(){
int i;
ArrayList<String> lineRelations = new ArrayList<String>();
String[] lines = test.split("\n");
for(i = 0; i < lines.length; i++){
lineRelations.add(lines[i]);
}
return lineRelations;
}
private ArrayList<ArrayList<String>> allRelations(){
int i;
ArrayList<ArrayList<String>> allRelations = new ArrayList<ArrayList<String>>();
ArrayList<String> lineRelations = lineRelations();
for(i = 0; i < lineRelations.size(); i++){
ArrayList<String> eachLine = new ArrayList<String>(Arrays.asList(lineRelations.get(i).split("\\s*,\\s*")));
allRelations.add(eachLine);
}
return allRelations;
}
public boolean hasSameAncestor(String person1, String person2){
ArrayList<ArrayList<String>> allRelations = allRelations();
int i = 0;
int j = 0;
String name1 = person1;
String name2 = person2;
String parent1;
String parent2;
for(i = 0, parent1 = ""; i < allRelations.size(); i++){
if(name1.equals(allRelations.get(i).get(1))){
parent1 = allRelations.get(i).get(0);
for(j = 0, name2 = person2, parent2 = ""; j < allRelations.size(); j++){
if(name2.equals(allRelations.get(j).get(1))){
parent2 = allRelations.get(j).get(0);
if(parent2.equals(parent1)){
return true;
}
else{
name2 = parent2;
j = 0;
}
}
}
name1 = parent1;
i = 0;
}
}
return false;
}
}
测试用例
package test;
import static org.junit.Assert.*;
import test.Test1;
import org.junit.Test;
public class Test1Test {
@Test
public void testHasSameAncestor(){
Test1 test1 = new Test1("John Doe , Mary Smith" + "\n" + "Brian William , John Doe" + "\n" + "Brian William ,Robert Andrew" + "\n" + "Mary Smith , Max Jackson");
assertEquals(true, test1.hasSameAncestor("Max Jackson", "Robert Andrew"));
}
}