Мой проект структур данных по очереди требует прохождения всех тестов junit, кроме одного. тест «removeFrontRemoveRear». Он продолжает возвращать ноль вместо имени студента.
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Dequeue <E> {
private int front;
private int rear;
private E[] elements;
private static final int DEFAULT_CAPACITY = 5;
/**
* Constructor sets up an empty double-ended
* queue
*/
@SuppressWarnings("unchecked")
public Dequeue() {
elements = (E[]) new Object[DEFAULT_CAPACITY];
front = 0;
rear = elements.length - 1;
}
/**
* Inserts item at the front of the dequeue. Throws
* an exception if the item could not be inserted
* @param anEntry the item to be added (Student)
* @return true
*/
public boolean addFront(E anEntry) {
if (empty()) {
front = rear = 0;
elements[front] = anEntry;
}
else if (isFull()) {
reallocate();
front = (front + (elements.length - 1)) % elements.length;
}
else {
front = (front + (elements.length - 1)) % elements.length;
}
elements[front] = anEntry;
return true;
}
/**
* private method to check if the dequeue is full
* @return true only if dequeue is full (has
* 1 empty spot)
*/
private boolean isFull() {
if (size() == elements.length -1) {
return true;
}
return false;
}
/**
*Doubles and adds 1 to the size of the dequeue
*then reallocates the data.
*/
@SuppressWarnings("unchecked")
private void reallocate() {
E[] newData = (E[]) new Object[size() * 2 + 1];
int indexOfFront = front;
for(int i = 0; i < size(); i++) {
newData[i] = elements[indexOfFront];
indexOfFront = (indexOfFront + 1) % elements.length;
}
rear = size() - 1;
front = 0;
elements = newData;
}
/**
* Inserts item at the rear of the dequeue.
* Throws an exception if the item could not be inserted
* @param anEntry the entry being inserted into the end of the queue
* @return true
*/
public boolean addRear(E anEntry) {
if (empty()) {
front = rear = 0;
elements[rear] = anEntry;
}
else if (isFull()) {
reallocate();
rear = (rear + 1) % elements.length;
}
else {
rear = (rear + 1) % elements.length;
}
elements[rear] = anEntry;
return true;
}
/**
* This method checks if the dequeue is empty
* @return true if the dequeue is empty. otherwise
* returns false
*/
public boolean empty() {
if (front == 0 && rear == elements.length-1) {
return true;
}
return false;
}
/**
* @return the dequeue's iterator
*/
public Iterator<E> iterator() {
return new dequeueIterator();
}
/**
* implementation of Iterator interface
* with inner class
*/
private class dequeueIterator implements Iterator<E> {
private int index;
private int count = 0;
/**
* references the front dequeue element
* and initializes the dequeueIterator object
*/
public dequeueIterator(){
index = front;
}
/**
*@return true it there are additional
* elements in the dequeue
*/
@Override
public boolean hasNext() {
return count < size();
}
/**
* @return the next element in the queue
*/
@Override
public E next() {
if(!hasNext()) {
throw new NoSuchElementException();
}
E returnValue = elements[index];
index = (index + 1) % elements.length;
count++;
return returnValue;
}
/**
* removes the elements accessed by the
* iterator object
*/
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
/**
* //Returns the entry at the front of the
* dequeue without removing it; returns
* NoSuchElementException if the dequeue
* is empty
* @return the object at the front of dequeue
*/
public E peekFront() {
if (empty())
throw new NoSuchElementException();
else
return elements[front];
}
/**
* returns the item at the rear of the dequeue, throws
* NoSuchElementException if empty.
* @return the element in the rear
*/
public E peekRear() {
if (empty())
throw new NoSuchElementException();
else
return elements[rear];
}
/**
*Removes the entry at the front of the dequeue and
*returns it if the dequeue is not empty. If the
*dequeue is empty, throws a NoSuchElementException
* @return front element before removing it
*/
public E removeFront() {
if (empty())
throw new NoSuchElementException();
E temp = elements[front];
elements[front] = null;
front = (front++) % elements.length;
return temp;
}
/**
* Removes the entry at the rear of the dequeue and
*returns it if the dequeue is not empty. If the
*dequeue is empty, throws a NoSuchElementException
* @return rear element before removing it
*/
public E removeRear() {
if (empty())
throw new NoSuchElementException();
E temp = elements[rear];
elements[rear] = null;
rear = (rear + elements.length - 1) % elements.length;
return temp;
}
/**
* Gets the amount of elements in the dequeue
* @return the number of elements in the dequeue
*/
public int size() {
int count = 0;
int indexOfFront = front;
int nextRearPosition = (rear + 1) % elements.length;
while(indexOfFront != nextRearPosition) {
count++;
indexOfFront = (indexOfFront + 1) % elements.length;
}
return count;
}
}
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.NoSuchElementException;
public class DequeueTest {
Dequeue<Student> q;
Student s1, s2, s3, s4, s5, s6, s7, s8;
@BeforeEach
public void setUp() throws Exception {
q = new Dequeue<Student> ();
s1 = new Student("John", "Doe");
s2 = new Student ("Jane", "Smith");
s3 = new Student ("Bob", "Taylor");
s4 = new Student ("Anne", "Frank");
s5 = new Student("Frank", "Gauvin");
s6 = new Student("Kevin", "Austin");
s7 = new Student ("Cindy", "Bryant");
s8 = new Student ("Peter", "Lander");
}
@Test
public void testaddFrontAddRear() {
q.addFront(s1);
q.addFront(s2);
q.addFront(s3);
assertThat(q.peekFront(), is(s3)); // assertEquals(s3, q.peekFront());
q.addRear(s4);
q.addRear(s5);
q.addFront(s6);
q.addRear(s7);
assertThat(q.peekRear(), is(s7)); // assertEquals(s7, q.peekRear());
assertThat(q.size(), is(7)); // assertEquals(7, q.size());
}
@Test
public void testRemoveFrontRemoveRear() {
q.addFront(s1);
q.addFront(s2);
q.addFront(s3);
q.addRear(s4);
q.addRear(s5);
q.addFront(s6);
q.addRear(s7);
assertThat(q.removeFront(), is(s6)); // assertEquals(s6, q.removeFront());
assertThat(q.removeRear(), is(s7)); // assertEquals(s7, q.removeRear());
assertThat(q.removeFront(), is(s3)); // assertEquals(s3, q.removeFront() );
assertThat(q.size(), is(4)); // assertEquals(4, q.size());
assertThat(q.removeRear(), is(s5)); // assertEquals(s5, q.removeRear());
assertThat(q.removeFront(), is(s2)); // assertEquals(s2, q.removeFront());
assertThat(q.size(), is(2)); // assertEquals(2, q.size());
assertThat(q.removeFront(), is(s1)); // assertEquals(s1, q.removeFront());
assertThat(q.removeRear(), is(s4)); // assertEquals(s4, q.removeRear());
assertTrue(q.empty());
assertTrue(q.size() == 0);
}
@Test
public void testIterator() {
q.addFront(s1);
q.addFront(s2);
q.addFront(s3);
q.addRear(s4);
assertEquals(4, q.size() );
q.addRear(s5);
q.addFront(s6);
q.addRear(s7);
assertEquals(7, q.size() );
Iterator <Student> iter = q.iterator();
ArrayList<Student> list = new ArrayList<Student>();
while (iter.hasNext()) {
list.add(iter.next() );
}
assertThat(list.get(0), is(s6)); // assertEquals(s6, list.get(0));
assertThat(list.get(1), is(s3)); // assertEquals(s3, list.get(1));
assertThat(list.get(2), is(s2)); // assertEquals(s2, list.get(2));
assertThat(list.get(3), is(s1)); // assertEquals(s1, list.get(3));
assertThat(list.get(4), is(s4)); // assertEquals(s4, list.get(4));
assertThat(list.get(5), is(s5)); // assertEquals(s5, list.get(5));
assertThat(list.get(6), is(s7)); // assertEquals(s7, list.get(6));
}
@Test
public void testPeekFrontOnEmptyQueue() throws NoSuchElementException {
assertThrows(NoSuchElementException.class, () ->
{Dequeue<Student> q = new Dequeue<Student>();
q.peekFront();});
}
@Test
public void testRearFrontOnEmptyQueue() throws NoSuchElementException{
assertThrows(NoSuchElementException.class, () ->
{Dequeue<Student> q = new Dequeue<Student>();
q.peekRear();});
}
@Test
public void testRemoveFrontOnEmptyQueue() throws NoSuchElementException {
assertThrows(NoSuchElementException.class, () ->
{Dequeue<Student> q = new Dequeue<Student>();
q.removeFront();});
}
@Test
public void testRemoveRearOnEmptyQueue() throws NoSuchElementException
{
assertThrows(NoSuchElementException.class, () ->
{Dequeue<Student> q = new Dequeue<Student>();
q.removeRear();});
}
}
/** Abstraction of a Student entity */
public class Student implements Comparable <Student> {
private String firstName;
private String lastName;
/** Initialize a Student
@param first firstName
@param last lastName
*/
public Student(String first, String last) {
firstName = first;
lastName = last;
}
/** Mutator Method
@param aName firstName
*/
public void setFirstName(String aName) {
firstName = aName;
}
/** Accessor Method
@return firstName of this Student
*/
public String getFirstName() {
return firstName;
}
/** Mutator Method
@param aName lastName
*/
public void setLastName(String aName) {
lastName = aName;
}
/** Accessor Method
@return lastName of this Student
*/
public String getLastName() {
return lastName;
}
@Override
public String toString() {
String str = "";
str += (lastName + "," + firstName);
return str;
}
/* this version overloads the equals method (note the
signature of this method).
*/
public boolean equals(Student s) {
return ( (this.lastName.equalsIgnoreCase(s.lastName)) &&
(this.firstName.equalsIgnoreCase(s.firstName)));
}
/* We need to override this method so indexOf and
contains methods work. Both of them use this version
equals method
*/
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (obj == this) return true;
if (!(obj instanceof Student)) return false;
else {
Student s = (Student) obj;
return ( this.equals(s)); // calls the
equals(Student) method
}
}
/**
@return
a negative integer if "s1 < s2"; 0 if "s1 == s2"
a positive integer if "s1 > s2"
*/
@Override
public int compareTo(Student s) {
int result = lastName.compareToIgnoreCase(s.lastName);
if (result != 0) return result;
else
return (firstName.compareToIgnoreCase(s.firstName));
}
}
Ожидаемый результат, но фактический результат был нулевым. Я не уверен, что попробовать. Я рисую бланк. Ошибка выявлена в строке 55 dequeueTest.