IteratorAtEnd.java revision 0
0N/A/*
0N/A * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6529795
0N/A * @summary next() does not change iterator state if throws NoSuchElementException
0N/A * @author Martin Buchholz
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport java.util.concurrent.*;
0N/A
0N/A@SuppressWarnings("unchecked")
0N/Apublic class IteratorAtEnd {
0N/A private static final int SIZE = 6;
0N/A
0N/A static void realMain(String[] args) throws Throwable {
0N/A testCollection(new ArrayList());
0N/A testCollection(new Vector());
0N/A testCollection(new LinkedList());
0N/A testCollection(new ArrayDeque());
0N/A testCollection(new TreeSet());
0N/A testCollection(new CopyOnWriteArrayList());
0N/A testCollection(new CopyOnWriteArraySet());
0N/A testCollection(new ConcurrentSkipListSet());
0N/A
0N/A testCollection(new PriorityQueue());
0N/A testCollection(new LinkedBlockingQueue());
0N/A testCollection(new ArrayBlockingQueue(100));
0N/A testCollection(new ConcurrentLinkedQueue());
0N/A
0N/A testMap(new HashMap());
0N/A testMap(new Hashtable());
0N/A testMap(new LinkedHashMap());
0N/A testMap(new WeakHashMap());
0N/A testMap(new IdentityHashMap());
0N/A testMap(new ConcurrentHashMap());
0N/A testMap(new ConcurrentSkipListMap());
0N/A testMap(new TreeMap());
0N/A }
0N/A
0N/A static void testCollection(Collection c) {
0N/A try {
0N/A for (int i = 0; i < SIZE; i++)
0N/A c.add(i);
0N/A test(c);
0N/A } catch (Throwable t) { unexpected(t); }
0N/A }
0N/A
0N/A static void testMap(Map m) {
0N/A try {
0N/A for (int i = 0; i < 3*SIZE; i++)
0N/A m.put(i, i);
0N/A test(m.values());
0N/A test(m.keySet());
0N/A test(m.entrySet());
0N/A } catch (Throwable t) { unexpected(t); }
0N/A }
0N/A
0N/A static void test(Collection c) {
0N/A try {
0N/A final Iterator it = c.iterator();
0N/A THROWS(NoSuchElementException.class,
0N/A new Fun() {void f() { while (true) it.next(); }});
0N/A try { it.remove(); }
0N/A catch (UnsupportedOperationException _) { return; }
0N/A pass();
0N/A } catch (Throwable t) { unexpected(t); }
0N/A
0N/A if (c instanceof List) {
0N/A final List list = (List) c;
0N/A try {
0N/A final ListIterator it = list.listIterator(0);
0N/A it.next();
0N/A final Object x = it.previous();
0N/A THROWS(NoSuchElementException.class,
0N/A new Fun() {void f() { it.previous(); }});
0N/A try { it.remove(); }
0N/A catch (UnsupportedOperationException _) { return; }
0N/A pass();
0N/A check(! list.get(0).equals(x));
0N/A } catch (Throwable t) { unexpected(t); }
0N/A
0N/A try {
0N/A final ListIterator it = list.listIterator(list.size());
0N/A it.previous();
0N/A final Object x = it.next();
0N/A THROWS(NoSuchElementException.class,
0N/A new Fun() {void f() { it.next(); }});
0N/A try { it.remove(); }
0N/A catch (UnsupportedOperationException _) { return; }
0N/A pass();
0N/A check(! list.get(list.size()-1).equals(x));
0N/A } catch (Throwable t) { unexpected(t); }
0N/A }
0N/A }
0N/A
0N/A //--------------------- Infrastructure ---------------------------
0N/A static volatile int passed = 0, failed = 0;
0N/A static void pass() {passed++;}
0N/A static void fail() {failed++; Thread.dumpStack();}
0N/A static void fail(String msg) {System.out.println(msg); fail();}
0N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
0N/A static void check(boolean cond) {if (cond) pass(); else fail();}
0N/A static void equal(Object x, Object y) {
0N/A if (x == null ? y == null : x.equals(y)) pass();
0N/A else fail(x + " not equal to " + y);}
0N/A public static void main(String[] args) throws Throwable {
0N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
0N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
0N/A if (failed > 0) throw new AssertionError("Some tests failed");}
0N/A private static abstract class Fun {abstract void f() throws Throwable;}
0N/A static void THROWS(Class<? extends Throwable> k, Fun... fs) {
0N/A for (Fun f : fs)
0N/A try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
0N/A catch (Throwable t) {
0N/A if (k.isAssignableFrom(t.getClass())) pass();
0N/A else unexpected(t);}}
0N/A}