[英]junit testing equality of an Iterable
I am trying to write unit tests for a BinarySearchTree
class The keys()
return an Iterable
.It uses another class called Queue
in which the keys are enqueued and returned.. 我正在尝试为
BinarySearchTree
类编写单元测试keys()
返回一个Iterable
它使用另一个名为Queue
类,其中键被入队并返回。
The Queue (third party class) however doesn't have any equals() defined. 但是Queue(第三方类)没有定义任何equals()。
public class BinarySearchTree<Key extends Comparable<Key>,Value> {
Node root ;
private class Node{
private Key key;
private Value val;
private Node left;
private Node right;
private int N;
public Node(Key k, Value v,int N) {
super();
this.key = k;
this.val = v;
this.N = N;
}
}
public Iterable<Key> keys(){
Queue<Key> q = new Queue<Key>();
inOrder(root,q);
return q;
}
private void inOrder(Node x,Queue q){
if(x == null)return;
inOrder(x.left,q);
q.enqueue(x.key);
inOrder(x.right,q);
}
...
}
trying to write unit test 试图编写单元测试
@Test
public void testKeys(){
MyBST<String, Integer> st = new MyBST<String, Integer>();
st.put("S",7);
st.put("E",2);
st.put("X",8);
st.put("A",3);
st.put("R",4);
st.put("C",1);
st.put("H",5);
st.put("M",6);
Queue<String> q = new Queue<String>();
q.enqueue("A");
q.enqueue("C");
q.enqueue("E");
q.enqueue("H");
q.enqueue("M");
q.enqueue("R");
q.enqueue("S");
q.enqueue("X");
Iterable<String> actual = st.keys();
assertEquals(q,actual);
}
This fails 这失败了
java.lang.AssertionError: expected: std.Queue<A C E H M R S X > but was: std.Queue<A C E H M R S X >
at org.junit.Assert.fail(Assert.java:93)
at org.junit.Assert.failNotEquals(Assert.java:647)
at org.junit.Assert.assertEquals(Assert.java:128)
at org.junit.Assert.assertEquals(Assert.java:147)
at week4.MyBSTTests.testKeys(BSTTests.java:304)
Do I have to implement an equals() in the third party class or is there any other way to do this to check equality? 我是否必须在第三方类中实现equals(),还是有其他方法来检查相等性? All I could think of was running a loop dequeueing from queue q and comparing it with what the iterator returned.I am not sure if there is a better way.. Please advise..
我能想到的只是从队列q中运行一个循环,并将它与迭代器返回的内容进行比较。我不确定是否有更好的方法..请指教..
Iterable<String> actual = st.keys();
Iterator<String> actualit = actual.iterator();
while(actualit.hasNext()){
String actualkey = actualit.next();
String exp = q.dequeue();
assertEquals(actualkey,exp);
}
Use Hamcrest's Matchers.contains
(described here ). 使用Hamcrest的
Matchers.contains
( 这里描述)。 For example: 例如:
assertThat(queue1.keys(), Matchers.contains("A", "C", "E", "H", "M", "R", "S", "X"));
This will check the elements that the Iterable
returns without needing equality implemented on your queue class. 这将检查
Iterable
返回的元素,而不需要在队列类上实现相等。
you can use the utility class java.util.Arrays. 您可以使用实用程序类java.util.Arrays。 From what I remember the Queue interface has a toArray method.
从我记得Queue接口有一个toArray方法。 So it would be something like this:
所以它会是这样的:
assertTrue(Arrays.equals(queue1.toArray(),queue2.toArray()));
As it is a third party library, you could use apache commons: 由于它是第三方库,您可以使用apache commons:
Object[] o = IteratorUtils.toArray(queue1.iterator());
Object[] o2 = IteratorUtils.toArray(queue1.iterator());
assertTrue(Arrays.equals(o,o2));
This is how I did this. 这就是我这样做的方式。
I converted the Iterable to the ArrayList. 我将Iterable转换为ArrayList。 Then I made another arraylist of expected key values.
然后我制作了另一个预期键值的arraylist。 That way I am able to check if two arrayLists are equal using assertEquals(arrayList1, arrayList2).
这样我就可以使用assertEquals(arrayList1,arrayList2)检查两个arrayLists是否相等。 Here is the code I wrote for testing my preOrder traversal method.
这是我为测试preOrder遍历方法而编写的代码。
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.Test;
public class BSTTest
{
BST<Integer, String> binaryTree = new BST<Integer, String>();
@Test
public void preOrdertest()
{
binaryTree.put(87, "Orange");
binaryTree.put(77, "Black");
binaryTree.put(81, "Green");
binaryTree.put(89, "Blue");
binaryTree.put(4, "Yellow");
binaryTree.put(26, "white");
binaryTree.put(23, "Purple");
binaryTree.put(27, "Violet");
binaryTree.put(57, "red");
binaryTree.put(1, "crimson");
ArrayList<Integer> testList = new ArrayList<>();
testList.add(87);
testList.add(77);
testList.add(4);
testList.add(1);
testList.add(26);
testList.add(23);
testList.add(27);
testList.add(57);
testList.add(81);
testList.add(89);
Iterable<Integer> actual = binaryTree.preOrder();
ArrayList<Integer> actualList = new ArrayList<>();
if (actual != null)
{
for (Integer e : actual)
actualList.add(e);
}
assertEquals(testList, actualList);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.