Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Collection/IteratorAtEnd.java
41149 views
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); }
131
132
try {
133
final ListIterator it = list.listIterator(list.size());
134
it.previous();
135
final Object x = it.next();
136
THROWS(NoSuchElementException.class, () -> it.next());
137
try { it.remove(); }
138
catch (UnsupportedOperationException exc) { return; }
139
pass();
140
check(! list.get(list.size()-1).equals(x));
141
} catch (Throwable t) { unexpected(t); }
142
}
143
}
144
145
//--------------------- Infrastructure ---------------------------
146
static volatile int passed = 0, failed = 0;
147
static void pass() {passed++;}
148
static void fail() {failed++; Thread.dumpStack();}
149
static void fail(String msg) {System.out.println(msg); fail();}
150
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
151
static void check(boolean cond) {if (cond) pass(); else fail();}
152
static void equal(Object x, Object y) {
153
if (x == null ? y == null : x.equals(y)) pass();
154
else fail(x + " not equal to " + y);}
155
public static void main(String[] args) throws Throwable {
156
try {realMain(args);} catch (Throwable t) {unexpected(t);}
157
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
158
if (failed > 0) throw new AssertionError("Some tests failed");}
159
interface Fun {void f() throws Throwable;}
160
static void THROWS(Class<? extends Throwable> k, Fun... fs) {
161
for (Fun f : fs)
162
try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
163
catch (Throwable t) {
164
if (k.isAssignableFrom(t.getClass())) pass();
165
else unexpected(t);}}
166
}
167
168