ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collection/IteratorAtEnd.java
Revision: 1.5
Committed: Mon Feb 2 07:33:31 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +3 -3 lines
Log Message:
error: as of release 9, '_' is a keyword, and may not be used as an identifier

File Contents

# Content
1 /*
2 * Copyright (c) 2007, 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.*;
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 testCollection(new ConcurrentLinkedDeque());
52 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 new Fun() {void f() { while (true) it.next(); }});
88 try { it.remove(); }
89 catch (UnsupportedOperationException ok) { return; }
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();
99 THROWS(NoSuchElementException.class,
100 new Fun() {void f() { it.previous(); }});
101 try { it.remove(); }
102 catch (UnsupportedOperationException ok) { return; }
103 pass();
104 check(! list.get(0).equals(x));
105 } catch (Throwable t) { unexpected(t); }
106
107 try {
108 final ListIterator it = list.listIterator(list.size());
109 it.previous();
110 final Object x = it.next();
111 THROWS(NoSuchElementException.class,
112 new Fun() {void f() { it.next(); }});
113 try { it.remove(); }
114 catch (UnsupportedOperationException ok) { return; }
115 pass();
116 check(! list.get(list.size()-1).equals(x));
117 } catch (Throwable t) { unexpected(t); }
118 }
119 }
120
121 //--------------------- Infrastructure ---------------------------
122 static volatile int passed = 0, failed = 0;
123 static void pass() {passed++;}
124 static void fail() {failed++; Thread.dumpStack();}
125 static void fail(String msg) {System.out.println(msg); fail();}
126 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
127 static void check(boolean cond) {if (cond) pass(); else fail();}
128 static void equal(Object x, Object y) {
129 if (x == null ? y == null : x.equals(y)) pass();
130 else fail(x + " not equal to " + y);}
131 public static void main(String[] args) throws Throwable {
132 try {realMain(args);} catch (Throwable t) {unexpected(t);}
133 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
134 if (failed > 0) throw new AssertionError("Some tests failed");}
135 private abstract static class Fun {abstract void f() throws Throwable;}
136 static void THROWS(Class<? extends Throwable> k, Fun... fs) {
137 for (Fun f : fs)
138 try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
139 catch (Throwable t) {
140 if (k.isAssignableFrom(t.getClass())) pass();
141 else unexpected(t);}}
142 }