mercredi 27 janvier 2016

Unit Test for 'Merge Sort on doubly linked list'

Need to create unit test please help When i created unit test, it shows error "No parameters allowed in test"

package MergeDoublyLL;

import java.util.ArrayList;
import java.util.Iterator;

public class doublell {

    static Node head;

    static class Node {

        int data;
        Node next, prev;

        Node(int d) {
            data = d;
            next = prev = null;
        }
    }

    Node split(Node head) {
        Node fast = head, slow = head;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        Node temp = slow.next;
        slow.next = null;
        return temp;
    }

    Node mergeSort(Node node) {
        if (node == null || node.next == null) {
            return node;
        }
        Node second = split(node);

        node = mergeSort(node);
        second = mergeSort(second);

        return merge(node, second);
    }

    Node merge(Node first, Node second) {
        if (first == null) {
            return second;
        }

        if (second == null) {
            return first;
        }

        if (first.data < second.data) {
            first.next = merge(first.next, second);
            first.next.prev = first;
            first.prev = null;
            return first;
        } else {
            second.next = merge(first, second.next);
            second.next.prev = second;
            second.prev = null;
            return second;
        }
    }
    int[] print(Node node) {
        int a;
        ArrayList<Integer> al = new ArrayList<Integer>();
        Node temp = node;
        System.out.println("Forward Traversal using next pointer");
        while (temp != null) {
            a=(temp.data);
            al.add(a);
            temp = temp.next;
        }
        int[] ret = new int[al.size()];
        Iterator<Integer> iterator = al.iterator();
        for (int i = 0; i < ret.length; i++)
        {
            ret[i] = iterator.next().intValue();
        }
        return ret;
    }
    public static void main(String[] args) {

        doublell list = new doublell();
        doublell.head = new Node(10);
        doublell.head.next = new Node(30);
        doublell.head.next.next = new Node(3);
        doublell.head.next.next.next = new Node(4);
        doublell.head.next.next.next.next = new Node(20);
        doublell.head.next.next.next.next.next = new Node(5);

        Node node;
        node=head;
        node = list.mergeSort(head);
        System.out.println("Linked list after sorting :");
        int[] result =list.print(node);
        for (int j=0; j<result.length;j++)
            System.out.print(result[j]);
    }
}


Unit Test:

// Unit Test

package MergeDoublyLL;

import static org.junit.Assert.assertArrayEquals;

import org.junit.Test;

import MergeDoublyLL.doublell.Node;

public class testdoublell {

    @Test
    public void test(Node head) {
        doublell list = new doublell();
        doublell.head = new Node(10);
        doublell.head.next = new Node(30);
        doublell.head.next.next = new Node(3);
        doublell.head.next.next.next = new Node(4);
        doublell.head.next.next.next.next = new Node(20);
        doublell.head.next.next.next.next.next = new Node(5);

        Node node=head;
        node = list.mergeSort(head);
        int result[] =list.print(node);
        int expectedArray[] = {3,4,5,10,20,30};
        assertArrayEquals(expectedArray, result);
    }

}

Aucun commentaire:

Enregistrer un commentaire