Can someone write a JUnit test for my CircularArrayQueue class that implements I
ID: 3735977 • Letter: C
Question
Can someone write a JUnit test for my CircularArrayQueue class that implements ItemQueue. I use EclipseIDE and Java, Thank You.
CircluarArrayQueue.java:
import java.util.NoSuchElementException;
/**
* Circular array implementation of a queue
*
* @author
*
*/
public class CircularArrayQueue<E> implements ItemQueue<E> {
private static final int capacity = 4;
private Object[] Q;
private int N; // capacity
private int f = 0;
private int r = 0;
public CircularArrayQueue() {
this(capacity);
}
public CircularArrayQueue(int capacity) {
N = capacity;
Q = new Object[N];
}
@Override
public void enqueueItem(E item) {
if (isFull()) {
throw new NoSuchElementException("Queue is Full.");
} else {
Q[r] = item;
r = (r + 1) % N;
}
}
@SuppressWarnings("unchecked")
@Override
public E dequeueItem() {
E item;
if (isEmpty()) {
throw new NoSuchElementException("Queue is Empty.");
} else {
item = (E) Q[f];
Q[f] = null;
f = (f + 1) % N;
}
return (E) item;
}
@SuppressWarnings("unchecked")
@Override
public E peekItem() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is Empty.");
} else {
return (E) Q[r];
}
}
/**
* check for empty queue
*
*/
@Override
public boolean isEmpty() {
return (r == f) ? true : false;
}
/**
* check for full queue
*
*/
public boolean isFull() {
int diff = r - f;
if (diff == -1 || diff == (N - 1))
return true;
return false;
}
/**
* check number of items in the queue
*
*/
@Override
public int noItems() {
if (r > f)
return r - f;
return N - f + r;
}
}
ItemQueue.java:
/**
* An abstract data type for a Queue. Specifies the 5
* main methods of a queue
*
* @author
*/
public interface ItemQueue<E> {
/**
* Enqueue an object to the end of the queue
* @param item the object to be enqueued
*/
public void enqueueItem(E item);
/**
* Dequeue an object from the front of the queue
* @return the item removed from the front of the queue
* @throws NoSuchElementException if the queue is empty
*/
public E dequeueItem();
/**
* Examine the object at the head of the queue, but do
* not remove it.
* @return the item at the head of the queue
* @throws NoSuchElementException if the queue is empty
*/
public E peekItem();
/**
* Test if the queue is empty
* @return true if the queue is empty, otherwise false
*/
public boolean isEmpty();
/**
* Get the number of items in the queue
*
* @return the number of items in the queue
*/
public int noItems();
}
Explanation / Answer
Please find the program below.
Following issues have been identified during Unit Testing :
1) Peek() was returning the rear element whereas it should return the head element
2) noItems() were returning the capacity even if the queue is empty.
I have added fix for these issue as well.
Total 26 Unit Test cases are added to test the class. TESTING has been done for TWO different data types. Integer and String.
UNIT TEST CLASS :
==============
=======================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.