Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/EmptyIterator.java
38821 views
/*1* Copyright (c) 2007, 2012, 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 5017904 6356890 800492826* @summary Test empty iterators, enumerations, and collections27*/2829import static java.util.Collections.*;30import java.util.*;3132public class EmptyIterator {3334void test(String[] args) throws Throwable {35testEmptyCollection(Collections.<Object>emptyList());36testEmptyCollection(Collections.<Object>emptySet());3738testEmptyMap(Collections.<Object, Object>emptyMap());3940Hashtable<Object, Object> emptyTable = new Hashtable<Object, Object>();41testEmptyEnumeration(emptyTable.keys());42testEmptyEnumeration(emptyTable.elements());43testEmptyIterator(emptyTable.keySet().iterator());44testEmptyIterator(emptyTable.values().iterator());45testEmptyIterator(emptyTable.entrySet().iterator());4647final Enumeration<EmptyIterator> finalEmptyTyped =48Collections.emptyEnumeration();49testEmptyEnumeration(finalEmptyTyped);5051final Enumeration finalEmptyAbstract =52Collections.emptyEnumeration();53testEmptyEnumeration(finalEmptyAbstract);5455@SuppressWarnings("unchecked") Iterator<?> x =56new sun.tools.java.MethodSet()57.lookupName(sun.tools.java.Identifier.lookup(""));58testEmptyIterator(x);59}6061<T> void testEmptyEnumeration(final Enumeration<T> e) {62check(e == emptyEnumeration());63check(! e.hasMoreElements());64THROWS(NoSuchElementException.class,65new F(){void f(){ e.nextElement(); }});66}6768<T> void testEmptyIterator(final Iterator<T> it) {69check(it == emptyIterator());70check(! it.hasNext());71THROWS(NoSuchElementException.class,72new F(){void f(){ it.next(); }});73THROWS(IllegalStateException.class,74new F(){void f(){ it.remove(); }});75}7677void testEmptyMap(Map<Object, Object> m) {78check(m == emptyMap());79check(m.entrySet().iterator() ==80Collections.<Map.Entry<Object,Object>>emptyIterator());81check(m.values().iterator() == emptyIterator());82check(m.keySet().iterator() == emptyIterator());83equal(m, unmodifiableMap(m));8485testEmptyCollection(m.keySet());86testEmptyCollection(m.entrySet());87testEmptyCollection(m.values());88}8990<E> void testToArray(final Collection<E> c) {91Object[] a = c.toArray();92equal(a.length, 0);93equal(a.getClass().getComponentType(), Object.class);94THROWS(NullPointerException.class,95new F(){void f(){ c.toArray((Object[])null); }});9697{98String[] t = new String[0];99check(c.toArray(t) == t);100}101102{103String[] t = nCopies(10, "").toArray(new String[0]);104check(c.toArray(t) == t);105check(t[0] == null);106for (int i=1; i<t.length; i++)107check(t[i] == "");108}109}110111<E> void testEmptyCollection(final Collection<E> c) {112testEmptyIterator(c.iterator());113114check(c.iterator() == emptyIterator());115if (c instanceof List)116check(((List<?>)c).listIterator() == emptyListIterator());117118testToArray(c);119}120121//--------------------- Infrastructure ---------------------------122volatile int passed = 0, failed = 0;123void pass() {passed++;}124void fail() {failed++; Thread.dumpStack();}125void fail(String msg) {System.err.println(msg); fail();}126void unexpected(Throwable t) {failed++; t.printStackTrace();}127void check(boolean cond) {if (cond) pass(); else fail();}128void equal(Object x, Object y) {129if (x == null ? y == null : x.equals(y)) pass();130else fail(x + " not equal to " + y);}131public static void main(String[] args) throws Throwable {132new EmptyIterator().instanceMain(args);}133void instanceMain(String[] args) throws Throwable {134try {test(args);} catch (Throwable t) {unexpected(t);}135System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);136if (failed > 0) throw new AssertionError("Some tests failed");}137abstract class F {abstract void f() throws Throwable;}138void THROWS(Class<? extends Throwable> k, F... fs) {139for (F f : fs)140try {f.f(); fail("Expected " + k.getName() + " not thrown");}141catch (Throwable t) {142if (k.isAssignableFrom(t.getClass())) pass();143else unexpected(t);}}144}145146147