Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collection/IteratorAtEnd.java
38812 views
/*1* Copyright (c) 2007, 2014, 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 652979526* @summary next() does not change iterator state if throws NoSuchElementException27* @author Martin Buchholz28*/2930import java.util.*;31import java.util.concurrent.*;3233@SuppressWarnings("unchecked")34public class IteratorAtEnd {35private static final int SIZE = 6;3637static void realMain(String[] args) throws Throwable {38testCollection(new ArrayList());39testCollection(new Vector());40testCollection(new LinkedList());41testCollection(new ArrayDeque());42testCollection(new TreeSet());43testCollection(new CopyOnWriteArrayList());44testCollection(new CopyOnWriteArraySet());45testCollection(new ConcurrentSkipListSet());4647testCollection(new PriorityQueue());48testCollection(new LinkedBlockingQueue());49testCollection(new ArrayBlockingQueue(100));50testCollection(new ConcurrentLinkedDeque());51testCollection(new ConcurrentLinkedQueue());52testCollection(new LinkedTransferQueue());5354testMap(new HashMap());55testMap(new Hashtable());56testMap(new LinkedHashMap());57testMap(new WeakHashMap());58testMap(new IdentityHashMap());59testMap(new ConcurrentHashMap());60testMap(new ConcurrentSkipListMap());61testMap(new TreeMap());62}6364static void testCollection(Collection c) {65try {66for (int i = 0; i < SIZE; i++)67c.add(i);68test(c);69} catch (Throwable t) { unexpected(t); }70}7172static void testMap(Map m) {73try {74for (int i = 0; i < 3*SIZE; i++)75m.put(i, i);76test(m.values());77test(m.keySet());78test(m.entrySet());79} catch (Throwable t) { unexpected(t); }80}8182static void test(Collection c) {83try {84final Iterator it = c.iterator();85THROWS(NoSuchElementException.class,86() -> { while (true) it.next(); });87try { it.remove(); }88catch (UnsupportedOperationException exc) { return; }89pass();90} catch (Throwable t) { unexpected(t); }9192if (c instanceof List) {93final List list = (List) c;94try {95final ListIterator it = list.listIterator(0);96it.next();97final Object x = it.previous();98THROWS(NoSuchElementException.class, () -> it.previous());99try { it.remove(); }100catch (UnsupportedOperationException exc) { return; }101pass();102check(! list.get(0).equals(x));103} catch (Throwable t) { unexpected(t); }104105try {106final ListIterator it = list.listIterator(list.size());107it.previous();108final Object x = it.next();109THROWS(NoSuchElementException.class, () -> it.next());110try { it.remove(); }111catch (UnsupportedOperationException exc) { return; }112pass();113check(! list.get(list.size()-1).equals(x));114} catch (Throwable t) { unexpected(t); }115}116}117118//--------------------- Infrastructure ---------------------------119static volatile int passed = 0, failed = 0;120static void pass() {passed++;}121static void fail() {failed++; Thread.dumpStack();}122static void fail(String msg) {System.out.println(msg); fail();}123static void unexpected(Throwable t) {failed++; t.printStackTrace();}124static void check(boolean cond) {if (cond) pass(); else fail();}125static void equal(Object x, Object y) {126if (x == null ? y == null : x.equals(y)) pass();127else fail(x + " not equal to " + y);}128public static void main(String[] args) throws Throwable {129try {realMain(args);} catch (Throwable t) {unexpected(t);}130System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);131if (failed > 0) throw new AssertionError("Some tests failed");}132interface Fun {void f() throws Throwable;}133static void THROWS(Class<? extends Throwable> k, Fun... fs) {134for (Fun f : fs)135try { f.f(); fail("Expected " + k.getName() + " not thrown"); }136catch (Throwable t) {137if (k.isAssignableFrom(t.getClass())) pass();138else unexpected(t);}}139}140141142