1 16 package org.apache.commons.collections; 17 18 import java.util.Collection ; 19 import java.util.ConcurrentModificationException ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.Map ; 25 import java.util.NoSuchElementException ; 26 import java.util.Set ; 27 28 import junit.framework.Test; 29 30 import org.apache.commons.collections.map.AbstractTestMap; 31 32 45 public class TestDoubleOrderedMap extends AbstractTestMap { 46 47 52 public TestDoubleOrderedMap(final String name) { 53 super(name); 54 } 55 56 61 public static Test suite() { 62 return BulkTest.makeSuite(TestDoubleOrderedMap.class); 63 } 64 65 68 public boolean isAllowNullKey() { 69 return false; 70 } 71 72 76 public boolean isAllowNullValue() { 77 return false; 78 } 79 80 83 public boolean isAllowDuplicateValues() { 84 return false; 85 } 86 87 95 public boolean isPutChangeSupported() { 96 return false; 97 } 98 99 102 public boolean isSetValueSupported() { 103 return false; 104 } 105 106 public Map makeEmptyMap() { 107 return new DoubleOrderedMap(); 108 } 109 110 protected Map makeMap() { 111 return new DoubleOrderedMap(); 112 } 113 114 117 public void testSize() { 118 119 Map m = makeMap(); 120 121 assertEquals(0, m.size()); 122 123 LocalTestNode nodes[] = makeLocalNodes(); 124 125 for (int k = 0; k < nodes.length; k++) { 126 m.put(nodes[k].getKey(), nodes[k].getValue()); 127 assertEquals(k + 1, m.size()); 128 } 129 130 int count = m.size(); 131 132 for (int k = 0; k < nodes.length; k++) { 133 m.remove(nodes[k].getKey()); 134 135 --count; 136 137 assertEquals(count, m.size()); 138 139 m.remove(nodes[k].getKey()); 141 assertEquals(count, m.size()); 142 } 143 } 144 145 148 public void testIsEmpty() { 149 150 Map m = makeMap(); 151 152 assertTrue(m.isEmpty()); 153 154 LocalTestNode nodes[] = makeLocalNodes(); 155 156 for (int k = 0; k < nodes.length; k++) { 157 m.put(nodes[k].getKey(), nodes[k].getValue()); 158 assertTrue(!m.isEmpty()); 159 } 160 161 int count = m.size(); 162 163 for (int k = 0; k < nodes.length; k++) { 164 m.remove(nodes[k].getKey()); 165 166 --count; 167 168 if (count == 0) { 169 assertTrue(m.isEmpty()); 170 } else { 171 assertTrue(!m.isEmpty()); 172 } 173 174 m.remove(nodes[k].getKey()); 176 177 if (count == 0) { 178 assertTrue(m.isEmpty()); 179 } else { 180 assertTrue(!m.isEmpty()); 181 } 182 } 183 } 184 185 188 public void testContainsKey() { 189 190 Map m = makeMap(); 191 192 try { 193 m.containsKey(new Object ()); 194 fail("should have caught ClassCastException"); 195 } catch (ClassCastException ignored) {} 196 197 try { 198 m.containsKey(null); 199 fail("should have caught NullPointerException"); 200 } catch (NullPointerException ignored) {} 201 202 assertTrue(!m.containsKey("foo")); 203 204 LocalTestNode nodes[] = makeLocalNodes(); 205 206 for (int k = 0; k < nodes.length; k++) { 207 m.put(nodes[k].getKey(), nodes[k]); 208 assertTrue(m.containsKey(nodes[k].getKey())); 209 } 210 211 assertTrue(!m.containsKey(new Integer (-1))); 212 213 try { 214 m.containsKey("foo"); 215 fail("Should have caught ClassCastException"); 216 } catch (ClassCastException ignored) {} 217 218 for (int k = 0; k < nodes.length; k++) { 219 m.remove(nodes[k].getKey()); 220 assertTrue(!m.containsKey(nodes[k].getKey())); 221 } 222 } 223 224 227 public void testContainsValue() { 228 229 Map m = (DoubleOrderedMap) makeMap(); 230 LocalTestNode nodes[] = makeLocalNodes(); 231 232 for (int k = 0; k < nodes.length; k++) { 233 m.put(nodes[k].getKey(), nodes[k]); 234 assertTrue(m.containsValue(nodes[k])); 235 } 236 237 for (int k = 0; k < nodes.length; k++) { 238 m.remove(nodes[k].getKey()); 239 assertTrue(!m.containsValue(nodes[k])); 240 } 241 } 242 243 246 public void testGet() { 247 248 Map m = makeMap(); 249 250 try { 251 m.get(new Object ()); 252 fail("should have caught ClassCastException"); 253 } catch (ClassCastException ignored) {} 254 255 try { 256 m.get(null); 257 fail("should have caught NullPointerException"); 258 } catch (NullPointerException ignored) {} 259 260 assertNull(m.get("foo")); 261 262 LocalTestNode nodes[] = makeLocalNodes(); 263 264 for (int k = 0; k < nodes.length; k++) { 265 m.put(nodes[k].getKey(), nodes[k]); 266 assertSame(m.get(nodes[k].getKey()), nodes[k]); 267 } 268 269 assertNull(m.get(new Integer (-1))); 270 271 try { 272 m.get("foo"); 273 fail("Should have caught ClassCastException"); 274 } catch (ClassCastException ignored) {} 275 276 for (int k = 0; k < nodes.length; k++) { 277 assertNotNull(m.get(nodes[k].getKey())); 278 m.remove(nodes[k].getKey()); 279 assertNull(m.get(nodes[k].getKey())); 280 } 281 } 282 283 286 public void testPut() { 287 288 Map m = makeMap(); 289 290 try { 291 m.put(new Object (), "foo"); 292 fail("should have caught ClassCastException"); 293 } catch (ClassCastException ignored) {} 294 295 try { 296 m.put(null, "foo"); 297 fail("should have caught NullPointerException"); 298 } catch (NullPointerException ignored) {} 299 300 try { 301 m.put("foo", null); 302 fail("should have caught NullPointerException"); 303 } catch (NullPointerException ignored) {} 304 305 try { 306 m.put("foo", new Object ()); 307 fail("should have caught ClassCastException"); 308 } catch (ClassCastException ignored) {} 309 310 LocalTestNode[] nodes = makeLocalNodes(); 311 312 for (int k = 0; k < nodes.length; k++) { 313 assertNull(m.put(nodes[k].getKey(), nodes[k].getValue())); 314 315 try { 316 m.put(nodes[k].getKey(), "foo"); 317 } catch (IllegalArgumentException ignored) {} 318 } 319 } 320 321 324 public void testRemove() { 325 326 DoubleOrderedMap m = (DoubleOrderedMap) makeMap(); 327 LocalTestNode nodes[] = makeLocalNodes(); 328 329 for (int k = 0; k < nodes.length; k++) { 330 m.put(nodes[k].getKey(), nodes[k]); 331 } 332 333 try { 334 m.remove(null); 335 fail("should have caught NullPointerException"); 336 } catch (NullPointerException ignored) {} 337 338 try { 339 m.remove(new Object ()); 340 fail("should have caught ClassCastException"); 341 } catch (ClassCastException ignored) {} 342 343 assertNull(m.remove(new Integer (-1))); 344 345 try { 346 m.remove("foo"); 347 fail("should have caught ClassCastException"); 348 } catch (ClassCastException ignored) {} 349 350 for (int k = 0; k < nodes.length; k += 2) { 351 Comparable key = nodes[k].getKey(); 352 353 assertNotNull(m.get(key)); 354 assertSame(nodes[k], m.remove(key)); 355 assertNull(m.remove(key)); 356 assertNull(m.get(key)); 357 } 358 359 for (int k = 1; k < nodes.length; k += 2) { 360 Comparable key = nodes[k].getKey(); 361 362 assertNotNull(m.get(key)); 363 assertSame(nodes[k], m.remove(key)); 364 assertNull(m.remove(key)); 365 assertNull(m.get(key)); 366 } 367 368 assertTrue(m.isEmpty()); 369 } 370 371 374 public void testPutAll() { 375 376 Map m = (DoubleOrderedMap) makeMap(); 377 LocalTestNode nodes[] = makeLocalNodes(); 378 379 for (int k = 0; k < nodes.length; k++) { 380 m.put(nodes[k].getKey(), nodes[k]); 381 } 382 383 Map m1 = new HashMap (); 384 385 m1.put(null, "foo"); 386 387 try { 388 m.putAll(m1); 389 fail("Should have caught NullPointerException"); 390 } catch (NullPointerException ignored) {} 391 392 m1 = new HashMap (); 393 394 m1.put(new Object (), "bar"); 395 396 try { 397 m.putAll(m1); 398 fail("Should have caught ClassCastException"); 399 } catch (ClassCastException ignored) {} 400 401 m1 = new HashMap (); 402 403 m1.put("fubar", null); 404 405 try { 406 m.putAll(m1); 407 fail("Should have caught NullPointerException"); 408 } catch (NullPointerException ignored) {} 409 410 m1 = new HashMap (); 411 412 m1.put("fubar", new Object ()); 413 414 try { 415 m.putAll(m1); 416 fail("Should have caught ClassCastException"); 417 } catch (ClassCastException ignored) {} 418 419 assertEquals(nodes.length, m.size()); 420 421 m = (DoubleOrderedMap) makeMap(); 422 m1 = new HashMap (); 423 424 for (int k = 0; k < nodes.length; k++) { 425 m1.put(nodes[k].getKey(), nodes[k].getValue()); 426 } 427 428 m.putAll(m1); 429 assertEquals(nodes.length, m.size()); 430 431 for (int k = 0; k < nodes.length; k++) { 432 assertSame(nodes[k].getValue(), m.get(nodes[k].getKey())); 433 } 434 } 435 436 439 public void testClear() { 440 441 Map m = (DoubleOrderedMap) makeMap(); 442 LocalTestNode nodes[] = makeLocalNodes(); 443 444 for (int k = 0; k < nodes.length; k++) { 445 m.put(nodes[k].getKey(), nodes[k].getValue()); 446 assertTrue(!m.isEmpty()); 447 } 448 449 assertTrue(!m.isEmpty()); 450 451 for (int k = 0; k < nodes.length; k++) { 452 assertTrue(m.containsKey(nodes[k].getKey())); 453 assertTrue(m.containsValue(nodes[k].getValue())); 454 } 455 456 m.clear(); 457 assertTrue(m.isEmpty()); 458 459 for (int k = 0; k < nodes.length; k++) { 460 assertTrue(!m.containsKey(nodes[k].getKey())); 461 assertTrue(!m.containsValue(nodes[k].getValue())); 462 } 463 } 464 465 468 public void testKeySet() { 469 470 testKeySet((DoubleOrderedMap) makeMap()); 471 472 Map m = (DoubleOrderedMap) makeMap(); 473 LocalTestNode nodes[] = makeLocalNodes(); 474 475 for (int k = 0; k < nodes.length; k++) { 476 m.put(nodes[k].getKey(), nodes[k]); 477 } 478 479 testKeySet(m); 480 481 m = (DoubleOrderedMap) makeMap(); 482 483 for (int k = 0; k < nodes.length; k++) { 484 m.put(nodes[k].getKey(), nodes[k]); 485 } 486 487 int count = m.size(); 488 489 for (Iterator iter = m.keySet().iterator(); iter.hasNext(); ) { 490 iter.next(); 491 iter.remove(); 492 493 --count; 494 495 assertEquals(count, m.size()); 496 } 497 498 assertTrue(m.isEmpty()); 499 500 m = (DoubleOrderedMap) makeMap(); 501 502 for (int k = 0; k < nodes.length; k++) { 503 m.put(nodes[k].getKey(), nodes[k]); 504 } 505 506 Set s = m.keySet(); 507 508 try { 509 s.remove(null); 510 fail("should have caught NullPointerException"); 511 } catch (NullPointerException ignored) {} 512 513 try { 514 s.remove(new Object ()); 515 fail("should have caught ClassCastException"); 516 } catch (ClassCastException ignored) {} 517 518 for (int k = 0; k < nodes.length; k++) { 519 Comparable key = nodes[k].getKey(); 520 521 assertTrue(s.remove(key)); 522 assertTrue(!s.contains(key)); 523 assertTrue(!m.containsKey(key)); 524 assertTrue(!m.containsValue(nodes[k])); 525 } 526 527 assertTrue(m.isEmpty()); 528 529 m = (DoubleOrderedMap) makeMap(); 530 531 Collection c1 = new LinkedList (); 532 Collection c2 = new LinkedList (); 533 534 c2.add(new Integer (-99)); 535 536 for (int k = 0; k < nodes.length; k++) { 537 m.put(nodes[k].getKey(), nodes[k]); 538 c1.add(nodes[k].getKey()); 539 c2.add(nodes[k].getKey()); 540 } 541 542 assertTrue(m.keySet().containsAll(c1)); 543 assertTrue(!m.keySet().containsAll(c2)); 544 545 m = (DoubleOrderedMap) makeMap(); 546 c1 = new LinkedList (); 547 548 c1.add(new Integer (-55)); 549 550 try { 551 m.keySet().addAll(c1); 552 fail("should have caught exception of addAll()"); 553 } catch (UnsupportedOperationException ignored) {} 554 555 for (int k = 0; k < nodes.length; k++) { 556 m.put(nodes[k].getKey(), nodes[k]); 557 c1.add(nodes[k].getKey()); 558 } 559 560 assertTrue(!m.keySet().retainAll(c1)); 561 assertEquals(nodes.length, m.size()); 562 563 m = (DoubleOrderedMap) makeMap(); 564 c1 = new LinkedList (); 565 566 for (int k = 0; k < nodes.length; k++) { 567 m.put(nodes[k].getKey(), nodes[k]); 568 569 if (k % 2 == 1) { 570 c1.add(nodes[k].getKey()); 571 } 572 } 573 574 assertTrue(m.keySet().retainAll(c1)); 575 assertEquals(nodes.length / 2, m.size()); 576 577 m = (DoubleOrderedMap) makeMap(); 578 c1 = new LinkedList (); 579 580 for (int k = 0; k < nodes.length; k++) { 581 m.put(nodes[k].getKey(), nodes[k]); 582 } 583 584 assertTrue(m.keySet().retainAll(c1)); 585 assertEquals(0, m.size()); 586 587 m = (DoubleOrderedMap) makeMap(); 588 c1 = new LinkedList (); 589 590 for (int k = 0; k < nodes.length; k++) { 591 m.put(nodes[k].getKey(), nodes[k]); 592 } 593 594 assertTrue(!m.keySet().removeAll(c1)); 595 assertEquals(nodes.length, m.size()); 596 597 m = (DoubleOrderedMap) makeMap(); 598 c1 = new LinkedList (); 599 600 for (int k = 0; k < nodes.length; k++) { 601 m.put(nodes[k].getKey(), nodes[k]); 602 603 if (k % 2 == 0) { 604 c1.add(nodes[k].getKey()); 605 } 606 } 607 608 assertTrue(m.keySet().removeAll(c1)); 609 assertEquals(nodes.length / 2, m.size()); 610 611 m = (DoubleOrderedMap) makeMap(); 612 c1 = new LinkedList (); 613 614 for (int k = 0; k < nodes.length; k++) { 615 m.put(nodes[k].getKey(), nodes[k]); 616 c1.add(nodes[k].getKey()); 617 } 618 619 assertTrue(m.keySet().removeAll(c1)); 620 assertEquals(0, m.size()); 621 622 m = (DoubleOrderedMap) makeMap(); 623 624 for (int k = 0; k < nodes.length; k++) { 625 m.put(nodes[k].getKey(), nodes[k]); 626 } 627 628 m.keySet().clear(); 629 assertEquals(0, m.size()); 630 } 631 632 635 public void testValues() { 636 637 testValues((DoubleOrderedMap) makeMap()); 638 639 Map m = (DoubleOrderedMap) makeMap(); 640 LocalTestNode nodes[] = makeLocalNodes(); 641 642 for (int k = 0; k < nodes.length; k++) { 643 m.put(nodes[k].getKey(), nodes[k]); 644 } 645 646 testValues(m); 647 648 m = (DoubleOrderedMap) makeMap(); 649 650 for (int k = 0; k < nodes.length; k++) { 651 m.put(nodes[k].getKey(), nodes[k]); 652 } 653 654 int count = m.size(); 655 656 for (Iterator iter = m.values().iterator(); iter.hasNext(); ) { 657 iter.next(); 658 iter.remove(); 659 660 --count; 661 662 assertEquals(count, m.size()); 663 } 664 665 assertTrue(m.isEmpty()); 666 667 m = (DoubleOrderedMap) makeMap(); 668 669 for (int k = 0; k < nodes.length; k++) { 670 m.put(nodes[k].getKey(), nodes[k]); 671 } 672 673 count = m.size(); 674 675 Collection s = m.values(); 676 677 for (int k = 0; k < count; k++) { 678 assertTrue(s.remove(nodes[k])); 679 assertTrue(!s.contains(nodes[k])); 680 assertTrue(!m.containsKey(nodes[k].getKey())); 681 assertTrue(!m.containsValue(nodes[k])); 682 } 683 684 assertTrue(m.isEmpty()); 685 686 m = (DoubleOrderedMap) makeMap(); 687 688 Collection c1 = new LinkedList (); 689 Collection c2 = new LinkedList (); 690 691 c2.add(new LocalTestNode(-123)); 692 693 for (int k = 0; k < nodes.length; k++) { 694 m.put(nodes[k].getKey(), nodes[k]); 695 c1.add(nodes[k]); 696 c2.add(nodes[k]); 697 } 698 699 assertTrue(m.values().containsAll(c1)); 700 assertTrue(!m.values().containsAll(c2)); 701 702 m = (DoubleOrderedMap) makeMap(); 703 c1 = new LinkedList (); 704 705 for (int k = 0; k < nodes.length; k++) { 706 m.put(nodes[k].getKey(), nodes[k]); 707 c1.add(nodes[k]); 708 } 709 710 try { 711 m.values().addAll(c1); 712 fail("should have caught exception of addAll()"); 713 } catch (UnsupportedOperationException ignored) {} 714 715 m = (DoubleOrderedMap) makeMap(); 716 c1 = new LinkedList (); 717 718 for (int k = 0; k < nodes.length; k++) { 719 m.put(nodes[k].getKey(), nodes[k]); 720 c1.add(nodes[k]); 721 } 722 723 assertTrue(!m.values().retainAll(c1)); 724 assertEquals(nodes.length, m.size()); 725 726 m = (DoubleOrderedMap) makeMap(); 727 c1 = new LinkedList (); 728 729 for (int k = 0; k < nodes.length; k++) { 730 m.put(nodes[k].getKey(), nodes[k]); 731 732 if (k % 2 == 1) { 733 c1.add(nodes[k]); 734 } 735 } 736 737 assertTrue(m.values().retainAll(c1)); 738 assertEquals(nodes.length / 2, m.size()); 739 740 m = (DoubleOrderedMap) makeMap(); 741 c1 = new LinkedList (); 742 743 for (int k = 0; k < nodes.length; k++) { 744 m.put(nodes[k].getKey(), nodes[k]); 745 } 746 747 assertTrue(m.values().retainAll(c1)); 748 assertEquals(0, m.size()); 749 750 m = (DoubleOrderedMap) makeMap(); 751 c1 = new LinkedList (); 752 753 for (int k = 0; k < nodes.length; k++) { 754 m.put(nodes[k].getKey(), nodes[k]); 755 } 756 757 assertTrue(!m.values().removeAll(c1)); 758 assertEquals(nodes.length, m.size()); 759 760 m = (DoubleOrderedMap) makeMap(); 761 c1 = new LinkedList (); 762 763 for (int k = 0; k < nodes.length; k++) { 764 m.put(nodes[k].getKey(), nodes[k]); 765 766 if (k % 2 == 0) { 767 c1.add(nodes[k]); 768 } 769 } 770 771 assertTrue(m.values().removeAll(c1)); 772 assertEquals(nodes.length / 2, m.size()); 773 774 m = (DoubleOrderedMap) makeMap(); 775 c1 = new LinkedList (); 776 777 for (int k = 0; k < nodes.length; k++) { 778 m.put(nodes[k].getKey(), nodes[k]); 779 c1.add(nodes[k]); 780 } 781 782 assertTrue(m.values().removeAll(c1)); 783 assertEquals(0, m.size()); 784 785 m = (DoubleOrderedMap) makeMap(); 786 787 for (int k = 0; k < nodes.length; k++) { 788 m.put(nodes[k].getKey(), nodes[k]); 789 } 790 791 m.values().clear(); 792 assertEquals(0, m.size()); 793 } 794 795 798 public void testEntrySet() { 799 800 testEntrySet((DoubleOrderedMap) makeMap()); 801 802 Map m = (DoubleOrderedMap) makeMap(); 803 LocalTestNode nodes[] = makeLocalNodes(); 804 805 for (int k = 0; k < nodes.length; k++) { 806 m.put(nodes[k].getKey(), nodes[k]); 807 } 808 809 testEntrySet(m); 810 811 m = (DoubleOrderedMap) makeMap(); 812 813 for (int k = 0; k < nodes.length; k++) { 814 m.put(nodes[k].getKey(), nodes[k]); 815 } 816 817 try { 818 ((Map.Entry ) m.entrySet().iterator().next()) 819 .setValue(new LocalTestNode(-1)); 820 fail("Should have caught UnsupportedOperationException"); 821 } catch (UnsupportedOperationException ignored) {} 822 823 int count = m.size(); 824 825 for (Iterator iter = m.entrySet().iterator(); iter.hasNext(); ) { 826 iter.next(); 827 iter.remove(); 828 829 --count; 830 831 assertEquals(count, m.size()); 832 } 833 834 assertTrue(m.isEmpty()); 835 836 m = (DoubleOrderedMap) makeMap(); 837 838 Collection c1 = new LinkedList (); 839 840 for (int k = 0; k < nodes.length; k++) { 841 m.put(nodes[k].getKey(), nodes[k]); 842 c1.add(nodes[k].getKey()); 843 } 844 845 try { 846 m.entrySet().addAll(c1); 847 fail("should have caught exception of addAll()"); 848 } catch (UnsupportedOperationException ignored) {} 849 850 m = (DoubleOrderedMap) makeMap(); 851 852 for (int k = 0; k < nodes.length; k++) { 853 m.put(nodes[k].getKey(), nodes[k]); 854 } 855 856 m.entrySet().clear(); 857 assertEquals(0, m.size()); 858 859 m = (DoubleOrderedMap) makeMap(); 860 861 for (int k = 0; k < nodes.length; k++) { 862 m.put(nodes[k].getKey(), nodes[k]); 863 } 864 865 int x = 0; 866 867 for (Iterator iter = m.entrySet().iterator(); iter.hasNext(); ) { 868 Map.Entry entry = (Map.Entry ) iter.next(); 869 870 assertSame(entry.getKey(), nodes[x].getKey()); 871 assertSame(entry.getValue(), nodes[x]); 872 873 x++; 874 } 875 } 876 877 880 public void testEquals() { 881 882 Map m = (DoubleOrderedMap) makeMap(); 883 LocalTestNode nodes[] = makeLocalNodes(); 884 885 for (int k = 0; k < nodes.length; k++) { 886 m.put(nodes[k].getKey(), nodes[k]); 887 } 888 889 assertTrue(!m.equals(null)); 890 assertEquals(m, m); 891 892 Map m1 = new HashMap (); 893 894 for (int k = 0; k < nodes.length; k++) { 895 m1.put(nodes[k].getKey(), nodes[k]); 896 } 897 898 assertEquals(m, m1); 899 900 m1 = (DoubleOrderedMap) makeMap(); 901 902 for (int k = 0; k < (nodes.length - 1); k++) { 903 m1.put(nodes[k].getKey(), nodes[k]); 904 } 905 906 assertTrue(!m.equals(m1)); 907 908 m1 = (DoubleOrderedMap) makeMap(); 909 910 for (int k = 0; k < nodes.length; k++) { 911 m1.put(nodes[k].getKey(), nodes[k]); 912 } 913 914 LocalTestNode node1 = new LocalTestNode(-1000); 915 916 m1.put(node1.getKey(), node1); 917 assertTrue(!m.equals(m1)); 918 919 m1 = (DoubleOrderedMap) makeMap(); 920 921 for (int k = 0; k < nodes.length; k++) { 922 m1.put(nodes[k].getKey(), nodes[nodes.length - (k + 1)]); 923 } 924 925 assertTrue(!m.equals(m1)); 926 927 m1 = (DoubleOrderedMap) makeMap(); 928 929 for (int k = nodes.length - 1; k >= 0; k--) { 930 m1.put(nodes[k].getKey(), nodes[k]); 931 } 932 933 assertEquals(m, m1); 934 } 935 936 939 public void testHashCode() { 940 941 Map m = (DoubleOrderedMap) makeMap(); 942 LocalTestNode nodes[] = makeLocalNodes(); 943 944 for (int k = 0; k < nodes.length; k++) { 945 m.put(nodes[k].getKey(), nodes[k]); 946 } 947 948 Map m1 = (DoubleOrderedMap) makeMap(); 949 950 for (int k = nodes.length - 1; k >= 0; k--) { 951 m1.put(nodes[k].getKey(), nodes[k]); 952 } 953 954 assertEquals(m.hashCode(), m1.hashCode()); 955 } 956 957 960 public void testConstructors() { 961 962 DoubleOrderedMap m = (DoubleOrderedMap) makeMap(); 963 964 assertTrue(m.isEmpty()); 965 966 DoubleOrderedMap m1 = new DoubleOrderedMap(m); 967 968 assertTrue(m1.isEmpty()); 969 970 m1 = (DoubleOrderedMap) makeMap(); 971 972 LocalTestNode nodes[] = makeLocalNodes(); 973 974 for (int k = 0; k < nodes.length; k++) { 975 m1.put(nodes[k].getKey(), nodes[k]); 976 } 977 978 m = new DoubleOrderedMap(m1); 979 980 assertEquals(m, m1); 981 982 Map m2 = new HashMap (); 983 984 for (int k = 0; k < nodes.length; k++) { 985 m2.put(nodes[k].getKey(), nodes[k]); 986 } 987 988 m = new DoubleOrderedMap(m2); 989 990 assertEquals(m, m2); 991 992 m2 = new HashMap (); 994 995 m2.put("1", "foo"); 996 m2.put("2", "foo"); 997 998 try { 999 m = new DoubleOrderedMap(m2); 1000 1001 fail("Should have caught IllegalArgumentException"); 1002 } catch (IllegalArgumentException ignored) {} 1003 1004 m2.put("2", null); 1006 1007 try { 1008 m = new DoubleOrderedMap(m2); 1009 1010 fail("Should have caught NullPointerException"); 1011 } catch (NullPointerException ignored) {} 1012 1013 m2.put("2", new Object ()); 1015 1016 try { 1017 m = new DoubleOrderedMap(m2); 1018 1019 fail("Should have caught ClassCastException"); 1020 } catch (ClassCastException ignored) {} 1021 1022 m2.put("2", new Integer (2)); 1024 1025 try { 1026 m = new DoubleOrderedMap(m2); 1027 1028 fail("Should have caught ClassCastException"); 1029 } catch (ClassCastException ignored) {} 1030 1031 m2.remove("2"); 1033 m2.put(new Integer (2), "bad key"); 1034 1035 try { 1036 m = new DoubleOrderedMap(m2); 1037 1038 fail("Should have caught ClassCastException"); 1039 } catch (ClassCastException ignored) {} 1040 1041 m2.clear(); 1043 m2.put("1", "foo"); 1044 m2.put(new Object (), "bad key"); 1045 1046 try { 1047 m = new DoubleOrderedMap(m2); 1048 1049 fail("Should have caught ClassCastException"); 1050 } catch (ClassCastException ignored) {} 1051 } 1052 1053 1056 public void testGetKeyForValue() { 1057 1058 DoubleOrderedMap m = (DoubleOrderedMap) makeMap(); 1059 1060 try { 1061 m.getKeyForValue(new Object ()); 1062 fail("should have caught ClassCastException"); 1063 } catch (ClassCastException ignored) {} 1064 1065 try { 1066 m.getKeyForValue(null); 1067 fail("should have caught NullPointerException"); 1068 } catch (NullPointerException ignored) {} 1069 1070 assertNull(m.getKeyForValue("foo")); 1071 1072 LocalTestNode nodes[] = makeLocalNodes(); 1073 1074 for (int k = 0; k < nodes.length; k++) { 1075 m.put(nodes[k].getKey(), nodes[k]); 1076 assertSame(m.getKeyForValue(nodes[k]), nodes[k].getKey()); 1077 } 1078 1079 assertNull(m.getKeyForValue(new LocalTestNode(-1))); 1080 1081 try { 1082 m.getKeyForValue("foo"); 1083 fail("Should have caught ClassCastException"); 1084 } catch (ClassCastException ignored) {} 1085 1086 for (int k = 0; k < nodes.length; k++) { 1087 assertNotNull(m.getKeyForValue(nodes[k])); 1088 m.remove(nodes[k].getKey()); 1089 assertNull(m.getKeyForValue(nodes[k])); 1090 } 1091 } 1092 1093 1096 public void testRemoveValue() { 1097 1098 DoubleOrderedMap m = (DoubleOrderedMap) makeMap(); 1099 LocalTestNode nodes[] = makeLocalNodes(); 1100 1101 for (int k = 0; k < nodes.length; k++) { 1102 m.put(nodes[k].getKey(), nodes[k]); 1103 } 1104 1105 try { 1106 m.removeValue(null); 1107 fail("should have caught NullPointerException"); 1108 } catch (NullPointerException ignored) {} 1109 1110 try { 1111 m.removeValue(new Object ()); 1112 fail("should have caught ClassCastException"); 1113 } catch (ClassCastException ignored) {} 1114 1115 assertNull(m.remove(new Integer (-1))); 1116 1117 try { 1118 m.removeValue("foo"); 1119 fail("should have caught ClassCastException"); 1120 } catch (ClassCastException ignored) {} 1121 1122 for (int k = 0; k < nodes.length; k += 2) { 1123 assertNotNull(m.getKeyForValue(nodes[k])); 1124 assertSame(nodes[k].getKey(), m.removeValue(nodes[k])); 1125 assertNull(m.removeValue(nodes[k])); 1126 assertNull(m.getKeyForValue(nodes[k])); 1127 } 1128 1129 for (int k = 1; k < nodes.length; k += 2) { 1130 assertNotNull(m.getKeyForValue(nodes[k])); 1131 assertSame(nodes[k].getKey(), m.removeValue(nodes[k])); 1132 assertNull(m.removeValue(nodes[k])); 1133 assertNull(m.getKeyForValue(nodes[k])); 1134 } 1135 1136 assertTrue(m.isEmpty()); 1137 } 1138 1139 1142 public void testEntrySetByValue() { 1143 1144 testEntrySetByValue((DoubleOrderedMap) makeMap()); 1145 1146 DoubleOrderedMap m = (DoubleOrderedMap) makeMap(); 1147 LocalTestNode nodes[] = makeLocalNodes(); 1148 1149 for (int k = 0; k < nodes.length; k++) { 1150 m.put(nodes[k].getKey(), nodes[k]); 1151 } 1152 1153 testEntrySetByValue(m); 1154 1155 m = (DoubleOrderedMap) makeMap(); 1156 1157 for (int k = 0; k < nodes.length; k++) { 1158 m.put(nodes[k].getKey(), nodes[k]); 1159 } 1160 1161 try { 1162 ((Map.Entry ) m.entrySetByValue().iterator().next()) 1163 .setValue(new LocalTestNode(-1)); 1164 fail("Should have caught UnsupportedOperationException"); 1165 } catch (UnsupportedOperationException ignored) {} 1166 1167 int count = m.size(); 1168 1169 for (Iterator iter = m.entrySetByValue().iterator(); 1170 iter.hasNext(); ) { 1171 iter.next(); 1172 iter.remove(); 1173 1174 --count; 1175 1176 assertEquals(count, m.size()); 1177 } 1178 1179 assertTrue(m.isEmpty()); 1180 1181 m = (DoubleOrderedMap) makeMap(); 1182 1183 Collection c1 = new LinkedList (); 1184 1185 for (int k = 0; k < nodes.length; k++) { 1186 m.put(nodes[k].getKey(), nodes[k]); 1187 c1.add(nodes[k].getKey()); 1188 } 1189 1190 try { 1191 m.entrySetByValue().addAll(c1); 1192 fail("should have caught exception of addAll()"); 1193 } catch (UnsupportedOperationException ignored) {} 1194 1195 m = (DoubleOrderedMap) makeMap(); 1196 1197 for (int k = 0; k < nodes.length; k++) { 1198 m.put(nodes[k].getKey(), nodes[k]); 1199 } 1200 1201 m.entrySetByValue().clear(); 1202 assertEquals(0, m.size()); 1203 1204 m = (DoubleOrderedMap) makeMap(); 1205 1206 for (int k = 0; k < nodes.length; k++) { 1207 m.put(nodes[k].getKey(), nodes[k]); 1208 } 1209 1210 int x = 0; 1211 1212 for (Iterator iter = m.entrySetByValue().iterator(); 1213 iter.hasNext(); ) { 1214 Map.Entry entry = (Map.Entry ) iter.next(); 1215 1216 assertSame(entry.getKey(), nodes[x].getKey()); 1217 assertSame(entry.getValue(), nodes[x]); 1218 1219 x++; 1220 } 1221 } 1222 1223 1226 public void testKeySetByValue() { 1227 1228 testKeySetByValue((DoubleOrderedMap) makeMap()); 1229 1230 DoubleOrderedMap m = (DoubleOrderedMap) makeMap(); 1231 LocalTestNode nodes[] = makeLocalNodes(); 1232 1233 for (int k = 0; k < nodes.length; k++) { 1234 m.put(nodes[k].getKey(), nodes[k]); 1235 } 1236 1237 testKeySetByValue(m); 1238 1239 m = (DoubleOrderedMap) makeMap(); 1240 1241 for (int k = 0; k < nodes.length; k++) { 1242 m.put(nodes[k].getKey(), nodes[k]); 1243 } 1244 1245 int count = m.size(); 1246 1247 for (Iterator iter = m.keySetByValue().iterator(); iter.hasNext(); ) 1248{ 1249 iter.next(); 1250 iter.remove(); 1251 1252 --count; 1253 1254 assertEquals(count, m.size()); 1255 } 1256 1257 assertTrue(m.isEmpty()); 1258 1259 m = (DoubleOrderedMap) makeMap(); 1260 1261 for (int k = 0; k < nodes.length; k++) { 1262 m.put(nodes[k].getKey(), nodes[k]); 1263 } 1264 1265 Set s = m.keySetByValue(); 1266 1267 try { 1268 s.remove(null); 1269 fail("should have caught NullPointerException"); 1270 } catch (NullPointerException ignored) {} 1271 1272 try { 1273 s.remove(new Object ()); 1274 fail("should have caught ClassCastException"); 1275 } catch (ClassCastException ignored) {} 1276 1277 for (int k = 0; k < nodes.length; k++) { 1278 Comparable key = nodes[k].getKey(); 1279 1280 assertTrue(s.remove(key)); 1281 assertTrue(!s.contains(key)); 1282 assertTrue(!m.containsKey(key)); 1283 assertTrue(!m.containsValue(nodes[k])); 1284 } 1285 1286 assertTrue(m.isEmpty()); 1287 1288 m = (DoubleOrderedMap) makeMap(); 1289 1290 Collection c1 = new LinkedList (); 1291 Collection c2 = new LinkedList (); 1292 1293 c2.add(new Integer (-99)); 1294 1295 for (int k = 0; k < nodes.length; k++) { 1296 m.put(nodes[k].getKey(), nodes[k]); 1297 c1.add(nodes[k].getKey()); 1298 c2.add(nodes[k].getKey()); 1299 } 1300 1301 assertTrue(m.keySetByValue().containsAll(c1)); 1302 assertTrue(!m.keySetByValue().containsAll(c2)); 1303 1304 m = (DoubleOrderedMap) makeMap(); 1305 c1 = new LinkedList (); 1306 1307 c1.add(new Integer (-55)); 1308 1309 try { 1310 m.keySetByValue().addAll(c1); 1311 fail("should have caught exception of addAll()"); 1312 } catch (UnsupportedOperationException ignored) {} 1313 1314 for (int k = 0; k < nodes.length; k++) { 1315 m.put(nodes[k].getKey(), nodes[k]); 1316 c1.add(nodes[k].getKey()); 1317 } 1318 1319 assertTrue(!m.keySetByValue().retainAll(c1)); 1320 assertEquals(nodes.length, m.size()); 1321 1322 m = (DoubleOrderedMap) makeMap(); 1323 c1 = new LinkedList (); 1324 1325 for (int k = 0; k < nodes.length; k++) { 1326 m.put(nodes[k].getKey(), nodes[k]); 1327 1328 if (k % 2 == 1) { 1329 c1.add(nodes[k].getKey()); 1330 } 1331 } 1332 1333 assertTrue(m.keySetByValue().retainAll(c1)); 1334 assertEquals(nodes.length / 2, m.size()); 1335 1336 m = (DoubleOrderedMap) makeMap(); 1337 c1 = new LinkedList (); 1338 1339 for (int k = 0; k < nodes.length; k++) { 1340 m.put(nodes[k].getKey(), nodes[k]); 1341 } 1342 1343 assertTrue(m.keySetByValue().retainAll(c1)); 1344 assertEquals(0, m.size()); 1345 1346 m = (DoubleOrderedMap) makeMap(); 1347 c1 = new LinkedList (); 1348 1349 for (int k = 0; k < nodes.length; k++) { 1350 m.put(nodes[k].getKey(), nodes[k]); 1351 } 1352 1353 assertTrue(!m.keySetByValue().removeAll(c1)); 1354 assertEquals(nodes.length, m.size()); 1355 1356 m = (DoubleOrderedMap) makeMap(); 1357 c1 = new LinkedList (); 1358 1359 for (int k = 0; k < nodes.length; k++) { 1360 m.put(nodes[k].getKey(), nodes[k]); 1361 1362 if (k % 2 == 0) { 1363 c1.add(nodes[k].getKey()); 1364 } 1365 } 1366 1367 assertTrue(m.keySetByValue().removeAll(c1)); 1368 assertEquals(nodes.length / 2, m.size()); 1369 1370 m = (DoubleOrderedMap) makeMap(); 1371 c1 = new LinkedList (); 1372 1373 for (int k = 0; k < nodes.length; k++) { 1374 m.put(nodes[k].getKey(), nodes[k]); 1375 c1.add(nodes[k].getKey()); 1376 } 1377 1378 assertTrue(m.keySetByValue().removeAll(c1)); 1379 assertEquals(0, m.size()); 1380 1381 m = (DoubleOrderedMap) makeMap(); 1382 1383 for (int k = 0; k < nodes.length; k++) { 1384 m.put(nodes[k].getKey(), nodes[k]); 1385 } 1386 1387 m.keySetByValue().clear(); 1388 assertEquals(0, m.size()); 1389 } 1390 1391 1394 public void testValuesByValue() { 1395 1396 testValuesByValue((DoubleOrderedMap) makeMap()); 1397 1398 DoubleOrderedMap m = (DoubleOrderedMap) makeMap(); 1399 LocalTestNode nodes[] = makeLocalNodes(); 1400 1401 for (int k = 0; k < nodes.length; k++) { 1402 m.put(nodes[k].getKey(), nodes[k]); 1403 } 1404 1405 testValuesByValue(m); 1406 1407 m = (DoubleOrderedMap) makeMap(); 1408 1409 for (int k = 0; k < nodes.length; k++) { 1410 m.put(nodes[k].getKey(), nodes[k]); 1411 } 1412 1413 int count = m.size(); 1414 1415 for (Iterator iter = m.valuesByValue().iterator(); iter.hasNext(); ) 1416{ 1417 iter.next(); 1418 iter.remove(); 1419 1420 --count; 1421 1422 assertEquals(count, m.size()); 1423 } 1424 1425 assertTrue(m.isEmpty()); 1426 1427 m = (DoubleOrderedMap) makeMap(); 1428 1429 for (int k = 0; k < nodes.length; k++) { 1430 m.put(nodes[k].getKey(), nodes[k]); 1431 } 1432 1433 count = m.size(); 1434 1435 Collection s = m.valuesByValue(); 1436 1437 for (int k = 0; k < count; k++) { 1438 assertTrue(s.remove(nodes[k])); 1439 assertTrue(!s.contains(nodes[k])); 1440 assertTrue(!m.containsKey(nodes[k].getKey())); 1441 assertTrue(!m.containsValue(nodes[k])); 1442 } 1443 1444 assertTrue(m.isEmpty()); 1445 1446 m = (DoubleOrderedMap) makeMap(); 1447 1448 Collection c1 = new LinkedList (); 1449 Collection c2 = new LinkedList (); 1450 1451 c2.add(new LocalTestNode(-123)); 1452 1453 for (int k = 0; k < nodes.length; k++) { 1454 m.put(nodes[k].getKey(), nodes[k]); 1455 c1.add(nodes[k]); 1456 c2.add(nodes[k]); 1457 } 1458 1459 assertTrue(m.valuesByValue().containsAll(c1)); 1460 assertTrue(!m.valuesByValue().containsAll(c2)); 1461 1462 m = (DoubleOrderedMap) makeMap(); 1463 c1 = new LinkedList (); 1464 1465 for (int k = 0; k < nodes.length; k++) { 1466 m.put(nodes[k].getKey(), nodes[k]); 1467 c1.add(nodes[k]); 1468 } 1469 1470 try { 1471 m.valuesByValue().addAll(c1); 1472 fail("should have caught exception of addAll()"); 1473 } catch (UnsupportedOperationException ignored) {} 1474 1475 m = (DoubleOrderedMap) makeMap(); 1476 c1 = new LinkedList (); 1477 1478 for (int k = 0; k < nodes.length; k++) { 1479 m.put(nodes[k].getKey(), nodes[k]); 1480 c1.add(nodes[k]); 1481 } 1482 1483 assertTrue(!m.valuesByValue().retainAll(c1)); 1484 assertEquals(nodes.length, m.size()); 1485 1486 m = (DoubleOrderedMap) makeMap(); 1487 c1 = new LinkedList (); 1488 1489 for (int k = 0; k < nodes.length; k++) { 1490 m.put(nodes[k].getKey(), nodes[k]); 1491 1492 if (k % 2 == 1) { 1493 c1.add(nodes[k]); 1494 } 1495 } 1496 1497 assertTrue(m.valuesByValue().retainAll(c1)); 1498 assertEquals(nodes.length / 2, m.size()); 1499 1500 m = (DoubleOrderedMap) makeMap(); 1501 c1 = new LinkedList (); 1502 1503 for (int k = 0; k < nodes.length; k++) { 1504 m.put(nodes[k].getKey(), nodes[k]); 1505 } 1506 1507 assertTrue(m.valuesByValue().retainAll(c1)); 1508 assertEquals(0, m.size()); 1509 1510 m = (DoubleOrderedMap) makeMap(); 1511 c1 = new LinkedList (); 1512 1513 for (int k = 0; k < nodes.length; k++) { 1514 m.put(nodes[k].getKey(), nodes[k]); 1515 } 1516 1517 assertTrue(!m.valuesByValue().removeAll(c1)); 1518 assertEquals(nodes.length, m.size()); 1519 1520 m = (DoubleOrderedMap) makeMap(); 1521 c1 = new LinkedList (); 1522 1523 for (int k = 0; k < nodes.length; k++) { 1524 m.put(nodes[k].getKey(), nodes[k]); 1525 1526 if (k % 2 == 0) { 1527 c1.add(nodes[k]); 1528 } 1529 } 1530 1531 assertTrue(m.valuesByValue().removeAll(c1)); 1532 assertEquals(nodes.length / 2, m.size()); 1533 1534 m = (DoubleOrderedMap) makeMap(); 1535 c1 = new LinkedList (); 1536 1537 for (int k = 0; k < nodes.length; k++) { 1538 m.put(nodes[k].getKey(), nodes[k]); 1539 c1.add(nodes[k]); 1540 } 1541 1542 assertTrue(m.valuesByValue().removeAll(c1)); 1543 assertEquals(0, m.size()); 1544 1545 m = (DoubleOrderedMap) makeMap(); 1546 1547 for (int k = 0; k < nodes.length; k++) { 1548 m.put(nodes[k].getKey(), nodes[k]); 1549 } 1550 1551 m.valuesByValue().clear(); 1552 assertEquals(0, m.size()); 1553 } 1554 1555 1556 private void testKeySet(final Map m) { 1557 1558 Set s = m.keySet(); 1559 1560 assertEquals(m.size(), s.size()); 1561 assertEquals(m.isEmpty(), s.isEmpty()); 1562 1563 LocalTestNode node = new LocalTestNode(-1); 1564 1565 m.put(node.getKey(), node); 1566 assertTrue(s.contains(node.getKey())); 1567 assertEquals(m.size(), s.size()); 1568 assertEquals(m.isEmpty(), s.isEmpty()); 1569 m.remove(node.getKey()); 1570 assertTrue(!s.contains(node.getKey())); 1571 assertEquals(m.size(), s.size()); 1572 assertEquals(m.isEmpty(), s.isEmpty()); 1573 1574 try { 1575 s.contains(null); 1576 fail("should have caught NullPointerException"); 1577 } catch (NullPointerException ignored) {} 1578 1579 try { 1580 s.contains(new Object ()); 1581 fail("should have caught ClassCastException"); 1582 } catch (ClassCastException ignored) {} 1583 1584 for (int k = 0; k < m.size(); k++) { 1585 assertTrue(s.contains(new Integer (k))); 1586 } 1587 1588 int count = 0; 1589 1590 for (Iterator iter = s.iterator(); iter.hasNext(); ) { 1591 iter.next(); 1592 1593 ++count; 1594 } 1595 1596 assertEquals(count, s.size()); 1597 1598 m.put(node.getKey(), node); 1600 1601 Iterator iter = m.keySet().iterator(); 1602 LocalTestNode node2 = new LocalTestNode(-2); 1603 1604 m.put(node2.getKey(), node2); 1605 1606 try { 1607 iter.next(); 1608 fail("next() should have thrown an exception after a put"); 1609 } catch (ConcurrentModificationException ignored) {} 1610 1611 m.remove(node2.getKey()); 1612 1613 iter = s.iterator(); 1614 1615 m.remove(node.getKey()); 1616 1617 try { 1618 iter.next(); 1619 fail("next() should have thrown an exception after a Map remove"); 1620 } catch (ConcurrentModificationException ignored) {} 1621 1622 m.put(node.getKey(), node); 1623 1624 iter = s.iterator(); 1625 1626 s.remove(node.getKey()); 1627 1628 try { 1629 iter.next(); 1630 fail("next() should have thrown an exception after a Set remove"); 1631 } catch (ConcurrentModificationException ignored) {} 1632 1633 iter = s.iterator(); 1634 count = 0; 1635 1636 boolean terminated = false; 1637 1638 try { 1639 while (true) { 1640 iter.next(); 1641 1642 ++count; 1643 } 1644 } catch (NoSuchElementException ignored) { 1645 terminated = true; 1646 } 1647 1648 assertTrue(terminated); 1649 assertEquals(m.size(), count); 1650 1651 iter = s.iterator(); 1652 1653 try { 1654 iter.remove(); 1655 fail("Should have thrown exception"); 1656 } catch (IllegalStateException ignored) {} 1657 1658 m.put(node.getKey(), node); 1659 1660 iter = s.iterator(); 1661 1662 iter.next(); 1663 m.put(node2.getKey(), node2); 1664 1665 try { 1666 iter.remove(); 1667 fail("should have thrown exception"); 1668 } catch (ConcurrentModificationException ignored) {} 1669 1670 Iterator iter2 = s.iterator(); 1671 1672 iter2.next(); 1673 1674 LocalTestNode node3 = new LocalTestNode(-3); 1675 1676 m.put(node3.getKey(), node3); 1677 1678 try { 1679 iter2.remove(); 1680 fail("should have thrown exception"); 1681 } catch (ConcurrentModificationException ignored) {} 1682 1683 int removalCount = 0; 1684 1685 for (iter = s.iterator(); iter.hasNext(); ) { 1686 if (iter.next().equals(node.getKey())) { 1687 try { 1688 iter.remove(); 1689 1690 ++removalCount; 1691 1692 iter.remove(); 1693 fail("2nd remove should have failed"); 1694 } catch (IllegalStateException ignored) { 1695 assertEquals(1, removalCount); 1696 } 1697 } 1698 } 1699 1700 assertEquals(1, removalCount); 1701 assertTrue(!s.contains(node.getKey())); 1702 1703 removalCount = 0; 1704 1705 m.put(node.getKey(), node); 1706 1707 Object [] a1 = s.toArray(); 1708 1709 assertEquals(s.size(), a1.length); 1710 1711 if (a1.length > 1) { 1712 Comparable first = (Comparable ) a1[0]; 1713 1714 for (int k = 1; k < a1.length; k++) { 1715 Comparable second = (Comparable ) a1[k]; 1716 1717 assertTrue(first.compareTo(second) < 0); 1718 1719 first = second; 1720 } 1721 1722 iter = s.iterator(); 1723 first = (Comparable ) iter.next(); 1724 1725 for (; iter.hasNext(); ) { 1726 Comparable second = (Comparable ) iter.next(); 1727 1728 assertTrue(first.compareTo(second) < 0); 1729 1730 first = second; 1731 } 1732 } 1733 1734 try { 1735 String array2[] = (String []) s.toArray(new String [0]); 1736 1737 if (s.size() != 0) { 1738 fail("should have caught exception creating an invalid array"); 1739 } 1740 } catch (ArrayStoreException ignored) {} 1741 1742 Comparable array2[] = (Comparable []) s.toArray(new Comparable [0]); 1743 Integer array3[] = (Integer []) s.toArray(new Integer [s.size()]); 1744 1745 if (array3.length > 1) { 1746 Integer first = array3[0]; 1747 1748 for (int k = 1; k < array3.length; k++) { 1749 Integer second = array3[k]; 1750 1751 assertTrue(first.compareTo(second) < 0); 1752 1753 first = second; 1754 } 1755 } 1756 1757 try { 1758 s.add("foo"); 1759 fail("should have thrown an exception"); 1760 } catch (UnsupportedOperationException ignored) {} 1761 1762 assertTrue(!s.equals(null)); 1763 assertEquals(s, s); 1764 1765 Set hs = new HashSet (s); 1766 1767 assertEquals(s, hs); 1768 assertEquals(hs, s); 1769 assertEquals(s.hashCode(), hs.hashCode()); 1770 } 1771 1772 private void testKeySetByValue(final DoubleOrderedMap m) { 1773 1774 Set s = m.keySetByValue(); 1775 1776 assertEquals(m.size(), s.size()); 1777 assertEquals(m.isEmpty(), s.isEmpty()); 1778 1779 LocalTestNode node = new LocalTestNode(-1); 1780 1781 m.put(node.getKey(), node); 1782 assertTrue(s.contains(node.getKey())); 1783 assertEquals(m.size(), s.size()); 1784 assertEquals(m.isEmpty(), s.isEmpty()); 1785 m.remove(node.getKey()); 1786 assertTrue(!s.contains(node.getKey())); 1787 assertEquals(m.size(), s.size()); 1788 assertEquals(m.isEmpty(), s.isEmpty()); 1789 1790 try { 1791 s.contains(null); 1792 fail("should have caught NullPointerException"); 1793 } catch (NullPointerException ignored) {} 1794 1795 try { 1796 s.contains(new Object ()); 1797 fail("should have caught ClassCastException"); 1798 } catch (ClassCastException ignored) {} 1799 1800 for (int k = 0; k < m.size(); k++) { 1801 assertTrue(s.contains(new Integer (k))); 1802 } 1803 1804 int count = 0; 1805 1806 for (Iterator iter = s.iterator(); iter.hasNext(); ) { 1807 iter.next(); 1808 1809 ++count; 1810 } 1811 1812 assertEquals(count, s.size()); 1813 1814 m.put(node.getKey(), node); 1816 1817 Iterator iter = m.keySetByValue().iterator(); 1818 LocalTestNode node2 = new LocalTestNode(-2); 1819 1820 m.put(node2.getKey(), node2); 1821 1822 try { 1823 iter.next(); 1824 fail("next() should have thrown an exception after a put"); 1825 } catch (ConcurrentModificationException ignored) {} 1826 1827 m.remove(node2.getKey()); 1828 1829 iter = s.iterator(); 1830 1831 m.remove(node.getKey()); 1832 1833 try { 1834 iter.next(); 1835 fail("next() should have thrown an exception after a Map remove"); 1836 } catch (ConcurrentModificationException ignored) {} 1837 1838 m.put(node.getKey(), node); 1839 1840 iter = s.iterator(); 1841 1842 s.remove(node.getKey()); 1843 1844 try { 1845 iter.next(); 1846 fail("next() should have thrown an exception after a Set remove"); 1847 } catch (ConcurrentModificationException ignored) {} 1848 1849 iter = s.iterator(); 1850 count = 0; 1851 1852 boolean terminated = false; 1853 1854 try { 1855 while (true) { 1856 iter.next(); 1857 1858 ++count; 1859 } 1860 } catch (NoSuchElementException ignored) { 1861 terminated = true; 1862 } 1863 1864 assertTrue(terminated); 1865 assertEquals(m.size(), count); 1866 1867 iter = s.iterator(); 1868 1869 try { 1870 iter.remove(); 1871 fail("Should have thrown exception"); 1872 } catch (IllegalStateException ignored) {} 1873 1874 m.put(node.getKey(), node); 1875 1876 iter = s.iterator(); 1877 1878 iter.next(); 1879 m.put(node2.getKey(), node2); 1880 1881 try { 1882 iter.remove(); 1883 fail("should have thrown exception"); 1884 } catch (ConcurrentModificationException ignored) {} 1885 1886 Iterator iter2 = s.iterator(); 1887 1888 iter2.next(); 1889 1890 LocalTestNode node3 = new LocalTestNode(-3); 1891 1892 m.put(node3.getKey(), node3); 1893 1894 try { 1895 iter2.remove(); 1896 fail("should have thrown exception"); 1897 } catch (ConcurrentModificationException ignored) {} 1898 1899 int removalCount = 0; 1900 1901 for (iter = s.iterator(); iter.hasNext(); ) { 1902 if (iter.next().equals(node.getKey())) { 1903 try { 1904 iter.remove(); 1905 1906 ++removalCount; 1907 1908 iter.remove(); 1909 fail("2nd remove should have failed"); 1910 } catch (IllegalStateException ignored) { 1911 assertEquals(1, removalCount); 1912 } 1913 } 1914 } 1915 1916 assertEquals(1, removalCount); 1917 assertTrue(!s.contains(node.getKey())); 1918 1919 removalCount = 0; 1920 1921 m.put(node.getKey(), node); 1922 1923 Object [] a1 = s.toArray(); 1924 1925 assertEquals(s.size(), a1.length); 1926 1927 try { 1946 String array2[] = (String []) s.toArray(new String [0]); 1947 1948 if (s.size() != 0) { 1949 fail("should have caught exception creating an invalid array"); 1950 } 1951 } catch (ArrayStoreException ignored) {} 1952 1953 Comparable array2[] = (Comparable []) s.toArray(new Comparable [0]); 1954 Integer array3[] = (Integer []) s.toArray(new Integer [s.size()]); 1955 1956 try { 1967 s.add("foo"); 1968 fail("should have thrown an exception"); 1969 } catch (UnsupportedOperationException ignored) {} 1970 1971 assertTrue(!s.equals(null)); 1972 assertEquals(s, s); 1973 1974 Set hs = new HashSet (s); 1975 1976 assertEquals(s, hs); 1977 assertEquals(hs, s); 1978 assertEquals(s.hashCode(), hs.hashCode()); 1979 } 1980 1981 private void testValues(Map m) { 1982 1983 Collection s = m.values(); 1984 1985 assertEquals(m.size(), s.size()); 1986 assertEquals(m.isEmpty(), s.isEmpty()); 1987 1988 LocalTestNode node = new LocalTestNode(-1); 1989 1990 m.put(node.getKey(), node); 1991 assertEquals(m.size(), s.size()); 1992 assertEquals(m.isEmpty(), s.isEmpty()); 1993 m.remove(node.getKey()); 1994 assertEquals(m.size(), s.size()); 1995 assertEquals(m.isEmpty(), s.isEmpty()); 1996 assertTrue(!s.contains(node)); 1997 1998 for (int k = 0; k < m.size(); k++) { 1999 assertTrue(s.contains(new LocalTestNode(k))); 2000 } 2001 2002 m.put(node.getKey(), node); 2003 assertTrue(s.contains(node)); 2004 m.remove(node.getKey()); 2005 assertTrue(!s.contains(node)); 2006 2007 int count = 0; 2008 2009 for (Iterator iter = s.iterator(); iter.hasNext(); ) { 2010 iter.next(); 2011 2012 ++count; 2013 } 2014 2015 assertEquals(s.size(), count); 2016 2017 LocalTestNode node4 = new LocalTestNode(-4); 2018 2019 m.put(node4.getKey(), node4); 2020 2021 Iterator iter = s.iterator(); 2022 2023 m.put(node.getKey(), node); 2024 2025 try { 2026 iter.next(); 2027 fail("next() should have thrown an exception after a put"); 2028 } catch (ConcurrentModificationException ignored) {} 2029 2030 iter = s.iterator(); 2031 2032 m.remove(node.getKey()); 2033 2034 try { 2035 iter.next(); 2036 fail("next() should have thrown an exception after a Map remove"); 2037 } catch (ConcurrentModificationException ignored) {} 2038 2039 m.put(node.getKey(), node); 2040 2041 iter = s.iterator(); 2042 2043 s.remove(node); 2044 2045 try { 2046 iter.next(); 2047 fail("next() should have thrown an exception after a Set remove"); 2048 } catch (ConcurrentModificationException ignored) {} 2049 2050 iter = s.iterator(); 2051 count = 0; 2052 2053 boolean terminated = false; 2054 2055 try { 2056 while (true) { 2057 iter.next(); 2058 2059 ++count; 2060 } 2061 } catch (NoSuchElementException ignored) { 2062 terminated = true; 2063 } 2064 2065 assertTrue(terminated); 2066 assertEquals(m.size(), count); 2067 2068 iter = s.iterator(); 2069 2070 try { 2071 iter.remove(); 2072 fail("Should have thrown exception"); 2073 } catch (IllegalStateException ignored) {} 2074 2075 Iterator iter2 = s.iterator(); 2076 2077 try { 2078 iter2.remove(); 2079 fail("Should have thrown exception"); 2080 } catch (IllegalStateException ignored) {} 2081 2082 m.put(node.getKey(), node); 2083 2084 iter = s.iterator(); 2085 2086 iter.next(); 2087 2088 LocalTestNode node2 = new LocalTestNode(-2); 2089 2090 m.put(node2.getKey(), node2); 2091 2092 try { 2093 iter.remove(); 2094 fail("should have thrown exception"); 2095 } catch (ConcurrentModificationException ignored) {} 2096 2097 LocalTestNode node3 = new LocalTestNode(-3); 2098 2099 m.put(node3.getKey(), node3); 2100 2101 iter2 = s.iterator(); 2102 2103 while (iter2.hasNext()) { 2104 iter2.next(); 2105 } 2106 2107 int removalCount = 0; 2108 2109 for (iter = s.iterator(); iter.hasNext(); ) { 2110 if (iter.next().equals(node3)) { 2111 try { 2112 iter.remove(); 2113 2114 ++removalCount; 2115 2116 iter.remove(); 2117 fail("2nd remove should have failed"); 2118 } catch (IllegalStateException ignored) { 2119 assertEquals(1, removalCount); 2120 } 2121 } 2122 } 2123 2124 assertEquals(1, removalCount); 2125 assertTrue(!s.contains(node3)); 2126 2127 Object [] a1 = s.toArray(); 2128 2129 assertEquals(s.size(), a1.length); 2130 2131 if (a1.length > 1) { 2132 Comparable first = (Comparable ) a1[0]; 2133 2134 for (int k = 1; k < a1.length; k++) { 2135 Comparable second = (Comparable ) a1[k]; 2136 2137 assertTrue(first.compareTo(second) < 0); 2138 2139 first = second; 2140 } 2141 2142 iter = s.iterator(); 2143 first = (Comparable ) iter.next(); 2144 2145 for (; iter.hasNext(); ) { 2146 Comparable second = (Comparable ) iter.next(); 2147 2148 assertTrue(first.compareTo(second) < 0); 2149 2150 first = second; 2151 } 2152 } 2153 2154 try { 2155 String array2[] = (String []) s.toArray(new String [0]); 2156 2157 if (s.size() != 0) { 2158 fail("should have caught exception creating an invalid array"); 2159 } 2160 } catch (ArrayStoreException ignored) {} 2161 2162 m.remove(node.getKey()); 2163 m.remove(node2.getKey()); 2164 m.remove(node3.getKey()); 2165 2166 LocalTestNode array2[] = 2167 (LocalTestNode[]) s.toArray(new LocalTestNode[0]); 2168 LocalTestNode array3[] = 2169 (LocalTestNode[]) s.toArray(new LocalTestNode[s.size()]); 2170 2171 if (array3.length > 1) { 2172 LocalTestNode first = array3[0]; 2173 2174 for (int k = 1; k < array3.length; k++) { 2175 LocalTestNode second = array3[k]; 2176 2177 assertTrue(first.compareTo(second) < 0); 2178 2179 first = second; 2180 } 2181 } 2182 2183 try { 2184 s.add(node.getKey()); 2185 fail("should have thrown an exception"); 2186 } catch (UnsupportedOperationException ignored) {} 2187 2188 assertTrue(!s.equals(null)); 2189 assertEquals(s, s); 2190 2191 Set hs = new HashSet (s); 2192 2193 assertTrue(!s.equals(hs)); 2194 assertTrue(!hs.equals(s)); 2195 } 2196 2197 private void testValuesByValue(DoubleOrderedMap m) { 2198 2199 Collection s = m.valuesByValue(); 2200 2201 assertEquals(m.size(), s.size()); 2202 assertEquals(m.isEmpty(), s.isEmpty()); 2203 2204 LocalTestNode node = new LocalTestNode(-1); 2205 2206 m.put(node.getKey(), node); 2207 assertEquals(m.size(), s.size()); 2208 assertEquals(m.isEmpty(), s.isEmpty()); 2209 m.remove(node.getKey()); 2210 assertEquals(m.size(), s.size()); 2211 assertEquals(m.isEmpty(), s.isEmpty()); 2212 assertTrue(!s.contains(node)); 2213 2214 for (int k = 0; k < m.size(); k++) { 2215 assertTrue(s.contains(new LocalTestNode(k))); 2216 } 2217 2218 m.put(node.getKey(), node); 2219 assertTrue(s.contains(node)); 2220 m.remove(node.getKey()); 2221 assertTrue(!s.contains(node)); 2222 2223 int count = 0; 2224 2225 for (Iterator iter = s.iterator(); iter.hasNext(); ) { 2226 iter.next(); 2227 2228 ++count; 2229 } 2230 2231 assertEquals(s.size(), count); 2232 2233 LocalTestNode node4 = new LocalTestNode(-4); 2234 2235 m.put(node4.getKey(), node4); 2236 2237 Iterator iter = s.iterator(); 2238 2239 m.put(node.getKey(), node); 2240 2241 try { 2242 iter.next(); 2243 fail("next() should have thrown an exception after a put"); 2244 } catch (ConcurrentModificationException ignored) {} 2245 2246 iter = s.iterator(); 2247 2248 m.remove(node.getKey()); 2249 2250 try { 2251 iter.next(); 2252 fail("next() should have thrown an exception after a Map remove"); 2253 } catch (ConcurrentModificationException ignored) {} 2254 2255 m.put(node.getKey(), node); 2256 2257 iter = s.iterator(); 2258 2259 s.remove(node); 2260 2261 try { 2262 iter.next(); 2263 fail("next() should have thrown an exception after a Set remove"); 2264 } catch (ConcurrentModificationException ignored) {} 2265 2266 iter = s.iterator(); 2267 count = 0; 2268 2269 boolean terminated = false; 2270 2271 try { 2272 while (true) { 2273 iter.next(); 2274 2275 ++count; 2276 } 2277 } catch (NoSuchElementException ignored) { 2278 terminated = true; 2279 } 2280 2281 assertTrue(terminated); 2282 assertEquals(m.size(), count); 2283 2284 iter = s.iterator(); 2285 2286 try { 2287 iter.remove(); 2288 fail("Should have thrown exception"); 2289 } catch (IllegalStateException ignored) {} 2290 2291 Iterator iter2 = s.iterator(); 2292 2293 try { 2294 iter2.remove(); 2295 fail("Should have thrown exception"); 2296 } catch (IllegalStateException ignored) {} 2297 2298 m.put(node.getKey(), node); 2299 2300 iter = s.iterator(); 2301 2302 iter.next(); 2303 2304 LocalTestNode node2 = new LocalTestNode(-2); 2305 2306 m.put(node2.getKey(), node2); 2307 2308 try { 2309 iter.remove(); 2310 fail("should have thrown exception"); 2311 } catch (ConcurrentModificationException ignored) {} 2312 2313 LocalTestNode node3 = new LocalTestNode(-3); 2314 2315 m.put(node3.getKey(), node3); 2316 2317 iter2 = s.iterator(); 2318 2319 while (iter2.hasNext()) { 2320 iter2.next(); 2321 } 2322 2323 int removalCount = 0; 2324 2325 for (iter = s.iterator(); iter.hasNext(); ) { 2326 if (iter.next().equals(node3)) { 2327 try { 2328 iter.remove(); 2329 2330 ++removalCount; 2331 2332 iter.remove(); 2333 fail("2nd remove should have failed"); 2334 } catch (IllegalStateException ignored) { 2335 assertEquals(1, removalCount); 2336 } 2337 } 2338 } 2339 2340 assertEquals(1, removalCount); 2341 assertTrue(!s.contains(node3)); 2342 2343 Object [] a1 = s.toArray(); 2344 2345 assertEquals(s.size(), a1.length); 2346 2347 try { 2348 String array2[] = (String []) s.toArray(new String [0]); 2349 2350 if (s.size() != 0) { 2351 fail("should have caught exception creating an invalid array"); 2352 } 2353 } catch (ArrayStoreException ignored) {} 2354 2355 m.remove(node.getKey()); 2356 m.remove(node2.getKey()); 2357 m.remove(node3.getKey()); 2358 2359 LocalTestNode array2[] = 2360 (LocalTestNode[]) s.toArray(new LocalTestNode[0]); 2361 LocalTestNode array3[] = 2362 (LocalTestNode[]) s.toArray(new LocalTestNode[s.size()]); 2363 2364 try { 2365 s.add(node.getKey()); 2366 fail("should have thrown an exception"); 2367 } catch (UnsupportedOperationException ignored) {} 2368 2369 assertTrue(!s.equals(null)); 2370 assertEquals(s, s); 2371 2372 Set hs = new HashSet (s); 2373 2374 assertTrue(!s.equals(hs)); 2375 assertTrue(!hs.equals(s)); 2376 } 2377 2378 private void testEntrySet(Map m) { 2379 2380 Set s = m.entrySet(); 2381 2382 assertEquals(m.size(), s.size()); 2383 assertEquals(m.isEmpty(), s.isEmpty()); 2384 2385 LocalTestNode node = new LocalTestNode(-1); 2386 2387 m.put(node.getKey(), node); 2388 assertEquals(m.size(), s.size()); 2389 assertEquals(m.isEmpty(), s.isEmpty()); 2390 m.remove(node.getKey()); 2391 assertEquals(m.size(), s.size()); 2392 assertEquals(m.isEmpty(), s.isEmpty()); 2393 2394 int count = 0; 2395 2396 for (Iterator iter = s.iterator(); iter.hasNext(); ) { 2397 iter.next(); 2398 2399 ++count; 2400 } 2401 2402 assertEquals(s.size(), count); 2403 2404 LocalTestNode node2 = new LocalTestNode(-2); 2405 2406 if (m.size() == 0) { 2407 m.put(node2.getKey(), node2); 2408 } 2409 2410 Iterator iter = s.iterator(); 2411 2412 m.put(node.getKey(), node); 2413 2414 try { 2415 iter.next(); 2416 fail("next() should have thrown an exception after a put"); 2417 } catch (ConcurrentModificationException ignored) {} 2418 2419 m.remove(node2.getKey()); 2420 2421 iter = s.iterator(); 2422 2423 m.remove(node.getKey()); 2424 2425 try { 2426 iter.next(); 2427 fail("next() should have thrown an exception after a Map remove"); 2428 } catch (ConcurrentModificationException ignored) {} 2429 2430 m.put(node.getKey(), node); 2431 2432 iter = s.iterator(); 2433 count = 0; 2434 2435 boolean terminated = false; 2436 2437 try { 2438 while (true) { 2439 iter.next(); 2440 2441 ++count; 2442 } 2443 } catch (NoSuchElementException ignored) { 2444 terminated = true; 2445 } 2446 2447 assertTrue(terminated); 2448 assertEquals(m.size(), count); 2449 2450 iter = s.iterator(); 2451 2452 try { 2453 iter.remove(); 2454 fail("Should have thrown exception"); 2455 } catch (IllegalStateException ignored) {} 2456 2457 iter = s.iterator(); 2458 2459 iter.next(); 2460 2461 LocalTestNode node3 = new LocalTestNode(-3); 2462 2463 m.put(node3.getKey(), node3); 2464 2465 try { 2466 iter.remove(); 2467 fail("should have thrown exception"); 2468 } catch (ConcurrentModificationException ignored) {} 2469 2470 int removalCount = 0; 2471 int when = m.size() / 2; 2472 int timer = 0; 2473 2474 for (iter = s.iterator(); iter.hasNext(); ) { 2475 iter.next(); 2476 2477 if (timer == when) { 2478 try { 2479 iter.remove(); 2480 2481 ++removalCount; 2482 2483 iter.remove(); 2484 fail("2nd remove should have failed"); 2485 } catch (IllegalStateException ignored) { 2486 assertEquals(1, removalCount); 2487 } 2488 } 2489 2490 timer++; 2491 } 2492 2493 assertEquals(1, removalCount); 2494 2495 Iterator iter2 = s.iterator(); 2496 2497 try { 2498 iter2.remove(); 2499 fail("Should have thrown exception"); 2500 } catch (IllegalStateException ignored) {} 2501 2502 iter2 = s.iterator(); 2503 2504 while (iter2.hasNext()) { 2505 iter2.next(); 2506 } 2507 2508 LocalTestNode node4 = new LocalTestNode(-4); 2509 2510 m.put(node4.getKey(), node4); 2511 2512 try { 2513 iter2.remove(); 2514 fail("should have thrown exception"); 2515 } catch (ConcurrentModificationException ignored) {} 2516 2517 Object [] a1 = s.toArray(); 2518 2519 assertEquals(s.size(), a1.length); 2520 2521 if (a1.length > 1) { 2522 Map.Entry first = (Map.Entry ) a1[0]; 2523 2524 for (int k = 1; k < a1.length; k++) { 2525 Map.Entry second = (Map.Entry ) a1[k]; 2526 2527 assertTrue(((Comparable ) first.getKey()) 2528 .compareTo((Comparable ) second.getKey()) < 0); 2529 2530 first = second; 2531 } 2532 2533 iter = s.iterator(); 2534 first = (Map.Entry ) iter.next(); 2535 2536 for (; iter.hasNext(); ) { 2537 Map.Entry second = (Map.Entry ) iter.next(); 2538 2539 assertTrue(((Comparable ) first.getKey()) 2540 .compareTo((Comparable ) second.getKey()) < 0); 2541 2542 first = second; 2543 } 2544 } 2545 2546 try { 2547 Integer array2[] = (Integer []) s.toArray(new Integer [0]); 2548 2549 if (s.size() != 0) { 2550 fail("should have caught exception creating an invalid array"); 2551 } 2552 } catch (ArrayStoreException ignored) {} 2553 2554 Map.Entry array2[] = (Map.Entry []) s.toArray(new Map.Entry [0]); 2555 Map.Entry array3[] = (Map.Entry []) s.toArray(new Map.Entry [s.size()]); 2556 2557 if (array3.length > 1) { 2558 Comparable first = (Comparable ) ((Map.Entry ) array3[0]).getKey(); 2559 2560 for (int k = 1; k < array3.length; k++) { 2561 Comparable second = 2562 (Comparable ) ((Map.Entry ) array3[k]).getKey(); 2563 2564 assertTrue(first.compareTo(second) < 0); 2565 2566 first = second; 2567 } 2568 } 2569 2570 try { 2571 s.add(node.getKey()); 2572 fail("should have thrown an exception"); 2573 } catch (UnsupportedOperationException ignored) {} 2574 2575 assertTrue(!s.equals(null)); 2576 assertEquals("SetEquality 1", s, s); 2577 2578 Set hs = new HashSet (s); 2579 2580 assertEquals("SetEquality 2", s, hs); 2581 assertEquals("SetEquality 3", hs, s); 2582 assertEquals(s.hashCode(), hs.hashCode()); 2583 } 2584 2585 private void testEntrySetByValue(DoubleOrderedMap m) { 2586 2587 Set s = m.entrySetByValue(); 2588 2589 assertEquals(m.size(), s.size()); 2590 assertEquals(m.isEmpty(), s.isEmpty()); 2591 2592 LocalTestNode node = new LocalTestNode(-1); 2593 2594 m.put(node.getKey(), node); 2595 assertEquals(m.size(), s.size()); 2596 assertEquals(m.isEmpty(), s.isEmpty()); 2597 m.remove(node.getKey()); 2598 assertEquals(m.size(), s.size()); 2599 assertEquals(m.isEmpty(), s.isEmpty()); 2600 2601 int count = 0; 2602 2603 for (Iterator iter = s.iterator(); iter.hasNext(); ) { 2604 iter.next(); 2605 2606 ++count; 2607 } 2608 2609 assertEquals(s.size(), count); 2610 2611 LocalTestNode node2 = new LocalTestNode(-2); 2612 2613 if (m.size() == 0) { 2614 m.put(node2.getKey(), node2); 2615 } 2616 2617 Iterator iter = s.iterator(); 2618 2619 m.put(node.getKey(), node); 2620 2621 try { 2622 iter.next(); 2623 fail("next() should have thrown an exception after a put"); 2624 } catch (ConcurrentModificationException ignored) {} 2625 2626 m.remove(node2.getKey()); 2627 2628 iter = s.iterator(); 2629 2630 m.remove(node.getKey()); 2631 2632 try { 2633 iter.next(); 2634 fail("next() should have thrown an exception after a Map remove"); 2635 } catch (ConcurrentModificationException ignored) {} 2636 2637 m.put(node.getKey(), node); 2638 2639 iter = s.iterator(); 2640 count = 0; 2641 2642 boolean terminated = false; 2643 2644 try { 2645 while (true) { 2646 iter.next(); 2647 2648 ++count; 2649 } 2650 } catch (NoSuchElementException ignored) { 2651 terminated = true; 2652 } 2653 2654 assertTrue(terminated); 2655 assertEquals(m.size(), count); 2656 2657 iter = s.iterator(); 2658 2659 try { 2660 iter.remove(); 2661 fail("Should have thrown exception"); 2662 } catch (IllegalStateException ignored) {} 2663 2664 iter = s.iterator(); 2665 2666 iter.next(); 2667 2668 LocalTestNode node3 = new LocalTestNode(-3); 2669 2670 m.put(node3.getKey(), node3); 2671 2672 try { 2673 iter.remove(); 2674 fail("should have thrown exception"); 2675 } catch (ConcurrentModificationException ignored) {} 2676 2677 int removalCount = 0; 2678 int when = m.size() / 2; 2679 int timer = 0; 2680 2681 for (iter = s.iterator(); iter.hasNext(); ) { 2682 iter.next(); 2683 2684 if (timer == when) { 2685 try { 2686 iter.remove(); 2687 2688 ++removalCount; 2689 2690 iter.remove(); 2691 fail("2nd remove should have failed"); 2692 } catch (IllegalStateException ignored) { 2693 assertEquals(1, removalCount); 2694 } 2695 } 2696 2697 timer++; 2698 } 2699 2700 assertEquals(1, removalCount); 2701 2702 Iterator iter2 = s.iterator(); 2703 2704 try { 2705 iter2.remove(); 2706 fail("Should have thrown exception"); 2707 } catch (IllegalStateException ignored) {} 2708 2709 iter2 = s.iterator(); 2710 2711 while (iter2.hasNext()) { 2712 iter2.next(); 2713 } 2714 2715 LocalTestNode node4 = new LocalTestNode(-4); 2716 2717 m.put(node4.getKey(), node4); 2718 2719 try { 2720 iter2.remove(); 2721 fail("should have thrown exception"); 2722 } catch (ConcurrentModificationException ignored) {} 2723 2724 Object [] a1 = s.toArray(); 2725 2726 assertEquals(s.size(), a1.length); 2727 2728 if (a1.length > 1) { 2729 Map.Entry first = (Map.Entry ) a1[0]; 2730 2731 for (int k = 1; k < a1.length; k++) { 2732 Map.Entry second = (Map.Entry ) a1[k]; 2733 2734 assertTrue(((Comparable ) first.getKey()) 2735 .compareTo((Comparable ) second.getKey()) < 0); 2736 2737 first = second; 2738 } 2739 2740 iter = s.iterator(); 2741 first = (Map.Entry ) iter.next(); 2742 2743 for (; iter.hasNext(); ) { 2744 Map.Entry second = (Map.Entry ) iter.next(); 2745 2746 assertTrue(((Comparable ) first.getKey()) 2747 .compareTo((Comparable ) second.getKey()) < 0); 2748 2749 first = second; 2750 } 2751 } 2752 2753 try { 2754 Integer array2[] = (Integer []) s.toArray(new Integer [0]); 2755 2756 if (s.size() != 0) { 2757 fail("should have caught exception creating an invalid array"); 2758 } 2759 } catch (ArrayStoreException ignored) {} 2760 2761 Map.Entry array2[] = (Map.Entry []) s.toArray(new Map.Entry [0]); 2762 Map.Entry array3[] = (Map.Entry []) s.toArray(new Map.Entry [s.size()]); 2763 2764 if (array3.length > 1) { 2765 Comparable first = 2766 (Comparable ) ((Map.Entry ) array3[0]).getValue(); 2767 2768 for (int k = 1; k < array3.length; k++) { 2769 Comparable second = 2770 (Comparable ) ((Map.Entry ) array3[k]).getValue(); 2771 2772 assertTrue(first.compareTo(second) < 0); 2773 2774 first = second; 2775 } 2776 } 2777 2778 try { 2779 s.add(node.getKey()); 2780 fail("should have thrown an exception"); 2781 } catch (UnsupportedOperationException ignored) {} 2782 2783 assertTrue(!s.equals(null)); 2784 assertEquals("SetEquality 1", s, s); 2785 2786 Set hs = new HashSet (s); 2787 2788 assertEquals("SetEquality 2", s, hs); 2789 assertEquals("SetEquality 3", hs, s); 2790 assertEquals(s.hashCode(), hs.hashCode()); 2791 } 2792 2793 private LocalTestNode[] makeLocalNodes() { 2794 2795 LocalTestNode nodes[] = new LocalTestNode[1023]; 2796 2797 for (int k = 0; k < nodes.length; k++) { 2798 nodes[k] = new LocalTestNode(k); 2799 } 2800 2801 return nodes; 2802 } 2803 2804 2805 2806 2811 public static void main(final String unusedArgs[]) { 2812 junit.textui.TestRunner.run(TestDoubleOrderedMap.class); 2813 } 2814 2815} 2816 | Popular Tags |