You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2007/06/14 12:08:54 UTC
svn commit: r547194 - in
/harmony/enhanced/classlib/branches/java6/modules/luni/src:
main/java/java/util/ArrayDeque.java
test/java/tests/api/java/util/ArrayDequeTest.java
test/resources/serialization/tests/api/java/util/ArrayDequeTest.golden.ser
Author: tellison
Date: Thu Jun 14 03:08:51 2007
New Revision: 547194
URL: http://svn.apache.org/viewvc?view=rev&rev=547194
Log:
Apply patch HARMONY-4146 ([classlib][luni][java6] New class java.util.ArrayDeque for java6)
Added:
harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/ArrayDeque.java (with props)
harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/ArrayDequeTest.java (with props)
harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/tests/api/java/util/ArrayDequeTest.golden.ser (with props)
Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/ArrayDeque.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/ArrayDeque.java?view=auto&rev=547194
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/ArrayDeque.java (added)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/ArrayDeque.java Thu Jun 14 03:08:51 2007
@@ -0,0 +1,885 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.lang.reflect.Array;
+
+/**
+ * An implementation of Deque, backed by an array.
+ *
+ * ArrayDeques have no size limit, can not contain null element, and they are
+ * not thread-safe.
+ *
+ * All optional operations are supported, and the elements can be any objects.
+ *
+ * @param <E>
+ * the type of elements in this collection
+ *
+ * @since 1.6
+ */
+public class ArrayDeque<E> extends AbstractCollection<E> implements Deque<E>,
+ Cloneable, Serializable {
+
+ private static final long serialVersionUID = 2340985798034038923L;
+
+ private static final int DEFAULT_SIZE = 16;
+
+ private enum DequeStatus {
+ Empty, Normal, Full;
+ }
+
+ private transient DequeStatus status;
+
+ private transient int modCount;
+
+ // the pointer of the head element
+ private transient int front;
+
+ // the pointer of the "next" position of the tail element
+ private transient int rear;
+
+ private transient E[] elements;
+
+ @SuppressWarnings("hiding")
+ private class ArrayDequeIterator<E> implements Iterator<E> {
+ private int pos;
+
+ private final int expectedModCount;
+
+ private boolean canRemove;
+
+ @SuppressWarnings("synthetic-access")
+ ArrayDequeIterator() {
+ super();
+ pos = front;
+ expectedModCount = modCount;
+ canRemove = false;
+ }
+
+ @SuppressWarnings("synthetic-access")
+ public boolean hasNext() {
+ if (expectedModCount != modCount) {
+ return false;
+ }
+ return hasNextInternal();
+ }
+
+ private boolean hasNextInternal() {
+ // canRemove means "next" method is called, and the Full
+ // status can ensure that this method is not called just
+ // after "remove" method is call.(so, canRemove can keep
+ // true after "next" method called)
+ return (pos != rear)
+ || ((status == DequeStatus.Full) && !canRemove);
+ }
+
+ @SuppressWarnings( { "synthetic-access", "unchecked" })
+ public E next() {
+ if (hasNextInternal()) {
+ E result = (E) elements[pos];
+ if (expectedModCount == modCount && null != result) {
+ canRemove = true;
+ pos = circularBiggerPos(pos);
+ return result;
+ }
+ throw new ConcurrentModificationException();
+ }
+ throw new NoSuchElementException();
+ }
+
+ @SuppressWarnings("synthetic-access")
+ public void remove() {
+ if (canRemove) {
+ int removedPos = circularSmallerPos(pos);
+ if (expectedModCount == modCount
+ && null != elements[removedPos]) {
+ removeInternal(removedPos, true);
+ canRemove = false;
+ return;
+ }
+ throw new ConcurrentModificationException();
+ }
+ throw new IllegalStateException();
+ }
+ }
+
+ /*
+ * NOTES:descendingIterator is not fail-fast, according to the documentation
+ * and test case.
+ */
+ @SuppressWarnings("hiding")
+ private class ReverseArrayDequeIterator<E> implements Iterator<E> {
+ private int pos;
+
+ private final int expectedModCount;
+
+ private boolean canRemove;
+
+ @SuppressWarnings("synthetic-access")
+ ReverseArrayDequeIterator() {
+ super();
+ expectedModCount = modCount;
+ pos = circularSmallerPos(rear);
+ canRemove = false;
+ }
+
+ @SuppressWarnings("synthetic-access")
+ public boolean hasNext() {
+ if (expectedModCount != modCount) {
+ return false;
+ }
+ return hasNextInternal();
+ }
+
+ private boolean hasNextInternal() {
+ // canRemove means "next" method is called, and the Full
+ // status can ensure that this method is not called just
+ // after "remove" method is call.(so, canRemove can keep
+ // true after "next" method called)
+ return (circularBiggerPos(pos) != front)
+ || ((status == DequeStatus.Full) && !canRemove);
+ }
+
+ @SuppressWarnings( { "synthetic-access", "unchecked" })
+ public E next() {
+ if (hasNextInternal()) {
+ E result = (E) elements[pos];
+ canRemove = true;
+ pos = circularSmallerPos(pos);
+ return result;
+ }
+ throw new NoSuchElementException();
+ }
+
+ @SuppressWarnings("synthetic-access")
+ public void remove() {
+ if (canRemove) {
+ removeInternal(circularBiggerPos(pos), false);
+ canRemove = false;
+ return;
+ }
+ throw new IllegalStateException();
+ }
+ }
+
+ /**
+ * Constructs a new empty instance of ArrayDeque big enough for 16 elements.
+ */
+ public ArrayDeque() {
+ this(DEFAULT_SIZE);
+ }
+
+ /**
+ * Constructs a new empty instance of ArrayDeque big enough for specified
+ * number of elements.
+ *
+ * @param minSize
+ * the smallest size of the ArrayDeque
+ */
+ @SuppressWarnings("unchecked")
+ public ArrayDeque(final int minSize) {
+ int size = countInitSize(minSize);
+ elements = (E[]) new Object[size];
+ front = rear = 0;
+ status = DequeStatus.Empty;
+ modCount = 0;
+ }
+
+ /*
+ * count out the size for a new deque, and ensure that size >= minSize
+ */
+ private int countInitSize(final int minSize) {
+ int size = Math.max(minSize, DEFAULT_SIZE);
+ // get the smallest number that not smaller than size,
+ // and is a power of 2.
+ size = Integer.highestOneBit(size - 1) << 1;
+ if (0 >= size) {
+ size = minSize;
+ }
+ return size;
+ }
+
+ /**
+ * Constructs a new instance of ArrayDeque containing the elements of the
+ * specified collection, with the order returned by the collection's
+ * iterator.
+ *
+ * @param c
+ * the source of the elements
+ * @throws NullPointerException
+ * if the collection is null
+ */
+ @SuppressWarnings("unchecked")
+ public ArrayDeque(Collection<? extends E> c) {
+ elements = (E[]) new Object[countInitSize(c.size())];
+ front = rear = 0;
+ status = DequeStatus.Empty;
+ modCount = 0;
+ Iterator<? extends E> it = c.iterator();
+ while (it.hasNext()) {
+ addLastImpl(it.next());
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @param e
+ * the element
+ * @throws NullPointerException
+ * if the element is null
+ * @see java.util.Deque#addFirst(java.lang.Object)
+ */
+ public void addFirst(E e) {
+ offerFirst(e);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @param e
+ * the element
+ * @throws NullPointerException
+ * if the element is null
+ * @see java.util.Deque#addLast(java.lang.Object)
+ */
+ public void addLast(E e) {
+ addLastImpl(e);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @param e
+ * the element
+ * @return true
+ * @throws NullPointerException
+ * if the element is null
+ * @see java.util.Deque#offerFirst(java.lang.Object)
+ */
+ public boolean offerFirst(E e) {
+ checkNull(e);
+ checkAndExpand();
+ front = circularSmallerPos(front);
+ elements[front] = e;
+ resetStatus(true);
+ modCount++;
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @param e
+ * the element
+ * @return true if the operation succeeds or false if it fails
+ * @throws NullPointerException
+ * if the element is null
+ * @see java.util.Deque#offerLast(java.lang.Object)
+ */
+ public boolean offerLast(E e) {
+ return addLastImpl(e);
+ }
+
+ /**
+ * Inserts the element at the tail of the deque.
+ *
+ * @param e
+ * the element
+ * @return true if the operation succeeds or false if it fails.
+ * @throws NullPointerException
+ * if the element is null
+ * @see java.util.Queue#offer(java.lang.Object)
+ */
+ public boolean offer(E e) {
+ return addLastImpl(e);
+ }
+
+ /**
+ * Inserts the element to the tail of the deque.
+ *
+ * @param e
+ * the element
+ * @return true
+ * @see java.util.AbstractCollection#add(java.lang.Object)
+ */
+ @Override
+ public boolean add(E e) {
+ return addLastImpl(e);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @param e
+ * the element to push
+ * @throws NullPointerException
+ * if the element is null
+ * @see java.util.Deque#push(java.lang.Object)
+ */
+ public void push(E e) {
+ offerFirst(e);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return the head element
+ * @throws NoSuchElementException
+ * if the deque is empty
+ * @see java.util.Deque#removeFirst()
+ */
+ public E removeFirst() {
+ checkEmpty();
+ return removePollFirstImpl();
+ }
+
+ /**
+ * Gets and removes the head element of this deque. This method throws an
+ * exception if the deque is empty.
+ *
+ * @return the head element
+ * @throws NoSuchElementException
+ * if the deque is empty
+ * @see java.util.Queue#remove()
+ */
+ public E remove() {
+ return removeFirst();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return the head element
+ * @throws NoSuchElementException
+ * if the deque is empty
+ * @see java.util.Deque#pop()
+ */
+ public E pop() {
+ return removeFirst();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return the tail element
+ * @throws NoSuchElementException
+ * if the deque is empty
+ * @see java.util.Deque#removeLast()
+ */
+ public E removeLast() {
+ checkEmpty();
+ return removeLastImpl();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return the head element or null if the deque is empty
+ * @see java.util.Deque#pollFirst()
+ */
+ public E pollFirst() {
+ return (status == DequeStatus.Empty) ? null : removePollFirstImpl();
+ }
+
+ /**
+ * Gets and removes the head element of this deque. This method returns null
+ * if the deque is empty.
+ *
+ * @return the head element or null if the deque is empty
+ * @see java.util.Queue#poll()
+ */
+ public E poll() {
+ return pollFirst();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return the tail element or null if the deque is empty
+ * @see java.util.Deque#pollLast()
+ */
+ public E pollLast() {
+ return (status == DequeStatus.Empty) ? null : removeLastImpl();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return the head element
+ * @throws NoSuchElementException
+ * if the deque is empty
+ * @see java.util.Deque#getFirst()
+ */
+ public E getFirst() {
+ checkEmpty();
+ return elements[front];
+ }
+
+ /**
+ * Gets but does not remove the head element of this deque. It throws an
+ * exception if the deque is empty.
+ *
+ * @return the head element
+ * @throws NoSuchElementException
+ * if the deque is empty
+ * @see java.util.Queue#element()
+ */
+ public E element() {
+ return getFirst();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return the tail element
+ * @throws NoSuchElementException
+ * if the deque is empty
+ * @see java.util.Deque#getLast()
+ */
+ public E getLast() {
+ checkEmpty();
+ return elements[circularSmallerPos(rear)];
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return the head element or null if the deque is empty
+ * @see java.util.Deque#peekFirst()
+ */
+ public E peekFirst() {
+ return (status == DequeStatus.Empty) ? null : elements[front];
+ }
+
+ /**
+ * Gets but not removes the head element of this deque. This method returns
+ * null if the deque is empty.
+ *
+ * @return the head element or null if the deque is empty
+ * @see java.util.Queue#peek()
+ */
+ public E peek() {
+ return (status == DequeStatus.Empty) ? null : elements[front];
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return the tail element or null if the deque is empty
+ * @see java.util.Deque#peekLast()
+ */
+ public E peekLast() {
+ return (status == DequeStatus.Empty) ? null
+ : elements[circularSmallerPos(rear)];
+ }
+
+ private void checkNull(E e) {
+ if (null == e) {
+ throw new NullPointerException();
+ }
+ }
+
+ private void checkEmpty() {
+ if (status == DequeStatus.Empty) {
+ throw new NoSuchElementException();
+ }
+ }
+
+ private int circularSmallerPos(int current) {
+ return (current - 1 < 0) ? (elements.length - 1) : current - 1;
+ }
+
+ private int circularBiggerPos(int current) {
+ return (current + 1 >= elements.length) ? 0 : current + 1;
+ }
+
+ @SuppressWarnings("unchecked")
+ /*
+ * If array of elements is full, there will be a new bigger array to store
+ * the elements.
+ */
+ private void checkAndExpand() {
+ if (status != DequeStatus.Full) {
+ return;
+ }
+ if (Integer.MAX_VALUE == elements.length) {
+ throw new IllegalStateException();
+ }
+ int length = elements.length;
+ int newLength = length << 1;
+ // bigger than Integer.MAX_VALUE
+ if (newLength < 0) {
+ newLength = Integer.MAX_VALUE;
+ }
+ E[] newElements = (E[]) new Object[newLength];
+ System.arraycopy(elements, front, newElements, 0, length - front);
+ System.arraycopy(elements, 0, newElements, length - front, front);
+ front = 0;
+ rear = length;
+ status = DequeStatus.Normal;
+ elements = newElements;
+ }
+
+ /**
+ * Resets the status after adding or removing operation.
+ *
+ * @param adding
+ * if the method is called after an "adding" operation
+ */
+ private void resetStatus(boolean adding) {
+ if (front == rear) {
+ status = adding ? DequeStatus.Full : DequeStatus.Empty;
+ } else {
+ status = DequeStatus.Normal;
+ }
+ }
+
+ private boolean addLastImpl(E e) {
+ checkNull(e);
+ checkAndExpand();
+ elements[rear] = e;
+ rear = circularBiggerPos(rear);
+ resetStatus(true);
+ modCount++;
+ return true;
+ }
+
+ private E removePollFirstImpl() {
+ E element = elements[front];
+ elements[front] = null;
+ front = circularBiggerPos(front);
+ resetStatus(false);
+ modCount++;
+ return element;
+ }
+
+ private E removeLastImpl() {
+ int last = circularSmallerPos(rear);
+ E element = elements[last];
+ elements[last] = null;
+ rear = last;
+ resetStatus(false);
+ modCount++;
+ return element;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @param obj
+ * the element to be removed
+ * @return true if the operation succeeds or false if the deque does not
+ * contain the element
+ * @see java.util.Deque#removeFirstOccurrence(java.lang.Object)
+ */
+ public boolean removeFirstOccurrence(Object obj) {
+ return removeFirstOccurrenceImpl(obj);
+ }
+
+ /**
+ * Removes the first equivalent element of the specified object. If the
+ * deque does not contain the element, it is unchanged and returns false.
+ *
+ * @param obj
+ * the element to be removed
+ * @return true if the operation succeeds or false if the deque does not
+ * contain the element
+ * @see java.util.AbstractCollection#remove(java.lang.Object)
+ */
+ @Override
+ public boolean remove(Object obj) {
+ return removeFirstOccurrenceImpl(obj);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @param obj
+ * the element to be removed
+ * @return true if the operation succeeds or false if the deque does not
+ * contain the element.
+ * @see java.util.Deque#removeLastOccurrence(java.lang.Object)
+ */
+ public boolean removeLastOccurrence(final Object obj) {
+ if (null != obj) {
+ Iterator<E> iter = descendingIterator();
+ while (iter.hasNext()) {
+ if (iter.next().equals(obj)) {
+ iter.remove();
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ private boolean removeFirstOccurrenceImpl(final Object obj) {
+ if (null != obj) {
+ Iterator<E> iter = iterator();
+ while (iter.hasNext()) {
+ if (iter.next().equals(obj)) {
+ iter.remove();
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /*
+ * Removes the element in the cursor position and shifts front elements to
+ * fill the gap if frontShift is true, shifts rear elements otherwise.
+ *
+ */
+ private void removeInternal(final int current, final boolean frontShift) {
+ int cursor = current;
+ if (frontShift) {
+ while (cursor != front) {
+ int next = circularSmallerPos(cursor);
+ elements[cursor] = elements[next];
+ cursor = next;
+ }
+ front = circularBiggerPos(front);
+ } else {
+ while (cursor != rear) {
+ int next = circularBiggerPos(cursor);
+ elements[cursor] = elements[next];
+ cursor = next;
+ }
+ rear = circularSmallerPos(rear);
+ }
+ elements[cursor] = null;
+ resetStatus(false);
+ }
+
+ /**
+ * Returns the size of the deque.
+ *
+ * @return the size of the deque
+ * @see java.util.AbstractCollection#size()
+ */
+ @Override
+ public int size() {
+ if (status == DequeStatus.Full) {
+ return elements.length;
+ }
+ return (front <= rear) ? (rear - front)
+ : (rear + elements.length - front);
+ }
+
+ /**
+ * Returns true if the deque has no elements.
+ *
+ * @return true if the deque has no elements, false otherwise
+ * @see java.util.AbstractCollection#isEmpty()
+ */
+ @Override
+ public boolean isEmpty() {
+ return 0 == size();
+ }
+
+ /**
+ * Returns true if the specified element is in the deque.
+ *
+ * @param obj
+ * the element
+ * @return true if the element is in the deque, false otherwise
+ * @see java.util.AbstractCollection#contains(java.lang.Object)
+ */
+ @SuppressWarnings("cast")
+ @Override
+ public boolean contains(final Object obj) {
+ if (null != obj) {
+ Iterator<E> it = new ArrayDequeIterator<E>();
+ while (it.hasNext()) {
+ if (obj.equals((E) it.next())) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Empty the deque.
+ *
+ * @see java.util.AbstractCollection#clear()
+ */
+ @SuppressWarnings("cast")
+ @Override
+ public void clear() {
+ if (status != DequeStatus.Empty) {
+ int cursor = front;
+ do {
+ elements[cursor] = null;
+ cursor = circularBiggerPos(cursor);
+ } while (cursor != rear);
+ status = DequeStatus.Empty;
+ }
+ front = rear = 0;
+ modCount = 0;
+ }
+
+ /**
+ * Returns a clone of the deque.
+ *
+ * @return the clone of the deque
+ * @see java.lang.Object#clone()
+ * @see java.lang.Cloneable
+ */
+ @SuppressWarnings("unchecked")
+ @Override
+ public ArrayDeque<E> clone() {
+ try {
+ ArrayDeque<E> newDeque = (ArrayDeque<E>) super.clone();
+ newDeque.elements = elements.clone();
+ return newDeque;
+ } catch (CloneNotSupportedException e) {
+ return null;
+ }
+ }
+
+ /**
+ * Returns all the elements in an array from head to tail. The result is a
+ * copy of all the elements.
+ *
+ * @return the Array of all the elements
+ * @see java.util.AbstractCollection#toArray()
+ */
+ @Override
+ public Object[] toArray() {
+ return newArray(new Object[size()]);
+ }
+
+ /**
+ * Returns all the elements in an array from head to tail, and the type of
+ * the result array is the type of the argument array. If the argument array
+ * is big enough, the elements from the deque will be stored in it(elements
+ * following the tail of the deque is set to null, if any); otherwise, it
+ * will return a new array with the size of the argument array and size of
+ * the deque.
+ *
+ * @param <T>
+ * the type of elements in the array
+ * @param array
+ * the array stores all the elements from the deque, if it has
+ * enough space; otherwise, a new array of the same type and the
+ * size of the deque will be used
+ * @return the Array of all the elements
+ * @throws ArrayStoreException
+ * if the type of the argument array is not compatible with
+ * every element in the deque
+ * @throws NullPointerException
+ * if the argument array is null
+ * @see java.util.AbstractCollection#toArray(T[])
+ */
+ @Override
+ public <T> T[] toArray(T[] array) {
+ return newArray(array);
+ }
+
+ @SuppressWarnings("unchecked")
+ private <T> T[] newArray(T[] array) {
+ int size = size();
+ if (size > array.length) {
+ Class<?> clazz = array.getClass().getComponentType();
+ array = (T[]) Array.newInstance(clazz, size);
+ }
+ if (front < rear) {
+ System.arraycopy(elements, front, array, 0, size);
+ } else if (size != 0) {
+ int length = elements.length;
+ System.arraycopy(elements, front, array, 0, length - front);
+ System.arraycopy(elements, 0, array, length - front, rear);
+ }
+ if (size < array.length) {
+ array[size] = null;
+ }
+ return array;
+ }
+
+ /**
+ * Returns the iterator of the deque. The elements will be ordered from head
+ * to tail.
+ *
+ * @return the iterator
+ * @see java.util.AbstractCollection#iterator()
+ */
+ @SuppressWarnings("synthetic-access")
+ @Override
+ public Iterator<E> iterator() {
+ return new ArrayDequeIterator<E>();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return the reverse order Iterator
+ * @see java.util.Deque#descendingIterator()
+ */
+ public Iterator<E> descendingIterator() {
+ return new ReverseArrayDequeIterator<E>();
+ }
+
+ /**
+ * Deserialization method.
+ *
+ * @param stream
+ * the ObjectInputStream
+ * @throws IOException
+ * @throws ClassNotFoundException
+ */
+ @SuppressWarnings("unchecked")
+ private void readObject(ObjectInputStream stream) throws IOException,
+ ClassNotFoundException {
+ stream.defaultReadObject();
+ int size = stream.readInt();
+ elements = (E[]) new Object[countInitSize(size)];
+ front = rear = 0;
+ status = DequeStatus.Empty;
+ modCount = 0;
+ for (int i = 0; i < size; i++) {
+ addLastImpl((E) stream.readObject());
+ }
+ }
+
+ /**
+ * Serialization method.
+ *
+ * @param stream
+ * the ObjectOutputStream
+ * @serialData The current size of the deque, followed by all the elements
+ * from head to tail.
+ * @throws IOException
+ *
+ */
+ private void writeObject(ObjectOutputStream stream) throws IOException {
+ stream.defaultWriteObject();
+ stream.writeInt(size());
+ Iterator<?> it = new ArrayDequeIterator<E>();
+ while (it.hasNext()) {
+ stream.writeObject(it.next());
+ }
+ }
+
+}
Propchange: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/ArrayDeque.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/ArrayDequeTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/ArrayDequeTest.java?view=auto&rev=547194
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/ArrayDequeTest.java (added)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/ArrayDequeTest.java Thu Jun 14 03:08:51 2007
@@ -0,0 +1,932 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package tests.api.java.util;
+
+import java.io.Serializable;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.ConcurrentModificationException;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.testframework.serialization.SerializationTest;
+import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
+
+public class ArrayDequeTest extends TestCase {
+
+ private Object testObjOne;
+
+ private Object testObjTwo;
+
+ private Object testObjThree;
+
+ private Object testObjFour;
+
+ private Object testObjLast;
+
+ private ArrayDeque<Object> testQue;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ testObjOne = new Object();
+ testObjTwo = new Object();
+ testObjThree = new Object();
+ testObjFour = new Object();
+ testObjLast = new Object();
+ testQue = new ArrayDeque<Object>();
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#ArrayDeque()}
+ */
+ public void test_Constructor() throws Exception {
+ assertEquals(0, new ArrayDeque<Object>().size());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#ArrayDeque(java.util.Collection)}
+ */
+ public void test_Constructor_LCollection() throws Exception {
+ assertEquals(0, new ArrayDeque<Object>(new ArrayList<Object>()).size());
+ try {
+ new ArrayDeque<Object>(null);
+ fail("should throw NPE");
+ } catch (NullPointerException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#ArrayDeque(int)}
+ */
+ public void test_Constructor_Int() throws Exception {
+ assertEquals(0, new ArrayDeque<Object>(8).size());
+ ArrayDeque<Object> zeroCapQue = new ArrayDeque<Object>(0);
+ assertEquals(0, zeroCapQue.size());
+ zeroCapQue.add(testObjOne);
+ assertEquals(1, zeroCapQue.size());
+ assertEquals(0, new ArrayDeque<Object>(0).size());
+ ArrayDeque<Object> negCapQue = new ArrayDeque<Object>(-1);
+ assertEquals(0, negCapQue.size());
+ negCapQue.add(testObjOne);
+ assertEquals(1, negCapQue.size());
+ ArrayDeque<Object> oneCapQue = new ArrayDeque<Object>(1);
+ assertEquals(0, oneCapQue.size());
+ oneCapQue.add(testObjOne);
+ assertEquals(1, oneCapQue.size());
+ oneCapQue.add(testObjOne);
+ assertEquals(2, oneCapQue.size());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#addFirst(Object)}
+ */
+ public void test_addFirst() throws Exception {
+ testQue.addFirst(testObjOne);
+ assertEquals(1, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ testQue.addFirst(testObjOne);
+ assertEquals(2, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ testQue.addFirst(testObjTwo);
+ assertEquals(3, testQue.size());
+ assertEquals(testObjTwo, testQue.peek());
+ assertEquals(testObjOne, testQue.getLast());
+ try {
+ testQue.addFirst(null);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#addLast(Object)}
+ */
+ public void test_addLast() throws Exception {
+ testQue.addLast(testObjOne);
+ assertEquals(1, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ testQue.addLast(testObjOne);
+ assertEquals(2, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ testQue.addLast(testObjTwo);
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ assertEquals(testObjTwo, testQue.getLast());
+ try {
+ testQue.addLast(null);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#offerFirst(Object)}
+ */
+ public void test_offerFirst() throws Exception {
+ assertTrue(testQue.offerFirst(testObjOne));
+ assertEquals(1, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ assertTrue(testQue.offerFirst(testObjOne));
+ assertEquals(2, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ assertTrue(testQue.offerFirst(testObjTwo));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjTwo, testQue.peek());
+ assertEquals(testObjOne, testQue.getLast());
+ try {
+ testQue.offerFirst(null);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#offerLast(Object)}
+ */
+ public void test_offerLast() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertEquals(1, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ assertTrue(testQue.offerLast(testObjOne));
+ assertEquals(2, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ assertEquals(testObjTwo, testQue.getLast());
+ try {
+ testQue.offerLast(null);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#removeFirst()}
+ */
+ public void test_removeFirst() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.removeFirst());
+ assertEquals(2, testQue.size());
+ assertEquals(testObjTwo, testQue.removeFirst());
+ assertEquals(testObjThree, testQue.removeFirst());
+ assertEquals(0, testQue.size());
+ try {
+ testQue.removeFirst();
+ fail("should throw NoSuchElementException ");
+ } catch (NoSuchElementException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#removeLast()}
+ */
+ public void test_removeLast() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjThree, testQue.removeLast());
+ assertEquals(2, testQue.size());
+ assertEquals(testObjTwo, testQue.removeLast());
+ assertEquals(testObjOne, testQue.removeLast());
+ assertEquals(0, testQue.size());
+ try {
+ testQue.removeLast();
+ fail("should throw NoSuchElementException ");
+ } catch (NoSuchElementException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#pollFirst()}
+ */
+ public void test_pollFirst() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.pollFirst());
+ assertEquals(2, testQue.size());
+ assertEquals(testObjTwo, testQue.pollFirst());
+ assertEquals(testObjThree, testQue.pollFirst());
+ assertEquals(0, testQue.size());
+ assertNull(testQue.pollFirst());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#peekLast()}
+ */
+ public void test_pollLast() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjThree, testQue.pollLast());
+ assertEquals(2, testQue.size());
+ assertEquals(testObjTwo, testQue.pollLast());
+ assertEquals(testObjOne, testQue.pollLast());
+ assertEquals(0, testQue.size());
+ assertNull(testQue.pollFirst());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#getFirst()}
+ */
+ public void test_getFirst() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.getFirst());
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.pollFirst());
+ assertEquals(testObjTwo, testQue.getFirst());
+ assertEquals(testObjTwo, testQue.pollFirst());
+ assertEquals(testObjThree, testQue.pollFirst());
+ assertEquals(0, testQue.size());
+ try {
+ testQue.getFirst();
+ fail("should throw NoSuchElementException ");
+ } catch (NoSuchElementException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#getLast()}
+ */
+ public void test_getLast() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjThree, testQue.getLast());
+ assertEquals(3, testQue.size());
+ assertEquals(testObjThree, testQue.pollLast());
+ assertEquals(testObjTwo, testQue.getLast());
+ assertEquals(testObjTwo, testQue.pollLast());
+ assertEquals(testObjOne, testQue.pollLast());
+ assertEquals(0, testQue.size());
+ try {
+ testQue.getLast();
+ fail("should throw NoSuchElementException ");
+ } catch (NoSuchElementException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#peekFirst()}
+ */
+ public void test_peekFirst() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.peekFirst());
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.pollFirst());
+ assertEquals(testObjTwo, testQue.peekFirst());
+ assertEquals(testObjTwo, testQue.pollFirst());
+ assertEquals(testObjThree, testQue.pollFirst());
+ assertEquals(0, testQue.size());
+ assertEquals(null, testQue.peekFirst());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#peekLast()}
+ */
+ public void test_peekLast() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjThree, testQue.peekLast());
+ assertEquals(3, testQue.size());
+ assertEquals(testObjThree, testQue.pollLast());
+ assertEquals(testObjTwo, testQue.peekLast());
+ assertEquals(testObjTwo, testQue.pollLast());
+ assertEquals(testObjOne, testQue.pollLast());
+ assertEquals(0, testQue.size());
+ assertNull(testQue.peekLast());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#removeFirstOccurrence(Object)}
+ */
+ public void test_removeFirstOccurrence() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertTrue(testQue.offerLast(testObjOne));
+ assertEquals(5, testQue.size());
+ assertTrue(testQue.removeFirstOccurrence(testObjOne));
+ assertFalse(testQue.removeFirstOccurrence(testObjFour));
+ assertEquals(testObjTwo, testQue.peekFirst());
+ assertEquals(testObjOne, testQue.peekLast());
+ assertEquals(4, testQue.size());
+ assertTrue(testQue.removeFirstOccurrence(testObjOne));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.peekLast());
+ assertTrue(testQue.removeFirstOccurrence(testObjOne));
+ assertEquals(2, testQue.size());
+ assertEquals(testObjThree, testQue.peekLast());
+ assertFalse(testQue.removeFirstOccurrence(testObjOne));
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#removeLastOccurrence(Object)}
+ */
+ public void test_removeLastOccurrence() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertTrue(testQue.offerLast(testObjOne));
+ assertEquals(5, testQue.size());
+ assertTrue(testQue.removeLastOccurrence(testObjOne));
+ assertFalse(testQue.removeLastOccurrence(testObjFour));
+ assertEquals(testObjOne, testQue.peekFirst());
+ assertEquals(testObjThree, testQue.peekLast());
+ assertEquals(4, testQue.size());
+ assertTrue(testQue.removeLastOccurrence(testObjOne));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.peekFirst());
+ assertEquals(testObjThree, testQue.peekLast());
+ assertTrue(testQue.removeLastOccurrence(testObjOne));
+ assertEquals(2, testQue.size());
+ assertEquals(testObjThree, testQue.peekLast());
+ assertFalse(testQue.removeLastOccurrence(testObjOne));
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#add(Object)}
+ */
+ public void test_add() throws Exception {
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjTwo));
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjThree));
+ assertEquals(testObjOne, testQue.peekFirst());
+ assertEquals(testObjThree, testQue.peekLast());
+ try {
+ testQue.add(null);
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#offer(Object)}
+ */
+ public void test_offer() throws Exception {
+ assertTrue(testQue.offer(testObjOne));
+ assertTrue(testQue.offer(testObjTwo));
+ assertTrue(testQue.offer(testObjOne));
+ assertTrue(testQue.offer(testObjThree));
+ assertEquals(testObjOne, testQue.peekFirst());
+ assertEquals(testObjThree, testQue.peekLast());
+ try {
+ testQue.offer(null);
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#remove()}
+ */
+ public void test_remove() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.remove());
+ assertEquals(2, testQue.size());
+ assertEquals(testObjTwo, testQue.remove());
+ assertEquals(testObjThree, testQue.remove());
+ assertEquals(0, testQue.size());
+ try {
+ testQue.remove();
+ fail("should throw NoSuchElementException ");
+ } catch (NoSuchElementException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#poll()}
+ */
+ public void test_poll() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.poll());
+ assertEquals(2, testQue.size());
+ assertEquals(testObjTwo, testQue.poll());
+ assertEquals(testObjThree, testQue.poll());
+ assertEquals(0, testQue.size());
+ assertNull(testQue.poll());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#element()}
+ */
+ public void test_element() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.element());
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.pollFirst());
+ assertEquals(testObjTwo, testQue.element());
+ assertEquals(testObjTwo, testQue.pollFirst());
+ assertEquals(testObjThree, testQue.element());
+ assertEquals(testObjThree, testQue.pollFirst());
+ assertEquals(0, testQue.size());
+ try {
+ testQue.element();
+ fail("should throw NoSuchElementException ");
+ } catch (NoSuchElementException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#peek()}
+ */
+ public void test_peek() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.pollFirst());
+ assertEquals(testObjTwo, testQue.peek());
+ assertEquals(testObjTwo, testQue.pollFirst());
+ assertEquals(testObjThree, testQue.pollFirst());
+ assertEquals(0, testQue.size());
+ assertEquals(null, testQue.peek());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#push(Object)}
+ */
+ public void test_push() throws Exception {
+ testQue.push(testObjOne);
+ assertEquals(1, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ testQue.push(testObjOne);
+ assertEquals(2, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ testQue.push(testObjTwo);
+ assertEquals(3, testQue.size());
+ assertEquals(testObjTwo, testQue.peek());
+ assertEquals(testObjOne, testQue.getLast());
+ try {
+ testQue.push(null);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#pop()}
+ */
+ public void test_pop() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.pop());
+ assertEquals(2, testQue.size());
+ assertEquals(testObjTwo, testQue.pop());
+ assertEquals(testObjThree, testQue.pop());
+ assertEquals(0, testQue.size());
+ try {
+ testQue.pop();
+ fail("should throw NoSuchElementException ");
+ } catch (NoSuchElementException e) {
+ // expected
+ }
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#addFirst(Object)}
+ */
+ public void test_size() throws Exception {
+ assertEquals(0, testQue.size());
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjTwo));
+ assertEquals(2, testQue.size());
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjThree));
+ assertEquals(4, testQue.size());
+ testQue.remove();
+ testQue.remove();
+ assertEquals(2, testQue.size());
+ testQue.clear();
+ assertEquals(0, testQue.size());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#isEmpty()}
+ */
+ public void test_isEmpty() throws Exception {
+ assertTrue(testQue.isEmpty());
+ assertTrue(testQue.add(testObjOne));
+ assertFalse(testQue.isEmpty());
+ assertTrue(testQue.add(testObjTwo));
+ assertFalse(testQue.isEmpty());
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjThree));
+ assertFalse(testQue.isEmpty());
+ testQue.remove();
+ testQue.remove();
+ assertFalse(testQue.isEmpty());
+ testQue.clear();
+ assertTrue(testQue.isEmpty());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#iterator()}
+ */
+ public void test_iterator() throws Exception {
+ assertFalse(testQue.iterator().hasNext());
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjTwo));
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjThree));
+ assertTrue(testQue.add(testObjLast));
+ Iterator result = testQue.iterator();
+ assertEquals(5, testQue.size());
+ try {
+ result.remove();
+ fail("should throw IllegalStateException");
+ } catch (IllegalStateException e) {
+ // expected
+ }
+ assertTrue(testQue.add(testObjThree));
+ try {
+ result.next();
+ fail("should throw ConcurrentModificationException");
+ } catch (ConcurrentModificationException e) {
+ // expected
+ }
+ result = testQue.iterator();
+ assertEquals(testObjOne, result.next());
+ assertEquals(testObjTwo, result.next());
+ assertEquals(testObjOne, result.next());
+ assertEquals(testObjThree, result.next());
+ assertEquals(testObjLast, result.next());
+ assertTrue(result.hasNext());
+ result.remove();
+ assertEquals(testObjThree, result.next());
+ assertFalse(result.hasNext());
+ try {
+ result.next();
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // expected
+ }
+ // test a full array
+ ArrayDeque<Object> ad = new ArrayDeque<Object>();
+ // fill the array
+ for (int i = 0; i < 16; ++i) {
+ ad.addLast(new Object());
+ }
+ assertTrue(ad.iterator().hasNext());
+ Iterator<Object> iter = ad.iterator();
+ for (int i = 0; i < 16; ++i) {
+ assertTrue(iter.hasNext());
+ iter.next();
+ }
+ iter.remove();
+ // test un-full array
+ ad = new ArrayDeque<Object>();
+ // fill the array
+ for (int i = 0; i < 5; ++i) {
+ ad.addLast(new Object());
+ }
+ iter = ad.iterator();
+ for (int i = 0; i < 5; ++i) {
+ assertTrue(iter.hasNext());
+ iter.next();
+ }
+ iter.remove();
+
+ ad = new ArrayDeque<Object>();
+ // fill the array
+ for (int i = 0; i < 16; ++i) {
+ ad.addLast(new Object());
+ }
+ iter = ad.iterator();
+ assertTrue(iter.hasNext());
+ for (int i = 0; i < ad.size(); ++i) {
+ iter.next();
+ }
+ assertFalse(iter.hasNext());
+ iter.remove();
+ ad.add(new Object());
+ assertFalse(iter.hasNext());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#descendingIterator()}
+ */
+ public void test_descendingIterator() throws Exception {
+ assertFalse(testQue.descendingIterator().hasNext());
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjTwo));
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjThree));
+ assertTrue(testQue.add(testObjLast));
+ Iterator result = testQue.descendingIterator();
+ assertEquals(5, testQue.size());
+ try {
+ result.remove();
+ fail("should throw IllegalStateException");
+ } catch (IllegalStateException e) {
+ // expected
+ }
+ assertTrue(testQue.add(testObjFour));
+
+ // a strange behavior here, RI's descendingIterator() and iterator() is
+ // properly different. Notice spec: "The iterators returned by this
+ // class's iterator method are fail-fast". RI shows descendingIterator()
+ // is not an iterator method.
+ assertEquals(testObjLast,result.next());
+
+ result = testQue.descendingIterator();
+ assertEquals(testObjFour, result.next());
+ assertEquals(testObjLast, result.next());
+ assertEquals(testObjThree, result.next());
+ assertEquals(testObjOne, result.next());
+ assertEquals(testObjTwo, result.next());
+ assertTrue(result.hasNext());
+ result.remove();
+ assertEquals(testObjOne, result.next());
+ assertFalse(result.hasNext());
+ try {
+ result.next();
+ fail("should throw NoSuchElementException");
+ } catch (NoSuchElementException e) {
+ // expected
+ }
+ // test a full array
+ ArrayDeque<Object> ad = new ArrayDeque<Object>();
+ // fill the array
+ for (int i = 0; i < 16; ++i) {
+ ad.addLast(new Object());
+ }
+ assertTrue(ad.descendingIterator().hasNext());
+ Iterator<Object> iter = ad.descendingIterator();
+ for (int i = 0; i < 16; ++i) {
+ assertTrue(iter.hasNext());
+ iter.next();
+ }
+ iter.remove();
+ // test un-full array
+ ad = new ArrayDeque<Object>();
+ // fill the array
+ for (int i = 0; i < 5; ++i) {
+ ad.addLast(new Object());
+ }
+ iter = ad.descendingIterator();
+ for (int i = 0; i < 5; ++i) {
+ assertTrue(iter.hasNext());
+ iter.next();
+ }
+ iter.remove();
+
+ ad = new ArrayDeque<Object>();
+ // fill the array
+ for (int i = 0; i < 16; ++i) {
+ ad.addLast(new Object());
+ }
+ iter = ad.descendingIterator();
+ assertTrue(iter.hasNext());
+ for (int i = 0; i < ad.size(); ++i) {
+ iter.next();
+ }
+ assertFalse(iter.hasNext());
+ iter.remove();
+ ad.add(new Object());
+ assertFalse(iter.hasNext());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#contains(Object)}
+ */
+ public void test_contains() throws Exception {
+ assertFalse(testQue.contains(testObjFour));
+ assertFalse(testQue.contains(null));
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjTwo));
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjThree));
+ assertTrue(testQue.add(testObjLast));
+
+ assertTrue(testQue.contains(testObjOne));
+ assertTrue(testQue.contains(testObjTwo));
+ assertTrue(testQue.contains(testObjThree));
+ assertTrue(testQue.contains(testObjLast));
+ assertFalse(testQue.contains(null));
+ testQue.clear();
+ assertFalse(testQue.contains(testObjOne));
+ assertFalse(testQue.contains(testObjTwo));
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#remove(Object)}
+ */
+ public void test_remove_LObject() throws Exception {
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjTwo));
+ assertTrue(testQue.offerLast(testObjOne));
+ assertTrue(testQue.offerLast(testObjThree));
+ assertTrue(testQue.offerLast(testObjOne));
+ assertEquals(5, testQue.size());
+ assertTrue(testQue.remove(testObjOne));
+ assertFalse(testQue.remove(testObjFour));
+ assertEquals(testObjTwo, testQue.peekFirst());
+ assertEquals(testObjOne, testQue.peekLast());
+ assertEquals(4, testQue.size());
+ assertTrue(testQue.remove(testObjOne));
+ assertEquals(3, testQue.size());
+ assertEquals(testObjOne, testQue.peekLast());
+ assertTrue(testQue.remove(testObjOne));
+ assertEquals(2, testQue.size());
+ assertEquals(testObjThree, testQue.peekLast());
+ assertFalse(testQue.remove(testObjOne));
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#clear()}
+ */
+ public void test_clear() throws Exception {
+ assertTrue(testQue.isEmpty());
+ testQue.clear();
+ assertTrue(testQue.isEmpty());
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjTwo));
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjThree));
+ testQue.clear();
+ assertTrue(testQue.isEmpty());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#toArray()}
+ */
+ public void test_toArray() throws Exception {
+ assertEquals(0, testQue.toArray().length);
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjTwo));
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjThree));
+ assertTrue(testQue.add(testObjLast));
+ Object[] result = testQue.toArray();
+ assertEquals(5, testQue.size());
+ assertEquals(testObjOne, result[0]);
+ assertEquals(testObjTwo, result[1]);
+ assertEquals(testObjOne, result[2]);
+ assertEquals(testObjThree, result[3]);
+ assertEquals(testObjLast, result[4]);
+ // change in array do not affect ArrayDeque
+ result[0] = null;
+ assertEquals(5, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#toArray(Object[])}
+ */
+ public void test_toArray_$LObject() throws Exception {
+ Object[] array = new Object[0];
+ Object[] result = testQue.toArray(array);
+ assertEquals(0, result.length);
+ assertEquals(array, result);
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjTwo));
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjThree));
+ assertTrue(testQue.add(testObjLast));
+ result = testQue.toArray(array);
+ assertEquals(5, testQue.size());
+ assertEquals(5, result.length);
+ assertEquals(0, array.length);
+ assertFalse(array == result);
+ assertEquals(testObjOne, result[0]);
+ assertEquals(testObjTwo, result[1]);
+ assertEquals(testObjOne, result[2]);
+ assertEquals(testObjThree, result[3]);
+ assertEquals(testObjLast, result[4]);
+ // change in array do not affect ArrayDeque
+ result[0] = null;
+ assertEquals(5, testQue.size());
+ assertEquals(testObjOne, testQue.peek());
+ try {
+ testQue.toArray(null);
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ }
+
+ /**
+ * @tests {@link java.util.ArrayDeque#clone()}
+ */
+ public void test_clone() throws Exception {
+ ArrayDeque<Object> cloned = testQue.clone();
+ assertEquals(0, cloned.size());
+ assertFalse(cloned == testQue);
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjTwo));
+ assertTrue(testQue.add(testObjOne));
+ assertTrue(testQue.add(testObjThree));
+ assertTrue(testQue.add(testObjLast));
+ assertTrue(testQue.add(testQue));
+ cloned = testQue.clone();
+ assertEquals(6, cloned.size());
+ while (0 != testQue.size()) {
+ assertEquals(testQue.remove(), cloned.remove());
+ }
+ }
+
+ /**
+ * @tests java.util.ArrayDeque#Serialization()
+ */
+ public void test_serialization() throws Exception {
+ assertTrue(testQue.add(new Integer(1)));
+ assertTrue(testQue.add(new Integer(2)));
+ assertTrue(testQue.add(new Integer(3)));
+ assertTrue(testQue.add(new Integer(4)));
+ assertTrue(testQue.add(new Integer(5)));
+ SerializationTest.verifySelf(testQue, new SerializableAssert() {
+ public void assertDeserialized(Serializable initial,
+ Serializable deserialized) {
+ ArrayDeque<Object> formerQue = (ArrayDeque)initial;
+ ArrayDeque<Object> deserializedQue = (ArrayDeque)deserialized;
+ assertEquals(formerQue.remove(),deserializedQue.remove());
+ }
+ });
+ }
+
+ /**
+ * @tests serialization/deserialization compatibility with RI.
+ */
+ @SuppressWarnings( { "unchecked", "boxing" })
+ public void testSerializationCompatibility() throws Exception {
+ assertTrue(testQue.add(new Integer(1)));
+ assertTrue(testQue.add(new Integer(2)));
+ assertTrue(testQue.add(new Integer(3)));
+ assertTrue(testQue.add(new Integer(4)));
+ assertTrue(testQue.add(new Integer(5)));
+ SerializationTest.verifyGolden(this,testQue, new SerializableAssert() {
+ public void assertDeserialized(Serializable initial,
+ Serializable deserialized) {
+ ArrayDeque<Object> formerQue = (ArrayDeque)initial;
+ ArrayDeque<Object> deserializedQue = (ArrayDeque)deserialized;
+ assertEquals(formerQue.remove(),deserializedQue.remove());
+ }
+ });
+ }
+}
Propchange: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/ArrayDequeTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/tests/api/java/util/ArrayDequeTest.golden.ser
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/tests/api/java/util/ArrayDequeTest.golden.ser?view=auto&rev=547194
==============================================================================
Binary file - no diff available.
Propchange: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/resources/serialization/tests/api/java/util/ArrayDequeTest.golden.ser
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream