Junit equality testing Iterable

I am trying to write unit tests for a class that BinarySearchTree keys()returns a Iterable. It uses another class called Queuein which the keys are in the queue and returned.

A queue (third party class), however, does not have equal () defined ones.

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 does not work

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 need to execute equals () in a third-party class, or is there any other way to do this to check for equality? All I could think of was to start a loop deactivating from the q queue and compare it with what the iterator returned. I am not sure if there is a better way. Please, inform us.

Iterable<String> actual = st.keys();
Iterator<String> actualit = actual.iterator();
while(actualit.hasNext()){
    String actualkey = actualit.next();
    String exp = q.dequeue();
    assertEquals(actualkey,exp);
}
+5
source share
3 answers

Hamcrest Matchers.contains ( ). :

assertThat(queue1.keys(), Matchers.contains("A", "C", "E", "H", "M", "R", "S", "X"));

, Iterable, .

+3

java.util.Arrays. , , Queue toArray. :

    assertTrue(Arrays.equals(queue1.toArray(),queue2.toArray()));

, apache:

Object[] o = IteratorUtils.toArray(queue1.iterator());
Object[] o2 = IteratorUtils.toArray(queue1.iterator());
assertTrue(Arrays.equals(o,o2));
0

.

Iterable ArrayList. arraylist . , , assertEquals (arrayList1, arrayList2). , 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);

   }

  }
0

All Articles