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