I have this method, which will search a LinkedList(named ListNode) and check for chars and check if they contains uppercase chars, then store then in a new linkedlist, and return that. I wrote the code for it, tested it with JUnit, but it failed JUNit(On of those blue boxes). Does anyone know what went wrong?
Here is my LinkedList method:
public static ListNode copyUpperCase(ListNode head) {
ListNode newListNode = mkEmpty();
if(head == null){
throw new ListsException("");
}else{
while(head.next != null){
if(Character.isUpperCase(head.element)){
newListNode.element = head.element;
}
head = head.next;
}
}
return newListNode;
}
Here is ListNode:
public class ListNode {
public char element;
public ListNode next;
}
And here is the test method:
@Test
public void testCopyUpperCase()
{
// Inject upper case letters randomly in the test strings.
// Assert equal results when extracting the upper case chars from
// the corresponding list, as wheen extracting them from the
// injected string itself.
for ( String s : cases ) {
String uppersAndLowers = randInjectUpper(s);
// Extract the upper case characters
StringBuilder uppers = new StringBuilder();
for ( int i = 0; i < uppersAndLowers.length(); i++ ) {
final char c = uppersAndLowers.charAt(i);
if ( Character.isUpperCase(c) )
uppers.append(c);
}
ListNode temp = Lists.toList(uppersAndLowers);
ListNode lhs = Lists.copyUpperCase(temp);
assertFalse(hasSharedNodes(temp,lhs));
ListNode rhs = Lists.toList(uppers.toString());
assertTrue(Lists.equals(lhs,rhs));
}
}
THe failed line in testmethod were the last, which is:
assertTrue(Lists.equals(lhs,rhs));
What does it mean, if it failed at that line?
ps. here is the equals method also:
// Two lists are equal if both are empty, or if they have equal lengths
// and contain pairwise equal elements at the same positions.
public static boolean equals(ListNode l1,ListNode l2) {
if ( isEmpty(l1) && isEmpty(l2) )
return true;
else if ( isEmpty(l1) || isEmpty(l2) )
return false;
else { // both lists are non-empty
ListNode p1 = l1.next, p2 = l2.next;
while ( p1 != null && p2 != null ) {
char c1 = p1.element, c2 = p2.element;
if ( p1.element != p2.element )
return false;
p1 = p1.next;
p2 = p2.next;
}
return p1 == null && p2 == null;
}
}
EDIT: This is the new method:
public static ListNode copyUpperCase(ListNode head) {
ListNode newListNode = mkEmpty();
if(head == null){
throw new ListsException("Lists: null passed to copyUpperCase");
}else{
String cpy = toString(head);
char[] chry = cpy.toCharArray();
for(int i = 0; i < chry.length ; i++ )
if(Character.isUpperCase(chry[i])){
newListNode.element = chry[i];
}
newListNode = newListNode.next;
}
return newListNode;
}