Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/EmptyNavigableSet.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.emptyNavigableSet27* @run testng EmptyNavigableSet28*/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.NavigableSet;37import java.util.SortedSet;38import java.util.TreeSet;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 EmptyNavigableSet {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 assertEmptyNavigableSet(Object obj) {60assertInstance(obj, NavigableSet.class);61assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0));62}6364public static <T extends Throwable> void assertEmptyNavigableSet(Object obj, String message) {65assertInstance(obj, NavigableSet.class, message);66assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)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(SortedSet<?> 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 = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)115public void testComparatorIsNull(String description, NavigableSet<?> navigableSet) {116Comparator comparator = navigableSet.comparator();117118assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null.");119}120121/**122* Tests that contains requires Comparable123*/124@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)125public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) {126assertThrows(() -> {127navigableSet.contains(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 = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)137public void testContains(String description, NavigableSet<?> navigableSet) {138assertFalse(navigableSet.contains(new Integer(1)),139description + ": Should not contain any elements.");140}141142/**143* Tests that the containsAll method returns {@code false}.144*/145@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)146public void testContainsAll(String description, NavigableSet<?> navigableSet) {147TreeSet treeSet = new TreeSet();148treeSet.add("1");149treeSet.add("2");150treeSet.add("3");151152assertFalse(navigableSet.containsAll(treeSet), "Should not contain any elements.");153}154155/**156* Tests that the iterator is empty.157*/158@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)159public void testEmptyIterator(String description, NavigableSet<?> navigableSet) {160Iterator emptyIterator = navigableSet.iterator();161162assertFalse((emptyIterator != null) && (emptyIterator.hasNext()),163"The iterator is not empty.");164}165166/**167* Tests that the set is empty.168*/169@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)170public void testIsEmpty(String description, NavigableSet<?> navigableSet) {171assertTrue(navigableSet.isEmpty(), "The set is not empty.");172}173174/**175* Tests that the first() method throws NoSuchElementException176*/177@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)178public void testFirst(String description, NavigableSet<?> navigableSet) {179assertThrows(() -> {180navigableSet.first();181}, NoSuchElementException.class, description);182}183184/**185* Tests the headSet() method.186*/187@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)188public void testHeadSet(String description, NavigableSet navigableSet) {189assertThrows(190() -> { NavigableSet ns = navigableSet.headSet(null, false); },191NullPointerException.class,192description + ": Must throw NullPointerException for null element");193194assertThrows(195() -> { NavigableSet ns = navigableSet.headSet(new Object(), true); },196ClassCastException.class,197description + ": Must throw ClassCastException for non-Comparable element");198199NavigableSet ns = navigableSet.headSet("1", false);200201assertEmptyNavigableSet(ns, description + ": Returned value is not empty navigable set.");202}203204/**205* Tests that the last() method throws NoSuchElementException206*/207@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)208public void testLast(String description, NavigableSet<?> navigableSet) {209assertThrows(() -> {210navigableSet.last();211}, NoSuchElementException.class, description);212}213214/**215* Tests that the size is 0.216*/217@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)218public void testSizeIsZero(String description, NavigableSet<?> navigableSet) {219assertTrue(0 == navigableSet.size(), "The size of the set is not 0.");220}221222/**223* Tests the subSet() method.224*/225@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)226public void testSubSet(String description, NavigableSet navigableSet) {227assertThrows(228() -> {229SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);230},231NullPointerException.class,232description + ": Must throw NullPointerException for null element");233234assertThrows(235() -> {236SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);237},238NullPointerException.class,239description + ": Must throw NullPointerException for null element");240241assertThrows(242() -> {243SortedSet ss = navigableSet.subSet(null, null);244},245NullPointerException.class,246description + ": Must throw NullPointerException for null element");247248Object obj1 = new Object();249Object obj2 = new Object();250251assertThrows(252() -> {253SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);254},255ClassCastException.class, description256+ ": Must throw ClassCastException for parameter which is not Comparable.");257258assertThrows(259() -> {260SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);261},262ClassCastException.class, description263+ ": Must throw ClassCastException for parameter which is not Comparable.");264265assertThrows(266() -> {267SortedSet ss = navigableSet.subSet(obj1, obj2);268},269ClassCastException.class, description270+ ": Must throw ClassCastException for parameter which is not Comparable.");271272// minimal range273navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false);274navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, true);275navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, false);276navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, true);277278Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;279Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;280281assertThrows(282() -> {283navigableSet.subSet(last, true, first, false);284},285IllegalArgumentException.class, description286+ ": Must throw IllegalArgumentException when fromElement is not less then then toElement.");287288navigableSet.subSet(first, true, last, false);289}290291@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)292public void testSubSetRanges(String description, NavigableSet navigableSet) {293Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;294Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;295296NavigableSet subSet = navigableSet.subSet(first, true, last, true);297298// same subset299subSet.subSet(first, true, last, true);300301// slightly smaller302NavigableSet ns = subSet.subSet(first, false, last, false);303// slight exapansion304assertThrows(() -> {305ns.subSet(first, true, last, true);306},307IllegalArgumentException.class,308description + ": Expansion should not be allowed");309310// much smaller311subSet.subSet(first, false, BigInteger.ONE, false);312}313314@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)315public void testheadSetRanges(String description, NavigableSet navigableSet) {316NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);317318// same subset319subSet.headSet(BigInteger.ONE, true);320321// slightly smaller322NavigableSet ns = subSet.headSet(BigInteger.ONE, false);323324// slight exapansion325assertThrows(() -> {326ns.headSet(BigInteger.ONE, true);327},328IllegalArgumentException.class,329description + ": Expansion should not be allowed");330331// much smaller332subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);333}334335@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)336public void testTailSetRanges(String description, NavigableSet navigableSet) {337NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true);338339// same subset340subSet.tailSet(BigInteger.ONE, true);341342// slightly smaller343NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);344345// slight exapansion346assertThrows(() -> {347ns.tailSet(BigInteger.ONE, true);348},349IllegalArgumentException.class,350description + ": Expansion should not be allowed");351352// much smaller353subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);354}355356/**357* Tests the tailSet() method.358*/359@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)360public void testTailSet(String description, NavigableSet navigableSet) {361assertThrows(() -> {362navigableSet.tailSet(null);363},364NullPointerException.class,365description + ": Must throw NullPointerException for null element");366367assertThrows(() -> {368navigableSet.tailSet(new Object());369}, ClassCastException.class);370371NavigableSet ss = navigableSet.tailSet("1", true);372373assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");374}375376/**377* Tests that the array has a size of 0.378*/379@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)380public void testToArray(String description, NavigableSet<?> navigableSet) {381Object[] emptyNavigableSetArray = navigableSet.toArray();382383assertTrue(emptyNavigableSetArray.length == 0, "Returned non-empty Array.");384385emptyNavigableSetArray = new Object[20];386387Object[] result = navigableSet.toArray(emptyNavigableSetArray);388389assertSame(emptyNavigableSetArray, result);390391assertTrue(result[0] == null);392}393394@DataProvider(name = "NavigableSet<?>", parallel = true)395public static Iterator<Object[]> navigableSetsProvider() {396return makeNavigableSets().iterator();397}398399public static Collection<Object[]> makeNavigableSets() {400return Arrays.asList(401new Object[]{"UnmodifiableNavigableSet(TreeSet)", Collections.unmodifiableNavigableSet(new TreeSet())},402new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet())},403new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet().descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet().descendingSet())},404new Object[]{"emptyNavigableSet()", Collections.emptyNavigableSet()},405new Object[]{"emptyNavigableSet().descendingSet()", Collections.emptyNavigableSet().descendingSet()},406new Object[]{"emptyNavigableSet().descendingSet().descendingSet()", Collections.emptyNavigableSet().descendingSet().descendingSet()}407);408}409}410411412