blob: f9d30f56217d23ab6ba2597f41b073f88ef6b670 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
martineb76fa62008-05-10 11:49:25 -07002 * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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
martin4d0abcd2008-05-10 12:14:53 -070028 * 6431845 4802633 6570566 6570575 6570631 6570924 6691185 6691215
duke6e45e102007-12-01 00:00:00 +000029 * @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
51import java.io.*;
52import java.util.*;
53import java.util.concurrent.*;
54import static java.util.Collections.*;
55
56public 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>());
79 testCollection(new ConcurrentSkipListSet<Integer>());
80 testCollection(Arrays.asList(new Integer(42)));
81 testCollection(Arrays.asList(1,2,3));
82 testCollection(nCopies(25,1));
83 testImmutableList(nCopies(25,1));
84 testImmutableList(unmodifiableList(Arrays.asList(1,2,3)));
85
86 testMap(new HashMap<Integer,Integer>());
87 testMap(new LinkedHashMap<Integer,Integer>());
88 testMap(new WeakHashMap<Integer,Integer>());
89 testMap(new IdentityHashMap<Integer,Integer>());
90 testMap(new TreeMap<Integer,Integer>());
91 testMap(new Hashtable<Integer,Integer>());
92 testMap(new ConcurrentHashMap<Integer,Integer>(10, 0.5f));
93 testMap(new ConcurrentSkipListMap<Integer,Integer>());
94
95 // Empty collections
96 final List<Integer> emptyArray = Arrays.asList(new Integer[]{});
97 testCollection(emptyArray);
98 testEmptyList(emptyArray);
99 THROWS(IndexOutOfBoundsException.class,
100 new Fun(){void f(){ emptyArray.set(0,1); }});
101 THROWS(UnsupportedOperationException.class,
102 new Fun(){void f(){ emptyArray.add(0,1); }});
103
104 List<Integer> noOne = nCopies(0,1);
105 testCollection(noOne);
106 testEmptyList(noOne);
107 testImmutableList(noOne);
108
109 Set<Integer> emptySet = emptySet();
110 testCollection(emptySet);
111 testEmptySet(emptySet);
112 testEmptySet(EMPTY_SET);
113 testImmutableSet(emptySet);
114
115 List<Integer> emptyList = emptyList();
116 testCollection(emptyList);
117 testEmptyList(emptyList);
118 testEmptyList(EMPTY_LIST);
119 testImmutableList(emptyList);
120
121 Map<Integer,Integer> emptyMap = emptyMap();
122 testMap(emptyMap);
123 testEmptyMap(emptyMap);
124 testEmptyMap(EMPTY_MAP);
125 testImmutableMap(emptyMap);
126
127 // Singleton collections
128 Set<Integer> singletonSet = singleton(1);
129 equal(singletonSet.size(), 1);
130 testCollection(singletonSet);
131 testImmutableSet(singletonSet);
132
133 List<Integer> singletonList = singletonList(1);
134 equal(singletonList.size(), 1);
135 testCollection(singletonList);
136 testImmutableList(singletonList);
137 testImmutableList(singletonList.subList(0,1));
138 testImmutableList(singletonList.subList(0,1).subList(0,1));
139 testEmptyList(singletonList.subList(0,0));
140 testEmptyList(singletonList.subList(0,0).subList(0,0));
141
142 Map<Integer,Integer> singletonMap = singletonMap(1,2);
143 equal(singletonMap.size(), 1);
144 testMap(singletonMap);
145 testImmutableMap(singletonMap);
146 }
147
148 private static void checkContainsSelf(Collection<Integer> c) {
149 check(c.containsAll(c));
150 check(c.containsAll(Arrays.asList(c.toArray())));
151 check(c.containsAll(Arrays.asList(c.toArray(new Integer[0]))));
152 }
153
154 private static void checkContainsEmpty(Collection<Integer> c) {
155 check(c.containsAll(new ArrayList<Integer>()));
156 }
157
martineb76fa62008-05-10 11:49:25 -0700158 private static <T> void testEmptyCollection(Collection<T> c) {
duke6e45e102007-12-01 00:00:00 +0000159 check(c.isEmpty());
160 equal(c.size(), 0);
161 equal(c.toString(),"[]");
162 equal(c.toArray().length, 0);
163 equal(c.toArray(new Object[0]).length, 0);
164
165 Object[] a = new Object[1]; a[0] = Boolean.TRUE;
166 equal(c.toArray(a), a);
167 equal(a[0], null);
martineb76fa62008-05-10 11:49:25 -0700168 testEmptyIterator(c.iterator());
169 }
170
171 static <T> void testEmptyIterator(final Iterator<T> it) {
172 if (rnd.nextBoolean())
173 check(! it.hasNext());
174
175 THROWS(NoSuchElementException.class,
176 new Fun(){void f(){ it.next(); }});
177
178 try { it.remove(); }
179 catch (IllegalStateException _) { pass(); }
180 catch (UnsupportedOperationException _) { pass(); }
181 catch (Throwable t) { unexpected(t); }
182
183 if (rnd.nextBoolean())
184 check(! it.hasNext());
duke6e45e102007-12-01 00:00:00 +0000185 }
186
187 private static void testEmptyList(List<?> c) {
188 testEmptyCollection(c);
189 equal(c.hashCode(), 1);
190 equal2(c, Collections.<Integer>emptyList());
191 }
192
martineb76fa62008-05-10 11:49:25 -0700193 private static <T> void testEmptySet(Set<T> c) {
duke6e45e102007-12-01 00:00:00 +0000194 testEmptyCollection(c);
195 equal(c.hashCode(), 0);
196 equal2(c, Collections.<Integer>emptySet());
martineb76fa62008-05-10 11:49:25 -0700197 if (c instanceof NavigableSet<?>)
198 testEmptyIterator(((NavigableSet<T>)c).descendingIterator());
duke6e45e102007-12-01 00:00:00 +0000199 }
200
201 private static void testImmutableCollection(final Collection<Integer> c) {
202 THROWS(UnsupportedOperationException.class,
203 new Fun(){void f(){ c.add(99); }},
204 new Fun(){void f(){ c.addAll(singleton(99)); }});
205 if (! c.isEmpty()) {
206 final Integer first = c.iterator().next();
207 THROWS(UnsupportedOperationException.class,
208 new Fun(){void f(){ c.clear(); }},
209 new Fun(){void f(){ c.remove(first); }},
210 new Fun(){void f(){ c.removeAll(singleton(first)); }},
211 new Fun(){void f(){ c.retainAll(emptyList()); }}
212 );
213 }
214 }
215
216 private static void testImmutableSet(final Set<Integer> c) {
217 testImmutableCollection(c);
218 }
219
220 private static void testImmutableList(final List<Integer> c) {
221 testList(c);
222 testImmutableCollection(c);
223 THROWS(UnsupportedOperationException.class,
224 new Fun(){void f(){ c.set(0,42); }},
225 new Fun(){void f(){ c.add(0,42); }},
226 new Fun(){void f(){ c.addAll(0,singleton(86)); }});
227 if (! c.isEmpty())
228 THROWS(UnsupportedOperationException.class,
229 new Fun(){void f(){
230 Iterator<Integer> it = c.iterator();
231 it.next(); it.remove();}},
232 new Fun(){void f(){
233 ListIterator<Integer> it = c.listIterator();
234 it.next(); it.remove();}});
235 }
236
237 private static void clear(Collection<Integer> c) {
238 try { c.clear(); }
239 catch (Throwable t) { unexpected(t); }
240 testEmptyCollection(c);
241 }
242
martineb76fa62008-05-10 11:49:25 -0700243 private static <K,V> void testEmptyMap(final Map<K,V> m) {
duke6e45e102007-12-01 00:00:00 +0000244 check(m.isEmpty());
245 equal(m.size(), 0);
246 equal(m.toString(),"{}");
247 testEmptySet(m.keySet());
248 testEmptySet(m.entrySet());
249 testEmptyCollection(m.values());
martin4d0abcd2008-05-10 12:14:53 -0700250
251 try { check(! m.containsValue(null)); }
252 catch (NullPointerException _) { /* OK */ }
253 try { check(! m.containsKey(null)); }
254 catch (NullPointerException _) { /* OK */ }
255 check(! m.containsValue(1));
256 check(! m.containsKey(1));
duke6e45e102007-12-01 00:00:00 +0000257 }
258
259 private static void testImmutableMap(final Map<Integer,Integer> m) {
260 THROWS(UnsupportedOperationException.class,
261 new Fun(){void f(){ m.put(1,1); }},
262 new Fun(){void f(){ m.putAll(singletonMap(1,1)); }});
263 if (! m.isEmpty()) {
264 final Integer first = m.keySet().iterator().next();
265 THROWS(UnsupportedOperationException.class,
266 new Fun(){void f(){ m.remove(first); }},
267 new Fun(){void f(){ m.clear(); }});
268 final Map.Entry<Integer,Integer> me
269 = m.entrySet().iterator().next();
270 Integer key = me.getKey();
271 Integer val = me.getValue();
272 THROWS(UnsupportedOperationException.class,
273 new Fun(){void f(){ me.setValue(3); }});
274 equal(key, me.getKey());
275 equal(val, me.getValue());
276 }
277 testImmutableSet(m.keySet());
278 testImmutableCollection(m.values());
279 //testImmutableSet(m.entrySet());
280 }
281
282 private static void clear(Map<?,?> m) {
283 try { m.clear(); }
284 catch (Throwable t) { unexpected(t); }
285 testEmptyMap(m);
286 }
287
288 private static void oneElement(Collection<Integer> c) {
289 clear(c);
290 try {
291 check(c.add(-42));
292 equal(c.toString(), "[-42]");
293 if (c instanceof Set) check(! c.add(-42));
294 } catch (Throwable t) { unexpected(t); }
295 check(! c.isEmpty()); check(c.size() == 1);
296 }
297
298 private static boolean supportsAdd(Collection<Integer> c) {
299 try { check(c.add(778347983)); }
300 catch (UnsupportedOperationException t) { return false; }
301 catch (Throwable t) { unexpected(t); }
302
303 try {
304 check(c.contains(778347983));
305 check(c.remove(778347983));
306 } catch (Throwable t) { unexpected(t); }
307 return true;
308 }
309
310 private static boolean supportsRemove(Collection<Integer> c) {
311 try { check(! c.remove(19134032)); }
312 catch (UnsupportedOperationException t) { return false; }
313 catch (Throwable t) { unexpected(t); }
314 return true;
315 }
316
317 private static void checkFunctionalInvariants(Collection<Integer> c) {
318 try {
319 checkContainsSelf(c);
320 checkContainsEmpty(c);
321 check(c.size() != 0 ^ c.isEmpty());
322
323 {
324 int size = 0;
325 for (Integer i : c) size++;
326 check(c.size() == size);
327 }
328
329 check(c.toArray().length == c.size());
330 check(c.toArray().getClass() == Object[].class
331 ||
332 // !!!!
333 // 6260652: (coll) Arrays.asList(x).toArray().getClass()
334 // should be Object[].class
335 (c.getClass().getName().equals("java.util.Arrays$ArrayList"))
336 );
337 for (int size : new int[]{0,1,c.size(), c.size()+1}) {
338 Integer[] a = c.toArray(new Integer[size]);
339 check((size > c.size()) || a.length == c.size());
340 int i = 0; for (Integer j : c) check(a[i++] == j);
341 check((size <= c.size()) || (a[c.size()] == null));
342 check(a.getClass() == Integer[].class);
343 }
344
345 check(c.equals(c));
346 if (c instanceof Serializable) {
347 //System.out.printf("Serializing %s%n", c.getClass().getName());
348 try {
349 Object clone = serialClone(c);
350 equal(c instanceof Serializable,
351 clone instanceof Serializable);
352 equal(c instanceof RandomAccess,
353 clone instanceof RandomAccess);
354 if ((c instanceof List) || (c instanceof Set))
355 equal(c, clone);
356 else
357 equal(new HashSet<Integer>(c),
358 new HashSet<Integer>(serialClone(c)));
359 } catch (Error xxx) {
360 if (! (xxx.getCause() instanceof NotSerializableException))
361 throw xxx;
362 }
363 }
364 }
365 catch (Throwable t) { unexpected(t); }
366 }
367
368 //----------------------------------------------------------------
369 // If add(null) succeeds, contains(null) & remove(null) should succeed
370 //----------------------------------------------------------------
371 private static void testNullElement(Collection<Integer> c) {
372 // !!!! 5018849: (coll) TreeSet.contains(null) does not agree with Javadoc
373 if (c instanceof TreeSet) return;
374
375 try {
376 check(c.add(null));
377 if (c.size() == 1)
378 equal(c.toString(), "[null]");
379 try {
380 checkFunctionalInvariants(c);
381 check(c.contains(null));
382 check(c.remove(null));
383 }
384 catch (Throwable t) { unexpected(t); }
385 }
386 catch (NullPointerException e) { /* OK */ }
387 catch (Throwable t) { unexpected(t); }
388 }
389
390
391 //----------------------------------------------------------------
392 // If add("x") succeeds, contains("x") & remove("x") should succeed
393 //----------------------------------------------------------------
394 @SuppressWarnings("unchecked")
395 private static void testStringElement(Collection<Integer> c) {
396 Collection x = (Collection)c; // Make type-unsafe
397 try {
398 check(x.add("x"));
399 try {
400 check(x.contains("x"));
401 check(x.remove("x"));
402 } catch (Throwable t) { unexpected(t); }
403 }
404 catch (ClassCastException e) { /* OK */ }
405 catch (Throwable t) { unexpected(t); }
406 }
407
408 private static void testConcurrentCollection(Collection<Integer> c) {
409 try {
410 c.add(1);
411 Iterator<Integer> it = c.iterator();
412 check(it.hasNext());
413 clear(c);
414 check(it.next() instanceof Integer); // No CME
415 check(c.isEmpty());
416 }
417 catch (Throwable t) { unexpected(t); }
418 }
419
420 private static void testQueue(Queue<Integer> q) {
421 q.clear();
422 for (int i = 0; i < 5; i++)
423 q.add(i);
424 equal(q.size(), 5);
425 checkFunctionalInvariants(q);
426 q.poll();
427 equal(q.size(), 4);
428 checkFunctionalInvariants(q);
429 }
430
431 private static void testList(final List<Integer> l) {
432 //----------------------------------------------------------------
433 // 4802633: (coll) AbstractList.addAll(-1,emptyCollection)
434 // doesn't throw IndexOutOfBoundsException
435 //----------------------------------------------------------------
436 try {
437 l.addAll(-1, Collections.<Integer>emptyList());
438 fail("Expected IndexOutOfBoundsException not thrown");
439 }
440 catch (UnsupportedOperationException _) {/* OK */}
441 catch (IndexOutOfBoundsException _) {/* OK */}
442 catch (Throwable t) { unexpected(t); }
443
444// equal(l instanceof Serializable,
445// l.subList(0,0) instanceof Serializable);
446 if (l.subList(0,0) instanceof Serializable)
447 check(l instanceof Serializable);
448
449 equal(l instanceof RandomAccess,
450 l.subList(0,0) instanceof RandomAccess);
451 }
452
453 private static void testCollection(Collection<Integer> c) {
454
455 System.out.println("\n==> " + c.getClass().getName());
456
457 checkFunctionalInvariants(c);
458
459 if (! supportsAdd(c)) return;
460 //System.out.println("add() supported");
461
martineb76fa62008-05-10 11:49:25 -0700462 if (c instanceof NavigableSet) {
463 System.out.println("NavigableSet tests...");
464
465 NavigableSet<Integer> ns = (NavigableSet<Integer>)c;
466 testNavigableSet(ns);
467 testNavigableSet(ns.headSet(6, false));
468 testNavigableSet(ns.headSet(5, true));
469 testNavigableSet(ns.tailSet(0, false));
470 testNavigableSet(ns.tailSet(1, true));
471 testNavigableSet(ns.subSet(0, false, 5, true));
472 testNavigableSet(ns.subSet(1, true, 6, false));
473 }
duke6e45e102007-12-01 00:00:00 +0000474
475 if (c instanceof Queue)
476 testQueue((Queue<Integer>)c);
477
478 if (c instanceof List)
479 testList((List<Integer>)c);
480
481 check(supportsRemove(c));
482
483 try {
484 oneElement(c);
485 checkFunctionalInvariants(c);
486 }
487 catch (Throwable t) { unexpected(t); }
488
489 clear(c); testNullElement(c);
490 oneElement(c); testNullElement(c);
491
492 clear(c); testStringElement(c);
493 oneElement(c); testStringElement(c);
494
495 if (c.getClass().getName().matches(".*concurrent.*"))
496 testConcurrentCollection(c);
497
498 //----------------------------------------------------------------
499 // The "all" operations should throw NPE when passed null
500 //----------------------------------------------------------------
501 {
502 oneElement(c);
503 try {
504 c.removeAll(null);
505 fail("Expected NullPointerException");
506 }
507 catch (NullPointerException e) { pass(); }
508 catch (Throwable t) { unexpected(t); }
509
510 oneElement(c);
511 try {
512 c.retainAll(null);
513 fail("Expected NullPointerException");
514 }
515 catch (NullPointerException e) { pass(); }
516 catch (Throwable t) { unexpected(t); }
517
518 oneElement(c);
519 try {
520 c.addAll(null);
521 fail("Expected NullPointerException");
522 }
523 catch (NullPointerException e) { pass(); }
524 catch (Throwable t) { unexpected(t); }
525
526 oneElement(c);
527 try {
528 c.containsAll(null);
529 fail("Expected NullPointerException");
530 }
531 catch (NullPointerException e) { pass(); }
532 catch (Throwable t) { unexpected(t); }
533 }
534 }
535
536 //----------------------------------------------------------------
537 // Map
538 //----------------------------------------------------------------
539 private static void checkFunctionalInvariants(Map<Integer,Integer> m) {
540 check(m.keySet().size() == m.entrySet().size());
541 check(m.keySet().size() == m.size());
542 checkFunctionalInvariants(m.keySet());
543 checkFunctionalInvariants(m.values());
544 check(m.size() != 0 ^ m.isEmpty());
545 }
546
547 private static void testMap(Map<Integer,Integer> m) {
548 System.out.println("\n==> " + m.getClass().getName());
549
550 if (m instanceof ConcurrentMap)
551 testConcurrentMap((ConcurrentMap<Integer,Integer>) m);
552
martineb76fa62008-05-10 11:49:25 -0700553 if (m instanceof NavigableMap) {
554 System.out.println("NavigableMap tests...");
555
556 NavigableMap<Integer,Integer> nm =
557 (NavigableMap<Integer,Integer>) m;
dl0ad61612009-03-24 19:42:23 -0700558 testNavigableMapRemovers(nm);
martineb76fa62008-05-10 11:49:25 -0700559 testNavigableMap(nm);
560 testNavigableMap(nm.headMap(6, false));
561 testNavigableMap(nm.headMap(5, true));
562 testNavigableMap(nm.tailMap(0, false));
563 testNavigableMap(nm.tailMap(1, true));
564 testNavigableMap(nm.subMap(1, true, 6, false));
565 testNavigableMap(nm.subMap(0, false, 5, true));
566 }
duke6e45e102007-12-01 00:00:00 +0000567
568 checkFunctionalInvariants(m);
569
570 if (supportsClear(m)) {
571 try { clear(m); }
572 catch (Throwable t) { unexpected(t); }
573 }
574
575 if (supportsPut(m)) {
576 try {
577 check(m.put(3333, 77777) == null);
578 check(m.put(9134, 74982) == null);
579 check(m.get(9134) == 74982);
580 check(m.put(9134, 1382) == 74982);
581 check(m.get(9134) == 1382);
582 check(m.size() == 2);
583 checkFunctionalInvariants(m);
584 checkNPEConsistency(m);
585 }
586 catch (Throwable t) { unexpected(t); }
587 }
588 }
589
590 private static boolean supportsPut(Map<Integer,Integer> m) {
591 // We're asking for .equals(...) semantics
592 if (m instanceof IdentityHashMap) return false;
593
594 try { check(m.put(778347983,12735) == null); }
595 catch (UnsupportedOperationException t) { return false; }
596 catch (Throwable t) { unexpected(t); }
597
598 try {
599 check(m.containsKey(778347983));
600 check(m.remove(778347983) != null);
601 } catch (Throwable t) { unexpected(t); }
602 return true;
603 }
604
605 private static boolean supportsClear(Map<?,?> m) {
606 try { m.clear(); }
607 catch (UnsupportedOperationException t) { return false; }
608 catch (Throwable t) { unexpected(t); }
609 return true;
610 }
611
612 //----------------------------------------------------------------
613 // ConcurrentMap
614 //----------------------------------------------------------------
615 private static void testConcurrentMap(ConcurrentMap<Integer,Integer> m) {
616 System.out.println("ConcurrentMap tests...");
617
618 try {
619 clear(m);
620
621 check(m.putIfAbsent(18357,7346) == null);
622 check(m.containsKey(18357));
623 check(m.putIfAbsent(18357,8263) == 7346);
624 try { m.putIfAbsent(18357,null); fail("NPE"); }
625 catch (NullPointerException t) { }
626 check(m.containsKey(18357));
627
628 check(! m.replace(18357,8888,7777));
629 check(m.containsKey(18357));
630 try { m.replace(18357,null,7777); fail("NPE"); }
631 catch (NullPointerException t) { }
632 check(m.containsKey(18357));
633 check(m.get(18357) == 7346);
634 check(m.replace(18357,7346,5555));
635 check(m.replace(18357,5555,7346));
636 check(m.get(18357) == 7346);
637
638 check(m.replace(92347,7834) == null);
639 try { m.replace(18357,null); fail("NPE"); }
640 catch (NullPointerException t) { }
641 check(m.replace(18357,7346) == 7346);
642 check(m.replace(18357,5555) == 7346);
643 check(m.get(18357) == 5555);
644 check(m.replace(18357,7346) == 5555);
645 check(m.get(18357) == 7346);
646
647 check(! m.remove(18357,9999));
648 check(m.get(18357) == 7346);
649 check(m.containsKey(18357));
650 check(! m.remove(18357,null)); // 6272521
651 check(m.get(18357) == 7346);
652 check(m.remove(18357,7346));
653 check(m.get(18357) == null);
654 check(! m.containsKey(18357));
655 check(m.isEmpty());
656
657 m.putIfAbsent(1,2);
658 check(m.size() == 1);
659 check(! m.remove(1,null));
660 check(! m.remove(1,null));
661 check(! m.remove(1,1));
662 check(m.remove(1,2));
663 check(m.isEmpty());
664
665 testEmptyMap(m);
666 }
667 catch (Throwable t) { unexpected(t); }
668 }
669
670 private static void throwsConsistently(Class<? extends Throwable> k,
671 Iterable<Fun> fs) {
672 List<Class<? extends Throwable>> threw
673 = new ArrayList<Class<? extends Throwable>>();
674 for (Fun f : fs)
675 try { f.f(); threw.add(null); }
676 catch (Throwable t) {
677 check(k.isAssignableFrom(t.getClass()));
678 threw.add(t.getClass());
679 }
680 if (new HashSet<Object>(threw).size() != 1)
681 fail(threw.toString());
682 }
683
684 private static <T> void checkNPEConsistency(final Map<T,Integer> m) {
685 m.clear();
686 final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap)
687 ? (ConcurrentMap<T,Integer>) m
688 : null;
689 List<Fun> fs = new ArrayList<Fun>();
690 fs.add(new Fun(){void f(){ check(! m.containsKey(null));}});
691 fs.add(new Fun(){void f(){ equal(m.remove(null), null);}});
692 fs.add(new Fun(){void f(){ equal(m.get(null), null);}});
693 if (cm != null) {
694 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});}
695 throwsConsistently(NullPointerException.class, fs);
696
697 fs.clear();
698 final Map<T,Integer> sm = singletonMap(null,1);
699 fs.add(new Fun(){void f(){ equal(m.put(null,1), null); m.clear();}});
700 fs.add(new Fun(){void f(){ m.putAll(sm); m.clear();}});
701 if (cm != null) {
702 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});
703 fs.add(new Fun(){void f(){ equal(cm.putIfAbsent(null,1), 1);}});
704 fs.add(new Fun(){void f(){ equal(cm.replace(null,1), null);}});
705 fs.add(new Fun(){void f(){ equal(cm.replace(null,1, 1), 1);}});
706 }
707 throwsConsistently(NullPointerException.class, fs);
708 }
709
710 //----------------------------------------------------------------
711 // NavigableMap
712 //----------------------------------------------------------------
713 private static void
714 checkNavigableMapKeys(NavigableMap<Integer,Integer> m,
715 Integer i,
716 Integer lower,
717 Integer floor,
718 Integer ceiling,
719 Integer higher) {
720 equal(m.lowerKey(i), lower);
721 equal(m.floorKey(i), floor);
722 equal(m.ceilingKey(i), ceiling);
723 equal(m.higherKey(i), higher);
724 }
725
726 private static void
727 checkNavigableSetKeys(NavigableSet<Integer> m,
728 Integer i,
729 Integer lower,
730 Integer floor,
731 Integer ceiling,
732 Integer higher) {
733 equal(m.lower(i), lower);
734 equal(m.floor(i), floor);
735 equal(m.ceiling(i), ceiling);
736 equal(m.higher(i), higher);
737 }
738
739 static final Random rnd = new Random();
740 static void equalNext(final Iterator<?> it, Object expected) {
741 if (rnd.nextBoolean())
742 check(it.hasNext());
743 equal(it.next(), expected);
744 }
745
dl0ad61612009-03-24 19:42:23 -0700746 static void equalMaps(Map m1, Map m2) {
747 equal(m1, m2);
748 equal(m2, m1);
749 equal(m1.size(), m2.size());
750 equal(m1.isEmpty(), m2.isEmpty());
751 equal(m1.toString(), m2.toString());
752 check(Arrays.equals(m1.entrySet().toArray(), m2.entrySet().toArray()));
753 }
754
755 @SuppressWarnings({"unchecked", "rawtypes"})
756 static void testNavigableMapRemovers(NavigableMap m)
757 {
758 final Map emptyMap = new HashMap();
759
760 final Map singletonMap = new HashMap();
761 singletonMap.put(1, 2);
762
763 abstract class NavigableMapView {
764 abstract NavigableMap view(NavigableMap m);
765 }
766
767 NavigableMapView[] views = {
768 new NavigableMapView() { NavigableMap view(NavigableMap m) {
769 return m; }},
770 new NavigableMapView() { NavigableMap view(NavigableMap m) {
771 return m.headMap(99, true); }},
772 new NavigableMapView() { NavigableMap view(NavigableMap m) {
773 return m.tailMap(-99, false); }},
774 new NavigableMapView() { NavigableMap view(NavigableMap m) {
775 return m.subMap(-99, true, 99, false); }},
776 };
777
778 abstract class Remover {
779 abstract void remove(NavigableMap m, Object k, Object v);
780 }
781
782 Remover[] removers = {
783 new Remover() { void remove(NavigableMap m, Object k, Object v) {
784 equal(m.remove(k), v); }},
785
786 new Remover() { void remove(NavigableMap m, Object k, Object v) {
787 equal(m.descendingMap().remove(k), v); }},
788 new Remover() { void remove(NavigableMap m, Object k, Object v) {
789 equal(m.descendingMap().headMap(-86, false).remove(k), v); }},
790 new Remover() { void remove(NavigableMap m, Object k, Object v) {
791 equal(m.descendingMap().tailMap(86, true).remove(k), v); }},
792
793 new Remover() { void remove(NavigableMap m, Object k, Object v) {
794 equal(m.headMap(86, true).remove(k), v); }},
795 new Remover() { void remove(NavigableMap m, Object k, Object v) {
796 equal(m.tailMap(-86, true).remove(k), v); }},
797 new Remover() { void remove(NavigableMap m, Object k, Object v) {
798 equal(m.subMap(-86, false, 86, true).remove(k), v); }},
799
800 new Remover() { void remove(NavigableMap m, Object k, Object v) {
801 check(m.keySet().remove(k)); }},
802 new Remover() { void remove(NavigableMap m, Object k, Object v) {
803 check(m.navigableKeySet().remove(k)); }},
804
805 new Remover() { void remove(NavigableMap m, Object k, Object v) {
806 check(m.navigableKeySet().headSet(86, true).remove(k)); }},
807 new Remover() { void remove(NavigableMap m, Object k, Object v) {
808 check(m.navigableKeySet().tailSet(-86, false).remove(k)); }},
809 new Remover() { void remove(NavigableMap m, Object k, Object v) {
810 check(m.navigableKeySet().subSet(-86, true, 86, false)
811 .remove(k)); }},
812
813 new Remover() { void remove(NavigableMap m, Object k, Object v) {
814 check(m.descendingKeySet().headSet(-86, false).remove(k)); }},
815 new Remover() { void remove(NavigableMap m, Object k, Object v) {
816 check(m.descendingKeySet().tailSet(86, true).remove(k)); }},
817 new Remover() { void remove(NavigableMap m, Object k, Object v) {
818 check(m.descendingKeySet().subSet(86, true, -86, false)
819 .remove(k)); }},
820 };
821
822 for (NavigableMapView view : views) {
823 for (Remover remover : removers) {
824 try {
825 m.clear();
826 equalMaps(m, emptyMap);
827 equal(m.put(1, 2), null);
828 equalMaps(m, singletonMap);
829 NavigableMap v = view.view(m);
830 remover.remove(v, 1, 2);
831 equalMaps(m, emptyMap);
832 } catch (Throwable t) { unexpected(t); }
833 }
834 }
835 }
836
duke6e45e102007-12-01 00:00:00 +0000837 private static void testNavigableMap(NavigableMap<Integer,Integer> m)
838 {
duke6e45e102007-12-01 00:00:00 +0000839 clear(m);
840 checkNavigableMapKeys(m, 1, null, null, null, null);
841
842 equal(m.put(1, 2), null);
843 equal(m.put(3, 4), null);
844 equal(m.put(5, 9), null);
845
846 equal(m.put(1, 2), 2);
847 equal(m.put(3, 4), 4);
848 equal(m.put(5, 6), 9);
849
850 checkNavigableMapKeys(m, 0, null, null, 1, 1);
851 checkNavigableMapKeys(m, 1, null, 1, 1, 3);
852 checkNavigableMapKeys(m, 2, 1, 1, 3, 3);
853 checkNavigableMapKeys(m, 3, 1, 3, 3, 5);
854 checkNavigableMapKeys(m, 5, 3, 5, 5, null);
855 checkNavigableMapKeys(m, 6, 5, 5, null, null);
856
martineb76fa62008-05-10 11:49:25 -0700857 for (final Iterator<Integer> it :
858 (Iterator<Integer>[])
859 new Iterator<?>[] {
860 m.descendingKeySet().iterator(),
861 m.navigableKeySet().descendingIterator()}) {
duke6e45e102007-12-01 00:00:00 +0000862 equalNext(it, 5);
863 equalNext(it, 3);
864 equalNext(it, 1);
865 check(! it.hasNext());
866 THROWS(NoSuchElementException.class,
867 new Fun(){void f(){it.next();}});
868 }
869
870 {
871 final Iterator<Map.Entry<Integer,Integer>> it
872 = m.descendingMap().entrySet().iterator();
873 check(it.hasNext()); equal(it.next().getKey(), 5);
874 check(it.hasNext()); equal(it.next().getKey(), 3);
875 check(it.hasNext()); equal(it.next().getKey(), 1);
876 check(! it.hasNext());
877 THROWS(NoSuchElementException.class,
878 new Fun(){void f(){it.next();}});
879 }
880 }
881
882
883 private static void testNavigableSet(NavigableSet<Integer> s) {
duke6e45e102007-12-01 00:00:00 +0000884 clear(s);
885 checkNavigableSetKeys(s, 1, null, null, null, null);
886
887 check(s.add(1));
888 check(s.add(3));
889 check(s.add(5));
890
891 check(! s.add(1));
892 check(! s.add(3));
893 check(! s.add(5));
894
895 checkNavigableSetKeys(s, 0, null, null, 1, 1);
896 checkNavigableSetKeys(s, 1, null, 1, 1, 3);
897 checkNavigableSetKeys(s, 2, 1, 1, 3, 3);
898 checkNavigableSetKeys(s, 3, 1, 3, 3, 5);
899 checkNavigableSetKeys(s, 5, 3, 5, 5, null);
900 checkNavigableSetKeys(s, 6, 5, 5, null, null);
901
martineb76fa62008-05-10 11:49:25 -0700902 for (final Iterator<Integer> it :
903 (Iterator<Integer>[])
904 new Iterator<?>[] {
905 s.descendingIterator(),
906 s.descendingSet().iterator()}) {
duke6e45e102007-12-01 00:00:00 +0000907 equalNext(it, 5);
908 equalNext(it, 3);
909 equalNext(it, 1);
910 check(! it.hasNext());
911 THROWS(NoSuchElementException.class,
912 new Fun(){void f(){it.next();}});
913 }
914 }
915
916 //--------------------- Infrastructure ---------------------------
917 static volatile int passed = 0, failed = 0;
918 static void pass() { passed++; }
919 static void fail() { failed++; Thread.dumpStack(); }
920 static void fail(String msg) { System.out.println(msg); fail(); }
921 static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
922 static void check(boolean cond) { if (cond) pass(); else fail(); }
923 static void equal(Object x, Object y) {
924 if (x == null ? y == null : x.equals(y)) pass();
925 else {System.out.println(x + " not equal to " + y); fail();}}
926 static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
927 public static void main(String[] args) throws Throwable {
928 try { realMain(args); } catch (Throwable t) { unexpected(t); }
929
930 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
931 if (failed > 0) throw new Exception("Some tests failed");
932 }
933 private static abstract class Fun {abstract void f() throws Throwable;}
934 private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
935 for (Fun f : fs)
936 try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
937 catch (Throwable t) {
938 if (k.isAssignableFrom(t.getClass())) pass();
939 else unexpected(t);}}
940 static byte[] serializedForm(Object obj) {
941 try {
942 ByteArrayOutputStream baos = new ByteArrayOutputStream();
943 new ObjectOutputStream(baos).writeObject(obj);
944 return baos.toByteArray();
945 } catch (IOException e) { throw new Error(e); }}
946 static Object readObject(byte[] bytes)
947 throws IOException, ClassNotFoundException {
948 InputStream is = new ByteArrayInputStream(bytes);
949 return new ObjectInputStream(is).readObject();}
950 @SuppressWarnings("unchecked")
951 static <T> T serialClone(T obj) {
952 try { return (T) readObject(serializedForm(obj)); }
953 catch (Exception e) { throw new Error(e); }}
954}