• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package org.apache.harmony.luni.tests.java.util;
18 
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Comparator;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.NoSuchElementException;
26 import java.util.PriorityQueue;
27 import java.util.SortedSet;
28 import java.util.TreeSet;
29 
30 import junit.framework.TestCase;
31 import tests.util.SerializationTester;
32 
33 public class PriorityQueueTest extends TestCase {
34 
35     private static final String SERIALIZATION_FILE_NAME = "serialization/java/util/PriorityQueue.golden.ser"; //$NON-NLS-1$
36 
37     /**
38      * @tests java.util.PriorityQueue#iterator()
39      */
test_iterator()40     public void test_iterator() {
41         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
42         Integer[] array = { 2, 45, 7, -12, 9 };
43         for (int i = 0; i < array.length; i++) {
44             integerQueue.offer(array[i]);
45         }
46         Iterator<Integer> iter = integerQueue.iterator();
47         assertNotNull(iter);
48         ArrayList<Integer> iterResult = new ArrayList<Integer>();
49         while (iter.hasNext()) {
50             iterResult.add(iter.next());
51         }
52         Object[] resultArray = iterResult.toArray();
53         Arrays.sort(array);
54         Arrays.sort(resultArray);
55         assertTrue(Arrays.equals(array, resultArray));
56     }
57 
58     /**
59      * @tests java.util.PriorityQueue#iterator()
60      */
test_iterator_empty()61     public void test_iterator_empty() {
62         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
63         Iterator<Integer> iter = integerQueue.iterator();
64         try {
65             iter.next();
66             fail("should throw NoSuchElementException");
67         } catch (NoSuchElementException e) {
68             // expected
69         }
70 
71         iter = integerQueue.iterator();
72         try {
73             iter.remove();
74             fail("should throw IllegalStateException");
75         } catch (IllegalStateException e) {
76             // expected
77         }
78     }
79 
80     /**
81      * @tests java.util.PriorityQueue#iterator()
82      */
test_iterator_outofbound()83     public void test_iterator_outofbound() {
84         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
85         integerQueue.offer(0);
86         Iterator<Integer> iter = integerQueue.iterator();
87         iter.next();
88         try {
89             iter.next();
90             fail("should throw NoSuchElementException");
91         } catch (NoSuchElementException e) {
92             // expected
93         }
94 
95         iter = integerQueue.iterator();
96         iter.next();
97         iter.remove();
98         try {
99             iter.next();
100             fail("should throw NoSuchElementException");
101         } catch (NoSuchElementException e) {
102             // expected
103         }
104     }
105 
106     /**
107      * @tests java.util.PriorityQueue#iterator()
108      */
test_iterator_remove()109     public void test_iterator_remove() {
110         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
111         Integer[] array = { 2, 45, 7, -12, 9 };
112         for (int i = 0; i < array.length; i++) {
113             integerQueue.offer(array[i]);
114         }
115         Iterator<Integer> iter = integerQueue.iterator();
116         assertNotNull(iter);
117         for (int i = 0; i < array.length; i++) {
118             iter.next();
119             if (2 == i) {
120                 iter.remove();
121             }
122         }
123         assertEquals(array.length - 1, integerQueue.size());
124 
125         iter = integerQueue.iterator();
126         Integer[] newArray = new Integer[array.length - 1];
127         for (int i = 0; i < newArray.length; i++) {
128             newArray[i] = iter.next();
129         }
130 
131         Arrays.sort(newArray);
132         for (int i = 0; i < integerQueue.size(); i++) {
133             assertEquals(newArray[i], integerQueue.poll());
134         }
135     }
136 
test_iterator_removeEquals()137     public void test_iterator_removeEquals() {
138         PriorityQueue<String> integerQueue = new PriorityQueue<String>(10, new MockComparatorStringByLength());
139         String[] array = { "ONE", "TWO", "THREE", "FOUR", "FIVE" };
140         for (int i = 0; i < array.length; i++) {
141             integerQueue.offer(array[i]);
142         }
143         // Try removing an entry that the comparator says is equal
144         assertFalse(integerQueue.remove("123"));
145         assertFalse(integerQueue.remove("one"));
146         assertTrue(integerQueue.remove("THREE"));
147     }
148 
149     /**
150      * @tests java.util.PriorityQueue#iterator()
151      */
test_iterator_remove_illegalState()152     public void test_iterator_remove_illegalState() {
153         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
154         Integer[] array = { 2, 45, 7, -12, 9 };
155         for (int i = 0; i < array.length; i++) {
156             integerQueue.offer(array[i]);
157         }
158         Iterator<Integer> iter = integerQueue.iterator();
159         assertNotNull(iter);
160         try {
161             iter.remove();
162             fail("should throw IllegalStateException");
163         } catch (IllegalStateException e) {
164             // expected
165         }
166         iter.next();
167         iter.remove();
168         try {
169             iter.remove();
170             fail("should throw IllegalStateException");
171         } catch (IllegalStateException e) {
172             // expected
173         }
174 
175     }
176 
177     /**
178      * @tests java.util.PriorityQueue.size()
179      */
test_size()180     public void test_size() {
181         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
182         assertEquals(0, integerQueue.size());
183         int[] array = { 2, 45, 7, -12, 9 };
184         for (int i = 0; i < array.length; i++) {
185             integerQueue.offer(array[i]);
186         }
187         assertEquals(array.length, integerQueue.size());
188     }
189 
190     /**
191      * @tests java.util.PriorityQueue#PriorityQueue()
192      */
test_Constructor()193     public void test_Constructor() {
194         PriorityQueue<Object> queue = new PriorityQueue<Object>();
195         assertNotNull(queue);
196         assertEquals(0, queue.size());
197         assertNull(queue.comparator());
198     }
199 
200     /**
201      * @tests java.util.PriorityQueue#PriorityQueue(int)
202      */
test_ConstructorI()203     public void test_ConstructorI() {
204         PriorityQueue<Object> queue = new PriorityQueue<Object>(100);
205         assertNotNull(queue);
206         assertEquals(0, queue.size());
207         assertNull(queue.comparator());
208     }
209 
210     /**
211      * @tests java.util.PriorityQueue#PriorityQueue(int, Comparator<? super E>)
212      */
test_ConstructorILjava_util_Comparator()213     public void test_ConstructorILjava_util_Comparator() {
214         PriorityQueue<Object> queue = new PriorityQueue<Object>(100,
215                 (Comparator<Object>) null);
216         assertNotNull(queue);
217         assertEquals(0, queue.size());
218         assertNull(queue.comparator());
219 
220         MockComparator<Object> comparator = new MockComparator<Object>();
221         queue = new PriorityQueue<Object>(100, comparator);
222         assertNotNull(queue);
223         assertEquals(0, queue.size());
224         assertEquals(comparator, queue.comparator());
225     }
226 
227     /**
228      * @tests java.util.PriorityQueue#PriorityQueue(int, Comparator<? super E>)
229      */
test_ConstructorILjava_util_Comparator_illegalCapacity()230     public void test_ConstructorILjava_util_Comparator_illegalCapacity() {
231         try {
232             new PriorityQueue<Object>(0, new MockComparator<Object>());
233             fail("should throw IllegalArgumentException");
234         } catch (IllegalArgumentException e) {
235             // expected
236         }
237 
238         try {
239             new PriorityQueue<Object>(-1, new MockComparator<Object>());
240             fail("should throw IllegalArgumentException");
241         } catch (IllegalArgumentException e) {
242             // expected
243         }
244     }
245 
246     /**
247      * @tests java.util.PriorityQueue#PriorityQueue(int, Comparator<? super E>)
248      */
test_ConstructorILjava_util_Comparator_cast()249     public void test_ConstructorILjava_util_Comparator_cast() {
250         MockComparatorCast<Object> objectComparator = new MockComparatorCast<Object>();
251         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(100,
252                 objectComparator);
253         assertNotNull(integerQueue);
254         assertEquals(0, integerQueue.size());
255         assertEquals(objectComparator, integerQueue.comparator());
256         Integer[] array = { 2, 45, 7, -12, 9 };
257         List<Integer> list = Arrays.asList(array);
258         integerQueue.addAll(list);
259         assertEquals(list.size(), integerQueue.size());
260         // just test here no cast exception raises.
261     }
262 
263     /**
264      * @tests java.util.PriorityQueue#PriorityQueue(Collection)
265      */
test_ConstructorLjava_util_Colleciton()266     public void test_ConstructorLjava_util_Colleciton() {
267         Integer[] array = { 2, 45, 7, -12, 9 };
268         List<Integer> list = Arrays.asList(array);
269         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
270         assertEquals(array.length, integerQueue.size());
271         assertNull(integerQueue.comparator());
272         Arrays.sort(array);
273         for (int i = 0; i < array.length; i++) {
274             assertEquals(array[i], integerQueue.poll());
275         }
276     }
277 
278     /**
279      * @tests java.util.PriorityQueue#PriorityQueue(Collection)
280      */
test_ConstructorLjava_util_Colleciton_null()281     public void test_ConstructorLjava_util_Colleciton_null() {
282         ArrayList<Object> list = new ArrayList<Object>();
283         list.add(new Float(11));
284         list.add(null);
285         list.add(new Integer(10));
286         try {
287             new PriorityQueue<Object>(list);
288             fail("should throw NullPointerException");
289         } catch (NullPointerException e) {
290             // expected
291         }
292     }
293 
294     /**
295      * @tests java.util.PriorityQueue#PriorityQueue(Collection)
296      */
test_ConstructorLjava_util_Colleciton_non_comparable()297     public void test_ConstructorLjava_util_Colleciton_non_comparable() {
298         ArrayList<Object> list = new ArrayList<Object>();
299         list.add(new Float(11));
300         list.add(new Integer(10));
301         try {
302             new PriorityQueue<Object>(list);
303             fail("should throw ClassCastException");
304         } catch (ClassCastException e) {
305             // expected
306         }
307     }
308 
309     /**
310      * @tests java.util.PriorityQueue#PriorityQueue(Collection)
311      */
test_ConstructorLjava_util_Colleciton_from_priorityqueue()312     public void test_ConstructorLjava_util_Colleciton_from_priorityqueue() {
313         String[] array = { "AAAAA", "AA", "AAAA", "AAAAAAAA" };
314         PriorityQueue<String> queue = new PriorityQueue<String>(4,
315                 new MockComparatorStringByLength());
316         for (int i = 0; i < array.length; i++) {
317             queue.offer(array[i]);
318         }
319         Collection<String> c = queue;
320         PriorityQueue<String> constructedQueue = new PriorityQueue<String>(c);
321         assertEquals(queue.comparator(), constructedQueue.comparator());
322         while (queue.size() > 0) {
323             assertEquals(queue.poll(), constructedQueue.poll());
324         }
325         assertEquals(0, constructedQueue.size());
326     }
327 
328     /**
329      * @tests java.util.PriorityQueue#PriorityQueue(Collection)
330      */
test_ConstructorLjava_util_Colleciton_from_sortedset()331     public void test_ConstructorLjava_util_Colleciton_from_sortedset() {
332         int[] array = { 3, 5, 79, -17, 5 };
333         TreeSet<Integer> treeSet = new TreeSet<Integer>(new MockComparator<Integer>());
334         for (int i = 0; i < array.length; i++) {
335             treeSet.add(array[i]);
336         }
337         Collection<? extends Integer> c = treeSet;
338         PriorityQueue<Integer> queue = new PriorityQueue<Integer>(c);
339         assertEquals(treeSet.comparator(), queue.comparator());
340         Iterator<Integer> iter = treeSet.iterator();
341         while (iter.hasNext()) {
342             assertEquals(iter.next(), queue.poll());
343         }
344         assertEquals(0, queue.size());
345     }
346 
347     /**
348      * @tests java.util.PriorityQueue#PriorityQueue(PriorityQueue<? * extends
349      *        E>)
350      */
test_ConstructorLjava_util_PriorityQueue()351     public void test_ConstructorLjava_util_PriorityQueue() {
352         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
353         int[] array = { 2, 45, 7, -12, 9 };
354         for (int i = 0; i < array.length; i++) {
355             integerQueue.offer(array[i]);
356         }
357         PriorityQueue<Object> objectQueue = new PriorityQueue<Object>(
358                 integerQueue);
359         assertEquals(integerQueue.size(), objectQueue.size());
360         assertEquals(integerQueue.comparator(), objectQueue.comparator());
361         Arrays.sort(array);
362         for (int i = 0; i < array.length; i++) {
363             assertEquals(array[i], objectQueue.poll());
364         }
365     }
366 
367     /**
368      * @tests java.util.PriorityQueue#PriorityQueue(PriorityQueue<? * extends
369      *        E>)
370      */
test_ConstructorLjava_util_PriorityQueue_null()371     public void test_ConstructorLjava_util_PriorityQueue_null() {
372         try {
373             new PriorityQueue<Object>((PriorityQueue<Integer>) null);
374             fail("should throw NullPointerException");
375         } catch (NullPointerException e) {
376             // expected
377         }
378     }
379 
380     /**
381      * @tests java.util.PriorityQueue#PriorityQueue(SortedSet<? extends E>)
382      */
test_ConstructorLjava_util_SortedSet()383     public void test_ConstructorLjava_util_SortedSet() {
384         int[] array = { 3, 5, 79, -17, 5 };
385         TreeSet<Integer> treeSet = new TreeSet<Integer>();
386         for (int i = 0; i < array.length; i++) {
387             treeSet.add(array[i]);
388         }
389         PriorityQueue<Integer> queue = new PriorityQueue<Integer>(treeSet);
390         Iterator<Integer> iter = treeSet.iterator();
391         while (iter.hasNext()) {
392             assertEquals(iter.next(), queue.poll());
393         }
394     }
395 
396     /**
397      * @tests java.util.PriorityQueue#PriorityQueue(SortedSet<? extends E>)
398      */
test_ConstructorLjava_util_SortedSet_null()399     public void test_ConstructorLjava_util_SortedSet_null() {
400         try {
401             new PriorityQueue<Integer>((SortedSet<? extends Integer>) null);
402             fail("should throw NullPointerException");
403         } catch (NullPointerException e) {
404             // expected
405         }
406     }
407 
408     /**
409      * @tests java.util.PriorityQueue#offer(Object)
410      */
test_offerLjava_lang_Object()411     public void test_offerLjava_lang_Object() {
412         PriorityQueue<String> queue = new PriorityQueue<String>(10,
413                 new MockComparatorStringByLength());
414         String[] array = { "AAAAA", "AA", "AAAA", "AAAAAAAA" };
415         for (int i = 0; i < array.length; i++) {
416             queue.offer(array[i]);
417         }
418         String[] sortedArray = { "AA", "AAAA", "AAAAA", "AAAAAAAA" };
419         for (int i = 0; i < sortedArray.length; i++) {
420             assertEquals(sortedArray[i], queue.poll());
421         }
422         assertEquals(0, queue.size());
423         assertNull(queue.poll());
424     }
425 
426     /**
427      * @tests java.util.PriorityQueue#offer(Object)
428      */
test_offerLjava_lang_Object_null()429     public void test_offerLjava_lang_Object_null() {
430         PriorityQueue<Object> queue = new PriorityQueue<Object>();
431         try {
432             queue.offer(null);
433             fail("should throw NullPointerException");
434         } catch (NullPointerException e) {
435             // expected
436         }
437     }
438 
439     /**
440      * @tests java.util.PriorityQueue#offer(Object)
441      */
test_offer_Ljava_lang_Object_non_Comparable()442     public void test_offer_Ljava_lang_Object_non_Comparable() {
443         PriorityQueue<Object> queue = new PriorityQueue<Object>();
444         queue.offer(new Integer(10));
445         try {
446             queue.offer(new Float(1.3));
447             fail("should throw ClassCastException");
448         } catch (ClassCastException e) {
449             // expected
450         }
451 
452         queue = new PriorityQueue<Object>();
453         queue.offer(new Integer(10));
454         try {
455             queue.offer(new Object());
456             fail("should throw ClassCastException");
457         } catch (ClassCastException e) {
458             // expected
459         }
460     }
461 
462     /**
463      * @tests java.util.PriorityQueue#poll()
464      */
test_poll()465     public void test_poll() {
466         PriorityQueue<String> stringQueue = new PriorityQueue<String>();
467         String[] array = { "MYTESTSTRING", "AAAAA", "BCDEF", "ksTRD", "AAAAA" };
468         for (int i = 0; i < array.length; i++) {
469             stringQueue.offer(array[i]);
470         }
471         Arrays.sort(array);
472         for (int i = 0; i < array.length; i++) {
473             assertEquals(array[i], stringQueue.poll());
474         }
475         assertEquals(0, stringQueue.size());
476         assertNull(stringQueue.poll());
477     }
478 
479     /**
480      * @tests java.util.PriorityQueue#poll()
481      */
test_poll_empty()482     public void test_poll_empty() {
483         PriorityQueue<Object> queue = new PriorityQueue<Object>();
484         assertEquals(0, queue.size());
485         assertNull(queue.poll());
486     }
487 
488     /**
489      * @tests java.util.PriorityQueue#peek()
490      */
test_peek()491     public void test_peek() {
492         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
493         int[] array = { 2, 45, 7, -12, 9 };
494         for (int i = 0; i < array.length; i++) {
495             integerQueue.add(array[i]);
496         }
497         Arrays.sort(array);
498         assertEquals(new Integer(array[0]), integerQueue.peek());
499         assertEquals(new Integer(array[0]), integerQueue.peek());
500     }
501 
502     /**
503      * @tests java.util.PriorityQueue#peek()
504      */
test_peek_empty()505     public void test_peek_empty() {
506         PriorityQueue<Object> queue = new PriorityQueue<Object>();
507         assertEquals(0, queue.size());
508         assertNull(queue.peek());
509         assertNull(queue.peek());
510     }
511 
512     /**
513      * @tests java.util.PriorityQueue#Clear()
514      */
test_clear()515     public void test_clear() {
516         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
517         int[] array = { 2, 45, 7, -12, 9 };
518         for (int i = 0; i < array.length; i++) {
519             integerQueue.offer(array[i]);
520         }
521         integerQueue.clear();
522         assertTrue(integerQueue.isEmpty());
523     }
524 
525     /**
526      * @tests java.util.PriorityQueue#add(Object)
527      */
test_add_Ljava_lang_Object()528     public void test_add_Ljava_lang_Object() {
529         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
530         Integer[] array = { 2, 45, 7, -12, 9 };
531         for (int i = 0; i < array.length; i++) {
532             integerQueue.add(array[i]);
533         }
534         Arrays.sort(array);
535         assertEquals(array.length, integerQueue.size());
536         for (int i = 0; i < array.length; i++) {
537             assertEquals(array[i], integerQueue.poll());
538         }
539         assertEquals(0, integerQueue.size());
540     }
541 
542     /**
543      * @tests java.util.PriorityQueue#add(Object)
544      */
test_add_Ljava_lang_Object_null()545     public void test_add_Ljava_lang_Object_null() {
546         PriorityQueue<Object> queue = new PriorityQueue<Object>();
547         try {
548             queue.add(null);
549             fail("should throw NullPointerException");
550         } catch (NullPointerException e) {
551             // expected
552         }
553     }
554 
555     /**
556      * @tests java.util.PriorityQueue#add(Object)
557      */
test_add_Ljava_lang_Object_non_Comparable()558     public void test_add_Ljava_lang_Object_non_Comparable() {
559         PriorityQueue<Object> queue = new PriorityQueue<Object>();
560         queue.add(new Integer(10));
561         try {
562             queue.add(new Float(1.3));
563             fail("should throw ClassCastException");
564         } catch (ClassCastException e) {
565             // expected
566         }
567 
568         queue = new PriorityQueue<Object>();
569         queue.add(new Integer(10));
570         try {
571             queue.add(new Object());
572             fail("should throw ClassCastException");
573         } catch (ClassCastException e) {
574             // expected
575         }
576     }
577 
578     /**
579      * @tests java.util.PriorityQueue#remove(Object)
580      *
581      */
test_remove_Ljava_lang_Object()582     public void test_remove_Ljava_lang_Object() {
583         Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
584         List<Integer> list = Arrays.asList(array);
585         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
586         assertTrue(integerQueue.remove(16));
587         Integer[] newArray = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 39 };
588         Arrays.sort(newArray);
589         for (int i = 0; i < newArray.length; i++) {
590             assertEquals(newArray[i], integerQueue.poll());
591         }
592         assertEquals(0, integerQueue.size());
593     }
594 
595     /**
596      * @tests java.util.PriorityQueue#remove(Object)
597      *
598      */
test_remove_Ljava_lang_Object_using_comparator()599     public void test_remove_Ljava_lang_Object_using_comparator() {
600         PriorityQueue<String> queue = new PriorityQueue<String>(10,
601                 new MockComparatorStringByLength());
602         String[] array = { "AAAAA", "AA", "AAAA", "AAAAAAAA" };
603         for (int i = 0; i < array.length; i++) {
604             queue.offer(array[i]);
605         }
606         assertFalse(queue.contains("BB"));
607         assertTrue(queue.remove("AA"));
608     }
609 
610     /**
611      * @tests java.util.PriorityQueue#remove(Object)
612      *
613      */
test_remove_Ljava_lang_Object_not_exists()614     public void test_remove_Ljava_lang_Object_not_exists() {
615         Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
616         List<Integer> list = Arrays.asList(array);
617         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
618         assertFalse(integerQueue.remove(111));
619         assertFalse(integerQueue.remove(null));
620         assertFalse(integerQueue.remove(""));
621     }
622 
623     /**
624      * @tests java.util.PriorityQueue#remove(Object)
625      *
626      */
test_remove_Ljava_lang_Object_null()627     public void test_remove_Ljava_lang_Object_null() {
628         Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
629         List<Integer> list = Arrays.asList(array);
630         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
631         assertFalse(integerQueue.remove(null));
632     }
633 
634     /**
635      * @tests java.util.PriorityQueue#remove(Object)
636      *
637      */
test_remove_Ljava_lang_Object_not_Compatible()638     public void test_remove_Ljava_lang_Object_not_Compatible() {
639         Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
640         List<Integer> list = Arrays.asList(array);
641         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>(list);
642         assertFalse(integerQueue.remove(new Float(1.3F)));
643 
644         // although argument element type is not compatible with those in queue,
645         // but comparator supports it.
646         MockComparator<Object> comparator = new MockComparator<Object>();
647         PriorityQueue<Integer> integerQueue1 = new PriorityQueue<Integer>(100,
648                 comparator);
649         integerQueue1.offer(1);
650         assertFalse(integerQueue1.remove(new Float(1.3F)));
651 
652         PriorityQueue<Object> queue = new PriorityQueue<Object>();
653         Object o = new Object();
654         queue.offer(o);
655         assertTrue(queue.remove(o));
656     }
657 
658     /**
659      * @tests java.util.PriorityQueue#comparator()
660      */
test_comparator()661     public void test_comparator() {
662         PriorityQueue<Object> queue = new PriorityQueue<Object>();
663         assertNull(queue.comparator());
664 
665         MockComparator<Object> comparator = new MockComparator<Object>();
666         queue = new PriorityQueue<Object>(100, comparator);
667         assertEquals(comparator, queue.comparator());
668     }
669 
670     /**
671      * @tests serialization/deserialization.
672      */
test_Serialization()673     public void test_Serialization() throws Exception {
674         Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
675         List<Integer> list = Arrays.asList(array);
676         PriorityQueue<Integer> srcIntegerQueue = new PriorityQueue<Integer>(
677                 list);
678         PriorityQueue<Integer> destIntegerQueue = (PriorityQueue<Integer>) SerializationTester
679                 .getDeserilizedObject(srcIntegerQueue);
680         Arrays.sort(array);
681         for (int i = 0; i < array.length; i++) {
682             assertEquals(array[i], destIntegerQueue.poll());
683         }
684         assertEquals(0, destIntegerQueue.size());
685     }
686 
687     /**
688      * @tests serialization/deserialization.
689      */
test_Serialization_casting()690     public void test_Serialization_casting() throws Exception {
691         Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
692         List<Integer> list = Arrays.asList(array);
693         PriorityQueue<Integer> srcIntegerQueue = new PriorityQueue<Integer>(
694                 list);
695         PriorityQueue<String> destStringQueue = (PriorityQueue<String>) SerializationTester
696                 .getDeserilizedObject(srcIntegerQueue);
697         // will not incur class cast exception.
698         Object o = destStringQueue.peek();
699         Arrays.sort(array);
700         Integer I = (Integer) o;
701         assertEquals(array[0], I);
702     }
703 
704     /**
705      * @tests serialization/deserialization compatibility with RI.
706      */
test_SerializationCompatibility_cast()707     public void test_SerializationCompatibility_cast() throws Exception {
708         Integer[] array = { 2, 45, 7, -12, 9, 23, 17, 1118, 10, 16, 39 };
709         List<Integer> list = Arrays.asList(array);
710         PriorityQueue<Integer> srcIntegerQueue = new PriorityQueue<Integer>(
711                 list);
712         PriorityQueue<String> destStringQueue = (PriorityQueue<String>) SerializationTester
713                 .readObject(srcIntegerQueue, SERIALIZATION_FILE_NAME);
714 
715         // will not incur class cast exception.
716         Object o = destStringQueue.peek();
717         Arrays.sort(array);
718         Integer I = (Integer) o;
719         assertEquals(array[0], I);
720     }
721 
722     /**
723      * @tests {@link PriorityQueue#contains(Object)}
724      */
test_contains()725     public void test_contains() throws Exception {
726         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
727         Integer[] array = { 2, 45, 7, -12, 9 };
728         for (int i = 0; i < array.length; i++) {
729             integerQueue.add(array[i]);
730         }
731         for (int i = 0; i < array.length; i++) {
732             assertTrue(integerQueue.contains(array[i]));
733         }
734         assertFalse(integerQueue.contains(null));
735     }
736 
737     /**
738      * @tests {@link PriorityQueue#toArray()}
739      */
test_toArray()740     public void test_toArray() throws Exception {
741         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
742         Integer[] array = { 2, 45, 7, -12, 9 };
743         for (int i = 0; i < array.length; i++) {
744             integerQueue.add(array[i]);
745         }
746         Object[] returnArray = integerQueue.toArray();
747         assertEquals(returnArray.length,integerQueue.size());
748         for (int i = 0; i < returnArray.length; i++) {
749             assertTrue(integerQueue.contains(returnArray[i]));
750         }
751     }
752 
753     /**
754      * @tests {@link PriorityQueue#toArray(T[])}
755      */
test_toArray_$T()756     public void test_toArray_$T() throws Exception {
757         PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
758         Integer[] array = { 2, 45, 7, -12, 9 };
759         for (int i = 0; i < array.length; i++) {
760             integerQueue.add(array[i]);
761         }
762         Object[] returnArray = integerQueue.toArray(new Integer[0]);
763         assertEquals(returnArray.length,integerQueue.size());
764         for (int i = 0; i < returnArray.length; i++) {
765             assertTrue(integerQueue.contains(returnArray[i]));
766         }
767         returnArray = integerQueue.toArray(new Integer[10]);
768         assertEquals(10,returnArray.length);
769         for (int i = 0; i < array.length; i++) {
770             assertTrue(integerQueue.contains(returnArray[i]));
771         }
772         for (int i = array.length; i < 10; i++) {
773             assertNull(returnArray[i]);
774         }
775         try {
776             integerQueue.toArray(null);
777             fail("should throw NullPointerException");
778         } catch (NullPointerException e) {
779             // expected
780         }
781         try {
782             integerQueue.toArray(new String[1]);
783             fail("should throw ArrayStoreException");
784         } catch (ArrayStoreException e) {
785             // expected
786         }
787     }
788 
789     private static class MockComparator<E> implements Comparator<E> {
790 
compare(E object1, E object2)791         public int compare(E object1, E object2) {
792             int hashcode1 = object1.hashCode();
793             int hashcode2 = object2.hashCode();
794             if (hashcode1 > hashcode2) {
795                 return 1;
796             } else if (hashcode1 == hashcode2) {
797                 return 0;
798             } else {
799                 return -1;
800             }
801         }
802     }
803 
804     private static class MockComparatorStringByLength implements
805             Comparator<String> {
806 
compare(String object1, String object2)807         public int compare(String object1, String object2) {
808             int length1 = object1.length();
809             int length2 = object2.length();
810             if (length1 > length2) {
811                 return 1;
812             } else if (length1 == length2) {
813                 return 0;
814             } else {
815                 return -1;
816             }
817         }
818 
819     }
820 
821     private static class MockComparatorCast<E> implements Comparator<E> {
822 
compare(E object1, E object2)823         public int compare(E object1, E object2) {
824             return 0;
825         }
826     }
827 
828 }
829