Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/EmptyNavigableMap.java
38812 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4533691 712918526* @summary Unit test for Collections.emptyNavigableMap27* @run testng EmptyNavigableMap28*/29import java.math.BigInteger;30import java.util.Arrays;31import java.util.Collection;32import java.util.Collections;33import java.util.Comparator;34import java.util.Iterator;35import java.util.NoSuchElementException;36import java.util.NavigableMap;37import java.util.SortedMap;38import java.util.TreeMap;39import org.testng.annotations.Test;40import org.testng.annotations.DataProvider;4142import static org.testng.Assert.fail;43import static org.testng.Assert.assertEquals;44import static org.testng.Assert.assertTrue;45import static org.testng.Assert.assertFalse;46import static org.testng.Assert.assertSame;4748public class EmptyNavigableMap {4950public static <T> void assertInstance(T actual, Class<? extends T> expected) {51assertInstance(expected.isInstance(actual), null);52}5354public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) {55assertTrue(expected.isInstance(actual), ((null != message) ? message : "")56+ " " + (actual == null ? "<null>" : actual.getClass().getSimpleName()) + " != " + expected.getSimpleName() + ". ");57}5859public static <T extends Throwable> void assertEmptyNavigableMap(Object obj) {60assertInstance(obj, NavigableMap.class);61assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0));62}6364public static <T extends Throwable> void assertEmptyNavigableMap(Object obj, String message) {65assertInstance(obj, NavigableMap.class, message);66assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0),67((null != message) ? message : "") + " Not empty. ");68}6970public interface Thrower<T extends Throwable> {7172public void run() throws T;73}7475public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {76assertThrows(thrower, throwable, null);77}7879public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {80Throwable result;81try {82thrower.run();83fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");84return;85} catch (Throwable caught) {86result = caught;87}8889assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");90}9192public static final boolean isDescending(SortedMap<?,?> set) {93if (null == set.comparator()) {94// natural order95return false;96}9798if (Collections.reverseOrder() == set.comparator()) {99// reverse natural order.100return true;101}102103if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) {104// it's a Collections.reverseOrder(Comparator).105return true;106}107108throw new IllegalStateException("can't determine ordering for " + set);109}110111/**112* Tests that the comparator is {@code null}.113*/114@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)115public void testComparatorIsNull(String description, NavigableMap<?,?> navigableMap) {116Comparator comparator = navigableMap.comparator();117118assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null.");119}120121/**122* Tests that contains requires Comparable123*/124@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)125public void testContainsRequiresComparable(String description, NavigableMap<?,?> navigableMap) {126assertThrows(() -> {127navigableMap.containsKey(new Object());128},129ClassCastException.class,130description + ": Compareable should be required");131}132133/**134* Tests that the contains method returns {@code false}.135*/136@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)137public void testContains(String description, NavigableMap<?,?> navigableMap) {138assertFalse(navigableMap.containsKey(new Integer(1)),139description + ": Should not contain any elements.");140assertFalse(navigableMap.containsValue(new Integer(1)),141description + ": Should not contain any elements.");142}143144/**145* Tests that the containsAll method returns {@code false}.146*/147@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)148public void testContainsAll(String description, NavigableMap<?,?> navigableMap) {149TreeMap treeMap = new TreeMap();150treeMap.put("1", 1);151treeMap.put("2", 2);152treeMap.put("3", 3);153154assertFalse(navigableMap.equals(treeMap), "Should not contain any elements.");155}156157/**158* Tests that the iterator is empty.159*/160@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)161public void testEmptyIterator(String description, NavigableMap<?,?> navigableMap) {162assertFalse(navigableMap.keySet().iterator().hasNext(), "The iterator is not empty.");163assertFalse(navigableMap.values().iterator().hasNext(), "The iterator is not empty.");164assertFalse(navigableMap.entrySet().iterator().hasNext(), "The iterator is not empty.");165}166167/**168* Tests that the set is empty.169*/170@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)171public void testIsEmpty(String description, NavigableMap<?,?> navigableMap) {172assertTrue(navigableMap.isEmpty(), "The set is not empty.");173}174175/**176* Tests the headMap() method.177*/178@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)179public void testHeadMap(String description, NavigableMap navigableMap) {180assertThrows(181() -> { NavigableMap ss = navigableMap.headMap(null, false); },182NullPointerException.class,183description + ": Must throw NullPointerException for null element");184185assertThrows(186() -> { NavigableMap ss = navigableMap.headMap(new Object(), true); },187ClassCastException.class,188description + ": Must throw ClassCastException for non-Comparable element");189190NavigableMap ss = navigableMap.headMap("1", false);191192assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");193}194195/**196* Tests that the size is 0.197*/198@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)199public void testSizeIsZero(String description, NavigableMap<?,?> navigableMap) {200assertTrue(0 == navigableMap.size(), "The size of the set is not 0.");201}202203/**204* Tests the subMap() method.205*/206@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)207public void testSubMap(String description, NavigableMap navigableMap) {208assertThrows(209() -> {210SortedMap ss = navigableMap.subMap(null, BigInteger.TEN);211},212NullPointerException.class,213description + ": Must throw NullPointerException for null element");214215assertThrows(216() -> {217SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null);218},219NullPointerException.class,220description + ": Must throw NullPointerException for null element");221222assertThrows(223() -> {224SortedMap ss = navigableMap.subMap(null, null);225},226NullPointerException.class,227description + ": Must throw NullPointerException for null element");228229Object obj1 = new Object();230Object obj2 = new Object();231232assertThrows(233() -> {234SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN);235},236ClassCastException.class, description237+ ": Must throw ClassCastException for parameter which is not Comparable.");238239assertThrows(240() -> {241SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2);242},243ClassCastException.class, description244+ ": Must throw ClassCastException for parameter which is not Comparable.");245246assertThrows(247() -> {248SortedMap ss = navigableMap.subMap(obj1, obj2);249},250ClassCastException.class, description251+ ": Must throw ClassCastException for parameter which is not Comparable.");252253// minimal range254navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false);255navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, true);256navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, false);257navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, true);258259Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;260Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;261262assertThrows(263() -> {264navigableMap.subMap(last, true, first, false);265},266IllegalArgumentException.class, description267+ ": Must throw IllegalArgumentException when fromElement is not less then then toElement.");268269navigableMap.subMap(first, true, last, false);270}271272@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)273public void testSubMapRanges(String description, NavigableMap navigableMap) {274Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;275Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;276277NavigableMap subMap = navigableMap.subMap(first, true, last, true);278279// same subset280subMap.subMap(first, true, last, true);281282// slightly smaller283NavigableMap ns = subMap.subMap(first, false, last, false);284// slight exapansion285assertThrows(() -> {286ns.subMap(first, true, last, true);287},288IllegalArgumentException.class,289description + ": Expansion should not be allowed");290291// much smaller292subMap.subMap(first, false, BigInteger.ONE, false);293}294295@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)296public void testheadMapRanges(String description, NavigableMap navigableMap) {297NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);298299// same subset300subMap.headMap(BigInteger.ONE, true);301302// slightly smaller303NavigableMap ns = subMap.headMap(BigInteger.ONE, false);304305// slight exapansion306assertThrows(() -> {307ns.headMap(BigInteger.ONE, true);308},309IllegalArgumentException.class,310description + ": Expansion should not be allowed");311312// much smaller313subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);314}315316@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)317public void testTailMapRanges(String description, NavigableMap navigableMap) {318NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true);319320// same subset321subMap.tailMap(BigInteger.ONE, true);322323// slightly smaller324NavigableMap ns = subMap.tailMap(BigInteger.ONE, false);325326// slight exapansion327assertThrows(() -> {328ns.tailMap(BigInteger.ONE, true);329},330IllegalArgumentException.class,331description + ": Expansion should not be allowed");332333// much smaller334subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false);335}336337/**338* Tests the tailMap() method.339*/340@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)341public void testTailMap(String description, NavigableMap navigableMap) {342assertThrows(() -> {343navigableMap.tailMap(null);344},345NullPointerException.class,346description + ": Must throw NullPointerException for null element");347348assertThrows(() -> {349navigableMap.tailMap(new Object());350}, ClassCastException.class);351352NavigableMap ss = navigableMap.tailMap("1", true);353354assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");355}356357@DataProvider(name = "NavigableMap<?,?>", parallel = true)358public static Iterator<Object[]> navigableMapsProvider() {359return makeNavigableMaps().iterator();360}361362public static Collection<Object[]> makeNavigableMaps() {363return Arrays.asList(364new Object[]{"UnmodifiableNavigableMap(TreeMap)", Collections.unmodifiableNavigableMap(new TreeMap())},365new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap())},366new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap().descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap().descendingMap())},367new Object[]{"emptyNavigableMap()", Collections.emptyNavigableMap()},368new Object[]{"emptyNavigableMap().descendingMap()", Collections.emptyNavigableMap().descendingMap()},369new Object[]{"emptyNavigableMap().descendingMap().descendingMap()", Collections.emptyNavigableMap().descendingMap().descendingMap()}370);371}372}373374375