author | pli |
Tue, 16 Jul 2019 00:57:00 +0000 | |
changeset 55689 | 8c5c9d86e1d6 |
parent 48541 | 946e34c2dec9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
2 |
* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
||
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
31 |
import java.util.ArrayDeque; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
32 |
import java.util.ArrayList; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
33 |
import java.util.Collection; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
34 |
import java.util.HashMap; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
35 |
import java.util.Hashtable; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
36 |
import java.util.IdentityHashMap; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
37 |
import java.util.Iterator; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
38 |
import java.util.LinkedHashMap; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
39 |
import java.util.LinkedList; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
40 |
import java.util.List; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
41 |
import java.util.ListIterator; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
42 |
import java.util.Map; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
43 |
import java.util.NoSuchElementException; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
44 |
import java.util.PriorityQueue; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
45 |
import java.util.TreeMap; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
46 |
import java.util.TreeSet; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
47 |
import java.util.Vector; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
48 |
import java.util.WeakHashMap; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
49 |
import java.util.concurrent.ArrayBlockingQueue; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
50 |
import java.util.concurrent.ConcurrentHashMap; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
51 |
import java.util.concurrent.ConcurrentLinkedDeque; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
52 |
import java.util.concurrent.ConcurrentLinkedQueue; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
53 |
import java.util.concurrent.ConcurrentSkipListMap; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
54 |
import java.util.concurrent.ConcurrentSkipListSet; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
55 |
import java.util.concurrent.CopyOnWriteArrayList; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
56 |
import java.util.concurrent.CopyOnWriteArraySet; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
57 |
import java.util.concurrent.LinkedBlockingQueue; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
58 |
import java.util.concurrent.LinkedTransferQueue; |
2 | 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)); |
|
6672 | 77 |
testCollection(new ConcurrentLinkedDeque()); |
2 | 78 |
testCollection(new ConcurrentLinkedQueue()); |
4110 | 79 |
testCollection(new LinkedTransferQueue()); |
2 | 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, |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
113 |
() -> { while (true) it.next(); }); |
2 | 114 |
try { it.remove(); } |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
115 |
catch (UnsupportedOperationException exc) { return; } |
2 | 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(); |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
125 |
THROWS(NoSuchElementException.class, () -> it.previous()); |
2 | 126 |
try { it.remove(); } |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
127 |
catch (UnsupportedOperationException exc) { return; } |
2 | 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(); |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
136 |
THROWS(NoSuchElementException.class, () -> it.next()); |
2 | 137 |
try { it.remove(); } |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
138 |
catch (UnsupportedOperationException exc) { return; } |
2 | 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");} |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
159 |
interface Fun {void f() throws Throwable;} |
2 | 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 |
} |