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;
47 import java.util.Vector;
48 import java.util.WeakHashMap;
49 import java.util.concurrent.ArrayBlockingQueue;
50 import java.util.concurrent.ConcurrentHashMap;
51 import java.util.concurrent.ConcurrentLinkedDeque;
52 import java.util.concurrent.ConcurrentLinkedQueue;
53 import java.util.concurrent.ConcurrentSkipListMap;
54 import java.util.concurrent.ConcurrentSkipListSet;
55 import java.util.concurrent.CopyOnWriteArrayList;
56 import java.util.concurrent.CopyOnWriteArraySet;
57 import java.util.concurrent.LinkedBlockingQueue;
58 import java.util.concurrent.LinkedTransferQueue;
59
60 @SuppressWarnings("unchecked")
61 public class IteratorAtEnd {
62 private static final int SIZE = 6;
63
64 static void realMain(String[] args) throws Throwable {
65 testCollection(new ArrayList());
66 testCollection(new Vector());
67 testCollection(new LinkedList());
68 testCollection(new ArrayDeque());
69 testCollection(new TreeSet());
70 testCollection(new CopyOnWriteArrayList());
71 testCollection(new CopyOnWriteArraySet());
72 testCollection(new ConcurrentSkipListSet());
73
74 testCollection(new PriorityQueue());
75 testCollection(new LinkedBlockingQueue());
76 testCollection(new ArrayBlockingQueue(100));
77 testCollection(new ConcurrentLinkedDeque());
78 testCollection(new ConcurrentLinkedQueue());
79 testCollection(new LinkedTransferQueue());
80
81 testMap(new HashMap());
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);
123 it.next();
124 final Object x = it.previous();
125 THROWS(NoSuchElementException.class, () -> it.previous());
126 try { it.remove(); }
127 catch (UnsupportedOperationException exc) { return; }
128 pass();
129 check(! list.get(0).equals(x));
130 } catch (Throwable t) { unexpected(t); }
|
1 /*
2 * Copyright (c) 2007, 2026, 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;
47 import java.util.Vector;
48 import java.util.WeakHashMap;
49 import java.util.concurrent.ArrayBlockingQueue;
50 import java.util.concurrent.ConcurrentHashMap;
51 import java.util.concurrent.ConcurrentLinkedDeque;
52 import java.util.concurrent.ConcurrentLinkedQueue;
53 import java.util.concurrent.ConcurrentSkipListMap;
54 import java.util.concurrent.ConcurrentSkipListSet;
55 import java.util.concurrent.CopyOnWriteArrayList;
56 import java.util.concurrent.CopyOnWriteArraySet;
57 import java.util.concurrent.LinkedBlockingQueue;
58 import java.util.concurrent.LinkedTransferQueue;
59 import java.util.stream.IntStream;
60
61 @SuppressWarnings("unchecked")
62 public class IteratorAtEnd {
63 private static final int SIZE = 6;
64
65 static void realMain(String[] args) throws Throwable {
66 testCollection(new ArrayList());
67 testCollection(new Vector());
68 testCollection(new LinkedList());
69 testCollection(new ArrayDeque());
70 testCollection(new TreeSet());
71 testCollection(new CopyOnWriteArrayList());
72 testCollection(new CopyOnWriteArraySet());
73 testCollection(new ConcurrentSkipListSet());
74
75 testCollection(new PriorityQueue());
76 testCollection(new LinkedBlockingQueue());
77 testCollection(new ArrayBlockingQueue(100));
78 testCollection(new ConcurrentLinkedDeque());
79 testCollection(new ConcurrentLinkedQueue());
80 testCollection(new LinkedTransferQueue());
81
82 testMap(new HashMap());
83 testMap(new Hashtable());
84 testMap(new LinkedHashMap());
85 testMap(new WeakHashMap());
86 testMap(new IdentityHashMap());
87 testMap(new ConcurrentHashMap());
88 testMap(new ConcurrentSkipListMap());
89 testMap(new TreeMap());
90 }
91
92 static void testCollection(Collection c) {
93 try {
94 for (int i = 0; i < SIZE; i++)
95 c.add(i);
96 test(c);
97 } catch (Throwable t) { unexpected(t); }
98 }
99
100 // A strong list of reference to map keys to avoid
101 // spurious purges in WeakHashMap
102 private static final List<String> MAP_KEYS = IntStream
103 .range(0, 3 * SIZE)
104 .mapToObj(i -> "BASE-" + i)
105 .toList();
106
107 static void testMap(Map m) {
108 try {
109 for (int i = 0; i < 3*SIZE; i++)
110 m.put(MAP_KEYS.get(i), i);
111 test(m.values());
112 test(m.keySet());
113 test(m.entrySet());
114 } catch (Throwable t) { unexpected(t); }
115 }
116
117 static void test(Collection c) {
118 try {
119 assert !c.isEmpty() : "Calling remove on empty iterator will result in IllegalStateException";
120 final Iterator it = c.iterator();
121 THROWS(NoSuchElementException.class,
122 () -> { while (true) it.next(); });
123 try { it.remove(); }
124 catch (UnsupportedOperationException exc) { return; }
125 pass();
126 } catch (Throwable t) { unexpected(t); }
127
128 if (c instanceof List) {
129 final List list = (List) c;
130 try {
131 final ListIterator it = list.listIterator(0);
132 it.next();
133 final Object x = it.previous();
134 THROWS(NoSuchElementException.class, () -> it.previous());
135 try { it.remove(); }
136 catch (UnsupportedOperationException exc) { return; }
137 pass();
138 check(! list.get(0).equals(x));
139 } catch (Throwable t) { unexpected(t); }
|