I'm testing a alphabetically sort method for a Linked List of "Request" objects called LstRequest, and when I test in JUnit, it gives me failure, but when I tested by me own, the same test gives me true.
This is the tested method:
public static LstRequest sort(LstRequest lstRequest, int param){
for (int i=0; i<lstRequest.getSize()-1; i++){
for (int j=1; j<lstRequest.getSize()-i; j++){
if (lstRequest.getAt(j-1).compareOrder(lstRequest.getAt(j),param,0)){
lstRequest.insertAt(j-1,lstRequest.getAt(j));
lstRequest.removeAt(j+1);
}
}
}
return lstRequest;
}
and this is the method compareOrder() used in this method:
public boolean compareOrder(Request r, int param, int index){
if (param==1){
if (index!=this.source.length()){
if (this.source.charAt(index)>r.source.charAt(index)){
return true;
} else if (this.source.charAt(index)==r.source.charAt(index)){
return this.compareOrder(r,param,index+1);
} else {
return false;
}
} else {
return false;
}
} else if (param==2){
if (index!=this.target.length()){
if (this.target.charAt(index)>r.target.charAt(index)){
return true;
} else if (this.target.charAt(index)==r.target.charAt(index)){
return this.compareOrder(r,param,index+1);
} else {
return false;
}
} else {
return false;
}
} else {
if (index!=this.login.length()){
if (this.login.charAt(index)>r.login.charAt(index)){
return true;
} else if (this.login.charAt(index)==r.login.charAt(index)){
return this.compareOrder(r,param,index+1);
} else {
return false;
}
} else {
return false;
}
}
}
This is the JUnit test, it gives me the failure in the "Check by destination. " test:
public void testSort() {
assertEquals("Check sort by user.", true,
SharingCar.sort(lst1, 3).equals(sorted1User));
assertEquals("Check sort by origin.", true,
SharingCar.sort(lst1, 1).equals(sorted1Origin));
assertEquals("Check sort by destination.", true,
SharingCar.sort(lst1, 2).equals(sorted1Destination));
}
And this are the Linked List used for the test:
lst1.addLast(new Request("user1","Madrid","Barcelona"));
lst1.addLast(new Request("user2","Alicante","Barcelona"));
lst1.addLast(new Request("user3","Sevilla","Alicante"));
lst1.addLast(new Request("user4","Valencia","Sevilla"));
sorted1Destination.addLast(new Request("user3","Sevilla","Alicante"));
sorted1Destination.addLast(new Request("user1","Madrid","Barcelona"));
sorted1Destination.addLast(new Request("user2","Alicante","Barcelona"));
sorted1Destination.addLast(new Request("user4","Valencia","Sevilla"));
I've done another class, for testing the same and looking where is the mistake, but the results are contradictory, this is the class:
public static void main(String[] args) {
LstRequest lst1 = new LstRequest();
lst1.addLast(new Request("user1","Madrid","Barcelona"));
lst1.addLast(new Request("user2","Alicante","Barcelona"));
lst1.addLast(new Request("user3","Sevilla","Alicante"));
lst1.addLast(new Request("user4","Valencia","Sevilla"));
LstRequest sorted1Destination = new LstRequest();
sorted1Destination.addLast(new Request("user3","Sevilla","Alicante"));
sorted1Destination.addLast(new Request("user1","Madrid","Barcelona"));
sorted1Destination.addLast(new Request("user2","Alicante","Barcelona"));
sorted1Destination.addLast(new Request("user4","Valencia","Sevilla"));
SharingCar.sort(lst1,2).show();
System.out.println(SharingCar.sort(lst1, 2).equals(sorted1Destination));
}
And this are the results given:
As you can see, the results are correctly alphabetically sorted by destination, and the same comparation, gives true, when in JUnit gives false.
Does anybody know why it gives false, or if is there a mistake?
Aucun commentaire:
Enregistrer un commentaire