链表测试....返回类型问题

我有这段代码是IntLinkedList类

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;
        }
    }
}

我正在尝试使用以下JUnit代码测试复制方法

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());
    }
}

我想从Copy()方法获取IntLinkedList类返回的列表,并在JUnit中对其进行测试。我也尝试返回IntLinkedList和Object类型,但我不断收到诸如“类型不匹配:无法从IntLinkedList.Node转换为IntLinkedList”之类的错误。我对LinkedList的经验很少,但是对Java类,对象的引用却很有经验,但这对我来说是新领域。有人可以帮忙吗?

ether1984 回答:链表测试....返回类型问题

解决方案:-您正在将Node类toString与IntLinkedList类toString进行比较,从而导致Junit失败,请尝试覆盖Node和IntLinkedList的toString()方法类,您会清楚地看到堆栈跟踪为

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=null]]]]]>

此Junit可以按预期运行

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

修改:- 由于您的Node类是私有的,因此我做了一个小的更改以使IntLinkedList.Node起作用,因此我将签名更改为static来使junit起作用,即

static class Node  {
,

这是最终的IntLinkedList copy()方法,可以更好地解决我的问题。我只发布了copy()方法,因为IntLinkedList类保持不变,而JUnit测试保持不变。注意:在IntLinkedList类中,唯一更改的是copy()方法

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;
}
,

嗯。

您的copy()应该不会返回Node而是IntLinkedList 因此,您需要相应地调整copy()的代码。

此外,在您的equals()类中实现hashCode()(和IntLinkedList)。 equals实现应符合您对两个IntLinkedList相等的期望。

稍后在测试中,使用InLinkedList copy = copy(orig)并将结果与​​Assert.equals(orig,copy)进行比较。

本文链接:https://www.f2er.com/3147018.html

大家都在问