Skip to content

Instantly share code, notes, and snippets.

@azakharov3
Last active September 1, 2019 03:52
Show Gist options
  • Save azakharov3/8dd5460eb5b3f3da3b0786d11e1b16dc to your computer and use it in GitHub Desktop.
Save azakharov3/8dd5460eb5b3f3da3b0786d11e1b16dc to your computer and use it in GitHub Desktop.
/* *****************************************************************************
* Name:
* Date:
* Description:
**************************************************************************** */
import org.junit.Test;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.core.IsIterableContaining.hasItem;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
public class RandomizedQueueTest {
private final RandomizedQueue<String> randomizedQueue = new RandomizedQueue<>();
private final String[] helloWorldArray = { "Hello", ",", " ", "World", "!"};
@Test
public void isEmpty_initiallState_true() {
// Act
boolean empty = randomizedQueue.isEmpty();
// Assert
assertTrue(empty);
}
@Test
public void isEmpty_afterAdding_false() {
// Arrange
randomizedQueue.enqueue("Hi");
// Act
boolean empty = randomizedQueue.isEmpty();
// Assert
assertFalse(empty);
}
@Test
public void isEmpty_afterRemovingAll_true() {
// Arrange
randomizedQueue.enqueue("Hi");
randomizedQueue.dequeue();
// Act
boolean empty = randomizedQueue.isEmpty();
// Assert
assertTrue(empty);
}
@Test
public void iterator_shouldReturnItemsInRandomOrder() {
// Arrange
for (String word:
helloWorldArray) {
randomizedQueue.enqueue(word);
}
String[] wordsOutput = new String[helloWorldArray.length];
boolean allElementAreInTheSameOrder = true;
// Act
int index = 0;
for (String word:
randomizedQueue) {
wordsOutput[index] = word;
index++;
}
// Assert
for (int i = 0; i < helloWorldArray.length; i++) {
assertNotNull(wordsOutput[i]);
if (helloWorldArray[i] != wordsOutput[i]) {
allElementAreInTheSameOrder = false;
break;
}
}
assertFalse(allElementAreInTheSameOrder);
}
@Test
public void iterator_shouldNotReturnDuplicates() {
// Arrange
RandomizedQueue<Integer> randomizedQueue = new RandomizedQueue<>();
for (int i = 0; i < 100; i++) {
randomizedQueue.enqueue(i);
}
int[] numbersOutput = new int[100];
// Act
int index = 0;
for (int number: randomizedQueue) {
numbersOutput[index] = number;
index++;
}
// Assert
for (int i = 0; i < numbersOutput.length; i++) {
for (int j = i + 1; j < numbersOutput.length; j++) {
assertNotEquals(numbersOutput[i], numbersOutput[j]);
}
}
}
@Test
public void iterator_afterCallingDeque_shouldNotIncludeDequedElement() {
// Arrange
for (String word:
helloWorldArray) {
randomizedQueue.enqueue(word);
}
String dequed = randomizedQueue.dequeue();
String[] output = new String[randomizedQueue.size()];
// Act
int index = 0;
for (String word:
randomizedQueue) {
output[index] = word;
index++;
}
// Assert
assertThat(Arrays.asList(output), not(hasItem(dequed)));
}
@Test
public void iterator_ShouldNotReturnItemsInTheSameOrderTwoTimes() {
// Arrange
for (String word:
helloWorldArray) {
randomizedQueue.enqueue(word);
}
Iterator<String> iteratorA = randomizedQueue.iterator();
Iterator<String> iteratorB = randomizedQueue.iterator();
boolean iteratorsReturnItemsInTheSameOrder = true;
// Act
int index = 0;
while (iteratorA.hasNext()) {
if (!iteratorA.next().equals(iteratorB.next())) {
iteratorsReturnItemsInTheSameOrder = false;
}
}
// Assert
assertFalse(iteratorsReturnItemsInTheSameOrder);
}
@Test
public void deque_shouldShuffle() {
// Arrange
for (String word:
helloWorldArray) {
randomizedQueue.enqueue(word);
}
String[] wordsOutput = new String[helloWorldArray.length];
boolean allElementAreInTheSameOrder = true;
// Act
for (int i = 0; i < wordsOutput.length; i++) {
wordsOutput[i] = randomizedQueue.dequeue();
}
// Assert
for (int i = 0; i < wordsOutput.length; i++) {
assertNotNull(wordsOutput[i]);
if (helloWorldArray[i] != wordsOutput[i]) {
allElementAreInTheSameOrder = false;
break;
}
}
assertFalse(allElementAreInTheSameOrder);
}
@Test
public void deque_whenEmpty_shouldThrowNoSuchElementException() {
// Arrange
NoSuchElementException exception = null;
// Act
try {
randomizedQueue.dequeue();
} catch (NoSuchElementException e) {
exception = e;
}
// Assert
assertNotNull(exception);
}
@Test
public void deque_afterMultipleEnqueCalls_shouldReturnQuedItem() {
// Arrange
RandomizedQueue<Character> randomizedQueue = new RandomizedQueue<Character>();
char[] chars = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'};
char[] output = new char[chars.length];
for (char character: chars) {
randomizedQueue.enqueue(character);
}
// Act
for (int i = 0; i < chars.length; i++) {
output[i] = randomizedQueue.dequeue();
}
// Assert
for (int i = 0; i < chars.length; i++) {
assertNotNull(output[i]);
}
}
@Test
public void enque_afterCallingEnqueDeque_shouldAddItem() {
// Arrange
randomizedQueue.enqueue("zero");
randomizedQueue.dequeue();
// Act
randomizedQueue.enqueue("one");
// Assert
String word = randomizedQueue.dequeue();
assertEquals("one", word);
}
@Test
public void enque_whenItemIsNull_shouldThrowIllegalArgumentException() {
// Arrange
IllegalArgumentException exception = null;
// Act
try {
randomizedQueue.enqueue(null);
} catch (IllegalArgumentException e) {
exception = e;
}
// Assert
assertNotNull(exception);
}
@Test
public void sample_whenEmpty_shouldThrowNoSuchElementException() {
// Arrange
NoSuchElementException exception = null;
// Act
try {
randomizedQueue.sample();
} catch (NoSuchElementException e) {
exception = e;
}
// Assert
assertNotNull(exception);
}
@Test
public void sample_whenOneItemPresent_shouldReturnTheItem() {
// Arrange
randomizedQueue.enqueue("Hello");
// Act
String sample = randomizedQueue.sample();
// Assert
assertEquals("Hello", sample);
}
@Test
public void sample_whenMultipleItemsPresent_shouldReturnRandomElement() {
// Arrange
for (String word:
helloWorldArray) {
randomizedQueue.enqueue(word);
}
String[] wordsOutput = new String[helloWorldArray.length];
boolean allElementAreInTheSameOrder = true;
// Act
for (int i = 0; i < wordsOutput.length; i++) {
wordsOutput[i] = randomizedQueue.sample();
}
// Assert
for (int i = 0; i < wordsOutput.length; i++) {
assertNotNull(wordsOutput[i]);
if (helloWorldArray[i] != wordsOutput[i]) {
allElementAreInTheSameOrder = false;
break;
}
}
assertFalse(allElementAreInTheSameOrder);
assertEquals(helloWorldArray.length, randomizedQueue.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment