diff --git a/classpath/avian/Data.java b/classpath/avian/Data.java index a725d8150e..3612a03249 100644 --- a/classpath/avian/Data.java +++ b/classpath/avian/Data.java @@ -126,7 +126,7 @@ public class Data { } public 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[] toArray(T[] array) { - return toArray(this, array); + return Data.toArray(this, array); } public void clear() { @@ -256,7 +256,7 @@ public class Data { } public T[] toArray(T[] array) { - return toArray(this, array); + return Data.toArray(this, array); } public void clear() { diff --git a/classpath/java/util/AbstractDeque.java b/classpath/java/util/AbstractDeque.java new file mode 100644 index 0000000000..2ef3309e6c --- /dev/null +++ b/classpath/java/util/AbstractDeque.java @@ -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 extends AbstractQueue + implements Deque { + + @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(); + } +} diff --git a/classpath/java/util/AbstractQueue.java b/classpath/java/util/AbstractQueue.java index f75359d74f..6ba714465d 100644 --- a/classpath/java/util/AbstractQueue.java +++ b/classpath/java/util/AbstractQueue.java @@ -16,6 +16,7 @@ public abstract class AbstractQueue extends AbstractCollection implements super(); } + @Override public boolean add(T element) { if (offer(element)) { return true; @@ -23,7 +24,8 @@ public abstract class AbstractQueue extends AbstractCollection implements throw new IllegalStateException(); } } - + + @Override public boolean addAll(Collection collection) { if (collection == null) { throw new NullPointerException(); @@ -35,26 +37,31 @@ public abstract class AbstractQueue extends AbstractCollection 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; } } } diff --git a/classpath/java/util/ArrayDeque.java b/classpath/java/util/ArrayDeque.java new file mode 100644 index 0000000000..769c6aed97 --- /dev/null +++ b/classpath/java/util/ArrayDeque.java @@ -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 extends AbstractDeque + implements Deque { + 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 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 c) { + if (c == null || c.isEmpty()) { + return false; + } + + ensureCapacity(size() + c.size()); + + Iterator 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 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 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[] toArray(S[] array) { + return avian.Data.toArray(this, array); + } + + public Iterator iterator() { + return new GenericIterator() { + @Override + protected int getArrayIndex() { + int result = (currentIndex + startIndex) % dataArray.length; + return result; + } + }; + } + + public Iterator descendingIterator() { + return new GenericIterator() { + @Override + protected int getArrayIndex() { + int result = (endIndex - currentIndex) % dataArray.length; + return result; + } + }; + } + + private abstract class GenericIterator implements Iterator { + 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; + } + } + } +} diff --git a/classpath/java/util/concurrent/ConcurrentLinkedQueue.java b/classpath/java/util/concurrent/ConcurrentLinkedQueue.java index 357f8a7ded..b8b7d8b640 100644 --- a/classpath/java/util/concurrent/ConcurrentLinkedQueue.java +++ b/classpath/java/util/concurrent/ConcurrentLinkedQueue.java @@ -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 implements Queue { +public class ConcurrentLinkedQueue extends AbstractQueue { private static final long QueueHead; private static final long QueueTail; private static final long NodeNext; @@ -67,31 +66,11 @@ public class ConcurrentLinkedQueue implements Queue { 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 h = head; @@ -150,12 +129,6 @@ public class ConcurrentLinkedQueue implements Queue { throw new UnsupportedOperationException(); } - @Override - public boolean addAll(Collection collection) { - // TODO - implement - throw new UnsupportedOperationException(); - } - @Override public boolean remove(Object element) { // TODO - implement diff --git a/classpath/java/util/concurrent/LinkedBlockingQueue.java b/classpath/java/util/concurrent/LinkedBlockingQueue.java index bf655c2d5b..ac1df07ddc 100644 --- a/classpath/java/util/concurrent/LinkedBlockingQueue.java +++ b/classpath/java/util/concurrent/LinkedBlockingQueue.java @@ -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 implements BlockingQueue { +public class LinkedBlockingQueue extends AbstractQueue + implements BlockingQueue { private final Object collectionLock; private final LinkedList storage; private final int capacity; @@ -84,15 +95,6 @@ public class LinkedBlockingQueue implements BlockingQueue { } } - @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 implements BlockingQueue { } } - @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 implements BlockingQueue { } } - @Override - public T remove() { - T result = poll(); - - if (result == null) { - throw new NoSuchElementException(); - } - - return result; - } - @Override public int drainTo(Collection c) { return drainTo(c, Integer.MAX_VALUE); @@ -256,11 +236,6 @@ public class LinkedBlockingQueue implements BlockingQueue { } } - @Override - public boolean isEmpty() { - return size() == 0; - } - @Override public boolean contains(Object element) { synchronized (collectionLock) { diff --git a/test/ArrayDequeTest.java b/test/ArrayDequeTest.java new file mode 100644 index 0000000000..c1c4cbc133 --- /dev/null +++ b/test/ArrayDequeTest.java @@ -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()); + QueueTest.isEmptyTest(new ArrayDeque()); + QueueTest.addTest(new ArrayDeque()); + QueueTest.addAllTest(new ArrayDeque()); + QueueTest.elementTest(new ArrayDeque()); + QueueTest.elementFail(new ArrayDeque()); + QueueTest.removeEmptyFail(new ArrayDeque()); + QueueTest.removeTest(new ArrayDeque()); + QueueTest.containsTest(new ArrayDeque()); + QueueTest.containsAllTest(new ArrayDeque()); + QueueTest.removeObjectTest(new ArrayDeque()); + QueueTest.removeAllTest(new ArrayDeque()); + QueueTest.clearTest(new ArrayDeque()); + QueueTest.toArrayTest(new ArrayDeque()); + + DequeTest.addFirstTest(new ArrayDeque()); + DequeTest.addLastTest(new ArrayDeque()); + DequeTest.removeFirstTest(new ArrayDeque()); + DequeTest.removeLastTest(new ArrayDeque()); + + 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 compareList = new LinkedList(); + ArrayDeque ad = new ArrayDeque(); + + for (int i = 0; i < testQty; i++) { + Object o = new Object(); + compareList.add(o); + ad.add(o); + } + + Iterator compIt; + Iterator 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 compareList = new LinkedList(); + ArrayDeque ad = new ArrayDeque(); + + for (int i = 0; i < testQty; i++) { + Object o = new Object(); + compareList.add(o); + ad.add(o); + } + + Iterator compIt; + Iterator 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 ad = new ArrayDeque(); + ad.add(new Object()); + ad.add(new Object()); + + Iterator 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 ad = new ArrayDeque(); + + Iterator testIt; + if (desc) { + testIt = ad.descendingIterator(); + } else { + testIt = ad.iterator(); + } + + try { + testIt.next(); + throw new RuntimeException("Exception should have thrown"); + } catch (NoSuchElementException e) { + // expected + } + } +} diff --git a/test/DequeTest.java b/test/DequeTest.java new file mode 100644 index 0000000000..03828215dc --- /dev/null +++ b/test/DequeTest.java @@ -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 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 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 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 q) { + Object firstObject = new Object(); + Object lastObject = new Object(); + q.addLast(firstObject); + q.addLast(lastObject); + + verify(q.removeLast() == lastObject); + verify(q.removeLast() == firstObject); + } +} diff --git a/test/LinkedBlockingQueueTest.java b/test/LinkedBlockingQueueTest.java index 916817b529..58505d21a2 100644 --- a/test/LinkedBlockingQueueTest.java +++ b/test/LinkedBlockingQueueTest.java @@ -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()); + QueueTest.isEmptyTest(new LinkedBlockingQueue()); + QueueTest.addTest(new LinkedBlockingQueue()); addCapacityFail(); offerTest(); offerWithTimeoutTest(); offerTimeoutTest(); putTest(); - addAllTest(); + QueueTest.addAllTest(new LinkedBlockingQueue()); addAllFail(); - elementTest(); - elementFail(); + QueueTest.elementTest(new LinkedBlockingQueue()); + QueueTest.elementFail(new LinkedBlockingQueue()); pollEmptyTest(); pollTest(); pollTimeoutTest(); takeTest(); - removeEmptyTest(); - removeTest(); + QueueTest.removeEmptyFail(new LinkedBlockingQueue()); + QueueTest.removeTest(new LinkedBlockingQueue()); drainToTest(); drainToLimitTest(); - containsTest(); - containsAllTest(); - removeObjectTest(); - removeAllTest(); - clearTest(); - toArrayTest(); + QueueTest.containsTest(new LinkedBlockingQueue()); + QueueTest.containsAllTest(new LinkedBlockingQueue()); + QueueTest.removeObjectTest(new LinkedBlockingQueue()); + QueueTest.removeAllTest(new LinkedBlockingQueue()); + QueueTest.clearTest(new LinkedBlockingQueue()); + QueueTest.toArrayTest(new LinkedBlockingQueue()); } private static void verify(boolean val) { @@ -51,31 +49,6 @@ public class LinkedBlockingQueueTest { verify(lbq.remainingCapacity() == 1); } - private static void sizeTest() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - verify(lbq.size() == 0); - - lbq.add(new Object()); - verify(lbq.size() == 1); - } - - private static void isEmptyTest() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - verify(lbq.isEmpty()); - - lbq.add(new Object()); - verify(! lbq.isEmpty()); - } - - private static void addTest() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - Object testObject = new Object(); - lbq.add(testObject); - - verify(lbq.size() == 1); - verify(lbq.peek() == testObject); - } - private static void addCapacityFail() { LinkedBlockingQueue lbq = new LinkedBlockingQueue(1); Object testObject = new Object(); @@ -139,20 +112,6 @@ public class LinkedBlockingQueueTest { verify(lbq.peek() == testObject); } - private static void addAllTest() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - LinkedList toAdd = new LinkedList(); - 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 lbq = new LinkedBlockingQueue(1); LinkedList toAdd = new LinkedList(); @@ -167,25 +126,6 @@ public class LinkedBlockingQueueTest { } } - private static void elementTest() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - Object testObject = new Object(); - lbq.add(testObject); - - verify(lbq.element() == testObject); - } - - private static void elementFail() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - - try { - lbq.element(); - throw new RuntimeException("Exception should have thrown"); - } catch (NoSuchElementException e) { - // expected - } - } - private static void pollEmptyTest() { LinkedBlockingQueue lbq = new LinkedBlockingQueue(); @@ -240,25 +180,6 @@ public class LinkedBlockingQueueTest { verify(lbq.take() == testObject); } - private static void removeEmptyTest() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - - try { - lbq.remove(); - throw new RuntimeException("Exception should have thrown"); - } catch (NoSuchElementException e) { - // expected - } - } - - private static void removeTest() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - Object testObject = new Object(); - lbq.add(testObject); - - verify(lbq.remove() == testObject); - } - private static void drainToTest() { int objQty = 2; LinkedBlockingQueue lbq = new LinkedBlockingQueue(); @@ -284,78 +205,4 @@ public class LinkedBlockingQueueTest { verify(drainToResult.size() == limit); verify(lbq.size() == objQty - limit); } - - private static void containsTest() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - Object testObject = new Object(); - - verify(! lbq.contains(testObject)); - - lbq.add(testObject); - verify(lbq.contains(testObject)); - } - - private static void containsAllTest() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - Object testObject = new Object(); - lbq.add(testObject); - - LinkedList testList = new LinkedList(); - testList.add(testObject); - testList.add(new Object()); - - verify(! lbq.containsAll(testList)); - - lbq.addAll(testList); - verify(lbq.containsAll(testList)); - } - - private static void removeObjectTest() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - Object testObject = new Object(); - - verify(! lbq.remove(testObject)); - - lbq.add(testObject); - verify(lbq.remove(testObject)); - } - - private static void removeAllTest() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - Object testObject = new Object(); - lbq.add(testObject); - - LinkedList testList = new LinkedList(); - testList.add(testObject); - testList.add(new Object()); - - verify(lbq.removeAll(testList)); - - lbq.addAll(testList); - verify(lbq.removeAll(testList)); - } - - private static void clearTest() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - lbq.add(new Object()); - - lbq.clear(); - - verify(lbq.isEmpty()); - } - - private static void toArrayTest() { - LinkedBlockingQueue lbq = new LinkedBlockingQueue(); - - 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); - } } diff --git a/test/QueueTest.java b/test/QueueTest.java new file mode 100644 index 0000000000..a2c6d02917 --- /dev/null +++ b/test/QueueTest.java @@ -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 q) { + verify(q.size() == 0); + + q.add(new Object()); + verify(q.size() == 1); + } + + public static void isEmptyTest(Queue q) { + verify(q.isEmpty()); + + q.add(new Object()); + verify(! q.isEmpty()); + } + + public static void addTest(Queue q) { + Object testObject = new Object(); + q.add(testObject); + + verify(q.size() == 1); + verify(q.peek() == testObject); + } + + public static void addAllTest(Queue q) { + LinkedList toAdd = new LinkedList(); + 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 q) { + Object testObject = new Object(); + q.add(testObject); + + verify(q.element() == testObject); + } + + public static void elementFail(Queue q) { + try { + q.element(); + throw new RuntimeException("Exception should have thrown"); + } catch (NoSuchElementException e) { + // expected + } + } + + public static void removeTest(Queue q) { + Object testObject = new Object(); + q.add(testObject); + + verify(q.remove() == testObject); + } + + public static void removeEmptyFail(Queue q) { + try { + q.remove(); + throw new RuntimeException("Exception should have thrown"); + } catch (NoSuchElementException e) { + // expected + } + } + + public static void containsTest(Queue q) { + Object testObject = new Object(); + + verify(! q.contains(testObject)); + + q.add(testObject); + verify(q.contains(testObject)); + } + + public static void containsAllTest(Queue q) { + Object testObject = new Object(); + q.add(testObject); + + LinkedList testList = new LinkedList(); + testList.add(testObject); + testList.add(new Object()); + + verify(! q.containsAll(testList)); + + q.addAll(testList); + verify(q.containsAll(testList)); + } + + public static void removeObjectTest(Queue q) { + Object testObject = new Object(); + + verify(! q.remove(testObject)); + + q.add(testObject); + verify(q.remove(testObject)); + } + + public static void removeAllTest(Queue q) { + Object testObject = new Object(); + q.add(testObject); + + LinkedList testList = new LinkedList(); + testList.add(testObject); + testList.add(new Object()); + + verify(q.removeAll(testList)); + + q.addAll(testList); + verify(q.removeAll(testList)); + } + + public static void clearTest(Queue q) { + q.add(new Object()); + + q.clear(); + + verify(q.isEmpty()); + } + + public static void toArrayTest(Queue 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); + } +}