< prev index next >

test/jdk/java/util/Collection/IteratorAtEnd.java

Print this page

  1 /*
  2  * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 6529795
 27  * @summary next() does not change iterator state if throws NoSuchElementException
 28  * @author Martin Buchholz
 29  */
 30 
 31 import java.util.ArrayDeque;
 32 import java.util.ArrayList;
 33 import java.util.Collection;
 34 import java.util.HashMap;
 35 import java.util.Hashtable;
 36 import java.util.IdentityHashMap;
 37 import java.util.Iterator;
 38 import java.util.LinkedHashMap;
 39 import java.util.LinkedList;
 40 import java.util.List;
 41 import java.util.ListIterator;
 42 import java.util.Map;
 43 import java.util.NoSuchElementException;
 44 import java.util.PriorityQueue;
 45 import java.util.TreeMap;
 46 import java.util.TreeSet;

 82         testMap(new Hashtable());
 83         testMap(new LinkedHashMap());
 84         testMap(new WeakHashMap());
 85         testMap(new IdentityHashMap());
 86         testMap(new ConcurrentHashMap());
 87         testMap(new ConcurrentSkipListMap());
 88         testMap(new TreeMap());
 89     }
 90 
 91     static void testCollection(Collection c) {
 92         try {
 93             for (int i = 0; i < SIZE; i++)
 94                 c.add(i);
 95             test(c);
 96         } catch (Throwable t) { unexpected(t); }
 97     }
 98 
 99     static void testMap(Map m) {
100         try {
101             for (int i = 0; i < 3*SIZE; i++)
102                 m.put(i, i);
103             test(m.values());
104             test(m.keySet());
105             test(m.entrySet());
106         } catch (Throwable t) { unexpected(t); }
107     }
108 
109     static void test(Collection c) {
110         try {
111             final Iterator it = c.iterator();
112             THROWS(NoSuchElementException.class,
113                    () -> { while (true) it.next(); });
114             try { it.remove(); }
115             catch (UnsupportedOperationException exc) { return; }
116             pass();
117         } catch (Throwable t) { unexpected(t); }
118 
119         if (c instanceof List) {
120             final List list = (List) c;
121             try {
122                 final ListIterator it = list.listIterator(0);

  1 /*
  2  * Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 6529795 8336669
 27  * @summary next() does not change iterator state if throws NoSuchElementException
 28  * @author Martin Buchholz
 29  */
 30 
 31 import java.util.ArrayDeque;
 32 import java.util.ArrayList;
 33 import java.util.Collection;
 34 import java.util.HashMap;
 35 import java.util.Hashtable;
 36 import java.util.IdentityHashMap;
 37 import java.util.Iterator;
 38 import java.util.LinkedHashMap;
 39 import java.util.LinkedList;
 40 import java.util.List;
 41 import java.util.ListIterator;
 42 import java.util.Map;
 43 import java.util.NoSuchElementException;
 44 import java.util.PriorityQueue;
 45 import java.util.TreeMap;
 46 import java.util.TreeSet;

 82         testMap(new Hashtable());
 83         testMap(new LinkedHashMap());
 84         testMap(new WeakHashMap());
 85         testMap(new IdentityHashMap());
 86         testMap(new ConcurrentHashMap());
 87         testMap(new ConcurrentSkipListMap());
 88         testMap(new TreeMap());
 89     }
 90 
 91     static void testCollection(Collection c) {
 92         try {
 93             for (int i = 0; i < SIZE; i++)
 94                 c.add(i);
 95             test(c);
 96         } catch (Throwable t) { unexpected(t); }
 97     }
 98 
 99     static void testMap(Map m) {
100         try {
101             for (int i = 0; i < 3*SIZE; i++)
102                 m.put("BASE-" + i, i);
103             test(m.values());
104             test(m.keySet());
105             test(m.entrySet());
106         } catch (Throwable t) { unexpected(t); }
107     }
108 
109     static void test(Collection c) {
110         try {
111             final Iterator it = c.iterator();
112             THROWS(NoSuchElementException.class,
113                    () -> { while (true) it.next(); });
114             try { it.remove(); }
115             catch (UnsupportedOperationException exc) { return; }
116             pass();
117         } catch (Throwable t) { unexpected(t); }
118 
119         if (c instanceof List) {
120             final List list = (List) c;
121             try {
122                 final ListIterator it = list.listIterator(0);
< prev index next >