Kod Java z testami - nieskończona pętla?
Staram się nawiązać relacje między ludźmi. Jednak po uruchomieniu testu jednostkowego test działa wiecznie, nie uzyskuje wyniku i moje użycie procesora jest wysokie. Czy ktoś może zobaczyć, co jest nie tak z moim kodem?
Relacje łańcuchowe są wieloma wejściami liniowymi łańcucha w formacie"A , B C , D"
gdzieA
jest rodzicemB
iC
jest rodzicemD
.
Jest to domyślny konstruktor kodu i wejścia w formacie łańcuchowym. Nie musimy sprawdzać, czy format jest poprawny:
public SeeRelations(String relations){
this.relations = relations;
}
Jest to funkcja pomocnicza, która pobiera każdą linię ciągu z sformatowanego wejścia:
//helper function to get each line of the string
private ArrayList<String> lineRelations(){
int i;
ArrayList<String> lineRelations = new ArrayList<String>();
String[] lines = relations.split("\n");
for(i = 0; i < lines.length; i++){
lineRelations.add(lines[i]);
}
return lineRelations;
}
Jest to funkcja, która umieszcza wszystkie relacje z wejściowego ciągu sformatowanego na arraylists:
//helper function to put each of the relationship in arraylists
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;
}
Jest to metoda sprawdzenia, czy nazwa wejściowa istnieje:
//helper function to see if the name exist for seeRelations()
private boolean hasThisName(String name){
ArrayList<ArrayList<String>> allRelations = allRelations();
int i;
int j;
for(i = 0; i < allRelations.size(); i++){
for(j = 0; j < allRelations.get(i).size(); j++){
if(name.equals(allRelations.get(i).get(j))){
return true;
}
}
}
return false;
}
Jest to funkcja, aby uzyskać numer generacji między dwiema osobami:
//helper function to get Generation number of seeRelations()
private int getGenerationNum(String person, String ancestor){
ArrayList<ArrayList<String>> allRelations = allRelations();
String name;
int i;
int j;
int generationNum = 0;
for(i = 0, j = 0, name = ancestor; i < allRelations.size(); i++){
if(name.equals(allRelations.get(i).get(0)) && !person.equals(allRelations.get(i).get(1))){
generationNum++;
ancestor = allRelations.get(i).get(1);
i = 0;
j = 1;
}
else if(ancestor.equals(allRelations.get(i).get(0)) && person.equals(allRelations.get(i).get(1))){
generationNum++;
j = 1;
break;
}
}
if(j == 0){
return 0;
}
else{
return generationNum;
}
}
Jest to metoda uzyskiwania wielokrotności"great"
dla końcowego wyniku:
private String great(int num){
int i;
String great = "";
for(i = 0; i < num; i++){
great += "great";
}
return great;
}
To moja ostatnia metoda sprawdzenia relacji między dwiema osobami:
public String SeeRelations(String person, String ancestor){
int generationNum = getGenerationNum(person, ancestor);
String great = great(generationNum - 2);
if(!(hasThisName(person) && hasThisName(ancestor))){
return null;
}
else{
if(generationNum == 0){
return null;
}
else if(generationNum == 1){
return ancestor + " is the parent of " + person;
}
else if(generationNum == 2){
return ancestor + " is the grandparent of " + person;
}
else{
return ancestor + " is the" + " " + great +"grandparent of " + person;
}
}
}
To są moje przypadki testowe, które działają wiecznie i nie mogą uzyskać wyniku
public class FamilyTreeTest {
@Test
public void testSeeRelations() {
FamilyTree relation2 = new FamilyTree("John Doe , Mary Smith" + "\n" + "Martin Weasel , John Doe");
assertEquals("Martin Weasel is the grandparent of Mary Smith", familyTree2.SeeRelations("Mary Smith", "Martin Weasel"));
}