gpt4 book ai didi

java - 'Merge Sort on doubly linked list' 的单元测试

转载 作者:行者123 更新时间:2023-12-01 10:31:49 25 4
gpt4 key购买 nike

需要创建单元测试请帮忙当我创建单元测试时,它显示错误“测试中不允许使用参数”

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

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

}

最佳答案

单元测试应该独立于其他所有内容来测试单个代码单元。您正在向测试传递一个参数:

@Test
public void test(Node head) {

只需在测试中创建头节点并且不带参数:

@Test
public void test() {

关于java - 'Merge Sort on doubly linked list' 的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35047844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com