Could anyone tell me / explain how can I make a proper test of a Dequeue?
I have implemented a Priority Queue and in order to verify it I have done some junit tests.
I’m rather new to java so maybe I’m making some huge mistakes when trying to verify my implementation of a priority queue.
The test code :
@Test
public void testDequeue() throws MyException {
System.out.println("Dequeue");
PQueue q=new PQueue();
PQueue o=new PQueue();
q.Enqueue("abc", 1); // Enqueue with an object and a priority
q.Dequeue();
System.out.println(q.dim()); // to see if the dequeue worked
o.Enqueue("def", 2);
assertTrue(o.equals(q));
}
Pqueue Code:
public class PQueue<E> implements IPQueue<E>,Serializable{
private int size,front,rear;
private LinkedList<ListNode> list;
public PQueue()
{
front=0;
rear=0;
list=new LinkedList<ListNode>();
}
public void Enqueue(E obj, int p) throws MyException
{
if (obj==null) throw new MyException("Did not enqueued");
if (rear==0)
{
front=rear=1;
list.add(new ListNode(obj, p));
}
else
{
rear++;
int x= list.size();
for(int i=0;i<x-1;++i)
{
if(list.get(i).GetPriority() < p) list.add(i, new ListNode(obj, p));
}
}
}
public E Dequeue() throws MyException
{
if(rear==0) throw new MyException("Cannot dequeue; queue is empty!");
rear--;
return (E) list.getLast();
}
public int IsEmpty()
{
if(rear==0)
return 1;
else
return 0;
}
public int IsFull()
{
if(rear-front+2>size)
return 1;
else
return 0;
}
public void MakeEmpty()
{
size=0;
}
public int dim()
{
return rear;
}
public LinkedList<ListNode> getList()
{
return list;
}
@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if (!(obj instanceof PQueue)) {
return false;
}
PQueue p = (PQueue)obj;
return (obj==p);
}
}
Your tests should test all possiblities of how the code may react to inputs. It is usually helpful to think about the testcases prior of coding the actual code which shall be tested. (Search for ‘Test Driven Development’ for a interesting, more dogmatic view on this issue)
I just wrote 4 tests: 2 testing the regular behavior, 2 testing exceptional cases.
I usually create some ‘
instance‘ member which I used for testing, which reduces each unit test by one line where I would otherwise have to create an instance (less code, less work).Do not test
ListNodein the code (that should be tested inListNodeTest).My tests below assume that
new ListNode(2,1).equals( new ListNode(2,1) ).One point, you may define
new ListNode<Integer>(2, 1)as astatic finalfor the test. I did not. Maybe I would have if I had used it 3 times…Some other notes:
Have a look at http://www.oracle.com/technetwork/java/codeconventions-135099.html#367. Method names in Java are supposed to start with a lowercase letter.
You may argue that I myself violate that convention by introducing underscores ‘_’ in the method names of testcase. I think thats handy, so I knowningly violate that convention for unit tests. Flame me for that.
Maybe you should also have a closer look at the junit FAQ http://junit.sourceforge.net/doc/faq/faq.htm.
You may think about changing the name of
PQueuetoPrioQueueorPriorityQueue.And I would heavily recommend to test the
equals()method thoroughly, in order to get from the code what you expect. Have a look whatequals()is usually supposed to do. You are also missing ahashCode()method, which is commonly implemented when overwritingequals()yourself.