Jose Roman
Jose Roman

Reputation: 49

Linked List Testing....Return type issue

I have this code an IntLinkedList class

public class IntLinkedList {

    private Node head;

    public void addFirst(int data) {
        head = new Node(data, head);
    }

    public Node copy(){
       Node current = head; // used to iterate over original list
       Node newList = null; // head of the new list
       Node tail = null;    // point to last node in new list

       while (current != null)
       {
        // special case for the first new node
          if (newList == null)
          {
              newList = new Node(current.data, null);
              tail = newList;
          }
          else
          {
              tail.next = new Node(current.data, null);
              tail = tail.next;
          }
          current = current.next;
       }
       return newList;
    }


    private class Node  {
            int data;
            Node next;

        Node(int data, Node next) {
            this.data = data;
            this.next = next;
        }
    }
}

And I'm trying to test the copy method with the following JUnit code

public class IntLinkedListTest {

    /** Reference to linked list under test */
    private IntLinkedList lst;

    /** Creates a linked list for testing. */
    @Before
    public void setUp() {
        lst = new IntLinkedList();

        lst.addFirst(30);
        lst.addFirst(10);
        lst.addFirst(40);
        lst.addFirst(20);
    }

    /** Tests copying a non-empty list. */
    @Test
    public void testCopy() {
        IntLinkedList cpy = lst.copy();
        assertEquals(lst.toString(), cpy.toString());
    }
}

I want to get a list returned from the IntLinkedList class from the Copy() method and tested in JUnit. I tried returned type IntLinkedList and Object too but I keep getting errors like "Type mismatch: cannot convert from IntLinkedList.Node to IntLinkedList". I have very little experience with LinkedList but I'm experienced with java classes, references to objects, but this is new territory for me. Can anyone help?

Upvotes: 1

Views: 143

Answers (3)

Jose Roman
Jose Roman

Reputation: 49

This is the final IntLinkedList copy() method with a more elegant solution to my problem. I'm only posting the copy() method since the IntLinkedList class stays the same and JUnit testing stays the same. Note: In the IntLinkedList class the only thing that changes is the copy() method

public IntLinkedList copy(){
    IntLinkedList newList = new IntLinkedList();
    Node current = head;    // used to iterate over original list
    Node tail = null;   // point to last node in new list

    while (current != null)
    {
        // special case for the first new node
        if (newList.head == null)
        {
            newList.head = new Node(current.data, null);
            tail = newList.head;
        }
        else
        {
            tail.next = new Node(current.data, null);
            tail = tail.next;
        }
        current = current.next;
    }

    return newList;
}

Upvotes: 1

Bluddymarri
Bluddymarri

Reputation: 333

Hm.

Your copy() should imho not return Node but IntLinkedList So you need to adjust your code of copy() accordingly.

Also, implement equals() (and hashCode()) in your IntLinkedList class. The equals implementation should match your expectation of when two IntLinkedList are equal.

Later in the test, use InLinkedList copy = copy(orig) and compare the result with Assert.equals(orig, copy).

Upvotes: 0

Amit Kumar Lal
Amit Kumar Lal

Reputation: 5789

Solution:- You are comparing Node class toString with IntLinkedList class toString hence the Junit failure , Try overriding the toString() method of the Node and IntLinkedList class you will clearly see the stack trace as

org.junit.ComparisonFailure: expected:<[IntLinkedList [head=Node [data=20, next=Node [data=40, next=Node [data=10, next=Node [data=30, next=null]]]]]]> but was:<[Node [data=20, next=Node [data=40, next=Node [data=10, next=Node [data=30, next=null]]]]]>

This Junit is working as expected

 @Test
    public void testCopy() {
        IntLinkedList.Node cpy = lst.copy();
        assertEquals(lst.copy().toString(), cpy.toString());
    }

Edit:- I have made one minor change to make IntLinkedList.Node work since your Node class is private, hence I changed the signature to static to make the junit work i.e.

static class Node  {

Upvotes: 1

Related Questions