简体   繁体   English

如何实现一个测试程序(Java)来测试Csc2001LinkedList方法?

[英]How to implement a test program (in Java) to test Csc2001LinkedList methods?

I want to know how to implement a test program to test these methods thoroughly, and to do this by using two Csc2001LinkedList objects in the test program, one that is maintained as an ordered list, the other as an unordered list. 我想知道如何实现一个测试程序来彻底测试这些方法,并通过在测试程序中使用两个Csc2001LinkedList对象来做到这一点,一个对象保持为有序列表,另一个保持为无序列表。 One aspect of this that I'm aware of is to use the print method to check whether or not the methods are working properly. 我知道的这一方面是使用print方法来检查这些方法是否正常工作。

Class Csc2001LinkedList Csc2001LinkedList

public class Csc2001LinkedList {

    protected Node head;

    public Csc2001LinkedList(Node head)
    {
        this.head = null;
    }

    public Node getHead()
    {
        return head;
    }

    public void addFirst(char c)
    {
        head = new Node(c, head);
    }

    public void addAtEnd(char c)
    {
        Node n = new Node(c, head);
        Node temp = head;

        while(temp.next != null)
        {
            temp = temp.next;
        }
        temp.next = n;
    }

    public void addInOrder(char c)
    {
        Node n = new Node(c, head);

        if(isEmpty())
        {
            addFirst(c);
        }
        else
        {
            Node pre = head;
            Node succ = head.next;

            if(n.ch < pre.ch)
            {
                n.next = head;
                head = n;
            }
            else
            {
                while(succ != null && n.ch > succ.ch)
                {
                    pre = succ;
                    succ = pre.next;
                }
                n.next = succ;
                pre.next = n;
            }
        }
    }

    public boolean isEmpty()
    {
        return (head == null);
    }

    public char removeFirst()
    {
        Node temp = head.next;

        if(temp != null)
        {
            head.next = temp.next;
            return temp.ch;
        }
        else
        {
            System.out.println("List is empty");
            return '?';
        }
    }

    public void printList()
    {
        Node temp = head;

        while(temp != null)
        {
            System.out.println(temp.ch + " ");
            temp = temp.next;
        }
    }
}

class Node 节点

public class Node {

    protected char ch;
    protected Node next;

    public Node(char ch, Node next)
    {
        this.ch = ch;
        this.next = null;
    }

}

My suggestion would be to add a third class to your program. 我的建议是在您的程序中添加第三类。 This class would be a driver class, and will be used to test the list and node methods thoroughly. 此类将是驱动程序类,并将用于彻底测试列表和节点方法。 You can do this in one of two ways: 您可以通过以下两种方式之一执行此操作:

  • Create a very basic interface in the main method of the driver class. 在驱动程序类的main方法中创建一个非常基本的接口。 This method will allow you to add chars to the list through keyboard input, and will automatically print the list, and do anything else you want to do once you are done adding. 使用此方法,您可以通过键盘输入将字符添加到列表中,并且将自动打印列表,并在添加完成后执行其他操作。 This is a nice option because you don't have to mess with file input. 这是一个不错的选择,因为您不必弄乱文件输入。 However, it does make it difficult to test methods such as delete without putting a lot of work into the UI. 但是,这确实很难测试诸如删除之类的方法,而无需在UI中投入大量工作。
  • Create a method to read in values from files. 创建一种从文件中读取值的方法。 Create files of chars to test your methods with. 创建char文件以测试您的方法。 In the main method of the driver class, read in the file, add it to a list you create (either ordered or at the end), and print the list. 在驱动程序类的主要方法中,读取文件,将其添加到您创建的列表中(有序或末尾),然后打印该列表。 You can then test your other methods on a list whose size and characteristics you can control ahead of time. 然后,您可以在列表中测试其他方法,可以提前控制其大小和特征。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM