author | igerasim |
Mon, 02 Jun 2014 19:49:57 +0400 | |
changeset 24692 | 268fbc344d53 |
parent 7668 | d4a77089c587 |
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 |
||
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)); |
|
6672 | 51 |
testCollection(new ConcurrentLinkedDeque()); |
2 | 52 |
testCollection(new ConcurrentLinkedQueue()); |
4110 | 53 |
testCollection(new LinkedTransferQueue()); |
2 | 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, |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
87 |
() -> { while (true) it.next(); }); |
2 | 88 |
try { it.remove(); } |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
89 |
catch (UnsupportedOperationException exc) { return; } |
2 | 90 |
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(); |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
99 |
THROWS(NoSuchElementException.class, () -> it.previous()); |
2 | 100 |
try { it.remove(); } |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
101 |
catch (UnsupportedOperationException exc) { return; } |
2 | 102 |
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(); |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
110 |
THROWS(NoSuchElementException.class, () -> it.next()); |
2 | 111 |
try { it.remove(); } |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
112 |
catch (UnsupportedOperationException exc) { return; } |
2 | 113 |
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");} |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
7668
diff
changeset
|
133 |
interface Fun {void f() throws Throwable;} |
2 | 134 |
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 |
} |