You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/06/19 07:32:22 UTC

svn commit: r415247 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/ test/java/tests/api/java/util/ test/resources/serialization/java/util/

Author: mloenko
Date: Sun Jun 18 22:32:21 2006
New Revision: 415247

URL: http://svn.apache.org/viewvc?rev=415247&view=rev
Log:
integrated HARMONY-574
[classlib][util]Java 5 Enhancement:new class java.util.PriorityQueue
with minor changes

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/PriorityQueue.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/PriorityQueueTest.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/util/PriorityQueue.golden.ser   (with props)
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/PriorityQueue.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/PriorityQueue.java?rev=415247&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/PriorityQueue.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/PriorityQueue.java Sun Jun 18 22:32:21 2006
@@ -0,0 +1,386 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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;
+
+/**
+ * PriorityQueue holds elements on a priority heap, which orders elements
+ * according to the comparator specified at construction or their natural order.
+ * If the queue uses natural order, any element that is not comparable is not
+ * permitted to insert to the queue.
+ * 
+ * The least element of the specified ordering is stored at the head of the
+ * queue and the greatest element is stored at the tail of the queue.
+ * 
+ * PriorityQueue is not synchronized. If mutiple threads will access it
+ * concurrently, use the PriorityBlockingQueue.
+ */
+public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable {
+
+    private static final long serialVersionUID = -7720805057305804111L;
+
+    private static final int DEFAULT_CAPACITY = 11;
+
+    private static final double DEFAULT_INIT_CAPACITY_RATIO = 1.1;
+
+    private static final int DEFAULT_CAPACITY_RATIO = 2;
+
+    private int size = 0;
+
+    private Comparator<? super E> comparator;
+
+    private transient E[] elements;
+
+    /**
+     * Constructs a priority queue with the capacity of 11 and natural ordering.
+     */
+    public PriorityQueue() {
+        this(DEFAULT_CAPACITY);
+    }
+
+    /**
+     * Constructs a priority queue with specified capacity and natural ordering.
+     * 
+     * @param initialCapacity the specified capacity.
+     * @throws IllegalArgumentException if the initialCapacity is less than 1
+     */
+    public PriorityQueue(int initialCapacity) {
+        this(initialCapacity, null);
+    }
+
+    /**
+     * Constructs a priority queue with specified capacity and comparator.
+     * 
+     * @param initialCapacity the specified capacity.
+     * @param comparator the specified comparator. If it is null, the natural
+     *        ordering will be used.
+     * @throws IllegalArgumentException if the initialCapacity is less than 1
+     */
+    public PriorityQueue(int initialCapacity, Comparator<? super E> comparator) {
+        if (initialCapacity < 1) {
+            throw new IllegalArgumentException();
+        }
+        elements = newElementArray(initialCapacity);
+        this.comparator = comparator;
+    }
+
+    /**
+     * Constructs a priority queue that contains the elements of a collection.
+     * The constructed priority queue has the initial capacity of 110% the
+     * collection. And the priority queue uses natural ordering to order its
+     * elements.
+     * 
+     * @param c the collection whose elements will be added to the priority
+     *        queue to be constructed.
+     * @throws ClassCastException if any of the elements in the collection is
+     *         not comparable.
+     * @throws NullPointerExcepiton if any of the elements in the colleciton is
+     *         null.
+     */
+    public PriorityQueue(Collection<? extends E> c) {
+        if (c instanceof PriorityQueue) {
+            getFromPriorityQueue((PriorityQueue<? extends E>) c);
+        } else if (c instanceof SortedSet) {
+            getFromSortedSet((SortedSet<? extends E>) c);
+        } else {
+            initSize(c);
+            addAll(c);
+        }
+    }
+
+    /**
+     * Constructs a priority queue that contains the elements of another
+     * priority queue. The constructed priority queue has the initial capacity
+     * of 110% the latter one. And the two priority queue has the same
+     * comparator.
+     * 
+     * @param c the priority queue whose elements will be added to the priority
+     *        queue to be constructed.
+     */
+    public PriorityQueue(PriorityQueue<? extends E> c) {
+        getFromPriorityQueue(c);
+    }
+
+    /**
+     * Constructs a priority queue that contains the elements of a sorted set.
+     * The constructed priority queue has the initial capacity of 110% the
+     * sorted set. And the priority queue has the same comparator of the sorted
+     * set.
+     * 
+     * @param c the sorted set whose elements will be added to the priority
+     *        queue to be constructed.
+     */
+    public PriorityQueue(SortedSet<? extends E> c) {
+        getFromSortedSet(c);
+    }
+
+    /**
+     * Gets the iterator of the priority queue, which will not return elements
+     * in any specified ordering.
+     * 
+     * @return the iterator of the priority queue.
+     */
+    public Iterator<E> iterator() {
+        return new PriorityIterator();
+    }
+
+    /**
+     * Gets the size of the priority queue. If the size of the queue is greater
+     * than the Integer.MAX, then it returns Integer.MAX.
+     * 
+     * @return the size of the priority queue.
+     */
+    public int size() {
+        return size;
+    }
+
+    /**
+     * Removes all the elements of the priority queue.
+     */
+    public void clear() {
+        Arrays.fill(elements, null);
+        size = 0;
+    }
+
+    /**
+     * Inserts the element to the priority queue.
+     * 
+     * @return true
+     * @throws ClassCastException if the element cannot be compared with the
+     *         elements in the priority queue using the ordering of the priority
+     *         queue.
+     * @throws NullPointerExcepiton if the element is null.
+     */
+    public boolean offer(E o) {
+        if (null == o) {
+            throw new NullPointerException();
+        }
+        growToSize(size + 1);
+        elements[size] = o;
+        siftUp(size++);
+        return true;
+    }
+
+    /**
+     * Gets and removes the head of the queue.
+     * 
+     * @return the head of the queue. Null if the queue is empty.
+     */
+    public E poll() {
+        if (isEmpty()) {
+            return null;
+        }
+        E result = elements[0];
+        removeAt(0);
+        return result;
+    }
+
+    /**
+     * Gets but not removes the head of the queue.
+     * 
+     * @return the head of the queue. Null if the queue is empty.
+     */
+    public E peek() {
+        if (isEmpty()) {
+            return null;
+        }
+        return elements[0];
+    }
+
+    /**
+     * Gets the comparator of the priority queue.
+     * 
+     * @return the comparator of the priority queue. Null if the natural
+     *         ordering is used.
+     */
+    public Comparator<? super E> comparator() {
+        return comparator;
+    }
+
+    /**
+     * Removes the specified object of the priority queue.
+     * 
+     * @param o the object to be removed.
+     * @return true if the object is in the priority queue, false if the object
+     *         is not in the priority queue.
+     */
+    public boolean remove(Object o) {
+        if (o == null) {
+            return false;
+        }
+        int targetIndex;
+        for (targetIndex = 0; targetIndex < size; targetIndex++) {
+            if (0 == this.compare((E) o, elements[targetIndex])) {
+                break;
+            }
+        }
+        if (size == 0 || size == targetIndex) {
+            return false;
+        }
+        removeAt(targetIndex);
+        return true;
+    }
+
+    /**
+     * Adds the specified object to the priority queue.
+     * 
+     * @param o the object to be added.
+     * @return true.
+     * @throws ClassCastException if the element cannot be compared with the
+     *         elements in the priority queue using the ordering of the priority
+     *         queue.
+     * @throws NullPointerExcepiton if the element is null.
+     */
+    public boolean add(E o) {
+        return offer(o);
+    }
+
+    private class PriorityIterator implements Iterator<E> {
+
+        private int currentIndex = -1;
+
+        private boolean allowRemove = false;
+
+        public boolean hasNext() {
+            return currentIndex < size - 1;
+        }
+
+        public E next() {
+            if (!hasNext()) {
+                throw new NoSuchElementException();
+            }
+            allowRemove = true;
+            return elements[++currentIndex];
+        }
+
+        public void remove() {
+            if (!allowRemove) {
+                throw new IllegalStateException();
+            }
+            allowRemove = false;
+            removeAt(currentIndex--);
+        }
+    }
+
+    private void readObject(ObjectInputStream in) throws IOException,
+            ClassNotFoundException {
+        in.defaultReadObject();
+        int capacity = in.readInt();
+        elements = newElementArray(capacity);
+        for (int i = 0; i < size; i++) {
+            elements[i] = (E) in.readObject();
+        }
+    }
+
+    private E[] newElementArray(int capacity) {
+        return (E[]) new Object[capacity];
+    }
+
+    private void writeObject(ObjectOutputStream out) throws IOException {
+        out.defaultWriteObject();
+        out.writeInt(elements.length);
+        for (int i = 0; i < size; i++) {
+            out.writeObject(elements[i]);
+        }
+    }
+
+    private void getFromPriorityQueue(PriorityQueue<? extends E> c) {
+        initSize(c);
+        comparator = (Comparator<? super E>) c.comparator();
+        System.arraycopy(c.elements, 0, elements, 0, c.size());
+        size = c.size();
+    }
+
+    private void getFromSortedSet(SortedSet<? extends E> c) {
+        initSize(c);
+        comparator = (Comparator<? super E>) c.comparator();
+        Iterator<? extends E> iter = c.iterator();
+        while (iter.hasNext()) {
+            elements[size++] = iter.next();
+        }
+    }
+
+    private void removeAt(int index) {
+        size--;
+        elements[index] = elements[size];
+        siftDown(index);
+        elements[size] = null;
+    }
+
+    private int compare(E o1, E o2) {
+        if (null != comparator) {
+            return comparator.compare(o1, o2);
+        } else {
+            return ((Comparable<? super E>) o1).compareTo(o2);
+        }
+    }
+
+    private void siftUp(int childIndex) {
+        E target = elements[childIndex];
+        int parentIndex;
+        while (childIndex > 0) {
+            parentIndex = (childIndex - 1) / 2;
+            E parent = elements[parentIndex];
+            if (compare(parent, target) <= 0) {
+                break;
+            }
+            elements[childIndex] = parent;
+            childIndex = parentIndex;
+        }
+        elements[childIndex] = target;
+    }
+
+    private void siftDown(int rootIndex) {
+        E target = elements[rootIndex];
+        int childIndex;
+        while ((childIndex = rootIndex * 2 + 1) < size) {
+            if (childIndex + 1 < size
+                    && compare(elements[childIndex + 1], elements[childIndex]) < 0) {
+                childIndex++;
+            }
+            if (compare(target, elements[childIndex]) <= 0) {
+                break;
+            }
+            elements[rootIndex] = elements[childIndex];
+            rootIndex = childIndex;
+        }
+        elements[rootIndex] = target;
+    }
+
+    private void initSize(Collection<? extends E> c) {
+        if (null == c) {
+            throw new NullPointerException();
+        }
+        if (c.isEmpty()) {
+            elements = newElementArray(1);
+        } else {
+            int capacity = (int) Math.ceil(c.size()
+                    * DEFAULT_INIT_CAPACITY_RATIO);
+            elements = newElementArray(capacity);
+        }
+    }
+
+    private void growToSize(int size) {
+        if (size > elements.length) {
+            E[] newElements = newElementArray(size * DEFAULT_CAPACITY_RATIO);
+            System.arraycopy(elements, 0, newElements, 0, elements.length);
+            elements = newElements;
+        }
+    }
+}

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java?rev=415247&r1=415246&r2=415247&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/AllTests.java Sun Jun 18 22:32:21 2006
@@ -73,6 +73,7 @@
 		suite.addTestSuite(MissingResourceExceptionTest.class);
 		suite.addTestSuite(NoSuchElementExceptionTest.class);
 		suite.addTestSuite(ObservableTest.class);
+        suite.addTestSuite(PriorityQueueTest.class);
 		suite.addTestSuite(PropertiesTest.class);
 		suite.addTestSuite(PropertyPermissionTest.class);
 		suite.addTestSuite(PropertyResourceBundleTest.class);

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/PriorityQueueTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/PriorityQueueTest.java?rev=415247&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/PriorityQueueTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/PriorityQueueTest.java Sun Jun 18 22:32:21 2006
@@ -0,0 +1,765 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.PriorityQueue;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import tests.util.SerializationTester;
+
+import junit.framework.TestCase;
+
+public class PriorityQueueTest extends TestCase {
+
+    private static final String SERIALIZATION_FILE_NAME = "serialization/java/util/PriorityQueue.golden.ser"; //$NON-NLS-1$    
+
+    /**
+     * @tests java.util.PriorityQueue#iterator()
+     */
+    public void test_iterator() {
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
+        Integer[] array = { 2, 45, 7, -12, 9 };
+        for (int i = 0; i < array.length; i++) {
+            integerQueue.offer(array[i]);
+        }
+        Iterator<Integer> iter = integerQueue.iterator();
+        assertNotNull(iter);
+        ArrayList<Integer> iterResult = new ArrayList<Integer>();
+        while (iter.hasNext()) {
+            iterResult.add(iter.next());
+        }
+        Object[] resultArray = iterResult.toArray();
+        Arrays.sort(array);
+        Arrays.sort(resultArray);
+        assertTrue(Arrays.equals(array, resultArray));
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#iterator()
+     */
+    public void test_iterator_empty() {
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
+        Iterator<Integer> iter = integerQueue.iterator();
+        try {
+            iter.next();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+
+        iter = integerQueue.iterator();
+        try {
+            iter.remove();
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#iterator()
+     */
+    public void test_iterator_outofbound() {
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
+        integerQueue.offer(0);
+        Iterator<Integer> iter = integerQueue.iterator();
+        iter.next();
+        try {
+            iter.next();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+
+        iter = integerQueue.iterator();
+        iter.next();
+        iter.remove();
+        try {
+            iter.next();
+            fail("should throw NoSuchElementException");
+        } catch (NoSuchElementException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#iterator()
+     */
+    public void test_iterator_remove() {
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
+        Integer[] array = { 2, 45, 7, -12, 9 };
+        for (int i = 0; i < array.length; i++) {
+            integerQueue.offer(array[i]);
+        }
+        Iterator<Integer> iter = integerQueue.iterator();
+        assertNotNull(iter);
+        for (int i = 0; i < array.length; i++) {
+            iter.next();
+            if (2 == i) {
+                iter.remove();
+            }
+        }
+        assertEquals(array.length - 1, integerQueue.size());
+
+        iter = integerQueue.iterator();
+        Integer[] newArray = new Integer[array.length - 1];
+        for (int i = 0; i < newArray.length; i++) {
+            newArray[i] = iter.next();
+        }
+
+        Arrays.sort(newArray);
+        for (int i = 0; i < integerQueue.size(); i++) {
+            assertEquals(newArray[i], integerQueue.poll());
+        }
+
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#iterator()
+     */
+    public void test_iterator_remove_illegalState() {
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
+        Integer[] array = { 2, 45, 7, -12, 9 };
+        for (int i = 0; i < array.length; i++) {
+            integerQueue.offer(array[i]);
+        }
+        Iterator<Integer> iter = integerQueue.iterator();
+        assertNotNull(iter);
+        try {
+            iter.remove();
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // expected
+        }
+        iter.next();
+        iter.remove();
+        try {
+            iter.remove();
+            fail("should throw IllegalStateException");
+        } catch (IllegalStateException e) {
+            // expected
+        }
+
+    }
+
+    /**
+     * @tests java.util.PriorityQueue.size()
+     */
+    public void test_size() {
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
+        assertEquals(0, integerQueue.size());
+        int[] array = { 2, 45, 7, -12, 9 };
+        for (int i = 0; i < array.length; i++) {
+            integerQueue.offer(array[i]);
+        }
+        assertEquals(array.length, integerQueue.size());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue()
+     */
+    public void test_Constructor() {
+        PriorityQueue<Object> queue = new PriorityQueue<Object>();
+        assertNotNull(queue);
+        assertEquals(0, queue.size());
+        assertNull(queue.comparator());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue(int)
+     */
+    public void test_ConstructorI() {
+        PriorityQueue<Object> queue = new PriorityQueue<Object>(100);
+        assertNotNull(queue);
+        assertEquals(0, queue.size());
+        assertNull(queue.comparator());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue(int, Comparator<? super E>)
+     */
+    public void test_ConstructorILjava_util_Comparator() {
+        PriorityQueue<Object> queue = new PriorityQueue<Object>(100,
+                (Comparator<Object>) null);
+        assertNotNull(queue);
+        assertEquals(0, queue.size());
+        assertNull(queue.comparator());
+
+        MockComparator<Object> comparator = new MockComparator<Object>();
+        queue = new PriorityQueue<Object>(100, comparator);
+        assertNotNull(queue);
+        assertEquals(0, queue.size());
+        assertEquals(comparator, queue.comparator());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue(int, Comparator<? super E>)
+     */
+    public void test_ConstructorILjava_util_Comparator_illegalCapacity() {
+        try {
+            new PriorityQueue<Object>(0, new MockComparator<Object>());
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+        try {
+            new PriorityQueue<Object>(-1, new MockComparator<Object>());
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue(int, Comparator<? super E>)
+     */
+    public void test_ConstructorILjava_util_Comparator_cast() {
+        MockComparatorCast<Object> objectComparator = new MockComparatorCast<Object>();
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(100,
+                objectComparator);
+        assertNotNull(integerQueue);
+        assertEquals(0, integerQueue.size());
+        assertEquals(objectComparator, integerQueue.comparator());
+        Integer[] array = { 2, 45, 7, -12, 9 };
+        List<Integer> list = Arrays.asList(array);
+        integerQueue.addAll(list);
+        assertEquals(list.size(), integerQueue.size());
+        // just test here no cast exception raises.
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue(Collection)
+     */
+    public void test_ConstructorLjava_util_Colleciton() {
+        Integer[] array = { 2, 45, 7, -12, 9 };
+        List<Integer> list = Arrays.asList(array);
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
+        assertEquals(array.length, integerQueue.size());
+        assertNull(integerQueue.comparator());
+        Arrays.sort(array);
+        for (int i = 0; i < array.length; i++) {
+            assertEquals(array[i], integerQueue.poll());
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue(Collection)
+     */
+    public void test_ConstructorLjava_util_Colleciton_null() {
+        ArrayList<Object> list = new ArrayList<Object>();
+        list.add(new Float(11));
+        list.add(null);
+        list.add(new Integer(10));
+        try {
+            new PriorityQueue<Object>(list);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue(Collection)
+     */
+    public void test_ConstructorLjava_util_Colleciton_non_comparable() {
+        ArrayList<Object> list = new ArrayList<Object>();
+        list.add(new Float(11));
+        list.add(new Integer(10));
+        try {
+            new PriorityQueue<Object>(list);
+            fail("should throw ClassCastException");
+        } catch (ClassCastException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue(Collection)
+     */
+    public void test_ConstructorLjava_util_Colleciton_from_priorityqueue() {
+        String[] array = { "AAAAA", "AA", "AAAA", "AAAAAAAA" };
+        PriorityQueue<String> queue = new PriorityQueue<String>(4,
+                new MockComparatorStringByLength());
+        for (int i = 0; i < array.length; i++) {
+            queue.offer(array[i]);
+        }
+        Collection<String> c = queue;
+        PriorityQueue<String> constructedQueue = new PriorityQueue<String>(c);
+        assertEquals(queue.comparator(), constructedQueue.comparator());
+        while (queue.size() > 0) {
+            assertEquals(queue.poll(), constructedQueue.poll());
+        }
+        assertEquals(0, constructedQueue.size());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue(Collection)
+     */
+    public void test_ConstructorLjava_util_Colleciton_from_sortedset() {
+        int[] array = { 3, 5, 79, -17, 5 };
+        TreeSet<Integer> treeSet = new TreeSet<Integer>(new MockComparator<Integer>());
+        for (int i = 0; i < array.length; i++) {
+            treeSet.add(array[i]);
+        }
+        Collection<? extends Integer> c = treeSet;
+        PriorityQueue<Integer> queue = new PriorityQueue<Integer>(c);
+        assertEquals(treeSet.comparator(), queue.comparator());
+        Iterator<Integer> iter = treeSet.iterator();
+        while (iter.hasNext()) {
+            assertEquals(iter.next(), queue.poll());
+        }
+        assertEquals(0, queue.size());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue(PriorityQueue<? * extends
+     *        E>)
+     */
+    public void test_ConstructorLjava_util_PriorityQueue() {
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
+        int[] array = { 2, 45, 7, -12, 9 };
+        for (int i = 0; i < array.length; i++) {
+            integerQueue.offer(array[i]);
+        }
+        PriorityQueue<Object> objectQueue = new PriorityQueue<Object>(
+                integerQueue);
+        assertEquals(integerQueue.size(), objectQueue.size());
+        assertEquals(integerQueue.comparator(), objectQueue.comparator());
+        Arrays.sort(array);
+        for (int i = 0; i < array.length; i++) {
+            assertEquals(array[i], objectQueue.poll());
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue(PriorityQueue<? * extends
+     *        E>)
+     */
+    public void test_ConstructorLjava_util_PriorityQueue_null() {
+        try {
+            new PriorityQueue<Object>((PriorityQueue<Integer>) null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue(SortedSet<? extends E>)
+     */
+    public void test_ConstructorLjava_util_SortedSet() {
+        int[] array = { 3, 5, 79, -17, 5 };
+        TreeSet<Integer> treeSet = new TreeSet<Integer>();
+        for (int i = 0; i < array.length; i++) {
+            treeSet.add(array[i]);
+        }
+        PriorityQueue<Integer> queue = new PriorityQueue<Integer>(treeSet);
+        Iterator<Integer> iter = treeSet.iterator();
+        while (iter.hasNext()) {
+            assertEquals(iter.next(), queue.poll());
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#PriorityQueue(SortedSet<? extends E>)
+     */
+    public void test_ConstructorLjava_util_SortedSet_null() {
+        try {
+            new PriorityQueue<Integer>((SortedSet<? extends Integer>) null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#offer(Object)
+     */
+    public void test_offerLjava_lang_Object() {
+        PriorityQueue<String> queue = new PriorityQueue<String>(10,
+                new MockComparatorStringByLength());
+        String[] array = { "AAAAA", "AA", "AAAA", "AAAAAAAA" };
+        for (int i = 0; i < array.length; i++) {
+            queue.offer(array[i]);
+        }
+        String[] sortedArray = { "AA", "AAAA", "AAAAA", "AAAAAAAA" };
+        for (int i = 0; i < sortedArray.length; i++) {
+            assertEquals(sortedArray[i], queue.poll());
+        }
+        assertEquals(0, queue.size());
+        assertNull(queue.poll());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#offer(Object)
+     */
+    public void test_offerLjava_lang_Object_null() {
+        PriorityQueue<Object> queue = new PriorityQueue<Object>();
+        try {
+            queue.offer(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#offer(Object)
+     */
+    public void test_offer_Ljava_lang_Object_non_Comparable() {
+        PriorityQueue<Object> queue = new PriorityQueue<Object>();
+        queue.offer(new Integer(10));
+        try {
+            queue.offer(new Float(1.3));
+            fail("should throw ClassCastException");
+        } catch (ClassCastException e) {
+            // expected
+        }
+
+        queue = new PriorityQueue<Object>();
+        queue.offer(new Integer(10));
+        try {
+            queue.offer(new Object());
+            fail("should throw ClassCastException");
+        } catch (ClassCastException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#poll()
+     */
+    public void test_poll() {
+        PriorityQueue<String> stringQueue = new PriorityQueue<String>();
+        String[] array = { "MYTESTSTRING", "AAAAA", "BCDEF", "ksTRD", "AAAAA" };
+        for (int i = 0; i < array.length; i++) {
+            stringQueue.offer(array[i]);
+        }
+        Arrays.sort(array);
+        for (int i = 0; i < array.length; i++) {
+            assertEquals(array[i], stringQueue.poll());
+        }
+        assertEquals(0, stringQueue.size());
+        assertNull(stringQueue.poll());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#poll()
+     */
+    public void test_poll_empty() {
+        PriorityQueue<Object> queue = new PriorityQueue<Object>();
+        assertEquals(0, queue.size());
+        assertNull(queue.poll());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#peek()
+     */
+    public void test_peek() {
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
+        int[] array = { 2, 45, 7, -12, 9 };
+        for (int i = 0; i < array.length; i++) {
+            integerQueue.add(array[i]);
+        }
+        Arrays.sort(array);
+        assertEquals(new Integer(array[0]), integerQueue.peek());
+        assertEquals(new Integer(array[0]), integerQueue.peek());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#peek()
+     */
+    public void test_peek_empty() {
+        PriorityQueue<Object> queue = new PriorityQueue<Object>();
+        assertEquals(0, queue.size());
+        assertNull(queue.peek());
+        assertNull(queue.peek());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#Clear()
+     */
+    public void test_clear() {
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
+        int[] array = { 2, 45, 7, -12, 9 };
+        for (int i = 0; i < array.length; i++) {
+            integerQueue.offer(array[i]);
+        }
+        integerQueue.clear();
+        assertTrue(integerQueue.isEmpty());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#add(Object)
+     */
+    public void test_add_Ljava_lang_Object() {
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
+        Integer[] array = { 2, 45, 7, -12, 9 };
+        for (int i = 0; i < array.length; i++) {
+            integerQueue.add(array[i]);
+        }
+        Arrays.sort(array);
+        assertEquals(array.length, integerQueue.size());
+        for (int i = 0; i < array.length; i++) {
+            assertEquals(array[i], integerQueue.poll());
+        }
+        assertEquals(0, integerQueue.size());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#add(Object)
+     */
+    public void test_add_Ljava_lang_Object_null() {
+        PriorityQueue<Object> queue = new PriorityQueue<Object>();
+        try {
+            queue.add(null);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#add(Object)
+     */
+    public void test_add_Ljava_lang_Object_non_Comparable() {
+        PriorityQueue<Object> queue = new PriorityQueue<Object>();
+        queue.add(new Integer(10));
+        try {
+            queue.add(new Float(1.3));
+            fail("should throw ClassCastException");
+        } catch (ClassCastException e) {
+            // expected
+        }
+
+        queue = new PriorityQueue<Object>();
+        queue.add(new Integer(10));
+        try {
+            queue.add(new Object());
+            fail("should throw ClassCastException");
+        } catch (ClassCastException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#remove(Object)
+     * 
+     */
+    public void test_remove_Ljava_lang_Object() {
+        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
+        List<Integer> list = Arrays.asList(array);
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
+        assertTrue(integerQueue.remove(16));
+        Integer[] newArray = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 39 };
+        Arrays.sort(newArray);
+        for (int i = 0; i < newArray.length; i++) {
+            assertEquals(newArray[i], integerQueue.poll());
+        }
+        assertEquals(0, integerQueue.size());
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#remove(Object)
+     * 
+     */
+    public void test_remove_Ljava_lang_Object_using_comparator() {
+        PriorityQueue<String> queue = new PriorityQueue<String>(10,
+                new MockComparatorStringByLength());
+        String[] array = { "AAAAA", "AA", "AAAA", "AAAAAAAA" };
+        for (int i = 0; i < array.length; i++) {
+            queue.offer(array[i]);
+        }
+        assertFalse(queue.contains("BB"));
+        assertTrue(queue.remove("BB"));
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#remove(Object)
+     * 
+     */
+    public void test_remove_Ljava_lang_Object_not_exists() {
+        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
+        List<Integer> list = Arrays.asList(array);
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
+        assertFalse(integerQueue.remove(111));
+        assertFalse(integerQueue.remove(null));
+        try {
+            integerQueue.remove("");
+            fail("should throw ClassCastException");
+        } catch (ClassCastException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#remove(Object)
+     * 
+     */
+    public void test_remove_Ljava_lang_Object_null() {
+        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
+        List<Integer> list = Arrays.asList(array);
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
+        assertFalse(integerQueue.remove(null));
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#remove(Object)
+     * 
+     */
+    public void test_remove_Ljava_lang_Object_not_Compatible() {
+        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
+        List<Integer> list = Arrays.asList(array);
+        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
+        try {
+            integerQueue.remove(new Float(1.3F));
+            fail("should throw ClassCastException");
+        } catch (ClassCastException e) {
+            // expected
+        }
+
+        // although argument element type is not compatible with those in queue,
+        // but comparator supports it.
+        MockComparator<Object> comparator = new MockComparator<Object>();
+        PriorityQueue<Integer> integerQueue1 = new PriorityQueue<Integer>(100,
+                comparator);
+        integerQueue1.offer(1);
+        assertFalse(integerQueue1.remove(new Float(1.3F)));
+
+        PriorityQueue<Object> queue = new PriorityQueue<Object>();
+        Object o = new Object();
+        queue.offer(o);
+        try {
+            queue.remove(o);
+            fail("should throw ClassCastException");
+        } catch (ClassCastException e) {
+            // expected
+        }
+    }
+
+    /**
+     * @tests java.util.PriorityQueue#comparator()
+     */
+    public void test_comparator() {
+        PriorityQueue<Object> queue = new PriorityQueue<Object>();
+        assertNull(queue.comparator());
+
+        MockComparator<Object> comparator = new MockComparator<Object>();
+        queue = new PriorityQueue<Object>(100, comparator);
+        assertEquals(comparator, queue.comparator());
+    }
+
+    /**
+     * @tests serialization/deserialization.
+     */
+    public void test_Serialization() throws Exception {
+        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
+        List<Integer> list = Arrays.asList(array);
+        PriorityQueue<Integer> srcIntegerQueue = new PriorityQueue<Integer>(
+                list);
+        PriorityQueue<Integer> destIntegerQueue = (PriorityQueue<Integer>) SerializationTester
+                .getDeserilizedObject(srcIntegerQueue);
+        Arrays.sort(array);
+        for (int i = 0; i < array.length; i++) {
+            assertEquals(array[i], destIntegerQueue.poll());
+        }
+        assertEquals(0, destIntegerQueue.size());
+    }
+
+    /**
+     * @tests serialization/deserialization.
+     */
+    public void test_Serialization_casting() throws Exception {
+        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
+        List<Integer> list = Arrays.asList(array);
+        PriorityQueue<Integer> srcIntegerQueue = new PriorityQueue<Integer>(
+                list);
+        PriorityQueue<String> destStringQueue = (PriorityQueue<String>) SerializationTester
+                .getDeserilizedObject(srcIntegerQueue);
+        // will not incur class cast exception.
+        Object o = destStringQueue.peek();
+        Arrays.sort(array);
+        Integer I = (Integer) o;
+        assertEquals(array[0], I);
+    }
+
+    /**
+     * @tests serialization/deserialization compatibility with RI.
+     */
+    public void test_SerializationCompatibility_cast() throws Exception {
+        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
+        List<Integer> list = Arrays.asList(array);
+        PriorityQueue<Integer> srcIntegerQueue = new PriorityQueue<Integer>(
+                list);
+        PriorityQueue<String> destStringQueue = (PriorityQueue<String>) SerializationTester
+                .readObject(srcIntegerQueue, SERIALIZATION_FILE_NAME);
+
+        // will not incur class cast exception.
+        Object o = destStringQueue.peek();
+        Arrays.sort(array);
+        Integer I = (Integer) o;
+        assertEquals(array[0], I);
+    }
+
+    private static class MockComparator<E> implements Comparator<E> {
+
+        public int compare(E object1, E object2) {
+            int hashcode1 = object1.hashCode();
+            int hashcode2 = object2.hashCode();
+            if (hashcode1 > hashcode2) {
+                return 1;
+            } else if (hashcode1 == hashcode2) {
+                return 0;
+            } else {
+                return -1;
+            }
+        }
+    }
+
+    private static class MockComparatorStringByLength implements
+            Comparator<String> {
+
+        public int compare(String object1, String object2) {
+            int length1 = object1.length();
+            int length2 = object2.length();
+            if (length1 > length2) {
+                return 1;
+            } else if (length1 == length2) {
+                return 0;
+            } else {
+                return -1;
+            }
+        }
+
+    }
+
+    private static class MockComparatorCast<E> implements Comparator<E> {
+
+        public int compare(E object1, E object2) {
+            return 0;
+        }
+    }
+
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/util/PriorityQueue.golden.ser
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/util/PriorityQueue.golden.ser?rev=415247&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/util/PriorityQueue.golden.ser
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream