1 |
jsr166 |
1.1 |
/* |
2 |
jsr166 |
1.6 |
* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. |
3 |
jsr166 |
1.1 |
* 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 |
jsr166 |
1.3 |
* 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 |
jsr166 |
1.1 |
*/ |
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.*; |
32 |
|
|
import java.util.concurrent.*; |
33 |
|
|
|
34 |
|
|
@SuppressWarnings("unchecked") |
35 |
|
|
public class IteratorAtEnd { |
36 |
|
|
private static final int SIZE = 6; |
37 |
|
|
|
38 |
|
|
static void realMain(String[] args) throws Throwable { |
39 |
|
|
testCollection(new ArrayList()); |
40 |
|
|
testCollection(new Vector()); |
41 |
|
|
testCollection(new LinkedList()); |
42 |
|
|
testCollection(new ArrayDeque()); |
43 |
|
|
testCollection(new TreeSet()); |
44 |
|
|
testCollection(new CopyOnWriteArrayList()); |
45 |
|
|
testCollection(new CopyOnWriteArraySet()); |
46 |
|
|
testCollection(new ConcurrentSkipListSet()); |
47 |
|
|
|
48 |
|
|
testCollection(new PriorityQueue()); |
49 |
|
|
testCollection(new LinkedBlockingQueue()); |
50 |
|
|
testCollection(new ArrayBlockingQueue(100)); |
51 |
jsr166 |
1.2 |
testCollection(new ConcurrentLinkedDeque()); |
52 |
jsr166 |
1.1 |
testCollection(new ConcurrentLinkedQueue()); |
53 |
|
|
testCollection(new LinkedTransferQueue()); |
54 |
|
|
|
55 |
|
|
testMap(new HashMap()); |
56 |
|
|
testMap(new Hashtable()); |
57 |
|
|
testMap(new LinkedHashMap()); |
58 |
|
|
testMap(new WeakHashMap()); |
59 |
|
|
testMap(new IdentityHashMap()); |
60 |
|
|
testMap(new ConcurrentHashMap()); |
61 |
|
|
testMap(new ConcurrentSkipListMap()); |
62 |
|
|
testMap(new TreeMap()); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
static void testCollection(Collection c) { |
66 |
|
|
try { |
67 |
|
|
for (int i = 0; i < SIZE; i++) |
68 |
|
|
c.add(i); |
69 |
|
|
test(c); |
70 |
|
|
} catch (Throwable t) { unexpected(t); } |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
static void testMap(Map m) { |
74 |
|
|
try { |
75 |
|
|
for (int i = 0; i < 3*SIZE; i++) |
76 |
|
|
m.put(i, i); |
77 |
|
|
test(m.values()); |
78 |
|
|
test(m.keySet()); |
79 |
|
|
test(m.entrySet()); |
80 |
|
|
} catch (Throwable t) { unexpected(t); } |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
static void test(Collection c) { |
84 |
|
|
try { |
85 |
|
|
final Iterator it = c.iterator(); |
86 |
|
|
THROWS(NoSuchElementException.class, |
87 |
jsr166 |
1.6 |
() -> { while (true) it.next(); }); |
88 |
jsr166 |
1.1 |
try { it.remove(); } |
89 |
jsr166 |
1.6 |
catch (UnsupportedOperationException exc) { return; } |
90 |
jsr166 |
1.1 |
pass(); |
91 |
|
|
} catch (Throwable t) { unexpected(t); } |
92 |
|
|
|
93 |
|
|
if (c instanceof List) { |
94 |
|
|
final List list = (List) c; |
95 |
|
|
try { |
96 |
|
|
final ListIterator it = list.listIterator(0); |
97 |
|
|
it.next(); |
98 |
|
|
final Object x = it.previous(); |
99 |
jsr166 |
1.6 |
THROWS(NoSuchElementException.class, () -> it.previous()); |
100 |
jsr166 |
1.1 |
try { it.remove(); } |
101 |
jsr166 |
1.6 |
catch (UnsupportedOperationException exc) { return; } |
102 |
jsr166 |
1.1 |
pass(); |
103 |
|
|
check(! list.get(0).equals(x)); |
104 |
|
|
} catch (Throwable t) { unexpected(t); } |
105 |
|
|
|
106 |
|
|
try { |
107 |
|
|
final ListIterator it = list.listIterator(list.size()); |
108 |
|
|
it.previous(); |
109 |
|
|
final Object x = it.next(); |
110 |
jsr166 |
1.6 |
THROWS(NoSuchElementException.class, () -> it.next()); |
111 |
jsr166 |
1.1 |
try { it.remove(); } |
112 |
jsr166 |
1.6 |
catch (UnsupportedOperationException exc) { return; } |
113 |
jsr166 |
1.1 |
pass(); |
114 |
|
|
check(! list.get(list.size()-1).equals(x)); |
115 |
|
|
} catch (Throwable t) { unexpected(t); } |
116 |
|
|
} |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
//--------------------- Infrastructure --------------------------- |
120 |
|
|
static volatile int passed = 0, failed = 0; |
121 |
|
|
static void pass() {passed++;} |
122 |
|
|
static void fail() {failed++; Thread.dumpStack();} |
123 |
|
|
static void fail(String msg) {System.out.println(msg); fail();} |
124 |
|
|
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
125 |
|
|
static void check(boolean cond) {if (cond) pass(); else fail();} |
126 |
|
|
static void equal(Object x, Object y) { |
127 |
|
|
if (x == null ? y == null : x.equals(y)) pass(); |
128 |
|
|
else fail(x + " not equal to " + y);} |
129 |
|
|
public static void main(String[] args) throws Throwable { |
130 |
|
|
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
131 |
|
|
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
132 |
|
|
if (failed > 0) throw new AssertionError("Some tests failed");} |
133 |
jsr166 |
1.6 |
interface Fun {void f() throws Throwable;} |
134 |
jsr166 |
1.1 |
static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
135 |
|
|
for (Fun f : fs) |
136 |
|
|
try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
137 |
|
|
catch (Throwable t) { |
138 |
|
|
if (k.isAssignableFrom(t.getClass())) pass(); |
139 |
|
|
else unexpected(t);}} |
140 |
|
|
} |