1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
|
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.*;
import java.util.*;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
/*
* @test
* @bug 8266571
* @summary Basic tests for SequencedMap
* @modules java.base/java.util:open
* @build SimpleSortedMap
* @run testng BasicMap
*/
public class BasicMap {
// ========== Data Providers ==========
static final Class<? extends Throwable> CCE = ClassCastException.class;
static final Class<? extends Throwable> NSEE = NoSuchElementException.class;
static final Class<? extends Throwable> UOE = UnsupportedOperationException.class;
static final List<Map.Entry<String, Integer>> ORIGINAL =
List.of(Map.entry("a", 1),
Map.entry("b", 2),
Map.entry("c", 3),
Map.entry("d", 4),
Map.entry("e", 5));
static <M extends SequencedMap<String, Integer>>
M load(M map, List<Map.Entry<String, Integer>> mappings) {
for (var e : mappings)
map.put(e.getKey(), e.getValue());
return map;
}
static NavigableMap<String, Integer> cknav(NavigableMap<String, Integer> map) {
return Collections.checkedNavigableMap(map, String.class, Integer.class);
}
static SortedMap<String, Integer> cksorted(SortedMap<String, Integer> map) {
return Collections.checkedSortedMap(map, String.class, Integer.class);
}
static SequencedMap<String, Integer> umap(SequencedMap<String, Integer> map) {
return Collections.unmodifiableSequencedMap(map);
}
static SortedMap<String, Integer> usorted(SortedMap<String, Integer> map) {
return Collections.unmodifiableSortedMap(map);
}
static NavigableMap<String, Integer> unav(NavigableMap<String, Integer> map) {
return Collections.unmodifiableNavigableMap(map);
}
@DataProvider(name="all")
public Iterator<Object[]> all() {
var result = new ArrayList<Object[]>();
populated().forEachRemaining(result::add);
empties().forEachRemaining(result::add);
return result.iterator();
}
@DataProvider(name="populated")
public Iterator<Object[]> populated() {
return Arrays.asList(
new Object[] { "LinkedHashMap", load(new LinkedHashMap<>(), ORIGINAL), ORIGINAL },
new Object[] { "SimpleSortedMap", load(new SimpleSortedMap<>(), ORIGINAL), ORIGINAL },
new Object[] { "TreeMap", load(new TreeMap<>(), ORIGINAL), ORIGINAL },
new Object[] { "UnmodMap", umap(load(new LinkedHashMap<>(), ORIGINAL)), ORIGINAL }
).iterator();
}
@DataProvider(name="empties")
public Iterator<Object[]> empties() {
return Arrays.asList(
new Object[] { "EmptyNavigableMap", Collections.emptyNavigableMap(), List.of() },
new Object[] { "EmptySortedMap", Collections.emptySortedMap(), List.of() },
new Object[] { "LinkedHashMap", new LinkedHashMap<>(), List.of() },
new Object[] { "SimpleSortedMap", new SimpleSortedMap<>(), List.of() },
new Object[] { "TreeMap", new TreeMap<>(), List.of() },
new Object[] { "UnmodMap", umap(new LinkedHashMap<>()), List.of() }
).iterator();
}
@DataProvider(name="polls")
public Iterator<Object[]> polls() {
return Arrays.asList(
new Object[] { "LinkedHashMap", load(new LinkedHashMap<>(), ORIGINAL), ORIGINAL },
new Object[] { "SimpleSortedMap", load(new SimpleSortedMap<>(), ORIGINAL), ORIGINAL },
new Object[] { "TreeMap", load(new TreeMap<>(), ORIGINAL), ORIGINAL }
).iterator();
}
@DataProvider(name="emptyPolls")
public Iterator<Object[]> emptyPolls() {
return Arrays.asList(
new Object[] { "LinkedHashMap", new LinkedHashMap<>(), List.of() },
new Object[] { "SimpleSortedMap", new SimpleSortedMap<>(), List.of() },
new Object[] { "TreeMap", new TreeMap<>(), List.of() }
).iterator();
}
@DataProvider(name="puts")
public Iterator<Object[]> puts() {
return Arrays.<Object[]>asList(
new Object[] { "LinkedHashMap", load(new LinkedHashMap<>(), ORIGINAL), ORIGINAL }
).iterator();
}
@DataProvider(name="putUnpositioned")
public Iterator<Object[]> putUnpositioned() {
return Arrays.asList(
new Object[] { "LinkedHashMap", false, load(new LinkedHashMap<>(), ORIGINAL), ORIGINAL },
new Object[] { "LinkedHashMap", true, load(new LinkedHashMap<>(), ORIGINAL), ORIGINAL }
).iterator();
}
@DataProvider(name="putThrows")
public Iterator<Object[]> putThrows() {
return Arrays.asList(
new Object[] { "SimpleSortedMap", load(new SimpleSortedMap<>(), ORIGINAL), ORIGINAL },
new Object[] { "TreeMap", load(new TreeMap<>(), ORIGINAL), ORIGINAL }
).iterator();
}
@DataProvider(name="serializable")
public Iterator<Object[]> serializable() {
return Arrays.asList(
new Object[] { "LinkedHashMap", load(new LinkedHashMap<>(), ORIGINAL), ORIGINAL },
new Object[] { "TreeMap", load(new TreeMap<>(), ORIGINAL), ORIGINAL },
new Object[] { "UnmodMap", umap(load(new LinkedHashMap<>(), ORIGINAL)), ORIGINAL }
).iterator();
}
@DataProvider(name="notSerializable")
public Iterator<Object[]> notSerializable() {
return Arrays.asList(
new Object[] { "LinkedHashMap", load(new LinkedHashMap<>(), ORIGINAL).reversed() },
new Object[] { "UnmodMap", umap(load(new LinkedHashMap<>(), ORIGINAL)).reversed() }
).iterator();
}
@DataProvider(name="doubleReverse")
public Iterator<Object[]> doubleReverse() {
return Arrays.<Object[]>asList(
new Object[] { "LinkedHashMap", load(new LinkedHashMap<>(), ORIGINAL) }
).iterator();
}
@DataProvider(name="unmodifiable")
public Iterator<Object[]> unmodifible() {
return Arrays.<Object[]>asList(
new Object[] { "UnmodMap", umap(load(new LinkedHashMap<>(), ORIGINAL)), ORIGINAL },
new Object[] { "UnmodNav", unav(load(new TreeMap<>(), ORIGINAL)), ORIGINAL },
new Object[] { "UnmodSorted", usorted(load(new TreeMap<>(), ORIGINAL)), ORIGINAL }
).iterator();
}
@DataProvider(name="checked")
public Iterator<Object[]> checked() {
return Arrays.<Object[]>asList(
new Object[] { "ChkNav", cknav(load(new TreeMap<>(), ORIGINAL)), ORIGINAL },
new Object[] { "ChkSorted", cksorted(load(new TreeMap<>(), ORIGINAL)), ORIGINAL }
).iterator();
}
// mode bit tests
boolean reverseMap(int mode) { return (mode & 1) != 0; }
boolean reverseView(int mode) { return (mode & 2) != 0; }
boolean callLast(int mode) { return (mode & 4) != 0; }
boolean refLast(int mode) { return reverseMap(mode) ^ reverseView(mode) ^ callLast(mode); }
/**
* Generate cases for testing the removeFirst and removeLast methods of map views. For each
* different map implementation, generate 8 cases from the three bits of the testing mode
* int value:
*
* (bit 1) if true, the backing map is reversed
* (bit 2) if true, the view is reversed
* (bit 4) if true, the last element of the view is to be removed, otherwise the first
*
* The three bits XORed together (by refLast(), above) indicate (if true) the last
* or (if false) the first element of the reference entry list is to be removed.
*
* @return the generated cases
*/
@DataProvider(name="viewRemoves")
public Iterator<Object[]> viewRemoves() {
var cases = new ArrayList<Object[]>();
for (int mode = 0; mode < 8; mode++) {
cases.addAll(Arrays.asList(
new Object[] { "LinkedHashMap", mode, load(new LinkedHashMap<>(), ORIGINAL), ORIGINAL },
new Object[] { "SimpleSortedMap", mode, load(new SimpleSortedMap<>(), ORIGINAL), ORIGINAL },
new Object[] { "TreeMap", mode, load(new TreeMap<>(), ORIGINAL), ORIGINAL }
));
}
return cases.iterator();
}
@DataProvider(name="emptyViewRemoves")
public Iterator<Object[]> emptyViewRemoves() {
var cases = new ArrayList<Object[]>();
for (int mode = 0; mode < 8; mode++) {
cases.addAll(Arrays.asList(
new Object[] { "LinkedHashMap", mode, new LinkedHashMap<>(), List.of() },
new Object[] { "SimpleSortedMap", mode, new SimpleSortedMap<>(), List.of() },
new Object[] { "TreeMap", mode, new TreeMap<>(), List.of() }
));
}
return cases.iterator();
}
@DataProvider(name="viewAddThrows")
public Iterator<Object[]> viewAddThrows() {
var cases = new ArrayList<Object[]>();
for (int mode = 0; mode < 8; mode++) {
cases.addAll(Arrays.asList(
new Object[] { "LinkedHashMap", mode, load(new LinkedHashMap<>(), ORIGINAL), ORIGINAL },
new Object[] { "SimpleSortedMap", mode, load(new SimpleSortedMap<>(), ORIGINAL), ORIGINAL },
new Object[] { "TreeMap", mode, load(new TreeMap<>(), ORIGINAL), ORIGINAL }
));
}
return cases.iterator();
}
@DataProvider(name="nullableEntries")
public Iterator<Object[]> nullableEntries() {
return Arrays.asList(
new Object[] { "firstEntry" },
new Object[] { "lastEntry" },
new Object[] { "pollFirstEntry" },
new Object[] { "pollLastEntry" }
).iterator();
}
// ========== Assertions ==========
/**
* Basic checks over the contents of a SequencedMap, compared to a reference List of entries,
* in one direction.
*
* @param map the SequencedMap under test
* @param ref the reference list of entries
*/
public void checkContents1(SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> ref) {
var list1 = new ArrayList<Map.Entry<String, Integer>>();
map.forEach((k, v) -> list1.add(Map.entry(k, v)));
assertEquals(list1, ref);
assertEquals(map.size(), ref.size());
assertEquals(map.isEmpty(), ref.isEmpty());
for (var e : ref) {
assertTrue(map.containsKey(e.getKey()));
assertTrue(map.containsValue(e.getValue()));
assertEquals(map.get(e.getKey()), e.getValue());
}
}
public void checkContents(SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> ref) {
checkContents1(map, ref);
var rref = new ArrayList<>(ref);
Collections.reverse(rref);
var rmap = map.reversed();
checkContents1(rmap, rref);
var rrmap = rmap.reversed();
checkContents1(rrmap, ref);
}
/**
* Check the entrySet, keySet, or values view of a SequencedMap in one direction. The view
* collection is ordered even though the collection type is not sequenced.
*
* @param <T> the element type of the view
* @param mapView the actual map view
* @param expElements list of the expected elements
*/
public <T> void checkView1(Collection<T> mapView, List<T> expElements) {
var list1 = new ArrayList<T>();
for (var k : mapView)
list1.add(k);
assertEquals(list1, expElements);
var list2 = new ArrayList<T>();
mapView.forEach(list2::add);
assertEquals(list2, expElements);
var list3 = Arrays.asList(mapView.toArray());
assertEquals(list3, expElements);
var list4 = Arrays.asList(mapView.toArray(new Object[0]));
assertEquals(list4, expElements);
var list5 = Arrays.asList(mapView.toArray(Object[]::new));
assertEquals(list5, expElements);
var list6 = mapView.stream().toList();
assertEquals(list6, expElements);
var list7 = mapView.parallelStream().toList();
assertEquals(list7, expElements);
assertEquals(mapView.size(), expElements.size());
assertEquals(mapView.isEmpty(), expElements.isEmpty());
for (var k : expElements) {
assertTrue(mapView.contains(k));
}
var it = mapView.iterator();
if (expElements.isEmpty()) {
assertFalse(it.hasNext());
} else {
assertTrue(it.hasNext());
assertEquals(it.next(), expElements.get(0));
}
}
/**
* Check the sequenced entrySet, keySet, or values view of a SequencedMap in one direction.
*
* @param <T> the element type of the view
* @param mapView the actual map view
* @param expElements list of the expected elements
*/
public <T> void checkSeqView1(SequencedCollection<T> mapView, List<T> expElements) {
checkView1(mapView, expElements);
if (expElements.isEmpty()) {
assertThrows(NoSuchElementException.class, () -> mapView.getFirst());
assertThrows(NoSuchElementException.class, () -> mapView.getLast());
} else {
assertEquals(mapView.getFirst(), expElements.get(0));
assertEquals(mapView.getLast(), expElements.get(expElements.size() - 1));
}
}
/**
* Check the keySet and sequencedKeySet views of a map. It's possible to unify this with
* the corresponding checks for values and entrySet views, but doing this adds a bunch
* of generics and method references that tend to obscure more than they help.
*
* @param map the SequencedMap under test
* @param refEntries expected contents of the map
*/
public void checkKeySet(SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> refEntries) {
List<String> refKeys = refEntries.stream().map(Map.Entry::getKey).toList();
List<String> rrefKeys = new ArrayList<>(refKeys);
Collections.reverse(rrefKeys);
SequencedMap<String, Integer> rmap = map.reversed();
checkView1(map.keySet(), refKeys);
checkSeqView1(map.sequencedKeySet(), refKeys);
checkSeqView1(map.sequencedKeySet().reversed(), rrefKeys);
checkView1(rmap.keySet(), rrefKeys);
checkSeqView1(rmap.sequencedKeySet(), rrefKeys);
checkSeqView1(rmap.sequencedKeySet().reversed(), refKeys);
checkView1(rmap.reversed().keySet(), refKeys);
checkSeqView1(rmap.reversed().sequencedKeySet(), refKeys);
checkSeqView1(rmap.reversed().sequencedKeySet().reversed(), rrefKeys);
assertEquals(map.keySet().hashCode(), rmap.keySet().hashCode());
assertEquals(map.keySet().hashCode(), map.sequencedKeySet().hashCode());
assertEquals(rmap.keySet().hashCode(), rmap.sequencedKeySet().hashCode());
// Don't use assertEquals(), as we really want to test the equals() methods.
assertTrue(map.keySet().equals(map.sequencedKeySet()));
assertTrue(map.sequencedKeySet().equals(map.keySet()));
assertTrue(rmap.keySet().equals(map.sequencedKeySet()));
assertTrue(rmap.sequencedKeySet().equals(map.keySet()));
assertTrue(map.keySet().equals(rmap.sequencedKeySet()));
assertTrue(map.sequencedKeySet().equals(rmap.keySet()));
assertTrue(rmap.keySet().equals(rmap.sequencedKeySet()));
assertTrue(rmap.sequencedKeySet().equals(rmap.keySet()));
}
/**
* Check the values and sequencedValues views of a map.
*
* @param map the SequencedMap under test
* @param refEntries expected contents of the map
*/
public void checkValues(SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> refEntries) {
List<Integer> refValues = refEntries.stream().map(Map.Entry::getValue).toList();
List<Integer> rrefValues = new ArrayList<>(refValues);
Collections.reverse(rrefValues);
SequencedMap<String, Integer> rmap = map.reversed();
checkView1(map.values(), refValues);
checkSeqView1(map.sequencedValues(), refValues);
checkSeqView1(map.sequencedValues().reversed(), rrefValues);
checkView1(rmap.values(), rrefValues);
checkSeqView1(rmap.sequencedValues(), rrefValues);
checkSeqView1(rmap.sequencedValues().reversed(), refValues);
checkView1(rmap.reversed().values(), refValues);
checkSeqView1(rmap.reversed().sequencedValues(), refValues);
checkSeqView1(rmap.reversed().sequencedValues().reversed(), rrefValues);
// No assertions over hashCode(), as Collection inherits Object.hashCode
// which is usually but not guaranteed to give unequal results.
// It's permissible for an implementation to return the same instance for values()
// as for sequencedValues(). Either they're the same instance, or they must be
// unequal, because distinct collections should always be unequal.
var v = map.values();
var sv = map.sequencedValues();
assertTrue((v == sv) || ! (v.equals(sv) || sv.equals(v)));
var rv = rmap.values();
var rsv = rmap.sequencedValues();
assertTrue((rv == rsv) || ! (rv.equals(rsv) || rsv.equals(rv)));
}
/**
* Check the entrySet and sequencedEntrySet views of a map.
*
* @param map the SequencedMap under test
* @param refEntries expected contents of the map
*/
public void checkEntrySet(SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> refEntries) {
List<Map.Entry<String, Integer>> rref = new ArrayList<>(refEntries);
Collections.reverse(rref);
SequencedMap<String, Integer> rmap = map.reversed();
checkView1(map.entrySet(), refEntries);
checkSeqView1(map.sequencedEntrySet(), refEntries);
checkSeqView1(map.sequencedEntrySet().reversed(), rref);
checkView1(rmap.entrySet(), rref);
checkSeqView1(rmap.sequencedEntrySet(), rref);
checkSeqView1(rmap.sequencedEntrySet().reversed(), refEntries);
checkView1(rmap.reversed().entrySet(), refEntries);
checkSeqView1(rmap.reversed().sequencedEntrySet(), refEntries);
checkSeqView1(rmap.reversed().sequencedEntrySet().reversed(), rref);
assertEquals(map.entrySet().hashCode(), rmap.entrySet().hashCode());
assertEquals(map.entrySet().hashCode(), map.sequencedEntrySet().hashCode());
assertEquals(map.sequencedEntrySet().hashCode(), map.entrySet().hashCode());
assertTrue(map.entrySet().equals(map.sequencedEntrySet()));
assertTrue(map.sequencedEntrySet().equals(map.entrySet()));
assertTrue(rmap.entrySet().equals(map.sequencedEntrySet()));
assertTrue(rmap.sequencedEntrySet().equals(map.entrySet()));
assertTrue(map.entrySet().equals(rmap.sequencedEntrySet()));
assertTrue(map.sequencedEntrySet().equals(rmap.entrySet()));
assertTrue(rmap.entrySet().equals(rmap.sequencedEntrySet()));
assertTrue(rmap.sequencedEntrySet().equals(rmap.entrySet()));
}
/**
* Test attempted modifications to unmodifiable map views. The only mutating operation
* map views can support is removal.
*
* @param <T> element type of the map view
* @param view the map view
*/
public <T> void checkUnmodifiableView(Collection<T> view) {
assertThrows(UOE, () -> view.clear());
assertThrows(UOE, () -> { var it = view.iterator(); it.next(); it.remove(); });
assertThrows(UOE, () -> { var t = view.iterator().next(); view.remove(t); });
// TODO these ops should throw unconditionally, but they don't in some implementations
// assertThrows(UOE, () -> view.removeAll(List.of()));
// assertThrows(UOE, () -> view.removeIf(x -> false));
// assertThrows(UOE, () -> view.retainAll(view));
assertThrows(UOE, () -> view.removeAll(view));
assertThrows(UOE, () -> view.removeIf(x -> true));
assertThrows(UOE, () -> view.retainAll(List.of()));
}
/**
* Test removal methods on unmodifiable sequenced map views.
*
* @param <T> element type of the map view
* @param view the map view
*/
public <T> void checkUnmodifiableSeqView(SequencedCollection<T> view) {
checkUnmodifiableView(view);
assertThrows(UOE, () -> view.removeFirst());
assertThrows(UOE, () -> view.removeLast());
var rview = view.reversed();
checkUnmodifiableView(rview);
assertThrows(UOE, () -> rview.removeFirst());
assertThrows(UOE, () -> rview.removeLast());
}
public void checkUnmodifiableEntry(SequencedMap<String, Integer> map) {
assertThrows(UOE, () -> { map.firstEntry().setValue(99); });
assertThrows(UOE, () -> { map.lastEntry().setValue(99); });
assertThrows(UOE, () -> { map.sequencedEntrySet().getFirst().setValue(99); });
assertThrows(UOE, () -> { map.sequencedEntrySet().getLast().setValue(99); });
assertThrows(UOE, () -> { map.sequencedEntrySet().reversed().getFirst().setValue(99); });
assertThrows(UOE, () -> { map.sequencedEntrySet().reversed().getLast().setValue(99); });
}
public void checkUnmodifiable1(SequencedMap<String, Integer> map) {
assertThrows(UOE, () -> map.putFirst("x", 99));
assertThrows(UOE, () -> map.putLast("x", 99));
assertThrows(UOE, () -> { map.pollFirstEntry(); });
assertThrows(UOE, () -> { map.pollLastEntry(); });
checkUnmodifiableEntry(map);
checkUnmodifiableView(map.keySet());
checkUnmodifiableView(map.values());
checkUnmodifiableView(map.entrySet());
checkUnmodifiableSeqView(map.sequencedKeySet());
checkUnmodifiableSeqView(map.sequencedValues());
checkUnmodifiableSeqView(map.sequencedEntrySet());
}
public void checkUnmodifiable(SequencedMap<String, Integer> map) {
checkUnmodifiable1(map);
checkUnmodifiable1(map.reversed());
}
// The putFirst/putLast operations aren't tested here, because the only instances of
// checked, sequenced maps are SortedMap and NavigableMap, which don't support them.
public void checkChecked(SequencedMap<String, Integer> map) {
SequencedMap<Object, Object> objMap = (SequencedMap<Object, Object>)(SequencedMap)map;
assertThrows(CCE, () -> { objMap.put(new Object(), 99); });
assertThrows(CCE, () -> { objMap.put("x", new Object()); });
assertThrows(CCE, () -> { objMap.sequencedEntrySet().getFirst().setValue(new Object()); });
assertThrows(CCE, () -> { objMap.sequencedEntrySet().reversed().getFirst().setValue(new Object()); });
assertThrows(CCE, () -> { objMap.reversed().put(new Object(), 99); });
assertThrows(CCE, () -> { objMap.reversed().put("x", new Object()); });
assertThrows(CCE, () -> { objMap.reversed().sequencedEntrySet().getFirst().setValue(new Object()); });
assertThrows(CCE, () -> { objMap.reversed().sequencedEntrySet().reversed().getFirst().setValue(new Object()); });
}
public void checkEntry(Map.Entry<String, Integer> entry, String key, Integer value) {
assertEquals(entry.getKey(), key);
assertEquals(entry.getValue(), value);
}
// ========== Tests ==========
@Test(dataProvider="all")
public void testFundamentals(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> ref) {
checkContents(map, ref);
checkEntrySet(map, ref);
checkKeySet(map, ref);
checkValues(map, ref);
}
@Test(dataProvider="populated")
public void testFirstEntry(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> ref) {
assertEquals(map.firstEntry(), ref.get(0));
assertEquals(map.reversed().firstEntry(), ref.get(ref.size() - 1));
assertThrows(UOE, () -> { map.firstEntry().setValue(99); });
assertThrows(UOE, () -> { map.reversed().firstEntry().setValue(99); });
checkContents(map, ref);
}
@Test(dataProvider="populated")
public void testLastEntry(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> ref) {
assertEquals(map.lastEntry(), ref.get(ref.size() - 1));
assertEquals(map.reversed().lastEntry(), ref.get(0));
assertThrows(UOE, () -> { map.lastEntry().setValue(99); });
assertThrows(UOE, () -> { map.reversed().lastEntry().setValue(99); });
checkContents(map, ref);
}
@Test(dataProvider="empties")
public void testEmptyFirstEntry(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> ref) {
assertNull(map.firstEntry());
assertNull(map.reversed().firstEntry());
checkContents(map, ref);
}
@Test(dataProvider="empties")
public void testEmptyLastEntry(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> ref) {
assertNull(map.lastEntry());
assertNull(map.reversed().lastEntry());
checkContents(map, ref);
}
@Test(dataProvider="puts")
public void testPutFirst(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
ref.add(0, Map.entry("x", 99));
map.putFirst("x", 99);
checkContents(map, ref);
}
@Test(dataProvider="puts")
public void testPutFirstRev(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
ref.add(Map.entry("x", 99));
map.reversed().putFirst("x", 99);
checkContents(map, ref);
}
@Test(dataProvider="puts")
public void testPutLast(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
ref.add(Map.entry("x", 99));
map.putLast("x", 99);
checkContents(map, ref);
}
@Test(dataProvider="puts")
public void testPutLastRev(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
ref.add(0, Map.entry("x", 99));
map.reversed().putLast("x", 99);
checkContents(map, ref);
}
@Test(dataProvider="putUnpositioned")
public void testUnposPut(String label, boolean rev, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
ref.add(Map.entry("x", 99));
(rev ? map.reversed() : map).put("x", 99);
checkContents(map, ref);
}
@Test(dataProvider="putUnpositioned")
public void testUnposPutAll(String label, boolean rev, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
ref.add(Map.entry("x", 99));
(rev ? map.reversed() : map).putAll(Map.of("x", 99));
checkContents(map, ref);
}
@Test(dataProvider="putUnpositioned")
public void testUnposPutIfAbsent(String label, boolean rev, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
ref.add(Map.entry("x", 99));
(rev ? map.reversed() : map).putIfAbsent("x", 99);
checkContents(map, ref);
}
@Test(dataProvider="putUnpositioned")
public void testUnposCompute(String label, boolean rev, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
ref.add(Map.entry("x", 99));
(rev ? map.reversed() : map).compute("x", (k, v) -> 99);
checkContents(map, ref);
}
@Test(dataProvider="putUnpositioned")
public void testUnposComputeIfAbsent(String label, boolean rev, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
ref.add(Map.entry("x", 99));
(rev ? map.reversed() : map).computeIfAbsent("x", k -> 99);
checkContents(map, ref);
}
@Test(dataProvider="putUnpositioned")
public void testUnposMerge(String label, boolean rev, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
ref.add(Map.entry("x", 99));
(rev ? map.reversed() : map).merge("x", 99, /*unused*/ (k, v) -> -1);
checkContents(map, ref);
}
@Test(dataProvider="putThrows")
public void testPutThrows(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
assertThrows(UOE, () -> map.putFirst("x", 99));
assertThrows(UOE, () -> map.putLast("x", 99));
assertThrows(UOE, () -> map.reversed().putFirst("x", 99));
assertThrows(UOE, () -> map.reversed().putLast("x", 99));
checkContents(map, baseref);
}
@Test(dataProvider="polls")
public void testPollFirst(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
var act = map.pollFirstEntry();
assertEquals(act, ref.remove(0));
assertThrows(UOE, () -> { act.setValue(99); });
checkContents(map, ref);
}
@Test(dataProvider="polls")
public void testPollFirstRev(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
var act = map.reversed().pollFirstEntry();
assertEquals(act, ref.remove(ref.size() - 1));
assertThrows(UOE, () -> { act.setValue(99); });
checkContents(map, ref);
}
@Test(dataProvider="polls")
public void testPollLast(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
var act = map.pollLastEntry();
assertEquals(act, ref.remove(ref.size() - 1));
assertThrows(UOE, () -> { act.setValue(99); });
checkContents(map, ref);
}
@Test(dataProvider="polls")
public void testPollLastRev(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
var act = map.reversed().pollLastEntry();
assertEquals(act, ref.remove(0));
assertThrows(UOE, () -> { act.setValue(99); });
checkContents(map, ref);
}
@Test(dataProvider="emptyPolls")
public void testEmptyPollFirst(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> ref) {
assertNull(map.pollFirstEntry());
assertNull(map.reversed().pollFirstEntry());
checkContents(map, ref);
}
@Test(dataProvider="emptyPolls")
public void testEmptyPollLast(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> ref) {
assertNull(map.pollLastEntry());
assertNull(map.reversed().pollLastEntry());
checkContents(map, ref);
}
@Test(dataProvider="serializable")
public void testSerializable(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> ref)
throws ClassNotFoundException, IOException
{
var baos = new ByteArrayOutputStream();
try (var oos = new ObjectOutputStream(baos)) {
oos.writeObject(map);
}
try (var bais = new ByteArrayInputStream(baos.toByteArray());
var ois = new ObjectInputStream(bais)) {
var map2 = (SequencedMap<String, Integer>) ois.readObject();
checkContents(map2, ref);
}
}
@Test(dataProvider="notSerializable")
public void testNotSerializable(String label, SequencedMap<String, Integer> map)
throws ClassNotFoundException, IOException
{
var baos = new ByteArrayOutputStream();
try (var oos = new ObjectOutputStream(baos)) {
assertThrows(ObjectStreamException.class, () -> oos.writeObject(map));
}
}
@Test(dataProvider="doubleReverse")
public void testDoubleReverse(String label, SequencedMap<String, Integer> map) {
var rrmap = map.reversed().reversed();
assertSame(rrmap, map);
}
@Test(dataProvider="unmodifiable")
public void testUnmodifiable(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> ref) {
checkUnmodifiable(map);
checkContents(map, ref);
}
@Test(dataProvider="checked")
public void testChecked(String label, SequencedMap<String, Integer> map, List<Map.Entry<String, Integer>> ref) {
checkChecked(map);
checkContents(map, ref);
}
/**
* Test that a removal from the sequenedKeySet view is properly reflected in the original
* backing map. The mode value indicates whether the backing map is reversed, whether the
* sequencedKeySet view is reversed, and whether the removeFirst or removeLast is called
* on the view. See the viewRemoves() dataProvider for details.
*
* @param label the implementation label
* @param mode reversed and first/last modes
* @param map the original map instance
* @param baseref reference contents of the original map
*/
@Test(dataProvider="viewRemoves")
public void testKeySetRemoves(String label,
int mode,
SequencedMap<String, Integer> map,
List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
var exp = (refLast(mode) ? ref.remove(ref.size() - 1) : ref.remove(0)).getKey();
var tempmap = reverseMap(mode) ? map.reversed() : map;
var keySet = reverseView(mode) ? tempmap.sequencedKeySet().reversed() : tempmap.sequencedKeySet();
var act = callLast(mode) ? keySet.removeLast() : keySet.removeFirst();
assertEquals(act, exp);
checkContents(map, ref);
}
// As above, but for the sequencedValues view.
@Test(dataProvider="viewRemoves")
public void testValuesRemoves(String label,
int mode,
SequencedMap<String, Integer> map,
List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
var exp = (refLast(mode) ? ref.remove(ref.size() - 1) : ref.remove(0)).getValue();
var tempmap = reverseMap(mode) ? map.reversed() : map;
var values = reverseView(mode) ? tempmap.sequencedValues().reversed() : tempmap.sequencedValues();
var act = callLast(mode) ? values.removeLast() : values.removeFirst();
assertEquals(act, exp);
checkContents(map, ref);
}
// As above, but for the sequencedEntrySet view.
@Test(dataProvider="viewRemoves")
public void testEntrySetRemoves(String label,
int mode,
SequencedMap<String, Integer> map,
List<Map.Entry<String, Integer>> baseref) {
var ref = new ArrayList<>(baseref);
var exp = refLast(mode) ? ref.remove(ref.size() - 1) : ref.remove(0);
var tempmap = reverseMap(mode) ? map.reversed() : map;
var entrySet = reverseView(mode) ? tempmap.sequencedEntrySet().reversed() : tempmap.sequencedEntrySet();
var act = callLast(mode) ? entrySet.removeLast() : entrySet.removeFirst();
assertEquals(act, exp);
checkContents(map, ref);
}
// As above, but for the sequencedKeySet of an empty map.
@Test(dataProvider="emptyViewRemoves")
public void testEmptyKeySetRemoves(String label,
int mode,
SequencedMap<String, Integer> map,
List<Map.Entry<String, Integer>> baseref) {
var tempmap = reverseMap(mode) ? map.reversed() : map;
var keySet = reverseView(mode) ? tempmap.sequencedKeySet().reversed() : tempmap.sequencedKeySet();
if (callLast(mode))
assertThrows(NSEE, () -> keySet.removeLast());
else
assertThrows(NSEE, () -> keySet.removeFirst());
checkContents(map, baseref);
}
// As above, but for the sequencedValues view.
@Test(dataProvider="emptyViewRemoves")
public void testEmptyValuesRemoves(String label,
int mode,
SequencedMap<String, Integer> map,
List<Map.Entry<String, Integer>> baseref) {
var tempmap = reverseMap(mode) ? map.reversed() : map;
var values = reverseView(mode) ? tempmap.sequencedValues().reversed() : tempmap.sequencedValues();
if (callLast(mode))
assertThrows(NSEE, () -> values.removeLast());
else
assertThrows(NSEE, () -> values.removeFirst());
checkContents(map, baseref);
}
// As above, but for the sequencedEntrySet view.
@Test(dataProvider="emptyViewRemoves")
public void testEmptyEntrySetRemoves(String label,
int mode,
SequencedMap<String, Integer> map,
List<Map.Entry<String, Integer>> baseref) {
var tempmap = reverseMap(mode) ? map.reversed() : map;
var entrySet = reverseView(mode) ? tempmap.sequencedEntrySet().reversed() : tempmap.sequencedEntrySet();
if (callLast(mode))
assertThrows(NSEE, () -> entrySet.removeLast());
else
assertThrows(NSEE, () -> entrySet.removeFirst());
checkContents(map, baseref);
}
// Test that addFirst/addLast on the sequencedKeySetView throw UnsupportedOperationException.
@Test(dataProvider="viewAddThrows")
public void testKeySetAddThrows(String label,
int mode,
SequencedMap<String, Integer> map,
List<Map.Entry<String, Integer>> baseref) {
var tempmap = reverseMap(mode) ? map.reversed() : map;
var keySet = reverseView(mode) ? tempmap.sequencedKeySet().reversed() : tempmap.sequencedKeySet();
if (callLast(mode))
assertThrows(UOE, () -> keySet.addLast("x"));
else
assertThrows(UOE, () -> keySet.addFirst("x"));
checkContents(map, baseref);
}
// As above, but for the sequencedValues view.
@Test(dataProvider="viewAddThrows")
public void testValuesAddThrows(String label,
int mode,
SequencedMap<String, Integer> map,
List<Map.Entry<String, Integer>> baseref) {
var tempmap = reverseMap(mode) ? map.reversed() : map;
var values = reverseView(mode) ? tempmap.sequencedValues().reversed() : tempmap.sequencedValues();
if (callLast(mode))
assertThrows(UOE, () -> values.addLast(99));
else
assertThrows(UOE, () -> values.addFirst(99));
checkContents(map, baseref);
}
// As above, but for the sequencedEntrySet view.
@Test(dataProvider="viewAddThrows")
public void testEntrySetAddThrows(String label,
int mode,
SequencedMap<String, Integer> map,
List<Map.Entry<String, Integer>> baseref) {
var tempmap = reverseMap(mode) ? map.reversed() : map;
var entrySet = reverseView(mode) ? tempmap.sequencedEntrySet().reversed() : tempmap.sequencedEntrySet();
if (callLast(mode))
assertThrows(UOE, () -> entrySet.addLast(Map.entry("x", 99)));
else
assertThrows(UOE, () -> entrySet.addFirst(Map.entry("x", 99)));
checkContents(map, baseref);
}
@Test(dataProvider="nullableEntries")
public void testNullableKeyValue(String mode) {
// TODO this relies on LHM to inherit SequencedMap default
// methods which are actually being tested here.
SequencedMap<String, Integer> map = new LinkedHashMap<>();
map.put(null, 1);
map.put("two", null);
switch (mode) {
case "firstEntry" -> checkEntry(map.firstEntry(), null, 1);
case "lastEntry" -> checkEntry(map.lastEntry(), "two", null);
case "pollFirstEntry" -> checkEntry(map.pollFirstEntry(), null, 1);
case "pollLastEntry" -> checkEntry(map.pollLastEntry(), "two", null);
default -> throw new AssertionError("illegal mode " + mode);
}
}
}
|