Path: blob/master/test/jdk/java/util/Collection/IteratorAtEnd.java
41149 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.ArrayDeque;31import java.util.ArrayList;32import java.util.Collection;33import java.util.HashMap;34import java.util.Hashtable;35import java.util.IdentityHashMap;36import java.util.Iterator;37import java.util.LinkedHashMap;38import java.util.LinkedList;39import java.util.List;40import java.util.ListIterator;41import java.util.Map;42import java.util.NoSuchElementException;43import java.util.PriorityQueue;44import java.util.TreeMap;45import java.util.TreeSet;46import java.util.Vector;47import java.util.WeakHashMap;48import java.util.concurrent.ArrayBlockingQueue;49import java.util.concurrent.ConcurrentHashMap;50import java.util.concurrent.ConcurrentLinkedDeque;51import java.util.concurrent.ConcurrentLinkedQueue;52import java.util.concurrent.ConcurrentSkipListMap;53import java.util.concurrent.ConcurrentSkipListSet;54import java.util.concurrent.CopyOnWriteArrayList;55import java.util.concurrent.CopyOnWriteArraySet;56import java.util.concurrent.LinkedBlockingQueue;57import java.util.concurrent.LinkedTransferQueue;5859@SuppressWarnings("unchecked")60public class IteratorAtEnd {61private static final int SIZE = 6;6263static void realMain(String[] args) throws Throwable {64testCollection(new ArrayList());65testCollection(new Vector());66testCollection(new LinkedList());67testCollection(new ArrayDeque());68testCollection(new TreeSet());69testCollection(new CopyOnWriteArrayList());70testCollection(new CopyOnWriteArraySet());71testCollection(new ConcurrentSkipListSet());7273testCollection(new PriorityQueue());74testCollection(new LinkedBlockingQueue());75testCollection(new ArrayBlockingQueue(100));76testCollection(new ConcurrentLinkedDeque());77testCollection(new ConcurrentLinkedQueue());78testCollection(new LinkedTransferQueue());7980testMap(new HashMap());81testMap(new Hashtable());82testMap(new LinkedHashMap());83testMap(new WeakHashMap());84testMap(new IdentityHashMap());85testMap(new ConcurrentHashMap());86testMap(new ConcurrentSkipListMap());87testMap(new TreeMap());88}8990static void testCollection(Collection c) {91try {92for (int i = 0; i < SIZE; i++)93c.add(i);94test(c);95} catch (Throwable t) { unexpected(t); }96}9798static void testMap(Map m) {99try {100for (int i = 0; i < 3*SIZE; i++)101m.put(i, i);102test(m.values());103test(m.keySet());104test(m.entrySet());105} catch (Throwable t) { unexpected(t); }106}107108static void test(Collection c) {109try {110final Iterator it = c.iterator();111THROWS(NoSuchElementException.class,112() -> { while (true) it.next(); });113try { it.remove(); }114catch (UnsupportedOperationException exc) { return; }115pass();116} catch (Throwable t) { unexpected(t); }117118if (c instanceof List) {119final List list = (List) c;120try {121final ListIterator it = list.listIterator(0);122it.next();123final Object x = it.previous();124THROWS(NoSuchElementException.class, () -> it.previous());125try { it.remove(); }126catch (UnsupportedOperationException exc) { return; }127pass();128check(! list.get(0).equals(x));129} catch (Throwable t) { unexpected(t); }130131try {132final ListIterator it = list.listIterator(list.size());133it.previous();134final Object x = it.next();135THROWS(NoSuchElementException.class, () -> it.next());136try { it.remove(); }137catch (UnsupportedOperationException exc) { return; }138pass();139check(! list.get(list.size()-1).equals(x));140} catch (Throwable t) { unexpected(t); }141}142}143144//--------------------- Infrastructure ---------------------------145static volatile int passed = 0, failed = 0;146static void pass() {passed++;}147static void fail() {failed++; Thread.dumpStack();}148static void fail(String msg) {System.out.println(msg); fail();}149static void unexpected(Throwable t) {failed++; t.printStackTrace();}150static void check(boolean cond) {if (cond) pass(); else fail();}151static void equal(Object x, Object y) {152if (x == null ? y == null : x.equals(y)) pass();153else fail(x + " not equal to " + y);}154public static void main(String[] args) throws Throwable {155try {realMain(args);} catch (Throwable t) {unexpected(t);}156System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);157if (failed > 0) throw new AssertionError("Some tests failed");}158interface Fun {void f() throws Throwable;}159static void THROWS(Class<? extends Throwable> k, Fun... fs) {160for (Fun f : fs)161try { f.f(); fail("Expected " + k.getName() + " not thrown"); }162catch (Throwable t) {163if (k.isAssignableFrom(t.getClass())) pass();164else unexpected(t);}}165}166167168