Added an implemention of ArrayDeque, as well as unit tests

I also used this opportunity to reduce code duplication around other queue/deque implementations.
This commit is contained in:
Mike Jensen 2014-03-18 19:45:00 -06:00
parent e9e365d698
commit b5d388a718
10 changed files with 899 additions and 251 deletions

View File

@ -126,7 +126,7 @@ public class Data {
}
public <T> T[] toArray(T[] array) {
return toArray(this, array);
return Data.toArray(this, array);
}
public void clear() {
@ -170,7 +170,7 @@ public class Data {
}
public <T> T[] toArray(T[] array) {
return toArray(this, array);
return Data.toArray(this, array);
}
public void clear() {
@ -256,7 +256,7 @@ public class Data {
}
public <T> T[] toArray(T[] array) {
return toArray(this, array);
return Data.toArray(this, array);
}
public void clear() {

View File

@ -0,0 +1,90 @@
/* Copyright (c) 2008-2014, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util;
public abstract class AbstractDeque<T> extends AbstractQueue<T>
implements Deque<T> {
@Override
public void push(T e) {
addFirst(e);
}
@Override
public void addLast(T element) {
if (! offerLast(element)) {
throw new IllegalStateException();
}
}
@Override
public void addFirst(T element) {
if (! offerFirst(element)) {
throw new IllegalStateException();
}
}
@Override
public boolean offer(T element) {
return offerLast(element);
}
@Override
public T poll() {
return pollFirst();
}
@Override
public T pop() {
return removeFirst();
}
@Override
public T removeFirst() {
return remove();
}
@Override
public boolean remove(Object element) {
return removeFirstOccurrence(element);
}
@Override
public T removeLast() {
T result = pollLast();
if (result == null) {
throw new NoSuchElementException();
} else {
return result;
}
}
@Override
public T getFirst() {
return element();
}
@Override
public T getLast() {
T result = peekLast();
if (result == null) {
throw new NoSuchElementException();
}
return result;
}
@Override
public T peek() {
return peekFirst();
}
}

View File

@ -16,6 +16,7 @@ public abstract class AbstractQueue<T> extends AbstractCollection<T> implements
super();
}
@Override
public boolean add(T element) {
if (offer(element)) {
return true;
@ -23,7 +24,8 @@ public abstract class AbstractQueue<T> extends AbstractCollection<T> implements
throw new IllegalStateException();
}
}
@Override
public boolean addAll(Collection <? extends T> collection) {
if (collection == null) {
throw new NullPointerException();
@ -35,26 +37,31 @@ public abstract class AbstractQueue<T> extends AbstractCollection<T> implements
return true;
}
@Override
public void clear() {
while (size() > 0) {
poll();
}
}
@Override
public T element() {
emptyCheck();
return peek();
}
public T remove() {
emptyCheck();
return poll();
}
private void emptyCheck() {
if (size() == 0) {
T result = peek();
if (result == null) {
throw new NoSuchElementException();
} else {
return result;
}
}
@Override
public T remove() {
T result = poll();
if (result == null) {
throw new NoSuchElementException();
} else {
return result;
}
}
}

View File

@ -0,0 +1,364 @@
/* Copyright (c) 2008-2014, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util;
public class ArrayDeque<T> extends AbstractDeque<T>
implements Deque<T> {
private Object[] dataArray;
// both indexes are inclusive, except when size == 0
private int startIndex;
private int endIndex;
private int size;
private int modCount;
public ArrayDeque() {
this(16);
}
public ArrayDeque(int intialSize) {
dataArray = new Object[intialSize];
modCount = 0;
clear();
}
public ArrayDeque(Collection<? extends T> c) {
this(c.size());
addAll(c);
}
private void copyInto(Object[] array) {
if (startIndex <= endIndex) {
// only one copy needed
System.arraycopy(dataArray, startIndex, array, 0, size);
} else {
int firstCopyCount = dataArray.length - startIndex;
System.arraycopy(dataArray, startIndex, array, 0, firstCopyCount);
System.arraycopy(dataArray, 0, array, firstCopyCount, endIndex + 1);
}
}
private void ensureCapacity(int newSize) {
if (dataArray.length < newSize) {
Object[] newArray = new Object[dataArray.length * 2];
copyInto(newArray);
dataArray = newArray;
startIndex = 0;
endIndex = size - 1;
}
}
@Override
public boolean offerFirst(T e) {
ensureCapacity(size() + 1);
modCount++;
if (size > 0) {
// we don't need to move the head index for the first one
startIndex--;
if (startIndex < 0) { // wrapping to the end of the array
startIndex = dataArray.length - 1;
}
}
size++;
dataArray[startIndex] = e;
return true;
}
@Override
public boolean offerLast(T e) {
ensureCapacity(size() + 1);
modCount++;
if (size > 0) {
// we don't need to move the tail index for the first one
endIndex = (endIndex + 1) % dataArray.length;
}
size++;
dataArray[endIndex] = e;
return true;
}
@Override
public T pollFirst() {
modCount++;
if (size == 0) {
return null;
}
@SuppressWarnings("unchecked")
T result = (T)dataArray[startIndex];
size--;
if (size == 0) {
startIndex = endIndex = 0;
} else {
startIndex = (startIndex + 1) % dataArray.length;
}
return result;
}
@Override
public T pollLast() {
modCount++;
if (size == 0) {
return null;
}
@SuppressWarnings("unchecked")
T result = (T)dataArray[endIndex];
size--;
if (size == 0) {
startIndex = endIndex = 0;
} else {
endIndex--;
if (endIndex < 0) {
endIndex = dataArray.length - 1;
}
}
return result;
}
@Override
public T peekFirst() {
if (size == 0) {
return null;
} else {
@SuppressWarnings("unchecked")
T result = (T)dataArray[startIndex];
return result;
}
}
@Override
public T peekLast() {
if (size == 0) {
return null;
} else {
@SuppressWarnings("unchecked")
T result = (T)dataArray[endIndex];
return result;
}
}
@Override
public boolean addAll(Collection<? extends T> c) {
if (c == null || c.isEmpty()) {
return false;
}
ensureCapacity(size() + c.size());
Iterator<? extends T> it = c.iterator();
while (it.hasNext()) {
add(it.next());
}
return true;
}
@Override
public boolean removeAll(Collection<?> c) {
boolean removed = false;
Iterator<?> it = c.iterator();
while (it.hasNext()) {
removed = remove(it.next()) || removed;
}
return removed;
}
private boolean remove(Object o, boolean first) {
modCount++;
Iterator<T> it;
if (first) {
it = iterator();
} else {
it = descendingIterator();
}
while (it.hasNext()) {
T next = it.next();
if (next == null) {
if (o == null) {
it.remove();
return true;
}
} else if (next.equals(o)) {
it.remove();
return true;
}
}
return false;
}
@Override
public boolean removeFirstOccurrence(Object o) {
return remove(o, true);
}
@Override
public boolean removeLastOccurrence(Object o) {
return remove(o, false);
}
@Override
public void clear() {
size = 0;
startIndex = endIndex = 0;
modCount++;
}
@Override
public int size() {
return size;
}
@Override
public boolean contains(Object element) {
Iterator<T> it = iterator();
while (it.hasNext()) {
T next = it.next();
if (next == null) {
if (element == null) {
return true;
}
} else if (next.equals(element)) {
return true;
}
}
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
Iterator<?> it = c.iterator();
while (it.hasNext()) {
if (! contains(it.next())) {
return false;
}
}
return true;
}
@Override
public Object[] toArray() {
Object[] result = new Object[size];
copyInto(result);
return result;
}
@Override
public <S> S[] toArray(S[] array) {
return avian.Data.toArray(this, array);
}
public Iterator<T> iterator() {
return new GenericIterator() {
@Override
protected int getArrayIndex() {
int result = (currentIndex + startIndex) % dataArray.length;
return result;
}
};
}
public Iterator<T> descendingIterator() {
return new GenericIterator() {
@Override
protected int getArrayIndex() {
int result = (endIndex - currentIndex) % dataArray.length;
return result;
}
};
}
private abstract class GenericIterator implements Iterator<T> {
protected int expectedModCount = modCount;
protected int currentIndex = 0;
protected abstract int getArrayIndex();
@Override
public T next() {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
} else if (currentIndex == size) {
throw new NoSuchElementException();
}
@SuppressWarnings("unchecked")
T result = (T)dataArray[getArrayIndex()];
currentIndex++;
return result;
}
@Override
public boolean hasNext() {
return currentIndex < size;
}
@Override
public void remove() {
currentIndex--;
int removalIndex = getArrayIndex();
if (removalIndex == startIndex) {
// avoid array copy
pollFirst();
expectedModCount = modCount;
} else if (removalIndex == endIndex) {
// avoid array copy
pollLast();
expectedModCount = modCount;
} else { // array must be copied
Object[] newArray = new Object[dataArray.length];
if (startIndex <= endIndex) {
int firstCopyCount = removalIndex - startIndex;
System.arraycopy(dataArray, startIndex,
newArray, 0, firstCopyCount);
System.arraycopy(dataArray, removalIndex + 1,
newArray, firstCopyCount, size - firstCopyCount);
} else if (removalIndex > startIndex) {
int firstCopyCount = removalIndex - startIndex;
System.arraycopy(dataArray, startIndex,
newArray, 0, firstCopyCount);
System.arraycopy(dataArray, startIndex + firstCopyCount + 1,
newArray, firstCopyCount, dataArray.length - removalIndex - 1);
System.arraycopy(dataArray, 0, newArray, size - removalIndex, endIndex + 1);
} else {
int firstCopyCount = dataArray.length - startIndex;
System.arraycopy(dataArray, startIndex,
newArray, 0, firstCopyCount);
System.arraycopy(dataArray, 0,
newArray, firstCopyCount, removalIndex);
System.arraycopy(dataArray, removalIndex + 1,
newArray, firstCopyCount + removalIndex, endIndex - removalIndex);
}
dataArray = newArray;
startIndex = 0;
endIndex = --size - 1;
}
}
}
}

View File

@ -1,13 +1,12 @@
package java.util.concurrent;
import java.util.AbstractQueue;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Queue;
import avian.Atomic;
public class ConcurrentLinkedQueue<T> implements Queue<T> {
public class ConcurrentLinkedQueue<T> extends AbstractQueue<T> {
private static final long QueueHead;
private static final long QueueTail;
private static final long NodeNext;
@ -67,31 +66,11 @@ public class ConcurrentLinkedQueue<T> implements Queue<T> {
return poll(false);
}
@Override
public T element() {
T result = peek();
if (result == null) {
throw new NoSuchElementException();
} else {
return result;
}
}
@Override
public T poll() {
return poll(true);
}
@Override
public T remove() {
T result = poll();
if (result == null) {
throw new NoSuchElementException();
} else {
return result;
}
}
private T poll(boolean remove) {
while (true) {
Node<T> h = head;
@ -150,12 +129,6 @@ public class ConcurrentLinkedQueue<T> implements Queue<T> {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends T> collection) {
// TODO - implement
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object element) {
// TODO - implement

View File

@ -1,11 +1,22 @@
/* Copyright (c) 2008-2014, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util.concurrent;
import java.util.AbstractQueue;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class LinkedBlockingQueue<T> implements BlockingQueue<T> {
public class LinkedBlockingQueue<T> extends AbstractQueue<T>
implements BlockingQueue<T> {
private final Object collectionLock;
private final LinkedList<T> storage;
private final int capacity;
@ -84,15 +95,6 @@ public class LinkedBlockingQueue<T> implements BlockingQueue<T> {
}
}
@Override
public boolean add(T element) {
if (! offer(element)) {
throw new IllegalStateException("At capacity");
}
return true;
}
@Override
public boolean offer(T element) {
synchronized (collectionLock) {
@ -158,17 +160,6 @@ public class LinkedBlockingQueue<T> implements BlockingQueue<T> {
}
}
@Override
public T element() {
T result = peek();
if (result == null) {
throw new NoSuchElementException();
}
return result;
}
// should be synchronized on collectionLock before calling
private T removeFirst() {
T result = storage.removeFirst();
@ -209,17 +200,6 @@ public class LinkedBlockingQueue<T> implements BlockingQueue<T> {
}
}
@Override
public T remove() {
T result = poll();
if (result == null) {
throw new NoSuchElementException();
}
return result;
}
@Override
public int drainTo(Collection<? super T> c) {
return drainTo(c, Integer.MAX_VALUE);
@ -256,11 +236,6 @@ public class LinkedBlockingQueue<T> implements BlockingQueue<T> {
}
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public boolean contains(Object element) {
synchronized (collectionLock) {

188
test/ArrayDequeTest.java Normal file
View File

@ -0,0 +1,188 @@
import java.util.ArrayDeque;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class ArrayDequeTest {
private static void verify(boolean val) {
if (! val) {
throw new RuntimeException();
}
}
public static void main(String[] args) throws InterruptedException {
QueueTest.sizeTest(new ArrayDeque<Object>());
QueueTest.isEmptyTest(new ArrayDeque<Object>());
QueueTest.addTest(new ArrayDeque<Object>());
QueueTest.addAllTest(new ArrayDeque<Object>());
QueueTest.elementTest(new ArrayDeque<Object>());
QueueTest.elementFail(new ArrayDeque<Object>());
QueueTest.removeEmptyFail(new ArrayDeque<Object>());
QueueTest.removeTest(new ArrayDeque<Object>());
QueueTest.containsTest(new ArrayDeque<Object>());
QueueTest.containsAllTest(new ArrayDeque<Object>());
QueueTest.removeObjectTest(new ArrayDeque<Object>());
QueueTest.removeAllTest(new ArrayDeque<Object>());
QueueTest.clearTest(new ArrayDeque<Object>());
QueueTest.toArrayTest(new ArrayDeque<Object>());
DequeTest.addFirstTest(new ArrayDeque<Object>());
DequeTest.addLastTest(new ArrayDeque<Object>());
DequeTest.removeFirstTest(new ArrayDeque<Object>());
DequeTest.removeLastTest(new ArrayDeque<Object>());
iterateTest(false);
iterateTest(true);
iteratorRemoveTest(false);
iteratorRemoveTest(true);
iteratorConcurrentModificationFail(false);
iteratorConcurrentModificationFail(true);
iteratorNoElementFail(false);
iteratorNoElementFail(true);
}
private static void iterateTest(boolean desc) {
int testQty = 10;
LinkedList<Object> compareList = new LinkedList<Object>();
ArrayDeque<Object> ad = new ArrayDeque<Object>();
for (int i = 0; i < testQty; i++) {
Object o = new Object();
compareList.add(o);
ad.add(o);
}
Iterator<Object> compIt;
Iterator<Object> testIt;
if (desc) {
compIt = compareList.descendingIterator();
testIt = ad.descendingIterator();
} else {
compIt = compareList.iterator();
testIt = ad.iterator();
}
while (testIt.hasNext()) {
verify(testIt.next() == compIt.next());
}
// remove from the front
compareList.removeFirst();
ad.removeFirst();
if (desc) {
compIt = compareList.descendingIterator();
testIt = ad.descendingIterator();
} else {
compIt = compareList.iterator();
testIt = ad.iterator();
}
while (testIt.hasNext()) {
verify(testIt.next() == compIt.next());
}
// remove from the end
compareList.removeLast();
ad.removeLast();
if (desc) {
compIt = compareList.descendingIterator();
testIt = ad.descendingIterator();
} else {
compIt = compareList.iterator();
testIt = ad.iterator();
}
while (testIt.hasNext()) {
verify(testIt.next() == compIt.next());
}
}
private static void iteratorRemoveTest(boolean desc) {
int testQty = 20;
LinkedList<Object> compareList = new LinkedList<Object>();
ArrayDeque<Object> ad = new ArrayDeque<Object>();
for (int i = 0; i < testQty; i++) {
Object o = new Object();
compareList.add(o);
ad.add(o);
}
Iterator<Object> compIt;
Iterator<Object> testIt;
if (desc) {
compIt = compareList.descendingIterator();
testIt = ad.descendingIterator();
} else {
compIt = compareList.iterator();
testIt = ad.iterator();
}
boolean flip = true; // start with true to ensure first is removed
while (testIt.hasNext()) {
// advance iterators
testIt.next();
compIt.next();
if (flip || ! testIt.hasNext()) {
compIt.remove();
testIt.remove();
flip = false;
} else {
flip = true;
}
}
if (desc) {
compIt = compareList.descendingIterator();
testIt = ad.descendingIterator();
} else {
compIt = compareList.iterator();
testIt = ad.iterator();
}
while (testIt.hasNext()) {
verify(testIt.next() == compIt.next());
}
}
private static void iteratorConcurrentModificationFail(boolean desc) {
ArrayDeque<Object> ad = new ArrayDeque<Object>();
ad.add(new Object());
ad.add(new Object());
Iterator<Object> testIt;
if (desc) {
testIt = ad.descendingIterator();
} else {
testIt = ad.iterator();
}
testIt.next();
try {
// modify structure
ad.add(new Object());
testIt.next();
throw new RuntimeException("Exception should have thrown");
} catch (ConcurrentModificationException e) {
// expected
}
}
private static void iteratorNoElementFail(boolean desc) {
ArrayDeque<Object> ad = new ArrayDeque<Object>();
Iterator<Object> testIt;
if (desc) {
testIt = ad.descendingIterator();
} else {
testIt = ad.iterator();
}
try {
testIt.next();
throw new RuntimeException("Exception should have thrown");
} catch (NoSuchElementException e) {
// expected
}
}
}

55
test/DequeTest.java Normal file
View File

@ -0,0 +1,55 @@
import java.util.Deque;
public class DequeTest {
private static void verify(boolean val) {
if (! val) {
throw new RuntimeException();
}
}
public static void main(String args[]) {
// prevents unit test failure
}
public static void addFirstTest(Deque<Object> q) {
Object firstObject = new Object();
Object lastObject = new Object();
q.addFirst(lastObject);
q.addFirst(firstObject);
verify(q.size() == 2);
verify(q.peekFirst() == firstObject);
verify(q.peekLast() == lastObject);
}
public static void addLastTest(Deque<Object> q) {
Object firstObject = new Object();
Object lastObject = new Object();
q.addLast(firstObject);
q.addLast(lastObject);
verify(q.size() == 2);
verify(q.peekFirst() == firstObject);
verify(q.peekLast() == lastObject);
}
public static void removeFirstTest(Deque<Object> q) {
Object firstObject = new Object();
Object lastObject = new Object();
q.addLast(firstObject);
q.addLast(lastObject);
verify(q.removeFirst() == firstObject);
verify(q.removeFirst() == lastObject);
}
public static void removeLastTest(Deque<Object> q) {
Object firstObject = new Object();
Object lastObject = new Object();
q.addLast(firstObject);
q.addLast(lastObject);
verify(q.removeLast() == lastObject);
verify(q.removeLast() == firstObject);
}
}

View File

@ -1,40 +1,38 @@
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class LinkedBlockingQueueTest {
private static final int DELAY_TILL_ACTION = 10;
public static void main(String[] args) throws InterruptedException {
remainingCapacityTest();
sizeTest();
isEmptyTest();
addTest();
QueueTest.sizeTest(new LinkedBlockingQueue<Object>());
QueueTest.isEmptyTest(new LinkedBlockingQueue<Object>());
QueueTest.addTest(new LinkedBlockingQueue<Object>());
addCapacityFail();
offerTest();
offerWithTimeoutTest();
offerTimeoutTest();
putTest();
addAllTest();
QueueTest.addAllTest(new LinkedBlockingQueue<Object>());
addAllFail();
elementTest();
elementFail();
QueueTest.elementTest(new LinkedBlockingQueue<Object>());
QueueTest.elementFail(new LinkedBlockingQueue<Object>());
pollEmptyTest();
pollTest();
pollTimeoutTest();
takeTest();
removeEmptyTest();
removeTest();
QueueTest.removeEmptyFail(new LinkedBlockingQueue<Object>());
QueueTest.removeTest(new LinkedBlockingQueue<Object>());
drainToTest();
drainToLimitTest();
containsTest();
containsAllTest();
removeObjectTest();
removeAllTest();
clearTest();
toArrayTest();
QueueTest.containsTest(new LinkedBlockingQueue<Object>());
QueueTest.containsAllTest(new LinkedBlockingQueue<Object>());
QueueTest.removeObjectTest(new LinkedBlockingQueue<Object>());
QueueTest.removeAllTest(new LinkedBlockingQueue<Object>());
QueueTest.clearTest(new LinkedBlockingQueue<Object>());
QueueTest.toArrayTest(new LinkedBlockingQueue<Object>());
}
private static void verify(boolean val) {
@ -51,31 +49,6 @@ public class LinkedBlockingQueueTest {
verify(lbq.remainingCapacity() == 1);
}
private static void sizeTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
verify(lbq.size() == 0);
lbq.add(new Object());
verify(lbq.size() == 1);
}
private static void isEmptyTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
verify(lbq.isEmpty());
lbq.add(new Object());
verify(! lbq.isEmpty());
}
private static void addTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
Object testObject = new Object();
lbq.add(testObject);
verify(lbq.size() == 1);
verify(lbq.peek() == testObject);
}
private static void addCapacityFail() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>(1);
Object testObject = new Object();
@ -139,20 +112,6 @@ public class LinkedBlockingQueueTest {
verify(lbq.peek() == testObject);
}
private static void addAllTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
LinkedList<Object> toAdd = new LinkedList<Object>();
toAdd.add(new Object());
toAdd.add(new Object());
lbq.addAll(toAdd);
verify(lbq.size() == toAdd.size());
while (! lbq.isEmpty()) {
verify(lbq.remove() == toAdd.remove());
}
}
private static void addAllFail() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>(1);
LinkedList<Object> toAdd = new LinkedList<Object>();
@ -167,25 +126,6 @@ public class LinkedBlockingQueueTest {
}
}
private static void elementTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
Object testObject = new Object();
lbq.add(testObject);
verify(lbq.element() == testObject);
}
private static void elementFail() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
try {
lbq.element();
throw new RuntimeException("Exception should have thrown");
} catch (NoSuchElementException e) {
// expected
}
}
private static void pollEmptyTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
@ -240,25 +180,6 @@ public class LinkedBlockingQueueTest {
verify(lbq.take() == testObject);
}
private static void removeEmptyTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
try {
lbq.remove();
throw new RuntimeException("Exception should have thrown");
} catch (NoSuchElementException e) {
// expected
}
}
private static void removeTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
Object testObject = new Object();
lbq.add(testObject);
verify(lbq.remove() == testObject);
}
private static void drainToTest() {
int objQty = 2;
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
@ -284,78 +205,4 @@ public class LinkedBlockingQueueTest {
verify(drainToResult.size() == limit);
verify(lbq.size() == objQty - limit);
}
private static void containsTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
Object testObject = new Object();
verify(! lbq.contains(testObject));
lbq.add(testObject);
verify(lbq.contains(testObject));
}
private static void containsAllTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
Object testObject = new Object();
lbq.add(testObject);
LinkedList<Object> testList = new LinkedList<Object>();
testList.add(testObject);
testList.add(new Object());
verify(! lbq.containsAll(testList));
lbq.addAll(testList);
verify(lbq.containsAll(testList));
}
private static void removeObjectTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
Object testObject = new Object();
verify(! lbq.remove(testObject));
lbq.add(testObject);
verify(lbq.remove(testObject));
}
private static void removeAllTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
Object testObject = new Object();
lbq.add(testObject);
LinkedList<Object> testList = new LinkedList<Object>();
testList.add(testObject);
testList.add(new Object());
verify(lbq.removeAll(testList));
lbq.addAll(testList);
verify(lbq.removeAll(testList));
}
private static void clearTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
lbq.add(new Object());
lbq.clear();
verify(lbq.isEmpty());
}
private static void toArrayTest() {
LinkedBlockingQueue<Object> lbq = new LinkedBlockingQueue<Object>();
if (lbq.toArray().length != 0) {
throw new RuntimeException();
}
Object testObject = new Object();
lbq.add(testObject);
Object[] result = lbq.toArray();
verify(result.length == 1);
verify(result[0] == testObject);
}
}

149
test/QueueTest.java Normal file
View File

@ -0,0 +1,149 @@
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
public class QueueTest {
private static void verify(boolean val) {
if (! val) {
throw new RuntimeException();
}
}
public static void main(String args[]) {
// prevents unit test failure
}
public static void sizeTest(Queue<Object> q) {
verify(q.size() == 0);
q.add(new Object());
verify(q.size() == 1);
}
public static void isEmptyTest(Queue<Object> q) {
verify(q.isEmpty());
q.add(new Object());
verify(! q.isEmpty());
}
public static void addTest(Queue<Object> q) {
Object testObject = new Object();
q.add(testObject);
verify(q.size() == 1);
verify(q.peek() == testObject);
}
public static void addAllTest(Queue<Object> q) {
LinkedList<Object> toAdd = new LinkedList<Object>();
toAdd.add(new Object());
toAdd.add(new Object());
q.addAll(toAdd);
verify(q.size() == toAdd.size());
while (! q.isEmpty()) {
verify(q.remove() == toAdd.remove());
}
}
public static void elementTest(Queue<Object> q) {
Object testObject = new Object();
q.add(testObject);
verify(q.element() == testObject);
}
public static void elementFail(Queue<Object> q) {
try {
q.element();
throw new RuntimeException("Exception should have thrown");
} catch (NoSuchElementException e) {
// expected
}
}
public static void removeTest(Queue<Object> q) {
Object testObject = new Object();
q.add(testObject);
verify(q.remove() == testObject);
}
public static void removeEmptyFail(Queue<Object> q) {
try {
q.remove();
throw new RuntimeException("Exception should have thrown");
} catch (NoSuchElementException e) {
// expected
}
}
public static void containsTest(Queue<Object> q) {
Object testObject = new Object();
verify(! q.contains(testObject));
q.add(testObject);
verify(q.contains(testObject));
}
public static void containsAllTest(Queue<Object> q) {
Object testObject = new Object();
q.add(testObject);
LinkedList<Object> testList = new LinkedList<Object>();
testList.add(testObject);
testList.add(new Object());
verify(! q.containsAll(testList));
q.addAll(testList);
verify(q.containsAll(testList));
}
public static void removeObjectTest(Queue<Object> q) {
Object testObject = new Object();
verify(! q.remove(testObject));
q.add(testObject);
verify(q.remove(testObject));
}
public static void removeAllTest(Queue<Object> q) {
Object testObject = new Object();
q.add(testObject);
LinkedList<Object> testList = new LinkedList<Object>();
testList.add(testObject);
testList.add(new Object());
verify(q.removeAll(testList));
q.addAll(testList);
verify(q.removeAll(testList));
}
public static void clearTest(Queue<Object> q) {
q.add(new Object());
q.clear();
verify(q.isEmpty());
}
public static void toArrayTest(Queue<Object> q) {
if (q.toArray().length != 0) {
throw new RuntimeException();
}
Object testObject = new Object();
q.add(testObject);
Object[] result = q.toArray();
verify(result.length == 1);
verify(result[0] == testObject);
}
}