Skip to content

Instantly share code, notes, and snippets.

@azakharov3
Created September 1, 2019 03:51
Show Gist options
  • Save azakharov3/034700170bc7d764ac50fe22cf2d6389 to your computer and use it in GitHub Desktop.
Save azakharov3/034700170bc7d764ac50fe22cf2d6389 to your computer and use it in GitHub Desktop.
import org.junit.Test;
import java.util.Iterator;
import java.util.NoSuchElementException;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
public class DequeTest {
private final Deque<String> deque = new Deque<>();
@Test
public void isEmpty_initiallyTrue() {
// Assert
assertTrue(deque.isEmpty());
}
@Test
public void isEmpty_afterAddingFirst_false() {
// Arrange
deque.addFirst("Hello");
// Act
assertFalse(deque.isEmpty());
}
@Test
public void isEmpty_afterAddingLast_false() {
// Arrange
deque.addLast("!");
// Act
assertFalse(deque.isEmpty());
}
@Test
public void removeFirst_oneElement() {
// Arrange
deque.addFirst("Hello");
// Act
String item = deque.removeFirst();
// Assert
assertEquals("Hello", item);
}
@Test
public void removeFirst_multipleElements() {
// Arrange
deque.addFirst(" ");
deque.addLast("World");
deque.addFirst(",");
deque.addLast("!");
deque.addFirst("Hello");
String result = "";
// Act
result += deque.removeFirst() +
deque.removeFirst() +
deque.removeFirst() +
deque.removeFirst() +
deque.removeFirst();
// Assert
assertEquals("Hello, World!", result);
}
@Test
public void removeFirst_throwsOnEmpty() {
// Arrange
NoSuchElementException exception = null;
// Act
try {
deque.removeFirst();
} catch (NoSuchElementException e) {
exception = e;
}
// Assert
assertNotNull(exception);
}
@Test
public void removeLast_throwsOnEmpty() {
// Arrange
NoSuchElementException exception = null;
// Act
try {
deque.removeLast();
} catch (NoSuchElementException e) {
exception = e;
}
// Assert
assertNotNull(exception);
}
@Test
public void removeLast_afterCallingAddFirst_shouldRemoveFirstElement() {
// Arrange
deque.addFirst("One");
// Act
String last = deque.removeLast();
// Assert
assertEquals(last, "One");
}
@Test
public void addFirst_ThrowsOnNull() {
// Arrange
IllegalArgumentException exception = null;
// Act
try {
deque.addFirst(null);
} catch (IllegalArgumentException e) {
exception = e;
}
// Assert
assertNotNull(exception);
}
@Test
public void addLast_ThrowsOnNull() {
// Arrange
IllegalArgumentException exception = null;
// Act
try {
deque.addLast(null);
} catch (IllegalArgumentException e) {
exception = e;
}
// Assert
assertNotNull(exception);
}
@Test
public void forEach() {
// Arrange
deque.addFirst(" ");
deque.addLast("World");
deque.addFirst(",");
deque.addLast("!");
deque.addFirst("Hello");
String result = "";
// Act
for (String item:
deque) {
result += item;
}
// Assert
assertEquals("Hello, World!", result);
}
@Test
public void iteratorHasNext_afterAddingFirstAndRemovingLast_ShouldBeFalse() {
// Arrange
deque.addFirst("Hello");
deque.removeLast();
Iterator<String> iterator = deque.iterator();
// Act
boolean hasNext = iterator.hasNext();
// Assert
assertFalse(hasNext);
}
@Test
public void iteratorHasNext_afterAddingLastndRemovingFirst_ShouldBeFalse() {
// Arrange
deque.addLast("Hello");
deque.removeFirst();
Iterator<String> iterator = deque.iterator();
// Act
boolean hasNext = iterator.hasNext();
// Assert
assertFalse(hasNext);
}
@Test
public void iterator_afterRemovingOneItem_shouldNotHaveTheRemovedItem() {
deque.addLast("one");
deque.addLast("two");
String item = deque.removeLast();
int count = 0;
for (String word:
deque) {
count++;
}
assertEquals(1, count);
}
@Test
public void iterator_whenExhausted_throwsNoSuchElementException() {
// Arrange
Iterator<String> iterator = deque.iterator();
NoSuchElementException exception = null;
// Act
try {
iterator.next();
} catch (NoSuchElementException e) {
exception = e;
}
// Assert
assertNotNull(exception);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment