PriorityQueueTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package tests.api.java.util;
18
19import dalvik.annotation.TestTargetNew;
20import dalvik.annotation.TestTargets;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetClass;
23
24import java.util.ArrayList;
25import java.util.Arrays;
26import java.util.Collection;
27import java.util.Comparator;
28import java.util.Iterator;
29import java.util.List;
30import java.util.NoSuchElementException;
31import java.util.PriorityQueue;
32import java.util.SortedSet;
33import java.util.TreeSet;
34
35import tests.util.SerializationTester;
36
37import junit.framework.TestCase;
38
39@TestTargetClass(PriorityQueue.class)
40public class PriorityQueueTest extends TestCase {
41
42    private static final String SERIALIZATION_FILE_NAME = "/serialization/tests/api/java/util/PriorityQueue.golden.ser"; //$NON-NLS-1$
43
44    /**
45     * @tests java.util.PriorityQueue#iterator()
46     */
47    @TestTargetNew(
48        level = TestLevel.COMPLETE,
49        notes = "",
50        method = "iterator",
51        args = {}
52    )
53    public void test_iterator() {
54        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
55        Integer[] array = { 2, 45, 7, -12, 9 };
56        for (int i = 0; i < array.length; i++) {
57            integerQueue.offer(array[i]);
58        }
59        Iterator<Integer> iter = integerQueue.iterator();
60        assertNotNull(iter);
61        ArrayList<Integer> iterResult = new ArrayList<Integer>();
62        while (iter.hasNext()) {
63            iterResult.add(iter.next());
64        }
65        Object[] resultArray = iterResult.toArray();
66        Arrays.sort(array);
67        Arrays.sort(resultArray);
68        assertTrue(Arrays.equals(array, resultArray));
69    }
70
71    /**
72     * @tests java.util.PriorityQueue#iterator()
73     */
74    @TestTargetNew(
75        level = TestLevel.PARTIAL_COMPLETE,
76        notes = "Verifies NoSuchElementException, IllegalStateException.",
77        method = "iterator",
78        args = {}
79    )
80    public void test_iterator_empty() {
81        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
82        Iterator<Integer> iter = integerQueue.iterator();
83        try {
84            iter.next();
85            fail("should throw NoSuchElementException");
86        } catch (NoSuchElementException e) {
87            // expected
88        }
89
90        iter = integerQueue.iterator();
91        try {
92            iter.remove();
93            fail("should throw IllegalStateException");
94        } catch (IllegalStateException e) {
95            // expected
96        }
97    }
98
99    /**
100     * @tests java.util.PriorityQueue#iterator()
101     */
102    @TestTargetNew(
103        level = TestLevel.PARTIAL_COMPLETE,
104        notes = "Verifies NoSuchElementException.",
105        method = "iterator",
106        args = {}
107    )
108    public void test_iterator_outofbound() {
109        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
110        integerQueue.offer(0);
111        Iterator<Integer> iter = integerQueue.iterator();
112        iter.next();
113        try {
114            iter.next();
115            fail("should throw NoSuchElementException");
116        } catch (NoSuchElementException e) {
117            // expected
118        }
119
120        iter = integerQueue.iterator();
121        iter.next();
122        iter.remove();
123        try {
124            iter.next();
125            fail("should throw NoSuchElementException");
126        } catch (NoSuchElementException e) {
127            // expected
128        }
129    }
130
131    /**
132     * @tests java.util.PriorityQueue#iterator()
133     */
134    @TestTargetNew(
135        level = TestLevel.COMPLETE,
136        notes = "",
137        method = "iterator",
138        args = {}
139    )
140    public void test_iterator_remove() {
141        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
142        Integer[] array = { 2, 45, 7, -12, 9 };
143        for (int i = 0; i < array.length; i++) {
144            integerQueue.offer(array[i]);
145        }
146        Iterator<Integer> iter = integerQueue.iterator();
147        assertNotNull(iter);
148        for (int i = 0; i < array.length; i++) {
149            iter.next();
150            if (2 == i) {
151                iter.remove();
152            }
153        }
154        assertEquals(array.length - 1, integerQueue.size());
155
156        iter = integerQueue.iterator();
157        Integer[] newArray = new Integer[array.length - 1];
158        for (int i = 0; i < newArray.length; i++) {
159            newArray[i] = iter.next();
160        }
161
162        Arrays.sort(newArray);
163        for (int i = 0; i < integerQueue.size(); i++) {
164            assertEquals(newArray[i], integerQueue.poll());
165        }
166
167    }
168
169    /**
170     * @tests java.util.PriorityQueue#iterator()
171     */
172    @TestTargetNew(
173        level = TestLevel.PARTIAL_COMPLETE,
174        notes = "Verifies IllegalStateException.",
175        method = "iterator",
176        args = {}
177    )
178    public void test_iterator_remove_illegalState() {
179        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
180        Integer[] array = { 2, 45, 7, -12, 9 };
181        for (int i = 0; i < array.length; i++) {
182            integerQueue.offer(array[i]);
183        }
184        Iterator<Integer> iter = integerQueue.iterator();
185        assertNotNull(iter);
186        try {
187            iter.remove();
188            fail("should throw IllegalStateException");
189        } catch (IllegalStateException e) {
190            // expected
191        }
192        iter.next();
193        iter.remove();
194        try {
195            iter.remove();
196            fail("should throw IllegalStateException");
197        } catch (IllegalStateException e) {
198            // expected
199        }
200
201    }
202
203    /**
204     * @tests java.util.PriorityQueue.size()
205     */
206    @TestTargetNew(
207        level = TestLevel.COMPLETE,
208        notes = "",
209        method = "size",
210        args = {}
211    )
212    public void test_size() {
213        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
214        assertEquals(0, integerQueue.size());
215        int[] array = { 2, 45, 7, -12, 9 };
216        for (int i = 0; i < array.length; i++) {
217            integerQueue.offer(array[i]);
218        }
219        assertEquals(array.length, integerQueue.size());
220    }
221
222    /**
223     * @tests java.util.PriorityQueue#PriorityQueue()
224     */
225    @TestTargetNew(
226        level = TestLevel.COMPLETE,
227        notes = "",
228        method = "PriorityQueue",
229        args = {}
230    )
231    public void test_Constructor() {
232        PriorityQueue<Object> queue = new PriorityQueue<Object>();
233        assertNotNull(queue);
234        assertEquals(0, queue.size());
235        assertNull(queue.comparator());
236    }
237
238    /**
239     * @tests java.util.PriorityQueue#PriorityQueue(int)
240     */
241    @TestTargetNew(
242        level = TestLevel.COMPLETE,
243        notes = "",
244        method = "PriorityQueue",
245        args = {int.class}
246    )
247    public void test_ConstructorI() {
248        PriorityQueue<Object> queue = new PriorityQueue<Object>(100);
249        assertNotNull(queue);
250        assertEquals(0, queue.size());
251        assertNull(queue.comparator());
252
253        try {
254            new PriorityQueue(0);
255            fail("IllegalArgumentException expected");
256        } catch (IllegalArgumentException e) {
257            //expected
258        }
259    }
260
261    /**
262     * @tests java.util.PriorityQueue#PriorityQueue(int, Comparator<? super E>)
263     */
264    @TestTargetNew(
265        level = TestLevel.PARTIAL_COMPLETE,
266        notes = "Doesn't verify IllegalArgumentException.",
267        method = "PriorityQueue",
268        args = {int.class, java.util.Comparator.class}
269    )
270    public void test_ConstructorILjava_util_Comparator() {
271        PriorityQueue<Object> queue = new PriorityQueue<Object>(100,
272                (Comparator<Object>) null);
273        assertNotNull(queue);
274        assertEquals(0, queue.size());
275        assertNull(queue.comparator());
276
277        MockComparator<Object> comparator = new MockComparator<Object>();
278        queue = new PriorityQueue<Object>(100, comparator);
279        assertNotNull(queue);
280        assertEquals(0, queue.size());
281        assertEquals(comparator, queue.comparator());
282    }
283
284    /**
285     * @tests java.util.PriorityQueue#PriorityQueue(int, Comparator<? super E>)
286     */
287    @TestTargetNew(
288        level = TestLevel.PARTIAL_COMPLETE,
289        notes = "Verifies IllegalArgumentException.",
290        method = "PriorityQueue",
291        args = {int.class, java.util.Comparator.class}
292    )
293    public void test_ConstructorILjava_util_Comparator_illegalCapacity() {
294        try {
295            new PriorityQueue<Object>(0, new MockComparator<Object>());
296            fail("should throw IllegalArgumentException");
297        } catch (IllegalArgumentException e) {
298            // expected
299        }
300
301        try {
302            new PriorityQueue<Object>(-1, new MockComparator<Object>());
303            fail("should throw IllegalArgumentException");
304        } catch (IllegalArgumentException e) {
305            // expected
306        }
307    }
308
309    /**
310     * @tests java.util.PriorityQueue#PriorityQueue(int, Comparator<? super E>)
311     */
312    @TestTargetNew(
313        level = TestLevel.PARTIAL_COMPLETE,
314        notes = "Doesn't verify IllegalArgumentException.",
315        method = "PriorityQueue",
316        args = {int.class, java.util.Comparator.class}
317    )
318    public void test_ConstructorILjava_util_Comparator_cast() {
319        MockComparatorCast<Object> objectComparator = new MockComparatorCast<Object>();
320        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(100,
321                objectComparator);
322        assertNotNull(integerQueue);
323        assertEquals(0, integerQueue.size());
324        assertEquals(objectComparator, integerQueue.comparator());
325        Integer[] array = { 2, 45, 7, -12, 9 };
326        List<Integer> list = Arrays.asList(array);
327        integerQueue.addAll(list);
328        assertEquals(list.size(), integerQueue.size());
329        // just test here no cast exception raises.
330    }
331
332    /**
333     * @tests java.util.PriorityQueue#PriorityQueue(Collection)
334     */
335    @TestTargetNew(
336        level = TestLevel.PARTIAL_COMPLETE,
337        notes = "Doesn't verify ClassCastException, NullPointerException.",
338        method = "PriorityQueue",
339        args = {java.util.Collection.class}
340    )
341    public void test_ConstructorLjava_util_Colleciton() {
342        Integer[] array = { 2, 45, 7, -12, 9 };
343        List<Integer> list = Arrays.asList(array);
344        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
345        assertEquals(array.length, integerQueue.size());
346        assertNull(integerQueue.comparator());
347        Arrays.sort(array);
348        for (int i = 0; i < array.length; i++) {
349            assertEquals(array[i], integerQueue.poll());
350        }
351    }
352
353    /**
354     * @tests java.util.PriorityQueue#PriorityQueue(Collection)
355     */
356    @TestTargetNew(
357        level = TestLevel.PARTIAL_COMPLETE,
358        notes = "Verifies NullPointerException.",
359        method = "PriorityQueue",
360        args = {java.util.Collection.class}
361    )
362    public void test_ConstructorLjava_util_Colleciton_null() {
363        ArrayList<Object> list = new ArrayList<Object>();
364        list.add(new Float(11));
365        list.add(null);
366        list.add(new Integer(10));
367        try {
368            new PriorityQueue<Object>(list);
369            fail("should throw NullPointerException");
370        } catch (NullPointerException e) {
371            // expected
372        }
373    }
374
375    /**
376     * @tests java.util.PriorityQueue#PriorityQueue(Collection)
377     */
378    @TestTargetNew(
379        level = TestLevel.PARTIAL_COMPLETE,
380        notes = "Verifies ClassCastException.",
381        method = "PriorityQueue",
382        args = {java.util.Collection.class}
383    )
384    public void test_ConstructorLjava_util_Colleciton_non_comparable() {
385        ArrayList<Object> list = new ArrayList<Object>();
386        list.add(new Float(11));
387        list.add(new Integer(10));
388        try {
389            new PriorityQueue<Object>(list);
390            fail("should throw ClassCastException");
391        } catch (ClassCastException e) {
392            // expected
393        }
394    }
395
396    /**
397     * @tests java.util.PriorityQueue#PriorityQueue(Collection)
398     */
399    @TestTargetNew(
400        level = TestLevel.PARTIAL_COMPLETE,
401        notes = "Doesn't verify ClassCastException, NullPointerException.",
402        method = "PriorityQueue",
403        args = {java.util.Collection.class}
404    )
405    public void test_ConstructorLjava_util_Colleciton_from_priorityqueue() {
406        String[] array = { "AAAAA", "AA", "AAAA", "AAAAAAAA" };
407        PriorityQueue<String> queue = new PriorityQueue<String>(4,
408                new MockComparatorStringByLength());
409        for (int i = 0; i < array.length; i++) {
410            queue.offer(array[i]);
411        }
412        Collection<String> c = queue;
413        PriorityQueue<String> constructedQueue = new PriorityQueue<String>(c);
414        assertEquals(queue.comparator(), constructedQueue.comparator());
415        while (queue.size() > 0) {
416            assertEquals(queue.poll(), constructedQueue.poll());
417        }
418        assertEquals(0, constructedQueue.size());
419    }
420
421    /**
422     * @tests java.util.PriorityQueue#PriorityQueue(Collection)
423     */
424    @TestTargetNew(
425        level = TestLevel.PARTIAL_COMPLETE,
426        notes = "Doesn't verify ClassCastException, NullPointerException.",
427        method = "PriorityQueue",
428        args = {java.util.Collection.class}
429    )
430    public void test_ConstructorLjava_util_Colleciton_from_sortedset() {
431        int[] array = { 3, 5, 79, -17, 5 };
432        TreeSet<Integer> treeSet = new TreeSet<Integer>(new MockComparator<Integer>());
433        for (int i = 0; i < array.length; i++) {
434            treeSet.add(array[i]);
435        }
436        Collection<? extends Integer> c = treeSet;
437        PriorityQueue<Integer> queue = new PriorityQueue<Integer>(c);
438        assertEquals(treeSet.comparator(), queue.comparator());
439        Iterator<Integer> iter = treeSet.iterator();
440        while (iter.hasNext()) {
441            assertEquals(iter.next(), queue.poll());
442        }
443        assertEquals(0, queue.size());
444    }
445
446    /**
447     * @tests java.util.PriorityQueue#PriorityQueue(PriorityQueue<? * extends
448     *        E>)
449     */
450    @TestTargetNew(
451        level = TestLevel.COMPLETE,
452        notes = "ClassCastException can not be checked.",
453        method = "PriorityQueue",
454        args = {java.util.PriorityQueue.class}
455    )
456    public void test_ConstructorLjava_util_PriorityQueue() {
457        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
458        int[] array = { 2, 45, 7, -12, 9 };
459        for (int i = 0; i < array.length; i++) {
460            integerQueue.offer(array[i]);
461        }
462        PriorityQueue objectQueue = new PriorityQueue(
463                integerQueue);
464        assertEquals(integerQueue.size(), objectQueue.size());
465        assertEquals(integerQueue.comparator(), objectQueue.comparator());
466        Arrays.sort(array);
467        for (int i = 0; i < array.length; i++) {
468            assertEquals(array[i], objectQueue.poll());
469        }
470
471        try {
472            new PriorityQueue((PriorityQueue)null);
473            fail("NullPointerException expected");
474        } catch (NullPointerException e) {
475            //expected
476        }
477    }
478
479    /**
480     * @tests java.util.PriorityQueue#PriorityQueue(PriorityQueue<? * extends
481     *        E>)
482     */
483    @TestTargetNew(
484        level = TestLevel.PARTIAL_COMPLETE,
485        notes = "Verifies NullPointerException.",
486        method = "PriorityQueue",
487        args = {java.util.PriorityQueue.class}
488    )
489    public void test_ConstructorLjava_util_PriorityQueue_null() {
490        try {
491            new PriorityQueue<Object>((PriorityQueue<Integer>) null);
492            fail("should throw NullPointerException");
493        } catch (NullPointerException e) {
494            // expected
495        }
496    }
497
498    /**
499     * @tests java.util.PriorityQueue#PriorityQueue(SortedSet<? extends E>)
500     */
501    @TestTargetNew(
502        level = TestLevel.PARTIAL_COMPLETE,
503        notes = "Doesn't verify ClassCastException, NullPointerException.",
504        method = "PriorityQueue",
505        args = {java.util.SortedSet.class}
506    )
507    public void test_ConstructorLjava_util_SortedSet() {
508        int[] array = { 3, 5, 79, -17, 5 };
509        TreeSet<Integer> treeSet = new TreeSet<Integer>();
510        for (int i = 0; i < array.length; i++) {
511            treeSet.add(array[i]);
512        }
513        PriorityQueue<Integer> queue = new PriorityQueue<Integer>(treeSet);
514        Iterator<Integer> iter = treeSet.iterator();
515        while (iter.hasNext()) {
516            assertEquals(iter.next(), queue.poll());
517        }
518    }
519
520    /**
521     * @tests java.util.PriorityQueue#PriorityQueue(SortedSet<? extends E>)
522     */
523    @TestTargetNew(
524        level = TestLevel.PARTIAL_COMPLETE,
525        notes = "Verifies NullPointerException.",
526        method = "PriorityQueue",
527        args = {java.util.SortedSet.class}
528    )
529    public void test_ConstructorLjava_util_SortedSet_null() {
530        try {
531            new PriorityQueue<Integer>((SortedSet<? extends Integer>) null);
532            fail("should throw NullPointerException");
533        } catch (NullPointerException e) {
534            // expected
535        }
536    }
537
538    /**
539     * @tests java.util.PriorityQueue#offer(Object)
540     */
541    @TestTargetNew(
542        level = TestLevel.PARTIAL_COMPLETE,
543        notes = "Doesn't verify exceptions.",
544        method = "offer",
545        args = {java.lang.Object.class}
546    )
547    public void test_offerLjava_lang_Object() {
548        PriorityQueue<String> queue = new PriorityQueue<String>(10,
549                new MockComparatorStringByLength());
550        String[] array = { "AAAAA", "AA", "AAAA", "AAAAAAAA" };
551        for (int i = 0; i < array.length; i++) {
552            queue.offer(array[i]);
553        }
554        String[] sortedArray = { "AA", "AAAA", "AAAAA", "AAAAAAAA" };
555        for (int i = 0; i < sortedArray.length; i++) {
556            assertEquals(sortedArray[i], queue.poll());
557        }
558        assertEquals(0, queue.size());
559        assertNull(queue.poll());
560    }
561
562    /**
563     * @tests java.util.PriorityQueue#offer(Object)
564     */
565    @TestTargetNew(
566        level = TestLevel.PARTIAL_COMPLETE,
567        notes = "Verifies NullPointerException.",
568        method = "offer",
569        args = {java.lang.Object.class}
570    )
571    public void test_offerLjava_lang_Object_null() {
572        PriorityQueue<Object> queue = new PriorityQueue<Object>();
573        try {
574            queue.offer(null);
575            fail("should throw NullPointerException");
576        } catch (NullPointerException e) {
577            // expected
578        }
579    }
580
581    /**
582     * @tests java.util.PriorityQueue#offer(Object)
583     */
584    @TestTargetNew(
585        level = TestLevel.PARTIAL_COMPLETE,
586        notes = "Verifies ClassCastException.",
587        method = "offer",
588        args = {java.lang.Object.class}
589    )
590    public void test_offer_Ljava_lang_Object_non_Comparable() {
591        PriorityQueue<Object> queue = new PriorityQueue<Object>();
592        queue.offer(new Integer(10));
593        try {
594            queue.offer(new Float(1.3));
595            fail("should throw ClassCastException");
596        } catch (ClassCastException e) {
597            // expected
598        }
599
600        queue = new PriorityQueue<Object>();
601        queue.offer(new Integer(10));
602        try {
603            queue.offer(new Object());
604            fail("should throw ClassCastException");
605        } catch (ClassCastException e) {
606            // expected
607        }
608    }
609
610    /**
611     * @tests java.util.PriorityQueue#poll()
612     */
613    @TestTargetNew(
614        level = TestLevel.PARTIAL_COMPLETE,
615        notes = "",
616        method = "poll",
617        args = {}
618    )
619    public void test_poll() {
620        PriorityQueue<String> stringQueue = new PriorityQueue<String>();
621        String[] array = { "MYTESTSTRING", "AAAAA", "BCDEF", "ksTRD", "AAAAA" };
622        for (int i = 0; i < array.length; i++) {
623            stringQueue.offer(array[i]);
624        }
625        Arrays.sort(array);
626        for (int i = 0; i < array.length; i++) {
627            assertEquals(array[i], stringQueue.poll());
628        }
629        assertEquals(0, stringQueue.size());
630        assertNull(stringQueue.poll());
631    }
632
633    /**
634     * @tests java.util.PriorityQueue#poll()
635     */
636    @TestTargetNew(
637        level = TestLevel.PARTIAL_COMPLETE,
638        notes = "Verifies poll method for empty queue.",
639        method = "poll",
640        args = {}
641    )
642    public void test_poll_empty() {
643        PriorityQueue<Object> queue = new PriorityQueue<Object>();
644        assertEquals(0, queue.size());
645        assertNull(queue.poll());
646    }
647
648    /**
649     * @tests java.util.PriorityQueue#peek()
650     */
651    @TestTargetNew(
652        level = TestLevel.COMPLETE,
653        notes = "",
654        method = "peek",
655        args = {}
656    )
657    public void test_peek() {
658        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
659        int[] array = { 2, 45, 7, -12, 9 };
660        for (int i = 0; i < array.length; i++) {
661            integerQueue.add(array[i]);
662        }
663        Arrays.sort(array);
664        assertEquals(new Integer(array[0]), integerQueue.peek());
665        assertEquals(new Integer(array[0]), integerQueue.peek());
666    }
667
668    /**
669     * @tests java.util.PriorityQueue#peek()
670     */
671    @TestTargetNew(
672        level = TestLevel.PARTIAL_COMPLETE,
673        notes = "Verifies peek method for empty queue.",
674        method = "peek",
675        args = {}
676    )
677    public void test_peek_empty() {
678        PriorityQueue<Object> queue = new PriorityQueue<Object>();
679        assertEquals(0, queue.size());
680        assertNull(queue.peek());
681        assertNull(queue.peek());
682    }
683
684    /**
685     * @tests java.util.PriorityQueue#Clear()
686     */
687    @TestTargetNew(
688        level = TestLevel.COMPLETE,
689        notes = "",
690        method = "clear",
691        args = {}
692    )
693    public void test_clear() {
694        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
695        int[] array = { 2, 45, 7, -12, 9 };
696        for (int i = 0; i < array.length; i++) {
697            integerQueue.offer(array[i]);
698        }
699        integerQueue.clear();
700        assertTrue(integerQueue.isEmpty());
701    }
702
703    /**
704     * @tests java.util.PriorityQueue#add(Object)
705     */
706    @TestTargetNew(
707        level = TestLevel.PARTIAL_COMPLETE,
708        notes = "Doesn't verify NullPointerException, ClassCastException.",
709        method = "add",
710        args = {java.lang.Object.class}
711    )
712    public void test_add_Ljava_lang_Object() {
713        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
714        Integer[] array = { 2, 45, 7, -12, 9 };
715        for (int i = 0; i < array.length; i++) {
716            integerQueue.add(array[i]);
717        }
718        Arrays.sort(array);
719        assertEquals(array.length, integerQueue.size());
720        for (int i = 0; i < array.length; i++) {
721            assertEquals(array[i], integerQueue.poll());
722        }
723        assertEquals(0, integerQueue.size());
724    }
725
726    /**
727     * @tests java.util.PriorityQueue#add(Object)
728     */
729    @TestTargetNew(
730        level = TestLevel.PARTIAL_COMPLETE,
731        notes = "Verifies NullPointerException.",
732        method = "add",
733        args = {java.lang.Object.class}
734    )
735    public void test_add_Ljava_lang_Object_null() {
736        PriorityQueue<Object> queue = new PriorityQueue<Object>();
737        try {
738            queue.add(null);
739            fail("should throw NullPointerException");
740        } catch (NullPointerException e) {
741            // expected
742        }
743    }
744
745    /**
746     * @tests java.util.PriorityQueue#add(Object)
747     */
748    @TestTargetNew(
749        level = TestLevel.PARTIAL_COMPLETE,
750        notes = "Verifies ClassCastException.",
751        method = "add",
752        args = {java.lang.Object.class}
753    )
754    public void test_add_Ljava_lang_Object_non_Comparable() {
755        PriorityQueue<Object> queue = new PriorityQueue<Object>();
756        queue.add(new Integer(10));
757        try {
758            queue.add(new Float(1.3));
759            fail("should throw ClassCastException");
760        } catch (ClassCastException e) {
761            // expected
762        }
763
764        queue = new PriorityQueue<Object>();
765        queue.add(new Integer(10));
766        try {
767            queue.add(new Object());
768            fail("should throw ClassCastException");
769        } catch (ClassCastException e) {
770            // expected
771        }
772    }
773
774    /**
775     * @tests java.util.PriorityQueue#remove(Object)
776     *
777     */
778    @TestTargetNew(
779        level = TestLevel.PARTIAL_COMPLETE,
780        notes = "",
781        method = "remove",
782        args = {java.lang.Object.class}
783    )
784    public void test_remove_Ljava_lang_Object() {
785        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
786        List<Integer> list = Arrays.asList(array);
787        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
788        assertTrue(integerQueue.remove(16));
789        Integer[] newArray = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 39 };
790        Arrays.sort(newArray);
791        for (int i = 0; i < newArray.length; i++) {
792            assertEquals(newArray[i], integerQueue.poll());
793        }
794        assertEquals(0, integerQueue.size());
795    }
796
797    /**
798     * @tests java.util.PriorityQueue#remove(Object)
799     *
800     */
801    @TestTargetNew(
802        level = TestLevel.PARTIAL_COMPLETE,
803        notes = "",
804        method = "remove",
805        args = {java.lang.Object.class}
806    )
807    public void test_remove_Ljava_lang_Object_using_comparator() {
808        PriorityQueue<String> queue = new PriorityQueue<String>(10,
809                new MockComparatorStringByLength());
810        String[] array = { "AAAAA", "AA", "AAAA", "AAAAAAAA" };
811        for (int i = 0; i < array.length; i++) {
812            queue.offer(array[i]);
813        }
814        assertFalse(queue.contains("BB"));
815        assertTrue(queue.remove("BB"));
816    }
817
818    /**
819     * @tests java.util.PriorityQueue#remove(Object)
820     *
821     */
822    @TestTargetNew(
823        level = TestLevel.PARTIAL_COMPLETE,
824        notes = "Verifies ClassCastException.",
825        method = "remove",
826        args = {java.lang.Object.class}
827    )
828    public void test_remove_Ljava_lang_Object_not_exists() {
829        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
830        List<Integer> list = Arrays.asList(array);
831        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
832        assertFalse(integerQueue.remove(111));
833        assertFalse(integerQueue.remove(null));
834        try {
835            integerQueue.remove("");
836            fail("should throw ClassCastException");
837        } catch (ClassCastException e) {
838            // expected
839        }
840    }
841
842    /**
843     * @tests java.util.PriorityQueue#remove(Object)
844     *
845     */
846    @TestTargetNew(
847        level = TestLevel.PARTIAL_COMPLETE,
848        notes = "Verifies null as a parameter.",
849        method = "remove",
850        args = {java.lang.Object.class}
851    )
852    public void test_remove_Ljava_lang_Object_null() {
853        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
854        List<Integer> list = Arrays.asList(array);
855        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
856        assertFalse(integerQueue.remove(null));
857    }
858
859    /**
860     * @tests java.util.PriorityQueue#remove(Object)
861     *
862     */
863    @TestTargetNew(
864        level = TestLevel.PARTIAL_COMPLETE,
865        notes = "Verifies ClassCastException.",
866        method = "remove",
867        args = {java.lang.Object.class}
868    )
869    public void test_remove_Ljava_lang_Object_not_Compatible() {
870        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
871        List<Integer> list = Arrays.asList(array);
872        PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
873        try {
874            integerQueue.remove(new Float(1.3F));
875            fail("should throw ClassCastException");
876        } catch (ClassCastException e) {
877            // expected
878        }
879
880        // although argument element type is not compatible with those in queue,
881        // but comparator supports it.
882        MockComparator<Object> comparator = new MockComparator<Object>();
883        PriorityQueue<Integer> integerQueue1 = new PriorityQueue<Integer>(100,
884                comparator);
885        integerQueue1.offer(1);
886        assertFalse(integerQueue1.remove(new Float(1.3F)));
887
888        PriorityQueue<Object> queue = new PriorityQueue<Object>();
889        Object o = new Object();
890        queue.offer(o);
891        try {
892            queue.remove(o);
893            fail("should throw ClassCastException");
894        } catch (ClassCastException e) {
895            // expected
896        }
897    }
898
899    /**
900     * @tests java.util.PriorityQueue#comparator()
901     */
902    @TestTargetNew(
903        level = TestLevel.COMPLETE,
904        notes = "",
905        method = "comparator",
906        args = {}
907    )
908    public void test_comparator() {
909        PriorityQueue<Object> queue = new PriorityQueue<Object>();
910        assertNull(queue.comparator());
911
912        MockComparator<Object> comparator = new MockComparator<Object>();
913        queue = new PriorityQueue<Object>(100, comparator);
914        assertEquals(comparator, queue.comparator());
915    }
916
917    /**
918     * @tests serialization/deserialization.
919     */
920    @TestTargetNew(
921        level = TestLevel.COMPLETE,
922        notes = "Verifies serialization/deserialization.",
923        method = "!SerializationSelf",
924        args = {}
925    )
926    public void test_Serialization() throws Exception {
927        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
928        List<Integer> list = Arrays.asList(array);
929        PriorityQueue<Integer> srcIntegerQueue = new PriorityQueue<Integer>(
930                list);
931        PriorityQueue<Integer> destIntegerQueue = (PriorityQueue<Integer>) SerializationTester
932                .getDeserilizedObject(srcIntegerQueue);
933        Arrays.sort(array);
934        for (int i = 0; i < array.length; i++) {
935            assertEquals(array[i], destIntegerQueue.poll());
936        }
937        assertEquals(0, destIntegerQueue.size());
938    }
939
940    /**
941     * @tests serialization/deserialization.
942     */
943    @TestTargetNew(
944        level = TestLevel.COMPLETE,
945        notes = "Verifies serialization/deserialization.",
946        method = "!SerializationSelf",
947        args = {}
948    )
949    public void test_Serialization_casting() throws Exception {
950        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
951        List<Integer> list = Arrays.asList(array);
952        PriorityQueue<Integer> srcIntegerQueue = new PriorityQueue<Integer>(
953                list);
954        PriorityQueue<String> destStringQueue = (PriorityQueue<String>) SerializationTester
955                .getDeserilizedObject(srcIntegerQueue);
956        // will not incur class cast exception.
957        Object o = destStringQueue.peek();
958        Arrays.sort(array);
959        Integer I = (Integer) o;
960        assertEquals(array[0], I);
961    }
962
963    /**
964     * @tests serialization/deserialization compatibility with RI.
965     */
966    @TestTargetNew(
967        level = TestLevel.COMPLETE,
968        notes = "Verifies serialization/deserialization compatibility.",
969        method = "!SerializationGolden",
970        args = {}
971    )
972    public void test_SerializationCompatibility_cast() throws Exception {
973        Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
974        List<Integer> list = Arrays.asList(array);
975        PriorityQueue<Integer> srcIntegerQueue = new PriorityQueue<Integer>(
976                list);
977        PriorityQueue<String> destStringQueue = (PriorityQueue<String>) SerializationTester
978                .readObject(srcIntegerQueue, SERIALIZATION_FILE_NAME);
979
980        // will not incur class cast exception.
981        Object o = destStringQueue.peek();
982        Arrays.sort(array);
983        Integer I = (Integer) o;
984        assertEquals(array[0], I);
985    }
986
987    private static class MockComparator<E> implements Comparator<E> {
988
989        public int compare(E object1, E object2) {
990            int hashcode1 = object1.hashCode();
991            int hashcode2 = object2.hashCode();
992            if (hashcode1 > hashcode2) {
993                return 1;
994            } else if (hashcode1 == hashcode2) {
995                return 0;
996            } else {
997                return -1;
998            }
999        }
1000    }
1001
1002    private static class MockComparatorStringByLength implements
1003            Comparator<String> {
1004
1005        public int compare(String object1, String object2) {
1006            int length1 = object1.length();
1007            int length2 = object2.length();
1008            if (length1 > length2) {
1009                return 1;
1010            } else if (length1 == length2) {
1011                return 0;
1012            } else {
1013                return -1;
1014            }
1015        }
1016
1017    }
1018
1019    private static class MockComparatorCast<E> implements Comparator<E> {
1020
1021        public int compare(E object1, E object2) {
1022            return 0;
1023        }
1024    }
1025
1026}
1027