Path: blob/master/test/jdk/java/util/Collections/EmptyNavigableSet.java
41149 views
/*1* Copyright (c) 2011, 2017, 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*/2930import org.testng.Assert;31import org.testng.Assert.ThrowingRunnable;32import org.testng.annotations.DataProvider;33import org.testng.annotations.Test;3435import java.math.BigInteger;36import java.util.Arrays;37import java.util.Collection;38import java.util.Collections;39import java.util.Comparator;40import java.util.Iterator;41import java.util.NavigableSet;42import java.util.NoSuchElementException;43import java.util.SortedSet;44import java.util.TreeSet;4546import static org.testng.Assert.assertFalse;47import static org.testng.Assert.assertNull;48import static org.testng.Assert.assertSame;49import static org.testng.Assert.assertTrue;5051public class EmptyNavigableSet {5253public static <T> void assertInstance(T actual, Class<? extends T> expected) {54assertInstance(actual, expected, null);55}5657public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) {58assertTrue(expected.isInstance(actual), ((null != message) ? message : "")59+ " " + (actual == null ? "<null>" : actual.getClass().getSimpleName()) + " != " + expected.getSimpleName() + ". ");60}6162public static <T extends Throwable> void assertEmptyNavigableSet(Object obj) {63assertInstance(obj, NavigableSet.class);64assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0));65}6667public static <T extends Throwable> void assertEmptyNavigableSet(Object obj, String message) {68assertInstance(obj, NavigableSet.class, message);69assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0),70((null != message) ? message : "") + " Not empty. ");71}7273private <T extends Throwable> void assertThrows(Class<T> throwableClass,74ThrowingRunnable runnable,75String message) {76try {77Assert.assertThrows(throwableClass, runnable);78} catch (AssertionError e) {79throw new AssertionError(String.format("%s%n%s",80((null != message) ? message : ""), e.getMessage()), e);81}82}8384private void assertThrowsCCE(ThrowingRunnable r, String s) {85assertThrows(ClassCastException.class, r, s);86}8788private void assertThrowsNPE(ThrowingRunnable r, String s) {89assertThrows(NullPointerException.class, r, s);90}9192private void assertThrowsIAE(ThrowingRunnable r, String s) {93assertThrows(IllegalArgumentException.class, r, s);94}9596private void assertThrowsNSEE(ThrowingRunnable r, String s) {97assertThrows(NoSuchElementException.class, r, s);98}99100public static final boolean isDescending(SortedSet<?> set) {101if (null == set.comparator()) {102// natural order103return false;104}105106if (Collections.reverseOrder() == set.comparator()) {107// reverse natural order.108return true;109}110111if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) {112// it's a Collections.reverseOrder(Comparator).113return true;114}115116throw new IllegalStateException("can't determine ordering for " + set);117}118119/**120* Tests that the comparator is {@code null}.121*/122@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)123public void testComparatorIsNull(String description, NavigableSet<?> navigableSet) {124Comparator comparator = navigableSet.comparator();125126assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null.");127}128129/**130* Tests that contains requires Comparable131*/132@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)133public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) {134assertThrowsCCE(135() -> navigableSet.contains(new Object()),136description + ": Comparable should be required");137}138139/**140* Tests that the contains method returns {@code false}.141*/142@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)143public void testContains(String description, NavigableSet<?> navigableSet) {144assertFalse(navigableSet.contains(new Integer(1)),145description + ": Should not contain any elements.");146}147148/**149* Tests that the containsAll method returns {@code false}.150*/151@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)152public void testContainsAll(String description, NavigableSet<?> navigableSet) {153TreeSet treeSet = new TreeSet();154treeSet.add("1");155treeSet.add("2");156treeSet.add("3");157158assertFalse(navigableSet.containsAll(treeSet), "Should not contain any elements.");159}160161/**162* Tests that the iterator is empty.163*/164@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)165public void testEmptyIterator(String description, NavigableSet<?> navigableSet) {166assertFalse(navigableSet.iterator().hasNext(), "The iterator is not empty.");167}168169/**170* Tests that the set is empty.171*/172@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)173public void testIsEmpty(String description, NavigableSet<?> navigableSet) {174assertTrue(navigableSet.isEmpty(), "The set is not empty.");175}176177/**178* Tests that the first() method throws NoSuchElementException179*/180@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)181public void testFirst(String description, NavigableSet<?> navigableSet) {182assertThrowsNSEE(navigableSet::first, description);183}184185/**186* Tests the headSet() method.187*/188@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)189public void testHeadSet(String description, NavigableSet navigableSet) {190assertThrowsNPE(191() -> { NavigableSet ns = navigableSet.headSet(null, false); },192description + ": Must throw NullPointerException for null element");193194assertThrowsCCE(195() -> { NavigableSet ns = navigableSet.headSet(new Object(), true); },196description + ": Must throw ClassCastException for non-Comparable element");197198NavigableSet ns = navigableSet.headSet("1", false);199200assertEmptyNavigableSet(ns, description + ": Returned value is not empty navigable set.");201}202203/**204* Tests that the last() method throws NoSuchElementException205*/206@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)207public void testLast(String description, NavigableSet<?> navigableSet) {208assertThrowsNSEE(navigableSet::last, description);209}210211/**212* Tests that the size is 0.213*/214@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)215public void testSizeIsZero(String description, NavigableSet<?> navigableSet) {216assertTrue(0 == navigableSet.size(), "The size of the set is not 0.");217}218219/**220* Tests the subSet() method.221*/222@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)223public void testSubSet(String description, NavigableSet navigableSet) {224assertThrowsNPE(225() -> {226SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);227},228description + ": Must throw NullPointerException for null element");229230assertThrowsNPE(231() -> {232SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);233},234description + ": Must throw NullPointerException for null element");235236assertThrowsNPE(237() -> {238SortedSet ss = navigableSet.subSet(null, null);239},240description + ": Must throw NullPointerException for null element");241242Object obj1 = new Object();243Object obj2 = new Object();244245assertThrowsCCE(246() -> {247SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);248},249description + ": Must throw ClassCastException for parameter which is not Comparable.");250251assertThrowsCCE(252() -> {253SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);254},255description + ": Must throw ClassCastException for parameter which is not Comparable.");256257assertThrowsCCE(258() -> {259SortedSet ss = navigableSet.subSet(obj1, obj2);260},261description + ": Must throw ClassCastException for parameter which is not Comparable.");262263// minimal range264navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false);265navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, true);266navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, false);267navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, true);268269Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;270Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;271272assertThrowsIAE(273() -> navigableSet.subSet(last, true, first, false),274description275+ ": Must throw IllegalArgumentException when fromElement is not less than toElement.");276277navigableSet.subSet(first, true, last, false);278}279280@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)281public void testSubSetRanges(String description, NavigableSet navigableSet) {282Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;283Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;284285NavigableSet subSet = navigableSet.subSet(first, true, last, true);286287// same subset288subSet.subSet(first, true, last, true);289290// slightly smaller291NavigableSet ns = subSet.subSet(first, false, last, false);292// slight expansion293assertThrowsIAE(294() -> ns.subSet(first, true, last, true),295description + ": Expansion should not be allowed");296297// much smaller298subSet.subSet(first, false, BigInteger.ONE, false);299}300301@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)302public void testheadSetRanges(String description, NavigableSet navigableSet) {303NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);304305// same subset306subSet.headSet(BigInteger.ONE, true);307308// slightly smaller309NavigableSet ns = subSet.headSet(BigInteger.ONE, false);310311// slight expansion312assertThrowsIAE(313() -> ns.headSet(BigInteger.ONE, true),314description + ": Expansion should not be allowed");315316// much smaller317subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);318}319320@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)321public void testTailSetRanges(String description, NavigableSet navigableSet) {322NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true);323324// same subset325subSet.tailSet(BigInteger.ONE, true);326327// slightly smaller328NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);329330// slight expansion331assertThrowsIAE(332() -> ns.tailSet(BigInteger.ONE, true),333description + ": Expansion should not be allowed");334335// much smaller336subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);337}338339/**340* Tests the tailSet() method.341*/342@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)343public void testTailSet(String description, NavigableSet navigableSet) {344assertThrowsNPE(345() -> navigableSet.tailSet(null),346description + ": Must throw NullPointerException for null element");347348assertThrowsCCE(349() -> navigableSet.tailSet(new Object()),350description);351352NavigableSet ss = navigableSet.tailSet("1", true);353354assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");355}356357/**358* Tests that the array has a size of 0.359*/360@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)361public void testToArray(String description, NavigableSet<?> navigableSet) {362Object[] emptyNavigableSetArray = navigableSet.toArray();363364assertTrue(emptyNavigableSetArray.length == 0, "Returned non-empty Array.");365366emptyNavigableSetArray = new Object[20];367368Object[] result = navigableSet.toArray(emptyNavigableSetArray);369370assertSame(emptyNavigableSetArray, result);371372assertNull(result[0]);373}374375@DataProvider(name = "NavigableSet<?>", parallel = true)376public static Iterator<Object[]> navigableSetsProvider() {377return makeNavigableSets().iterator();378}379380public static Collection<Object[]> makeNavigableSets() {381return Arrays.asList(382new Object[]{"UnmodifiableNavigableSet(TreeSet)", Collections.unmodifiableNavigableSet(new TreeSet())},383new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet())},384new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet().descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet().descendingSet())},385new Object[]{"emptyNavigableSet()", Collections.emptyNavigableSet()},386new Object[]{"emptyNavigableSet().descendingSet()", Collections.emptyNavigableSet().descendingSet()},387new Object[]{"emptyNavigableSet().descendingSet().descendingSet()", Collections.emptyNavigableSet().descendingSet().descendingSet()}388);389}390}391392393