author | rupashka |
Mon, 17 May 2010 17:23:18 +0400 | |
changeset 5581 | 9c2282c6f080 |
parent 4186 | dccb7b436b13 |
child 5506 | 202f599c92aa |
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>()); |
|
4110 | 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(); |
|
4184 | 424 |
for (int i = 0; i < 5; i++) { |
425 |
testQueueAddRemove(q, null); |
|
426 |
testQueueAddRemove(q, 537); |
|
2 | 427 |
q.add(i); |
4184 | 428 |
} |
2 | 429 |
equal(q.size(), 5); |
430 |
checkFunctionalInvariants(q); |
|
431 |
q.poll(); |
|
432 |
equal(q.size(), 4); |
|
433 |
checkFunctionalInvariants(q); |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
434 |
if ((q instanceof LinkedBlockingQueue) || |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
435 |
(q instanceof LinkedBlockingDeque) || |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
436 |
(q instanceof ConcurrentLinkedQueue)) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
437 |
testQueueIteratorRemove(q); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
438 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
439 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
440 |
|
4184 | 441 |
private static void testQueueAddRemove(final Queue<Integer> q, |
442 |
final Integer e) { |
|
443 |
final List<Integer> originalContents = new ArrayList<Integer>(q); |
|
444 |
final boolean isEmpty = q.isEmpty(); |
|
445 |
final boolean isList = (q instanceof List); |
|
446 |
final List asList = isList ? (List) q : null; |
|
447 |
check(!q.contains(e)); |
|
448 |
try { |
|
449 |
q.add(e); |
|
450 |
} catch (NullPointerException npe) { |
|
451 |
check(e == null); |
|
452 |
return; // Null elements not supported |
|
453 |
} |
|
454 |
check(q.contains(e)); |
|
455 |
check(q.remove(e)); |
|
456 |
check(!q.contains(e)); |
|
457 |
equal(new ArrayList<Integer>(q), originalContents); |
|
458 |
||
459 |
if (q instanceof Deque<?>) { |
|
460 |
final Deque<Integer> deq = (Deque<Integer>) q; |
|
461 |
final List<Integer> singleton = Collections.singletonList(e); |
|
462 |
||
463 |
// insert, query, remove element at head |
|
464 |
if (isEmpty) { |
|
465 |
THROWS(NoSuchElementException.class, |
|
466 |
new Fun(){void f(){ deq.getFirst(); }}, |
|
467 |
new Fun(){void f(){ deq.element(); }}, |
|
468 |
new Fun(){void f(){ deq.iterator().next(); }}); |
|
469 |
check(deq.peekFirst() == null); |
|
470 |
check(deq.peek() == null); |
|
471 |
} else { |
|
472 |
check(deq.getFirst() != e); |
|
473 |
check(deq.element() != e); |
|
474 |
check(deq.iterator().next() != e); |
|
475 |
check(deq.peekFirst() != e); |
|
476 |
check(deq.peek() != e); |
|
477 |
} |
|
478 |
check(!deq.contains(e)); |
|
479 |
check(!deq.removeFirstOccurrence(e)); |
|
480 |
check(!deq.removeLastOccurrence(e)); |
|
481 |
if (isList) { |
|
482 |
check(asList.indexOf(e) == -1); |
|
483 |
check(asList.lastIndexOf(e) == -1); |
|
484 |
} |
|
485 |
switch (rnd.nextInt(isList ? 4 : 3)) { |
|
486 |
case 0: deq.addFirst(e); break; |
|
487 |
case 1: check(deq.offerFirst(e)); break; |
|
488 |
case 2: deq.push(e); break; |
|
489 |
case 3: asList.add(0, e); break; |
|
490 |
default: throw new AssertionError(); |
|
491 |
} |
|
492 |
check(deq.peekFirst() == e); |
|
493 |
check(deq.getFirst() == e); |
|
494 |
check(deq.element() == e); |
|
495 |
check(deq.peek() == e); |
|
496 |
check(deq.iterator().next() == e); |
|
497 |
check(deq.contains(e)); |
|
498 |
if (isList) { |
|
499 |
check(asList.get(0) == e); |
|
500 |
check(asList.indexOf(e) == 0); |
|
501 |
check(asList.lastIndexOf(e) == 0); |
|
502 |
check(asList.subList(0, 1).equals(singleton)); |
|
503 |
} |
|
504 |
switch (rnd.nextInt(isList ? 11 : 9)) { |
|
505 |
case 0: check(deq.pollFirst() == e); break; |
|
506 |
case 1: check(deq.removeFirst() == e); break; |
|
507 |
case 2: check(deq.remove() == e); break; |
|
508 |
case 3: check(deq.pop() == e); break; |
|
509 |
case 4: check(deq.removeFirstOccurrence(e)); break; |
|
510 |
case 5: check(deq.removeLastOccurrence(e)); break; |
|
511 |
case 6: check(deq.remove(e)); break; |
|
512 |
case 7: check(deq.removeAll(singleton)); break; |
|
513 |
case 8: Iterator it = deq.iterator(); it.next(); it.remove(); break; |
|
514 |
case 9: asList.remove(0); break; |
|
515 |
case 10: asList.subList(0, 1).clear(); break; |
|
516 |
default: throw new AssertionError(); |
|
517 |
} |
|
518 |
if (isEmpty) { |
|
519 |
THROWS(NoSuchElementException.class, |
|
520 |
new Fun(){void f(){ deq.getFirst(); }}, |
|
521 |
new Fun(){void f(){ deq.element(); }}, |
|
522 |
new Fun(){void f(){ deq.iterator().next(); }}); |
|
523 |
check(deq.peekFirst() == null); |
|
524 |
check(deq.peek() == null); |
|
525 |
} else { |
|
526 |
check(deq.getFirst() != e); |
|
527 |
check(deq.element() != e); |
|
528 |
check(deq.iterator().next() != e); |
|
529 |
check(deq.peekFirst() != e); |
|
530 |
check(deq.peek() != e); |
|
531 |
} |
|
532 |
check(!deq.contains(e)); |
|
533 |
check(!deq.removeFirstOccurrence(e)); |
|
534 |
check(!deq.removeLastOccurrence(e)); |
|
535 |
if (isList) { |
|
536 |
check(isEmpty || asList.get(0) != e); |
|
537 |
check(asList.indexOf(e) == -1); |
|
538 |
check(asList.lastIndexOf(e) == -1); |
|
539 |
} |
|
540 |
equal(new ArrayList<Integer>(deq), originalContents); |
|
541 |
||
542 |
// insert, query, remove element at tail |
|
543 |
if (isEmpty) { |
|
544 |
check(deq.peekLast() == null); |
|
545 |
THROWS(NoSuchElementException.class, |
|
546 |
new Fun(){void f(){ deq.getLast(); }}); |
|
547 |
} else { |
|
548 |
check(deq.peekLast() != e); |
|
549 |
check(deq.getLast() != e); |
|
550 |
} |
|
551 |
switch (rnd.nextInt(isList ? 6 : 4)) { |
|
552 |
case 0: deq.addLast(e); break; |
|
553 |
case 1: check(deq.offerLast(e)); break; |
|
554 |
case 2: check(deq.add(e)); break; |
|
555 |
case 3: deq.addAll(singleton); break; |
|
556 |
case 4: asList.addAll(deq.size(), singleton); break; |
|
557 |
case 5: asList.add(deq.size(), e); break; |
|
558 |
default: throw new AssertionError(); |
|
559 |
} |
|
560 |
check(deq.peekLast() == e); |
|
561 |
check(deq.getLast() == e); |
|
562 |
check(deq.contains(e)); |
|
563 |
if (isList) { |
|
564 |
ListIterator it = asList.listIterator(asList.size()); |
|
565 |
check(it.previous() == e); |
|
566 |
check(asList.get(asList.size() - 1) == e); |
|
567 |
check(asList.indexOf(e) == asList.size() - 1); |
|
568 |
check(asList.lastIndexOf(e) == asList.size() - 1); |
|
569 |
int size = asList.size(); |
|
570 |
check(asList.subList(size - 1, size).equals(singleton)); |
|
571 |
} |
|
572 |
switch (rnd.nextInt(isList ? 8 : 6)) { |
|
573 |
case 0: check(deq.pollLast() == e); break; |
|
574 |
case 1: check(deq.removeLast() == e); break; |
|
575 |
case 2: check(deq.removeFirstOccurrence(e)); break; |
|
576 |
case 3: check(deq.removeLastOccurrence(e)); break; |
|
577 |
case 4: check(deq.remove(e)); break; |
|
578 |
case 5: check(deq.removeAll(singleton)); break; |
|
579 |
case 6: asList.remove(asList.size() - 1); break; |
|
580 |
case 7: |
|
581 |
ListIterator it = asList.listIterator(asList.size()); |
|
582 |
it.previous(); |
|
583 |
it.remove(); |
|
584 |
break; |
|
585 |
default: throw new AssertionError(); |
|
586 |
} |
|
587 |
if (isEmpty) { |
|
588 |
check(deq.peekLast() == null); |
|
589 |
THROWS(NoSuchElementException.class, |
|
590 |
new Fun(){void f(){ deq.getLast(); }}); |
|
591 |
} else { |
|
592 |
check(deq.peekLast() != e); |
|
593 |
check(deq.getLast() != e); |
|
594 |
} |
|
595 |
check(!deq.contains(e)); |
|
596 |
equal(new ArrayList<Integer>(deq), originalContents); |
|
597 |
||
598 |
// Test operations on empty deque |
|
599 |
switch (rnd.nextInt(isList ? 4 : 2)) { |
|
600 |
case 0: deq.clear(); break; |
|
601 |
case 1: |
|
602 |
Iterator it = deq.iterator(); |
|
603 |
while (it.hasNext()) { |
|
604 |
it.next(); |
|
605 |
it.remove(); |
|
606 |
} |
|
607 |
break; |
|
608 |
case 2: asList.subList(0, asList.size()).clear(); break; |
|
609 |
case 3: |
|
610 |
ListIterator lit = asList.listIterator(asList.size()); |
|
611 |
while (lit.hasPrevious()) { |
|
612 |
lit.previous(); |
|
613 |
lit.remove(); |
|
614 |
} |
|
615 |
break; |
|
616 |
default: throw new AssertionError(); |
|
617 |
} |
|
618 |
testEmptyCollection(deq); |
|
619 |
check(!deq.iterator().hasNext()); |
|
620 |
if (isList) { |
|
621 |
check(!asList.listIterator().hasPrevious()); |
|
622 |
THROWS(NoSuchElementException.class, |
|
623 |
new Fun(){void f(){ asList.listIterator().previous(); }}); |
|
624 |
} |
|
625 |
THROWS(NoSuchElementException.class, |
|
626 |
new Fun(){void f(){ deq.iterator().next(); }}, |
|
627 |
new Fun(){void f(){ deq.element(); }}, |
|
628 |
new Fun(){void f(){ deq.getFirst(); }}, |
|
629 |
new Fun(){void f(){ deq.getLast(); }}, |
|
630 |
new Fun(){void f(){ deq.pop(); }}, |
|
631 |
new Fun(){void f(){ deq.remove(); }}, |
|
632 |
new Fun(){void f(){ deq.removeFirst(); }}, |
|
633 |
new Fun(){void f(){ deq.removeLast(); }}); |
|
634 |
||
635 |
check(deq.poll() == null); |
|
636 |
check(deq.pollFirst() == null); |
|
637 |
check(deq.pollLast() == null); |
|
638 |
check(deq.peek() == null); |
|
639 |
check(deq.peekFirst() == null); |
|
640 |
check(deq.peekLast() == null); |
|
641 |
check(!deq.removeFirstOccurrence(e)); |
|
642 |
check(!deq.removeLastOccurrence(e)); |
|
643 |
||
644 |
check(deq.addAll(originalContents) == !isEmpty); |
|
645 |
equal(new ArrayList<Integer>(deq), originalContents); |
|
646 |
check(!deq.addAll(Collections.<Integer>emptyList())); |
|
647 |
equal(new ArrayList<Integer>(deq), originalContents); |
|
648 |
} |
|
649 |
} |
|
650 |
||
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
651 |
private static void testQueueIteratorRemove(Queue<Integer> q) { |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
652 |
System.err.printf("testQueueIteratorRemove %s%n", |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
653 |
q.getClass().getSimpleName()); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
654 |
q.clear(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
655 |
for (int i = 0; i < 5; i++) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
656 |
q.add(i); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
657 |
Iterator<Integer> it = q.iterator(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
658 |
check(it.hasNext()); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
659 |
for (int i = 3; i >= 0; i--) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
660 |
q.remove(i); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
661 |
equal(it.next(), 0); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
662 |
equal(it.next(), 4); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
663 |
|
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
664 |
q.clear(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
665 |
for (int i = 0; i < 5; i++) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
666 |
q.add(i); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
667 |
it = q.iterator(); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
668 |
equal(it.next(), 0); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
669 |
check(it.hasNext()); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
670 |
for (int i = 1; i < 4; i++) |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
671 |
q.remove(i); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
672 |
equal(it.next(), 1); |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
673 |
equal(it.next(), 4); |
2 | 674 |
} |
675 |
||
676 |
private static void testList(final List<Integer> l) { |
|
677 |
//---------------------------------------------------------------- |
|
678 |
// 4802633: (coll) AbstractList.addAll(-1,emptyCollection) |
|
679 |
// doesn't throw IndexOutOfBoundsException |
|
680 |
//---------------------------------------------------------------- |
|
681 |
try { |
|
682 |
l.addAll(-1, Collections.<Integer>emptyList()); |
|
683 |
fail("Expected IndexOutOfBoundsException not thrown"); |
|
684 |
} |
|
685 |
catch (UnsupportedOperationException _) {/* OK */} |
|
686 |
catch (IndexOutOfBoundsException _) {/* OK */} |
|
687 |
catch (Throwable t) { unexpected(t); } |
|
688 |
||
689 |
// equal(l instanceof Serializable, |
|
690 |
// l.subList(0,0) instanceof Serializable); |
|
691 |
if (l.subList(0,0) instanceof Serializable) |
|
692 |
check(l instanceof Serializable); |
|
693 |
||
694 |
equal(l instanceof RandomAccess, |
|
695 |
l.subList(0,0) instanceof RandomAccess); |
|
696 |
} |
|
697 |
||
698 |
private static void testCollection(Collection<Integer> c) { |
|
3415
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
699 |
try { testCollection1(c); } |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
700 |
catch (Throwable t) { unexpected(t); } |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
701 |
} |
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
702 |
|
79309d6eab38
6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents:
2428
diff
changeset
|
703 |
private static void testCollection1(Collection<Integer> c) { |
2 | 704 |
|
705 |
System.out.println("\n==> " + c.getClass().getName()); |
|
706 |
||
707 |
checkFunctionalInvariants(c); |
|
708 |
||
709 |
if (! supportsAdd(c)) return; |
|
710 |
//System.out.println("add() supported"); |
|
711 |
||
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
712 |
if (c instanceof NavigableSet) { |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
713 |
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
|
714 |
|
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
715 |
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
|
716 |
testNavigableSet(ns); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
717 |
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
|
718 |
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
|
719 |
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
|
720 |
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
|
721 |
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
|
722 |
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
|
723 |
} |
2 | 724 |
|
725 |
if (c instanceof Queue) |
|
726 |
testQueue((Queue<Integer>)c); |
|
727 |
||
728 |
if (c instanceof List) |
|
729 |
testList((List<Integer>)c); |
|
730 |
||
731 |
check(supportsRemove(c)); |
|
732 |
||
733 |
try { |
|
734 |
oneElement(c); |
|
735 |
checkFunctionalInvariants(c); |
|
736 |
} |
|
737 |
catch (Throwable t) { unexpected(t); } |
|
738 |
||
739 |
clear(c); testNullElement(c); |
|
740 |
oneElement(c); testNullElement(c); |
|
741 |
||
742 |
clear(c); testStringElement(c); |
|
743 |
oneElement(c); testStringElement(c); |
|
744 |
||
745 |
if (c.getClass().getName().matches(".*concurrent.*")) |
|
746 |
testConcurrentCollection(c); |
|
747 |
||
748 |
//---------------------------------------------------------------- |
|
749 |
// The "all" operations should throw NPE when passed null |
|
750 |
//---------------------------------------------------------------- |
|
751 |
{ |
|
752 |
oneElement(c); |
|
753 |
try { |
|
754 |
c.removeAll(null); |
|
755 |
fail("Expected NullPointerException"); |
|
756 |
} |
|
757 |
catch (NullPointerException e) { pass(); } |
|
758 |
catch (Throwable t) { unexpected(t); } |
|
759 |
||
760 |
oneElement(c); |
|
761 |
try { |
|
762 |
c.retainAll(null); |
|
763 |
fail("Expected NullPointerException"); |
|
764 |
} |
|
765 |
catch (NullPointerException e) { pass(); } |
|
766 |
catch (Throwable t) { unexpected(t); } |
|
767 |
||
768 |
oneElement(c); |
|
769 |
try { |
|
770 |
c.addAll(null); |
|
771 |
fail("Expected NullPointerException"); |
|
772 |
} |
|
773 |
catch (NullPointerException e) { pass(); } |
|
774 |
catch (Throwable t) { unexpected(t); } |
|
775 |
||
776 |
oneElement(c); |
|
777 |
try { |
|
778 |
c.containsAll(null); |
|
779 |
fail("Expected NullPointerException"); |
|
780 |
} |
|
781 |
catch (NullPointerException e) { pass(); } |
|
782 |
catch (Throwable t) { unexpected(t); } |
|
783 |
} |
|
784 |
} |
|
785 |
||
786 |
//---------------------------------------------------------------- |
|
787 |
// Map |
|
788 |
//---------------------------------------------------------------- |
|
789 |
private static void checkFunctionalInvariants(Map<Integer,Integer> m) { |
|
790 |
check(m.keySet().size() == m.entrySet().size()); |
|
791 |
check(m.keySet().size() == m.size()); |
|
792 |
checkFunctionalInvariants(m.keySet()); |
|
793 |
checkFunctionalInvariants(m.values()); |
|
794 |
check(m.size() != 0 ^ m.isEmpty()); |
|
795 |
} |
|
796 |
||
797 |
private static void testMap(Map<Integer,Integer> m) { |
|
798 |
System.out.println("\n==> " + m.getClass().getName()); |
|
799 |
||
800 |
if (m instanceof ConcurrentMap) |
|
801 |
testConcurrentMap((ConcurrentMap<Integer,Integer>) m); |
|
802 |
||
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
803 |
if (m instanceof NavigableMap) { |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
804 |
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
|
805 |
|
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
806 |
NavigableMap<Integer,Integer> nm = |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
807 |
(NavigableMap<Integer,Integer>) m; |
2428
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
808 |
testNavigableMapRemovers(nm); |
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
809 |
testNavigableMap(nm); |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
810 |
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
|
811 |
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
|
812 |
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
|
813 |
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
|
814 |
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
|
815 |
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
|
816 |
} |
2 | 817 |
|
818 |
checkFunctionalInvariants(m); |
|
819 |
||
820 |
if (supportsClear(m)) { |
|
821 |
try { clear(m); } |
|
822 |
catch (Throwable t) { unexpected(t); } |
|
823 |
} |
|
824 |
||
825 |
if (supportsPut(m)) { |
|
826 |
try { |
|
827 |
check(m.put(3333, 77777) == null); |
|
828 |
check(m.put(9134, 74982) == null); |
|
829 |
check(m.get(9134) == 74982); |
|
830 |
check(m.put(9134, 1382) == 74982); |
|
831 |
check(m.get(9134) == 1382); |
|
832 |
check(m.size() == 2); |
|
833 |
checkFunctionalInvariants(m); |
|
834 |
checkNPEConsistency(m); |
|
835 |
} |
|
836 |
catch (Throwable t) { unexpected(t); } |
|
837 |
} |
|
838 |
} |
|
839 |
||
840 |
private static boolean supportsPut(Map<Integer,Integer> m) { |
|
841 |
// We're asking for .equals(...) semantics |
|
842 |
if (m instanceof IdentityHashMap) return false; |
|
843 |
||
844 |
try { check(m.put(778347983,12735) == null); } |
|
845 |
catch (UnsupportedOperationException t) { return false; } |
|
846 |
catch (Throwable t) { unexpected(t); } |
|
847 |
||
848 |
try { |
|
849 |
check(m.containsKey(778347983)); |
|
850 |
check(m.remove(778347983) != null); |
|
851 |
} catch (Throwable t) { unexpected(t); } |
|
852 |
return true; |
|
853 |
} |
|
854 |
||
855 |
private static boolean supportsClear(Map<?,?> m) { |
|
856 |
try { m.clear(); } |
|
857 |
catch (UnsupportedOperationException t) { return false; } |
|
858 |
catch (Throwable t) { unexpected(t); } |
|
859 |
return true; |
|
860 |
} |
|
861 |
||
862 |
//---------------------------------------------------------------- |
|
863 |
// ConcurrentMap |
|
864 |
//---------------------------------------------------------------- |
|
865 |
private static void testConcurrentMap(ConcurrentMap<Integer,Integer> m) { |
|
866 |
System.out.println("ConcurrentMap tests..."); |
|
867 |
||
868 |
try { |
|
869 |
clear(m); |
|
870 |
||
871 |
check(m.putIfAbsent(18357,7346) == null); |
|
872 |
check(m.containsKey(18357)); |
|
873 |
check(m.putIfAbsent(18357,8263) == 7346); |
|
874 |
try { m.putIfAbsent(18357,null); fail("NPE"); } |
|
875 |
catch (NullPointerException t) { } |
|
876 |
check(m.containsKey(18357)); |
|
877 |
||
878 |
check(! m.replace(18357,8888,7777)); |
|
879 |
check(m.containsKey(18357)); |
|
880 |
try { m.replace(18357,null,7777); fail("NPE"); } |
|
881 |
catch (NullPointerException t) { } |
|
882 |
check(m.containsKey(18357)); |
|
883 |
check(m.get(18357) == 7346); |
|
884 |
check(m.replace(18357,7346,5555)); |
|
885 |
check(m.replace(18357,5555,7346)); |
|
886 |
check(m.get(18357) == 7346); |
|
887 |
||
888 |
check(m.replace(92347,7834) == null); |
|
889 |
try { m.replace(18357,null); fail("NPE"); } |
|
890 |
catch (NullPointerException t) { } |
|
891 |
check(m.replace(18357,7346) == 7346); |
|
892 |
check(m.replace(18357,5555) == 7346); |
|
893 |
check(m.get(18357) == 5555); |
|
894 |
check(m.replace(18357,7346) == 5555); |
|
895 |
check(m.get(18357) == 7346); |
|
896 |
||
897 |
check(! m.remove(18357,9999)); |
|
898 |
check(m.get(18357) == 7346); |
|
899 |
check(m.containsKey(18357)); |
|
900 |
check(! m.remove(18357,null)); // 6272521 |
|
901 |
check(m.get(18357) == 7346); |
|
902 |
check(m.remove(18357,7346)); |
|
903 |
check(m.get(18357) == null); |
|
904 |
check(! m.containsKey(18357)); |
|
905 |
check(m.isEmpty()); |
|
906 |
||
907 |
m.putIfAbsent(1,2); |
|
908 |
check(m.size() == 1); |
|
909 |
check(! m.remove(1,null)); |
|
910 |
check(! m.remove(1,null)); |
|
911 |
check(! m.remove(1,1)); |
|
912 |
check(m.remove(1,2)); |
|
913 |
check(m.isEmpty()); |
|
914 |
||
915 |
testEmptyMap(m); |
|
916 |
} |
|
917 |
catch (Throwable t) { unexpected(t); } |
|
918 |
} |
|
919 |
||
920 |
private static void throwsConsistently(Class<? extends Throwable> k, |
|
921 |
Iterable<Fun> fs) { |
|
922 |
List<Class<? extends Throwable>> threw |
|
923 |
= new ArrayList<Class<? extends Throwable>>(); |
|
924 |
for (Fun f : fs) |
|
925 |
try { f.f(); threw.add(null); } |
|
926 |
catch (Throwable t) { |
|
927 |
check(k.isAssignableFrom(t.getClass())); |
|
928 |
threw.add(t.getClass()); |
|
929 |
} |
|
930 |
if (new HashSet<Object>(threw).size() != 1) |
|
931 |
fail(threw.toString()); |
|
932 |
} |
|
933 |
||
934 |
private static <T> void checkNPEConsistency(final Map<T,Integer> m) { |
|
935 |
m.clear(); |
|
936 |
final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap) |
|
937 |
? (ConcurrentMap<T,Integer>) m |
|
938 |
: null; |
|
939 |
List<Fun> fs = new ArrayList<Fun>(); |
|
940 |
fs.add(new Fun(){void f(){ check(! m.containsKey(null));}}); |
|
941 |
fs.add(new Fun(){void f(){ equal(m.remove(null), null);}}); |
|
942 |
fs.add(new Fun(){void f(){ equal(m.get(null), null);}}); |
|
943 |
if (cm != null) { |
|
944 |
fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});} |
|
945 |
throwsConsistently(NullPointerException.class, fs); |
|
946 |
||
947 |
fs.clear(); |
|
948 |
final Map<T,Integer> sm = singletonMap(null,1); |
|
949 |
fs.add(new Fun(){void f(){ equal(m.put(null,1), null); m.clear();}}); |
|
950 |
fs.add(new Fun(){void f(){ m.putAll(sm); m.clear();}}); |
|
951 |
if (cm != null) { |
|
952 |
fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}}); |
|
953 |
fs.add(new Fun(){void f(){ equal(cm.putIfAbsent(null,1), 1);}}); |
|
954 |
fs.add(new Fun(){void f(){ equal(cm.replace(null,1), null);}}); |
|
955 |
fs.add(new Fun(){void f(){ equal(cm.replace(null,1, 1), 1);}}); |
|
956 |
} |
|
957 |
throwsConsistently(NullPointerException.class, fs); |
|
958 |
} |
|
959 |
||
960 |
//---------------------------------------------------------------- |
|
961 |
// NavigableMap |
|
962 |
//---------------------------------------------------------------- |
|
963 |
private static void |
|
964 |
checkNavigableMapKeys(NavigableMap<Integer,Integer> m, |
|
965 |
Integer i, |
|
966 |
Integer lower, |
|
967 |
Integer floor, |
|
968 |
Integer ceiling, |
|
969 |
Integer higher) { |
|
970 |
equal(m.lowerKey(i), lower); |
|
971 |
equal(m.floorKey(i), floor); |
|
972 |
equal(m.ceilingKey(i), ceiling); |
|
973 |
equal(m.higherKey(i), higher); |
|
974 |
} |
|
975 |
||
976 |
private static void |
|
977 |
checkNavigableSetKeys(NavigableSet<Integer> m, |
|
978 |
Integer i, |
|
979 |
Integer lower, |
|
980 |
Integer floor, |
|
981 |
Integer ceiling, |
|
982 |
Integer higher) { |
|
983 |
equal(m.lower(i), lower); |
|
984 |
equal(m.floor(i), floor); |
|
985 |
equal(m.ceiling(i), ceiling); |
|
986 |
equal(m.higher(i), higher); |
|
987 |
} |
|
988 |
||
989 |
static final Random rnd = new Random(); |
|
990 |
static void equalNext(final Iterator<?> it, Object expected) { |
|
991 |
if (rnd.nextBoolean()) |
|
992 |
check(it.hasNext()); |
|
993 |
equal(it.next(), expected); |
|
994 |
} |
|
995 |
||
2428
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
996 |
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
|
997 |
equal(m1, m2); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
998 |
equal(m2, m1); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
999 |
equal(m1.size(), m2.size()); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1000 |
equal(m1.isEmpty(), m2.isEmpty()); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1001 |
equal(m1.toString(), m2.toString()); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1002 |
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
|
1003 |
} |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1004 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1005 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1006 |
static void testNavigableMapRemovers(NavigableMap m) |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1007 |
{ |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1008 |
final Map emptyMap = new HashMap(); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1009 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1010 |
final Map singletonMap = new HashMap(); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1011 |
singletonMap.put(1, 2); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1012 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1013 |
abstract class NavigableMapView { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1014 |
abstract NavigableMap view(NavigableMap m); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1015 |
} |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1016 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1017 |
NavigableMapView[] views = { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1018 |
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
|
1019 |
return m; }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1020 |
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
|
1021 |
return m.headMap(99, true); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1022 |
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
|
1023 |
return m.tailMap(-99, false); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1024 |
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
|
1025 |
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
|
1026 |
}; |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1027 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1028 |
abstract class Remover { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1029 |
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
|
1030 |
} |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1031 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1032 |
Remover[] removers = { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1033 |
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
|
1034 |
equal(m.remove(k), v); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1035 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1036 |
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
|
1037 |
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
|
1038 |
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
|
1039 |
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
|
1040 |
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
|
1041 |
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
|
1042 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1043 |
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
|
1044 |
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
|
1045 |
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
|
1046 |
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
|
1047 |
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
|
1048 |
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
|
1049 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1050 |
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
|
1051 |
check(m.keySet().remove(k)); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1052 |
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
|
1053 |
check(m.navigableKeySet().remove(k)); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1054 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1055 |
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
|
1056 |
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
|
1057 |
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
|
1058 |
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
|
1059 |
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
|
1060 |
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
|
1061 |
.remove(k)); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1062 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1063 |
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
|
1064 |
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
|
1065 |
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
|
1066 |
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
|
1067 |
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
|
1068 |
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
|
1069 |
.remove(k)); }}, |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1070 |
}; |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1071 |
|
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1072 |
for (NavigableMapView view : views) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1073 |
for (Remover remover : removers) { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1074 |
try { |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1075 |
m.clear(); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1076 |
equalMaps(m, emptyMap); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1077 |
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
|
1078 |
equalMaps(m, singletonMap); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1079 |
NavigableMap v = view.view(m); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1080 |
remover.remove(v, 1, 2); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1081 |
equalMaps(m, emptyMap); |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1082 |
} catch (Throwable t) { unexpected(t); } |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1083 |
} |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1084 |
} |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1085 |
} |
e63d91602813
6800572: Removing elements from views of NavigableMap implementations does not always work correctly.
dl
parents:
494
diff
changeset
|
1086 |
|
2 | 1087 |
private static void testNavigableMap(NavigableMap<Integer,Integer> m) |
1088 |
{ |
|
1089 |
clear(m); |
|
1090 |
checkNavigableMapKeys(m, 1, null, null, null, null); |
|
1091 |
||
1092 |
equal(m.put(1, 2), null); |
|
1093 |
equal(m.put(3, 4), null); |
|
1094 |
equal(m.put(5, 9), null); |
|
1095 |
||
1096 |
equal(m.put(1, 2), 2); |
|
1097 |
equal(m.put(3, 4), 4); |
|
1098 |
equal(m.put(5, 6), 9); |
|
1099 |
||
1100 |
checkNavigableMapKeys(m, 0, null, null, 1, 1); |
|
1101 |
checkNavigableMapKeys(m, 1, null, 1, 1, 3); |
|
1102 |
checkNavigableMapKeys(m, 2, 1, 1, 3, 3); |
|
1103 |
checkNavigableMapKeys(m, 3, 1, 3, 3, 5); |
|
1104 |
checkNavigableMapKeys(m, 5, 3, 5, 5, null); |
|
1105 |
checkNavigableMapKeys(m, 6, 5, 5, null, null); |
|
1106 |
||
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
1107 |
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
|
1108 |
(Iterator<Integer>[]) |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
1109 |
new Iterator<?>[] { |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
1110 |
m.descendingKeySet().iterator(), |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
1111 |
m.navigableKeySet().descendingIterator()}) { |
2 | 1112 |
equalNext(it, 5); |
1113 |
equalNext(it, 3); |
|
1114 |
equalNext(it, 1); |
|
1115 |
check(! it.hasNext()); |
|
1116 |
THROWS(NoSuchElementException.class, |
|
1117 |
new Fun(){void f(){it.next();}}); |
|
1118 |
} |
|
1119 |
||
1120 |
{ |
|
1121 |
final Iterator<Map.Entry<Integer,Integer>> it |
|
1122 |
= m.descendingMap().entrySet().iterator(); |
|
1123 |
check(it.hasNext()); equal(it.next().getKey(), 5); |
|
1124 |
check(it.hasNext()); equal(it.next().getKey(), 3); |
|
1125 |
check(it.hasNext()); equal(it.next().getKey(), 1); |
|
1126 |
check(! it.hasNext()); |
|
1127 |
THROWS(NoSuchElementException.class, |
|
1128 |
new Fun(){void f(){it.next();}}); |
|
1129 |
} |
|
1130 |
} |
|
1131 |
||
1132 |
||
1133 |
private static void testNavigableSet(NavigableSet<Integer> s) { |
|
1134 |
clear(s); |
|
1135 |
checkNavigableSetKeys(s, 1, null, null, null, null); |
|
1136 |
||
1137 |
check(s.add(1)); |
|
1138 |
check(s.add(3)); |
|
1139 |
check(s.add(5)); |
|
1140 |
||
1141 |
check(! s.add(1)); |
|
1142 |
check(! s.add(3)); |
|
1143 |
check(! s.add(5)); |
|
1144 |
||
1145 |
checkNavigableSetKeys(s, 0, null, null, 1, 1); |
|
1146 |
checkNavigableSetKeys(s, 1, null, 1, 1, 3); |
|
1147 |
checkNavigableSetKeys(s, 2, 1, 1, 3, 3); |
|
1148 |
checkNavigableSetKeys(s, 3, 1, 3, 3, 5); |
|
1149 |
checkNavigableSetKeys(s, 5, 3, 5, 5, null); |
|
1150 |
checkNavigableSetKeys(s, 6, 5, 5, null, null); |
|
1151 |
||
493
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
1152 |
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
|
1153 |
(Iterator<Integer>[]) |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
1154 |
new Iterator<?>[] { |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
1155 |
s.descendingIterator(), |
b8102e80be10
6691185: (coll) TreeMap.navigableKeySet's descendingIterator method starts at first instead of last entry
martin
parents:
2
diff
changeset
|
1156 |
s.descendingSet().iterator()}) { |
2 | 1157 |
equalNext(it, 5); |
1158 |
equalNext(it, 3); |
|
1159 |
equalNext(it, 1); |
|
1160 |
check(! it.hasNext()); |
|
1161 |
THROWS(NoSuchElementException.class, |
|
1162 |
new Fun(){void f(){it.next();}}); |
|
1163 |
} |
|
1164 |
} |
|
1165 |
||
1166 |
//--------------------- Infrastructure --------------------------- |
|
1167 |
static volatile int passed = 0, failed = 0; |
|
1168 |
static void pass() { passed++; } |
|
1169 |
static void fail() { failed++; Thread.dumpStack(); } |
|
1170 |
static void fail(String msg) { System.out.println(msg); fail(); } |
|
1171 |
static void unexpected(Throwable t) { failed++; t.printStackTrace(); } |
|
1172 |
static void check(boolean cond) { if (cond) pass(); else fail(); } |
|
1173 |
static void equal(Object x, Object y) { |
|
1174 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
1175 |
else {System.out.println(x + " not equal to " + y); fail();}} |
|
1176 |
static void equal2(Object x, Object y) {equal(x, y); equal(y, x);} |
|
1177 |
public static void main(String[] args) throws Throwable { |
|
1178 |
try { realMain(args); } catch (Throwable t) { unexpected(t); } |
|
1179 |
||
1180 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
1181 |
if (failed > 0) throw new Exception("Some tests failed"); |
|
1182 |
} |
|
1183 |
private static abstract class Fun {abstract void f() throws Throwable;} |
|
1184 |
private static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
|
1185 |
for (Fun f : fs) |
|
1186 |
try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
|
1187 |
catch (Throwable t) { |
|
1188 |
if (k.isAssignableFrom(t.getClass())) pass(); |
|
1189 |
else unexpected(t);}} |
|
1190 |
static byte[] serializedForm(Object obj) { |
|
1191 |
try { |
|
1192 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
1193 |
new ObjectOutputStream(baos).writeObject(obj); |
|
1194 |
return baos.toByteArray(); |
|
1195 |
} catch (IOException e) { throw new Error(e); }} |
|
1196 |
static Object readObject(byte[] bytes) |
|
1197 |
throws IOException, ClassNotFoundException { |
|
1198 |
InputStream is = new ByteArrayInputStream(bytes); |
|
1199 |
return new ObjectInputStream(is).readObject();} |
|
1200 |
@SuppressWarnings("unchecked") |
|
1201 |
static <T> T serialClone(T obj) { |
|
1202 |
try { return (T) readObject(serializedForm(obj)); } |
|
1203 |
catch (Exception e) { throw new Error(e); }} |
|
1204 |
} |