Tôi cố gắng để viết bài kiểm tra đơn vị cho một lớp BinarySearchTree
Các keys()
trả về một Iterable
Nó sử dụng một lớp được gọi là Queue
trong đó các phím được xếp hàng và trở về ..junit bình đẳng thử nghiệm của một Iterable
Các Queue (lớp bên thứ ba) tuy nhiên không có bất kỳ bằng() được xác định.
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);
}
...
}
cố gắng để viết đơn vị kiểm tra
@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);
}
này không
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)
Tôi có phải thực hiện một equals() trong lớp bên thứ ba hoặc là có cách nào khác để làm điều này để kiểm tra bình đẳng? Tất cả tôi có thể nghĩ ra được chạy một vòng lặp dequeueing từ hàng đợi q và so sánh nó với những gì returned.I iterator không chắc chắn nếu có một cách tốt hơn .. Xin cho biết ..
Iterable<String> actual = st.keys();
Iterator<String> actualit = actual.iterator();
while(actualit.hasNext()){
String actualkey = actualit.next();
String exp = q.dequeue();
assertEquals(actualkey,exp);
}
[So sánh hai bộ sưu tập trong Java] (http://stackoverflow.com/questions/4085353/comparing-two-collections-in-java) –