author | dl |
Tue, 25 Aug 2009 19:19:42 -0700 | |
changeset 3708 | f838f712922e |
parent 3415 | 79309d6eab38 |
child 4184 | 66ef929d58f2 |
child 4110 | ac033ba6ede4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
2 |
* Copyright 2005-2008 Sun Microsystems, Inc. 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 |
* |
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6207984 6272521 6192552 6269713 6197726 6260652 5073546 4137464 |
|
27 |
* 4155650 4216399 4294891 6282555 6318622 6355327 6383475 6420753 |
|
494
320ce398f07e
6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents:
493
diff
changeset
|
28 |
* 6431845 4802633 6570566 6570575 6570631 6570924 6691185 6691215 |
2 | 29 |
* @summary Run many tests on many Collection and Map implementations |
30 |
* @author Martin Buchholz |
|
31 |
*/ |
|
32 |
||
33 |
/* Mother Of All (Collection) Tests |
|
34 |
* |
|
35 |
* Testing of collection classes is often spotty, because many tests |
|
36 |
* need to be performed on many implementations, but the onus on |
|
37 |
* writing the tests falls on the engineer introducing the new |
|
38 |
* implementation. |
|
39 |
* |
|
40 |
* The idea of this mega-test is that: |
|
41 |
* |
|
42 |
* An engineer adding a new collection implementation could simply add |
|
43 |
* their new implementation to a list of implementations in this |
|
44 |
* test's main method. Any general purpose Collection<Integer> or |
|
45 |
* Map<Integer,Integer> class is appropriate. |
|
46 |
* |
|
47 |
* An engineer fixing a regression could add their regression test here and |
|
48 |
* simultaneously test all other implementations. |
|
49 |
*/ |
|
50 |
||
51 |
import java.io.*; |
|
52 |
import java.util.*; |
|
53 |
import java.util.concurrent.*; |
|
54 |
import static java.util.Collections.*; |
|
55 |
||
56 |
public class MOAT { |
|
57 |
public static void realMain(String[] args) { |
|
58 |
||
59 |
testCollection(new LinkedHashSet<Integer>()); |
|
60 |
testCollection(new HashSet<Integer>()); |
|
61 |
testCollection(new Vector<Integer>()); |
|
62 |
testCollection(new Vector<Integer>().subList(0,0)); |
|
63 |
testCollection(new ArrayDeque<Integer>()); |
|
64 |
testCollection(new ArrayList<Integer>()); |
|
65 |
testCollection(new ArrayList<Integer>().subList(0,0)); |
|
66 |
testCollection(new LinkedList<Integer>()); |
|
67 |
testCollection(new LinkedList<Integer>().subList(0,0)); |
|
68 |
testCollection(new TreeSet<Integer>()); |
|
69 |
||
70 |
testCollection(new CopyOnWriteArrayList<Integer>()); |
|
71 |
testCollection(new CopyOnWriteArrayList<Integer>().subList(0,0)); |
|
72 |
testCollection(new CopyOnWriteArraySet<Integer>()); |
|
73 |
testCollection(new PriorityQueue<Integer>()); |
|
74 |
testCollection(new PriorityBlockingQueue<Integer>()); |
|
75 |
testCollection(new ArrayBlockingQueue<Integer>(20)); |
|
76 |
testCollection(new LinkedBlockingQueue<Integer>(20)); |
|
77 |
testCollection(new LinkedBlockingDeque<Integer>(20)); |
|
78 |
testCollection(new ConcurrentLinkedQueue<Integer>()); |
|
3708 | 79 |
// testCollection(new LinkedTransferQueue<Integer>()); |
2 | 80 |
testCollection(new ConcurrentSkipListSet<Integer>()); |
81 |
testCollection(Arrays.asList(new Integer(42))); |
|
82 |
testCollection(Arrays.asList(1,2,3)); |
|
83 |
testCollection(nCopies(25,1)); |
|
84 |
testImmutableList(nCopies(25,1)); |
|
85 |
testImmutableList(unmodifiableList(Arrays.asList(1,2,3))); |
|
86 |
||
87 |
testMap(new HashMap<Integer,Integer>()); |
|
88 |
testMap(new LinkedHashMap<Integer,Integer>()); |
|
89 |
testMap(new WeakHashMap<Integer,Integer>()); |
|
90 |
testMap(new IdentityHashMap<Integer,Integer>()); |
|
91 |
testMap(new TreeMap<Integer,Integer>()); |
|
92 |
testMap(new Hashtable<Integer,Integer>()); |
|
93 |
testMap(new ConcurrentHashMap<Integer,Integer>(10, 0.5f)); |
|
94 |
testMap(new ConcurrentSkipListMap<Integer,Integer>()); |
|
95 |
||
96 |
// Empty collections |
|
97 |
final List<Integer> emptyArray = Arrays.asList(new Integer[]{}); |
|
98 |
testCollection(emptyArray); |
|
99 |
testEmptyList(emptyArray); |
|
100 |
THROWS(IndexOutOfBoundsException.class, |
|
101 |
new Fun(){void f(){ emptyArray.set(0,1); }}); |
|
102 |
THROWS(UnsupportedOperationException.class, |
|
103 |
new Fun(){void f(){ emptyArray.add(0,1); }}); |
|
104 |
||
105 |
List<Integer> noOne = nCopies(0,1); |
|
106 |
testCollection(noOne); |
|
107 |
testEmptyList(noOne); |
|
108 |
testImmutableList(noOne); |
|
109 |
||
110 |
Set<Integer> emptySet = emptySet(); |
|
111 |
testCollection(emptySet); |
|
112 |
testEmptySet(emptySet); |
|
113 |
testEmptySet(EMPTY_SET); |
|
114 |
testImmutableSet(emptySet); |
|
115 |
||
116 |
List<Integer> emptyList = emptyList(); |
|
117 |
testCollection(emptyList); |
|
118 |
testEmptyList(emptyList); |
|
119 |
testEmptyList(EMPTY_LIST); |
|
120 |
testImmutableList(emptyList); |
|
121 |
||
122 |
Map<Integer,Integer> emptyMap = emptyMap(); |
|
123 |
testMap(emptyMap); |
|
124 |
testEmptyMap(emptyMap); |
|
125 |
testEmptyMap(EMPTY_MAP); |
|
126 |
testImmutableMap(emptyMap); |
|
127 |
||
128 |
// Singleton collections |
|
129 |
Set<Integer> singletonSet = singleton(1); |
|
130 |
equal(singletonSet.size(), 1); |
|
131 |
testCollection(singletonSet); |
|
132 |
testImmutableSet(singletonSet); |
|
133 |
||
134 |
List<Integer> singletonList = singletonList(1); |
|
135 |
equal(singletonList.size(), 1); |
|
136 |
testCollection(singletonList); |
|
137 |
testImmutableList(singletonList); |
|
138 |
testImmutableList(singletonList.subList(0,1)); |
|
139 |
testImmutableList(singletonList.subList(0,1).subList(0,1)); |
|
140 |
testEmptyList(singletonList.subList(0,0)); |
|
141 |
testEmptyList(singletonList.subList(0,0).subList(0,0)); |
|
142 |
||
143 |
Map<Integer,Integer> singletonMap = singletonMap(1,2); |
|
144 |
equal(singletonMap.size(), 1); |
|
145 |
testMap(singletonMap); |
|
146 |
testImmutableMap(singletonMap); |
|
147 |
} |
|
148 |
||
149 |
private static void checkContainsSelf(Collection<Integer> c) { |
|
150 |
check(c.containsAll(c)); |
|
151 |
check(c.containsAll(Arrays.asList(c.toArray()))); |
|
152 |
check(c.containsAll(Arrays.asList(c.toArray(new Integer[0])))); |
|
153 |
} |
|
154 |
||
155 |
private static void checkContainsEmpty(Collection<Integer> c) { |
|
156 |
check(c.containsAll(new ArrayList<Integer>())); |
|
157 |
} |
|
158 |
||
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
159 |
private static <T> void testEmptyCollection(Collection<T> c) { |
2 | 160 |
check(c.isEmpty()); |
161 |
equal(c.size(), 0); |
|
162 |
equal(c.toString(),"[]"); |
|
163 |
equal(c.toArray().length, 0); |
|
164 |
equal(c.toArray(new Object[0]).length, 0); |
|
3708 | 165 |
check(c.toArray(new Object[]{42})[0] == null); |
2 | 166 |
|
167 |
Object[] a = new Object[1]; a[0] = Boolean.TRUE; |
|
168 |
equal(c.toArray(a), a); |
|
169 |
equal(a[0], null); |
|
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
170 |
testEmptyIterator(c.iterator()); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
171 |
} |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
172 |
|
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
173 |
static <T> void testEmptyIterator(final Iterator<T> it) { |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
174 |
if (rnd.nextBoolean()) |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
175 |
check(! it.hasNext()); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
176 |
|
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
177 |
THROWS(NoSuchElementException.class, |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
178 |
new Fun(){void f(){ it.next(); }}); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
179 |
|
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
180 |
try { it.remove(); } |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
181 |
catch (IllegalStateException _) { pass(); } |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
182 |
catch (UnsupportedOperationException _) { pass(); } |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
183 |
catch (Throwable t) { unexpected(t); } |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
184 |
|
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
185 |
if (rnd.nextBoolean()) |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
186 |
check(! it.hasNext()); |
2 | 187 |
} |
188 |
||
189 |
private static void testEmptyList(List<?> c) { |
|
190 |
testEmptyCollection(c); |
|
191 |
equal(c.hashCode(), 1); |
|
192 |
equal2(c, Collections.<Integer>emptyList()); |
|
193 |
} |
|
194 |
||
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
195 |
private static <T> void testEmptySet(Set<T> c) { |
2 | 196 |
testEmptyCollection(c); |
197 |
equal(c.hashCode(), 0); |
|
198 |
equal2(c, Collections.<Integer>emptySet()); |
|
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
199 |
if (c instanceof NavigableSet<?>) |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
200 |
testEmptyIterator(((NavigableSet<T>)c).descendingIterator()); |
2 | 201 |
} |
202 |
||
203 |
private static void testImmutableCollection(final Collection<Integer> c) { |
|
204 |
THROWS(UnsupportedOperationException.class, |
|
205 |
new Fun(){void f(){ c.add(99); }}, |
|
206 |
new Fun(){void f(){ c.addAll(singleton(99)); }}); |
|
207 |
if (! c.isEmpty()) { |
|
208 |
final Integer first = c.iterator().next(); |
|
209 |
THROWS(UnsupportedOperationException.class, |
|
210 |
new Fun(){void f(){ c.clear(); }}, |
|
211 |
new Fun(){void f(){ c.remove(first); }}, |
|
212 |
new Fun(){void f(){ c.removeAll(singleton(first)); }}, |
|
213 |
new Fun(){void f(){ c.retainAll(emptyList()); }} |
|
214 |
); |
|
215 |
} |
|
216 |
} |
|
217 |
||
218 |
private static void testImmutableSet(final Set<Integer> c) { |
|
219 |
testImmutableCollection(c); |
|
220 |
} |
|
221 |
||
222 |
private static void testImmutableList(final List<Integer> c) { |
|
223 |
testList(c); |
|
224 |
testImmutableCollection(c); |
|
225 |
THROWS(UnsupportedOperationException.class, |
|
226 |
new Fun(){void f(){ c.set(0,42); }}, |
|
227 |
new Fun(){void f(){ c.add(0,42); }}, |
|
228 |
new Fun(){void f(){ c.addAll(0,singleton(86)); }}); |
|
229 |
if (! c.isEmpty()) |
|
230 |
THROWS(UnsupportedOperationException.class, |
|
231 |
new Fun(){void f(){ |
|
232 |
Iterator<Integer> it = c.iterator(); |
|
233 |
it.next(); it.remove();}}, |
|
234 |
new Fun(){void f(){ |
|
235 |
ListIterator<Integer> it = c.listIterator(); |
|
236 |
it.next(); it.remove();}}); |
|
237 |
} |
|
238 |
||
239 |
private static void clear(Collection<Integer> c) { |
|
240 |
try { c.clear(); } |
|
241 |
catch (Throwable t) { unexpected(t); } |
|
242 |
testEmptyCollection(c); |
|
243 |
} |
|
244 |
||
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
245 |
private static <K,V> void testEmptyMap(final Map<K,V> m) { |
2 | 246 |
check(m.isEmpty()); |
247 |
equal(m.size(), 0); |
|
248 |
equal(m.toString(),"{}"); |
|
249 |
testEmptySet(m.keySet()); |
|
250 |
testEmptySet(m.entrySet()); |
|
251 |
testEmptyCollection(m.values()); |
|
494
320ce398f07e
6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents:
493
diff
changeset
|
252 |
|
320ce398f07e
6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents:
493
diff
changeset
|
253 |
try { check(! m.containsValue(null)); } |
320ce398f07e
6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents:
493
diff
changeset
|
254 |
catch (NullPointerException _) { /* OK */ } |
320ce398f07e
6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents:
493
diff
changeset
|
255 |
try { check(! m.containsKey(null)); } |
320ce398f07e
6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents:
493
diff
changeset
|
256 |
catch (NullPointerException _) { /* OK */ } |
320ce398f07e
6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents:
493
diff
changeset
|
257 |
check(! m.containsValue(1)); |
320ce398f07e
6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents:
493
diff
changeset
|
258 |
check(! m.containsKey(1)); |
2 | 259 |
} |
260 |
||
261 |
private static void testImmutableMap(final Map<Integer,Integer> m) { |
|
262 |
THROWS(UnsupportedOperationException.class, |
|
263 |
new Fun(){void f(){ m.put(1,1); }}, |
|
264 |
new Fun(){void f(){ m.putAll(singletonMap(1,1)); }}); |
|
265 |
if (! m.isEmpty()) { |
|
266 |
final Integer first = m.keySet().iterator().next(); |
|
267 |
THROWS(UnsupportedOperationException.class, |
|
268 |
new Fun(){void f(){ m.remove(first); }}, |
|
269 |
new Fun(){void f(){ m.clear(); }}); |
|
270 |
final Map.Entry<Integer,Integer> me |
|
271 |
= m.entrySet().iterator().next(); |
|
272 |
Integer key = me.getKey(); |
|
273 |
Integer val = me.getValue(); |
|
274 |
THROWS(UnsupportedOperationException.class, |
|
275 |
new Fun(){void f(){ me.setValue(3); }}); |
|
276 |
equal(key, me.getKey()); |
|
277 |
equal(val, me.getValue()); |
|
278 |
} |
|
279 |
testImmutableSet(m.keySet()); |
|
280 |
testImmutableCollection(m.values()); |
|
281 |
//testImmutableSet(m.entrySet()); |
|
282 |
} |
|
283 |
||
284 |
private static void clear(Map<?,?> m) { |
|
285 |
try { m.clear(); } |
|
286 |
catch (Throwable t) { unexpected(t); } |
|
287 |
testEmptyMap(m); |
|
288 |
} |
|
289 |
||
290 |
private static void oneElement(Collection<Integer> c) { |
|
291 |
clear(c); |
|
292 |
try { |
|
293 |
check(c.add(-42)); |
|
294 |
equal(c.toString(), "[-42]"); |
|
295 |
if (c instanceof Set) check(! c.add(-42)); |
|
296 |
} catch (Throwable t) { unexpected(t); } |
|
297 |
check(! c.isEmpty()); check(c.size() == 1); |
|
298 |
} |
|
299 |
||
300 |
private static boolean supportsAdd(Collection<Integer> c) { |
|
301 |
try { check(c.add(778347983)); } |
|
302 |
catch (UnsupportedOperationException t) { return false; } |
|
303 |
catch (Throwable t) { unexpected(t); } |
|
304 |
||
305 |
try { |
|
306 |
check(c.contains(778347983)); |
|
307 |
check(c.remove(778347983)); |
|
308 |
} catch (Throwable t) { unexpected(t); } |
|
309 |
return true; |
|
310 |
} |
|
311 |
||
312 |
private static boolean supportsRemove(Collection<Integer> c) { |
|
313 |
try { check(! c.remove(19134032)); } |
|
314 |
catch (UnsupportedOperationException t) { return false; } |
|
315 |
catch (Throwable t) { unexpected(t); } |
|
316 |
return true; |
|
317 |
} |
|
318 |
||
319 |
private static void checkFunctionalInvariants(Collection<Integer> c) { |
|
320 |
try { |
|
321 |
checkContainsSelf(c); |
|
322 |
checkContainsEmpty(c); |
|
323 |
check(c.size() != 0 ^ c.isEmpty()); |
|
324 |
||
325 |
{ |
|
326 |
int size = 0; |
|
327 |
for (Integer i : c) size++; |
|
328 |
check(c.size() == size); |
|
329 |
} |
|
330 |
||
331 |
check(c.toArray().length == c.size()); |
|
332 |
check(c.toArray().getClass() == Object[].class |
|
333 |
|| |
|
334 |
// !!!! |
|
335 |
// 6260652: (coll) Arrays.asList(x).toArray().getClass() |
|
336 |
// should be Object[].class |
|
337 |
(c.getClass().getName().equals("java.util.Arrays$ArrayList")) |
|
338 |
); |
|
339 |
for (int size : new int[]{0,1,c.size(), c.size()+1}) { |
|
340 |
Integer[] a = c.toArray(new Integer[size]); |
|
341 |
check((size > c.size()) || a.length == c.size()); |
|
342 |
int i = 0; for (Integer j : c) check(a[i++] == j); |
|
343 |
check((size <= c.size()) || (a[c.size()] == null)); |
|
344 |
check(a.getClass() == Integer[].class); |
|
345 |
} |
|
346 |
||
347 |
check(c.equals(c)); |
|
348 |
if (c instanceof Serializable) { |
|
349 |
//System.out.printf("Serializing %s%n", c.getClass().getName()); |
|
350 |
try { |
|
351 |
Object clone = serialClone(c); |
|
352 |
equal(c instanceof Serializable, |
|
353 |
clone instanceof Serializable); |
|
354 |
equal(c instanceof RandomAccess, |
|
355 |
clone instanceof RandomAccess); |
|
356 |
if ((c instanceof List) || (c instanceof Set)) |
|
357 |
equal(c, clone); |
|
358 |
else |
|
359 |
equal(new HashSet<Integer>(c), |
|
360 |
new HashSet<Integer>(serialClone(c))); |
|
361 |
} catch (Error xxx) { |
|
362 |
if (! (xxx.getCause() instanceof NotSerializableException)) |
|
363 |
throw xxx; |
|
364 |
} |
|
365 |
} |
|
366 |
} |
|
367 |
catch (Throwable t) { unexpected(t); } |
|
368 |
} |
|
369 |
||
370 |
//---------------------------------------------------------------- |
|
371 |
// If add(null) succeeds, contains(null) & remove(null) should succeed |
|
372 |
//---------------------------------------------------------------- |
|
373 |
private static void testNullElement(Collection<Integer> c) { |
|
374 |
// !!!! 5018849: (coll) TreeSet.contains(null) does not agree with Javadoc |
|
375 |
if (c instanceof TreeSet) return; |
|
376 |
||
377 |
try { |
|
378 |
check(c.add(null)); |
|
379 |
if (c.size() == 1) |
|
380 |
equal(c.toString(), "[null]"); |
|
381 |
try { |
|
382 |
checkFunctionalInvariants(c); |
|
383 |
check(c.contains(null)); |
|
384 |
check(c.remove(null)); |
|
385 |
} |
|
386 |
catch (Throwable t) { unexpected(t); } |
|
387 |
} |
|
388 |
catch (NullPointerException e) { /* OK */ } |
|
389 |
catch (Throwable t) { unexpected(t); } |
|
390 |
} |
|
391 |
||
392 |
||
393 |
//---------------------------------------------------------------- |
|
394 |
// If add("x") succeeds, contains("x") & remove("x") should succeed |
|
395 |
//---------------------------------------------------------------- |
|
396 |
@SuppressWarnings("unchecked") |
|
397 |
private static void testStringElement(Collection<Integer> c) { |
|
398 |
Collection x = (Collection)c; // Make type-unsafe |
|
399 |
try { |
|
400 |
check(x.add("x")); |
|
401 |
try { |
|
402 |
check(x.contains("x")); |
|
403 |
check(x.remove("x")); |
|
404 |
} catch (Throwable t) { unexpected(t); } |
|
405 |
} |
|
406 |
catch (ClassCastException e) { /* OK */ } |
|
407 |
catch (Throwable t) { unexpected(t); } |
|
408 |
} |
|
409 |
||
410 |
private static void testConcurrentCollection(Collection<Integer> c) { |
|
411 |
try { |
|
412 |
c.add(1); |
|
413 |
Iterator<Integer> it = c.iterator(); |
|
414 |
check(it.hasNext()); |
|
415 |
clear(c); |
|
416 |
check(it.next() instanceof Integer); // No CME |
|
417 |
check(c.isEmpty()); |
|
418 |
} |
|
419 |
catch (Throwable t) { unexpected(t); } |
|
420 |
} |
|
421 |
||
422 |
private static void testQueue(Queue<Integer> q) { |
|
423 |
q.clear(); |
|
424 |
for (int i = 0; i < 5; i++) |
|
425 |
q.add(i); |
|
426 |
equal(q.size(), 5); |
|
427 |
checkFunctionalInvariants(q); |
|
428 |
q.poll(); |
|
429 |
equal(q.size(), 4); |
|
430 |
checkFunctionalInvariants(q); |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
431 |
if ((q instanceof LinkedBlockingQueue) || |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
432 |
(q instanceof LinkedBlockingDeque) || |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
433 |
(q instanceof ConcurrentLinkedQueue)) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
434 |
testQueueIteratorRemove(q); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
435 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
436 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
437 |
|
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
438 |
private static void testQueueIteratorRemove(Queue<Integer> q) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
439 |
System.err.printf("testQueueIteratorRemove %s%n", |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
440 |
q.getClass().getSimpleName()); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
441 |
q.clear(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
442 |
for (int i = 0; i < 5; i++) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
443 |
q.add(i); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
444 |
Iterator<Integer> it = q.iterator(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
445 |
check(it.hasNext()); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
446 |
for (int i = 3; i >= 0; i--) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
447 |
q.remove(i); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
448 |
equal(it.next(), 0); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
449 |
equal(it.next(), 4); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
450 |
|
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
451 |
q.clear(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
452 |
for (int i = 0; i < 5; i++) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
453 |
q.add(i); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
454 |
it = q.iterator(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
455 |
equal(it.next(), 0); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
456 |
check(it.hasNext()); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
457 |
for (int i = 1; i < 4; i++) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
458 |
q.remove(i); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
459 |
equal(it.next(), 1); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
460 |
equal(it.next(), 4); |
2 | 461 |
} |
462 |
||
463 |
private static void testList(final List<Integer> l) { |
|
464 |
//---------------------------------------------------------------- |
|
465 |
// 4802633: (coll) AbstractList.addAll(-1,emptyCollection) |
|
466 |
// doesn't throw IndexOutOfBoundsException |
|
467 |
//---------------------------------------------------------------- |
|
468 |
try { |
|
469 |
l.addAll(-1, Collections.<Integer>emptyList()); |
|
470 |
fail("Expected IndexOutOfBoundsException not thrown"); |
|
471 |
} |
|
472 |
catch (UnsupportedOperationException _) {/* OK */} |
|
473 |
catch (IndexOutOfBoundsException _) {/* OK */} |
|
474 |
catch (Throwable t) { unexpected(t); } |
|
475 |
||
476 |
// equal(l instanceof Serializable, |
|
477 |
// l.subList(0,0) instanceof Serializable); |
|
478 |
if (l.subList(0,0) instanceof Serializable) |
|
479 |
check(l instanceof Serializable); |
|
480 |
||
481 |
equal(l instanceof RandomAccess, |
|
482 |
l.subList(0,0) instanceof RandomAccess); |
|
483 |
} |
|
484 |
||
485 |
private static void testCollection(Collection<Integer> c) { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
486 |
try { testCollection1(c); } |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
487 |
catch (Throwable t) { unexpected(t); } |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
488 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
489 |
|
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
490 |
private static void testCollection1(Collection<Integer> c) { |
2 | 491 |
|
492 |
System.out.println("\n==> " + c.getClass().getName()); |
|
493 |
||
494 |
checkFunctionalInvariants(c); |
|
495 |
||
496 |
if (! supportsAdd(c)) return; |
|
497 |
//System.out.println("add() supported"); |
|
498 |
||
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
499 |
if (c instanceof NavigableSet) { |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
500 |
System.out.println("NavigableSet tests..."); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
501 |
|
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
502 |
NavigableSet<Integer> ns = (NavigableSet<Integer>)c; |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
503 |
testNavigableSet(ns); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
504 |
testNavigableSet(ns.headSet(6, false)); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
505 |
testNavigableSet(ns.headSet(5, true)); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
506 |
testNavigableSet(ns.tailSet(0, false)); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
507 |
testNavigableSet(ns.tailSet(1, true)); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
508 |
testNavigableSet(ns.subSet(0, false, 5, true)); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
509 |
testNavigableSet(ns.subSet(1, true, 6, false)); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
510 |
} |
2 | 511 |
|
512 |
if (c instanceof Queue) |
|
513 |
testQueue((Queue<Integer>)c); |
|
514 |
||
515 |
if (c instanceof List) |
|
516 |
testList((List<Integer>)c); |
|
517 |
||
518 |
check(supportsRemove(c)); |
|
519 |
||
520 |
try { |
|
521 |
oneElement(c); |
|
522 |
checkFunctionalInvariants(c); |
|
523 |
} |
|
524 |
catch (Throwable t) { unexpected(t); } |
|
525 |
||
526 |
clear(c); testNullElement(c); |
|
527 |
oneElement(c); testNullElement(c); |
|
528 |
||
529 |
clear(c); testStringElement(c); |
|
530 |
oneElement(c); testStringElement(c); |
|
531 |
||
532 |
if (c.getClass().getName().matches(".*concurrent.*")) |
|
533 |
testConcurrentCollection(c); |
|
534 |
||
535 |
//---------------------------------------------------------------- |
|
536 |
// The "all" operations should throw NPE when passed null |
|
537 |
//---------------------------------------------------------------- |
|
538 |
{ |
|
539 |
oneElement(c); |
|
540 |
try { |
|
541 |
c.removeAll(null); |
|
542 |
fail("Expected NullPointerException"); |
|
543 |
} |
|
544 |
catch (NullPointerException e) { pass(); } |
|
545 |
catch (Throwable t) { unexpected(t); } |
|
546 |
||
547 |
oneElement(c); |
|
548 |
try { |
|
549 |
c.retainAll(null); |
|
550 |
fail("Expected NullPointerException"); |
|
551 |
} |
|
552 |
catch (NullPointerException e) { pass(); } |
|
553 |
catch (Throwable t) { unexpected(t); } |
|
554 |
||
555 |
oneElement(c); |
|
556 |
try { |
|
557 |
c.addAll(null); |
|
558 |
fail("Expected NullPointerException"); |
|
559 |
} |
|
560 |
catch (NullPointerException e) { pass(); } |
|
561 |
catch (Throwable t) { unexpected(t); } |
|
562 |
||
563 |
oneElement(c); |
|
564 |
try { |
|
565 |
c.containsAll(null); |
|
566 |
fail("Expected NullPointerException"); |
|
567 |
} |
|
568 |
catch (NullPointerException e) { pass(); } |
|
569 |
catch (Throwable t) { unexpected(t); } |
|
570 |
} |
|
571 |
} |
|
572 |
||
573 |
//---------------------------------------------------------------- |
|
574 |
// Map |
|
575 |
//---------------------------------------------------------------- |
|
576 |
private static void checkFunctionalInvariants(Map<Integer,Integer> m) { |
|
577 |
check(m.keySet().size() == m.entrySet().size()); |
|
578 |
check(m.keySet().size() == m.size()); |
|
579 |
checkFunctionalInvariants(m.keySet()); |
|
580 |
checkFunctionalInvariants(m.values()); |
|
581 |
check(m.size() != 0 ^ m.isEmpty()); |
|
582 |
} |
|
583 |
||
584 |
private static void testMap(Map<Integer,Integer> m) { |
|
585 |
System.out.println("\n==> " + m.getClass().getName()); |
|
586 |
||
587 |
if (m instanceof ConcurrentMap) |
|
588 |
testConcurrentMap((ConcurrentMap<Integer,Integer>) m); |
|
589 |
||
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
590 |
if (m instanceof NavigableMap) { |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
591 |
System.out.println("NavigableMap tests..."); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
592 |
|
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
593 |
NavigableMap<Integer,Integer> nm = |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
594 |
(NavigableMap<Integer,Integer>) m; |
2428
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
595 |
testNavigableMapRemovers(nm); |
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
596 |
testNavigableMap(nm); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
597 |
testNavigableMap(nm.headMap(6, false)); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
598 |
testNavigableMap(nm.headMap(5, true)); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
599 |
testNavigableMap(nm.tailMap(0, false)); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
600 |
testNavigableMap(nm.tailMap(1, true)); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
601 |
testNavigableMap(nm.subMap(1, true, 6, false)); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
602 |
testNavigableMap(nm.subMap(0, false, 5, true)); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
603 |
} |
2 | 604 |
|
605 |
checkFunctionalInvariants(m); |
|
606 |
||
607 |
if (supportsClear(m)) { |
|
608 |
try { clear(m); } |
|
609 |
catch (Throwable t) { unexpected(t); } |
|
610 |
} |
|
611 |
||
612 |
if (supportsPut(m)) { |
|
613 |
try { |
|
614 |
check(m.put(3333, 77777) == null); |
|
615 |
check(m.put(9134, 74982) == null); |
|
616 |
check(m.get(9134) == 74982); |
|
617 |
check(m.put(9134, 1382) == 74982); |
|
618 |
check(m.get(9134) == 1382); |
|
619 |
check(m.size() == 2); |
|
620 |
checkFunctionalInvariants(m); |
|
621 |
checkNPEConsistency(m); |
|
622 |
} |
|
623 |
catch (Throwable t) { unexpected(t); } |
|
624 |
} |
|
625 |
} |
|
626 |
||
627 |
private static boolean supportsPut(Map<Integer,Integer> m) { |
|
628 |
// We're asking for .equals(...) semantics |
|
629 |
if (m instanceof IdentityHashMap) return false; |
|
630 |
||
631 |
try { check(m.put(778347983,12735) == null); } |
|
632 |
catch (UnsupportedOperationException t) { return false; } |
|
633 |
catch (Throwable t) { unexpected(t); } |
|
634 |
||
635 |
try { |
|
636 |
check(m.containsKey(778347983)); |
|
637 |
check(m.remove(778347983) != null); |
|
638 |
} catch (Throwable t) { unexpected(t); } |
|
639 |
return true; |
|
640 |
} |
|
641 |
||
642 |
private static boolean supportsClear(Map<?,?> m) { |
|
643 |
try { m.clear(); } |
|
644 |
catch (UnsupportedOperationException t) { return false; } |
|
645 |
catch (Throwable t) { unexpected(t); } |
|
646 |
return true; |
|
647 |
} |
|
648 |
||
649 |
//---------------------------------------------------------------- |
|
650 |
// ConcurrentMap |
|
651 |
//---------------------------------------------------------------- |
|
652 |
private static void testConcurrentMap(ConcurrentMap<Integer,Integer> m) { |
|
653 |
System.out.println("ConcurrentMap tests..."); |
|
654 |
||
655 |
try { |
|
656 |
clear(m); |
|
657 |
||
658 |
check(m.putIfAbsent(18357,7346) == null); |
|
659 |
check(m.containsKey(18357)); |
|
660 |
check(m.putIfAbsent(18357,8263) == 7346); |
|
661 |
try { m.putIfAbsent(18357,null); fail("NPE"); } |
|
662 |
catch (NullPointerException t) { } |
|
663 |
check(m.containsKey(18357)); |
|
664 |
||
665 |
check(! m.replace(18357,8888,7777)); |
|
666 |
check(m.containsKey(18357)); |
|
667 |
try { m.replace(18357,null,7777); fail("NPE"); } |
|
668 |
catch (NullPointerException t) { } |
|
669 |
check(m.containsKey(18357)); |
|
670 |
check(m.get(18357) == 7346); |
|
671 |
check(m.replace(18357,7346,5555)); |
|
672 |
check(m.replace(18357,5555,7346)); |
|
673 |
check(m.get(18357) == 7346); |
|
674 |
||
675 |
check(m.replace(92347,7834) == null); |
|
676 |
try { m.replace(18357,null); fail("NPE"); } |
|
677 |
catch (NullPointerException t) { } |
|
678 |
check(m.replace(18357,7346) == 7346); |
|
679 |
check(m.replace(18357,5555) == 7346); |
|
680 |
check(m.get(18357) == 5555); |
|
681 |
check(m.replace(18357,7346) == 5555); |
|
682 |
check(m.get(18357) == 7346); |
|
683 |
||
684 |
check(! m.remove(18357,9999)); |
|
685 |
check(m.get(18357) == 7346); |
|
686 |
check(m.containsKey(18357)); |
|
687 |
check(! m.remove(18357,null)); // 6272521 |
|
688 |
check(m.get(18357) == 7346); |
|
689 |
check(m.remove(18357,7346)); |
|
690 |
check(m.get(18357) == null); |
|
691 |
check(! m.containsKey(18357)); |
|
692 |
check(m.isEmpty()); |
|
693 |
||
694 |
m.putIfAbsent(1,2); |
|
695 |
check(m.size() == 1); |
|
696 |
check(! m.remove(1,null)); |
|
697 |
check(! m.remove(1,null)); |
|
698 |
check(! m.remove(1,1)); |
|
699 |
check(m.remove(1,2)); |
|
700 |
check(m.isEmpty()); |
|
701 |
||
702 |
testEmptyMap(m); |
|
703 |
} |
|
704 |
catch (Throwable t) { unexpected(t); } |
|
705 |
} |
|
706 |
||
707 |
private static void throwsConsistently(Class<? extends Throwable> k, |
|
708 |
Iterable<Fun> fs) { |
|
709 |
List<Class<? extends Throwable>> threw |
|
710 |
= new ArrayList<Class<? extends Throwable>>(); |
|
711 |
for (Fun f : fs) |
|
712 |
try { f.f(); threw.add(null); } |
|
713 |
catch (Throwable t) { |
|
714 |
check(k.isAssignableFrom(t.getClass())); |
|
715 |
threw.add(t.getClass()); |
|
716 |
} |
|
717 |
if (new HashSet<Object>(threw).size() != 1) |
|
718 |
fail(threw.toString()); |
|
719 |
} |
|
720 |
||
721 |
private static <T> void checkNPEConsistency(final Map<T,Integer> m) { |
|
722 |
m.clear(); |
|
723 |
final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap) |
|
724 |
? (ConcurrentMap<T,Integer>) m |
|
725 |
: null; |
|
726 |
List<Fun> fs = new ArrayList<Fun>(); |
|
727 |
fs.add(new Fun(){void f(){ check(! m.containsKey(null));}}); |
|
728 |
fs.add(new Fun(){void f(){ equal(m.remove(null), null);}}); |
|
729 |
fs.add(new Fun(){void f(){ equal(m.get(null), null);}}); |
|
730 |
if (cm != null) { |
|
731 |
fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});} |
|
732 |
throwsConsistently(NullPointerException.class, fs); |
|
733 |
||
734 |
fs.clear(); |
|
735 |
final Map<T,Integer> sm = singletonMap(null,1); |
|
736 |
fs.add(new Fun(){void f(){ equal(m.put(null,1), null); m.clear();}}); |
|
737 |
fs.add(new Fun(){void f(){ m.putAll(sm); m.clear();}}); |
|
738 |
if (cm != null) { |
|
739 |
fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}}); |
|
740 |
fs.add(new Fun(){void f(){ equal(cm.putIfAbsent(null,1), 1);}}); |
|
741 |
fs.add(new Fun(){void f(){ equal(cm.replace(null,1), null);}}); |
|
742 |
fs.add(new Fun(){void f(){ equal(cm.replace(null,1, 1), 1);}}); |
|
743 |
} |
|
744 |
throwsConsistently(NullPointerException.class, fs); |
|
745 |
} |
|
746 |
||
747 |
//---------------------------------------------------------------- |
|
748 |
// NavigableMap |
|
749 |
//---------------------------------------------------------------- |
|
750 |
private static void |
|
751 |
checkNavigableMapKeys(NavigableMap<Integer,Integer> m, |
|
752 |
Integer i, |
|
753 |
Integer lower, |
|
754 |
Integer floor, |
|
755 |
Integer ceiling, |
|
756 |
Integer higher) { |
|
757 |
equal(m.lowerKey(i), lower); |
|
758 |
equal(m.floorKey(i), floor); |
|
759 |
equal(m.ceilingKey(i), ceiling); |
|
760 |
equal(m.higherKey(i), higher); |
|
761 |
} |
|
762 |
||
763 |
private static void |
|
764 |
checkNavigableSetKeys(NavigableSet<Integer> m, |
|
765 |
Integer i, |
|
766 |
Integer lower, |
|
767 |
Integer floor, |
|
768 |
Integer ceiling, |
|
769 |
Integer higher) { |
|
770 |
equal(m.lower(i), lower); |
|
771 |
equal(m.floor(i), floor); |
|
772 |
equal(m.ceiling(i), ceiling); |
|
773 |
equal(m.higher(i), higher); |
|
774 |
} |
|
775 |
||
776 |
static final Random rnd = new Random(); |
|
777 |
static void equalNext(final Iterator<?> it, Object expected) { |
|
778 |
if (rnd.nextBoolean()) |
|
779 |
check(it.hasNext()); |
|
780 |
equal(it.next(), expected); |
|
781 |
} |
|
782 |
||
2428
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
783 |
static void equalMaps(Map m1, Map m2) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
784 |
equal(m1, m2); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
785 |
equal(m2, m1); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
786 |
equal(m1.size(), m2.size()); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
787 |
equal(m1.isEmpty(), m2.isEmpty()); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
788 |
equal(m1.toString(), m2.toString()); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
789 |
check(Arrays.equals(m1.entrySet().toArray(), m2.entrySet().toArray())); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
790 |
} |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
791 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
792 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
793 |
static void testNavigableMapRemovers(NavigableMap m) |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
794 |
{ |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
795 |
final Map emptyMap = new HashMap(); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
796 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
797 |
final Map singletonMap = new HashMap(); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
798 |
singletonMap.put(1, 2); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
799 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
800 |
abstract class NavigableMapView { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
801 |
abstract NavigableMap view(NavigableMap m); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
802 |
} |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
803 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
804 |
NavigableMapView[] views = { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
805 |
new NavigableMapView() { NavigableMap view(NavigableMap m) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
806 |
return m; }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
807 |
new NavigableMapView() { NavigableMap view(NavigableMap m) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
808 |
return m.headMap(99, true); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
809 |
new NavigableMapView() { NavigableMap view(NavigableMap m) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
810 |
return m.tailMap(-99, false); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
811 |
new NavigableMapView() { NavigableMap view(NavigableMap m) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
812 |
return m.subMap(-99, true, 99, false); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
813 |
}; |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
814 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
815 |
abstract class Remover { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
816 |
abstract void remove(NavigableMap m, Object k, Object v); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
817 |
} |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
818 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
819 |
Remover[] removers = { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
820 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
821 |
equal(m.remove(k), v); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
822 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
823 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
824 |
equal(m.descendingMap().remove(k), v); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
825 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
826 |
equal(m.descendingMap().headMap(-86, false).remove(k), v); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
827 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
828 |
equal(m.descendingMap().tailMap(86, true).remove(k), v); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
829 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
830 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
831 |
equal(m.headMap(86, true).remove(k), v); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
832 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
833 |
equal(m.tailMap(-86, true).remove(k), v); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
834 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
835 |
equal(m.subMap(-86, false, 86, true).remove(k), v); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
836 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
837 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
838 |
check(m.keySet().remove(k)); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
839 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
840 |
check(m.navigableKeySet().remove(k)); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
841 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
842 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
843 |
check(m.navigableKeySet().headSet(86, true).remove(k)); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
844 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
845 |
check(m.navigableKeySet().tailSet(-86, false).remove(k)); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
846 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
847 |
check(m.navigableKeySet().subSet(-86, true, 86, false) |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
848 |
.remove(k)); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
849 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
850 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
851 |
check(m.descendingKeySet().headSet(-86, false).remove(k)); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
852 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
853 |
check(m.descendingKeySet().tailSet(86, true).remove(k)); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
854 |
new Remover() { void remove(NavigableMap m, Object k, Object v) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
855 |
check(m.descendingKeySet().subSet(86, true, -86, false) |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
856 |
.remove(k)); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
857 |
}; |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
858 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
859 |
for (NavigableMapView view : views) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
860 |
for (Remover remover : removers) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
861 |
try { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
862 |
m.clear(); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
863 |
equalMaps(m, emptyMap); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
864 |
equal(m.put(1, 2), null); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
865 |
equalMaps(m, singletonMap); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
866 |
NavigableMap v = view.view(m); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
867 |
remover.remove(v, 1, 2); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
868 |
equalMaps(m, emptyMap); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
869 |
} catch (Throwable t) { unexpected(t); } |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
870 |
} |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
871 |
} |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
872 |
} |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
873 |
|
2 | 874 |
private static void testNavigableMap(NavigableMap<Integer,Integer> m) |
875 |
{ |
|
876 |
clear(m); |
|
877 |
checkNavigableMapKeys(m, 1, null, null, null, null); |
|
878 |
||
879 |
equal(m.put(1, 2), null); |
|
880 |
equal(m.put(3, 4), null); |
|
881 |
equal(m.put(5, 9), null); |
|
882 |
||
883 |
equal(m.put(1, 2), 2); |
|
884 |
equal(m.put(3, 4), 4); |
|
885 |
equal(m.put(5, 6), 9); |
|
886 |
||
887 |
checkNavigableMapKeys(m, 0, null, null, 1, 1); |
|
888 |
checkNavigableMapKeys(m, 1, null, 1, 1, 3); |
|
889 |
checkNavigableMapKeys(m, 2, 1, 1, 3, 3); |
|
890 |
checkNavigableMapKeys(m, 3, 1, 3, 3, 5); |
|
891 |
checkNavigableMapKeys(m, 5, 3, 5, 5, null); |
|
892 |
checkNavigableMapKeys(m, 6, 5, 5, null, null); |
|
893 |
||
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
894 |
for (final Iterator<Integer> it : |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
895 |
(Iterator<Integer>[]) |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
896 |
new Iterator<?>[] { |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
897 |
m.descendingKeySet().iterator(), |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
898 |
m.navigableKeySet().descendingIterator()}) { |
2 | 899 |
equalNext(it, 5); |
900 |
equalNext(it, 3); |
|
901 |
equalNext(it, 1); |
|
902 |
check(! it.hasNext()); |
|
903 |
THROWS(NoSuchElementException.class, |
|
904 |
new Fun(){void f(){it.next();}}); |
|
905 |
} |
|
906 |
||
907 |
{ |
|
908 |
final Iterator<Map.Entry<Integer,Integer>> it |
|
909 |
= m.descendingMap().entrySet().iterator(); |
|
910 |
check(it.hasNext()); equal(it.next().getKey(), 5); |
|
911 |
check(it.hasNext()); equal(it.next().getKey(), 3); |
|
912 |
check(it.hasNext()); equal(it.next().getKey(), 1); |
|
913 |
check(! it.hasNext()); |
|
914 |
THROWS(NoSuchElementException.class, |
|
915 |
new Fun(){void f(){it.next();}}); |
|
916 |
} |
|
917 |
} |
|
918 |
||
919 |
||
920 |
private static void testNavigableSet(NavigableSet<Integer> s) { |
|
921 |
clear(s); |
|
922 |
checkNavigableSetKeys(s, 1, null, null, null, null); |
|
923 |
||
924 |
check(s.add(1)); |
|
925 |
check(s.add(3)); |
|
926 |
check(s.add(5)); |
|
927 |
||
928 |
check(! s.add(1)); |
|
929 |
check(! s.add(3)); |
|
930 |
check(! s.add(5)); |
|
931 |
||
932 |
checkNavigableSetKeys(s, 0, null, null, 1, 1); |
|
933 |
checkNavigableSetKeys(s, 1, null, 1, 1, 3); |
|
934 |
checkNavigableSetKeys(s, 2, 1, 1, 3, 3); |
|
935 |
checkNavigableSetKeys(s, 3, 1, 3, 3, 5); |
|
936 |
checkNavigableSetKeys(s, 5, 3, 5, 5, null); |
|
937 |
checkNavigableSetKeys(s, 6, 5, 5, null, null); |
|
938 |
||
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
939 |
for (final Iterator<Integer> it : |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
940 |
(Iterator<Integer>[]) |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
941 |
new Iterator<?>[] { |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
942 |
s.descendingIterator(), |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
943 |
s.descendingSet().iterator()}) { |
2 | 944 |
equalNext(it, 5); |
945 |
equalNext(it, 3); |
|
946 |
equalNext(it, 1); |
|
947 |
check(! it.hasNext()); |
|
948 |
THROWS(NoSuchElementException.class, |
|
949 |
new Fun(){void f(){it.next();}}); |
|
950 |
} |
|
951 |
} |
|
952 |
||
953 |
//--------------------- Infrastructure --------------------------- |
|
954 |
static volatile int passed = 0, failed = 0; |
|
955 |
static void pass() { passed++; } |
|
956 |
static void fail() { failed++; Thread.dumpStack(); } |
|
957 |
static void fail(String msg) { System.out.println(msg); fail(); } |
|
958 |
static void unexpected(Throwable t) { failed++; t.printStackTrace(); } |
|
959 |
static void check(boolean cond) { if (cond) pass(); else fail(); } |
|
960 |
static void equal(Object x, Object y) { |
|
961 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
962 |
else {System.out.println(x + " not equal to " + y); fail();}} |
|
963 |
static void equal2(Object x, Object y) {equal(x, y); equal(y, x);} |
|
964 |
public static void main(String[] args) throws Throwable { |
|
965 |
try { realMain(args); } catch (Throwable t) { unexpected(t); } |
|
966 |
||
967 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
968 |
if (failed > 0) throw new Exception("Some tests failed"); |
|
969 |
} |
|
970 |
private static abstract class Fun {abstract void f() throws Throwable;} |
|
971 |
private static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
|
972 |
for (Fun f : fs) |
|
973 |
try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
|
974 |
catch (Throwable t) { |
|
975 |
if (k.isAssignableFrom(t.getClass())) pass(); |
|
976 |
else unexpected(t);}} |
|
977 |
static byte[] serializedForm(Object obj) { |
|
978 |
try { |
|
979 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
980 |
new ObjectOutputStream(baos).writeObject(obj); |
|
981 |
return baos.toByteArray(); |
|
982 |
} catch (IOException e) { throw new Error(e); }} |
|
983 |
static Object readObject(byte[] bytes) |
|
984 |
throws IOException, ClassNotFoundException { |
|
985 |
InputStream is = new ByteArrayInputStream(bytes); |
|
986 |
return new ObjectInputStream(is).readObject();} |
|
987 |
@SuppressWarnings("unchecked") |
|
988 |
static <T> T serialClone(T obj) { |
|
989 |
try { return (T) readObject(serializedForm(obj)); } |
|
990 |
catch (Exception e) { throw new Error(e); }} |
|
991 |
} |