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