blob: 0dd89f355005d6baeaab7b6dff87f366c5dcd09f [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;
558 testNavigableMap(nm);
559 testNavigableMap(nm.headMap(6, false));
560 testNavigableMap(nm.headMap(5, true));
561 testNavigableMap(nm.tailMap(0, false));
562 testNavigableMap(nm.tailMap(1, true));
563 testNavigableMap(nm.subMap(1, true, 6, false));
564 testNavigableMap(nm.subMap(0, false, 5, true));
565 }
duke6e45e102007-12-01 00:00:00 +0000566
567 checkFunctionalInvariants(m);
568
569 if (supportsClear(m)) {
570 try { clear(m); }
571 catch (Throwable t) { unexpected(t); }
572 }
573
574 if (supportsPut(m)) {
575 try {
576 check(m.put(3333, 77777) == null);
577 check(m.put(9134, 74982) == null);
578 check(m.get(9134) == 74982);
579 check(m.put(9134, 1382) == 74982);
580 check(m.get(9134) == 1382);
581 check(m.size() == 2);
582 checkFunctionalInvariants(m);
583 checkNPEConsistency(m);
584 }
585 catch (Throwable t) { unexpected(t); }
586 }
587 }
588
589 private static boolean supportsPut(Map<Integer,Integer> m) {
590 // We're asking for .equals(...) semantics
591 if (m instanceof IdentityHashMap) return false;
592
593 try { check(m.put(778347983,12735) == null); }
594 catch (UnsupportedOperationException t) { return false; }
595 catch (Throwable t) { unexpected(t); }
596
597 try {
598 check(m.containsKey(778347983));
599 check(m.remove(778347983) != null);
600 } catch (Throwable t) { unexpected(t); }
601 return true;
602 }
603
604 private static boolean supportsClear(Map<?,?> m) {
605 try { m.clear(); }
606 catch (UnsupportedOperationException t) { return false; }
607 catch (Throwable t) { unexpected(t); }
608 return true;
609 }
610
611 //----------------------------------------------------------------
612 // ConcurrentMap
613 //----------------------------------------------------------------
614 private static void testConcurrentMap(ConcurrentMap<Integer,Integer> m) {
615 System.out.println("ConcurrentMap tests...");
616
617 try {
618 clear(m);
619
620 check(m.putIfAbsent(18357,7346) == null);
621 check(m.containsKey(18357));
622 check(m.putIfAbsent(18357,8263) == 7346);
623 try { m.putIfAbsent(18357,null); fail("NPE"); }
624 catch (NullPointerException t) { }
625 check(m.containsKey(18357));
626
627 check(! m.replace(18357,8888,7777));
628 check(m.containsKey(18357));
629 try { m.replace(18357,null,7777); fail("NPE"); }
630 catch (NullPointerException t) { }
631 check(m.containsKey(18357));
632 check(m.get(18357) == 7346);
633 check(m.replace(18357,7346,5555));
634 check(m.replace(18357,5555,7346));
635 check(m.get(18357) == 7346);
636
637 check(m.replace(92347,7834) == null);
638 try { m.replace(18357,null); fail("NPE"); }
639 catch (NullPointerException t) { }
640 check(m.replace(18357,7346) == 7346);
641 check(m.replace(18357,5555) == 7346);
642 check(m.get(18357) == 5555);
643 check(m.replace(18357,7346) == 5555);
644 check(m.get(18357) == 7346);
645
646 check(! m.remove(18357,9999));
647 check(m.get(18357) == 7346);
648 check(m.containsKey(18357));
649 check(! m.remove(18357,null)); // 6272521
650 check(m.get(18357) == 7346);
651 check(m.remove(18357,7346));
652 check(m.get(18357) == null);
653 check(! m.containsKey(18357));
654 check(m.isEmpty());
655
656 m.putIfAbsent(1,2);
657 check(m.size() == 1);
658 check(! m.remove(1,null));
659 check(! m.remove(1,null));
660 check(! m.remove(1,1));
661 check(m.remove(1,2));
662 check(m.isEmpty());
663
664 testEmptyMap(m);
665 }
666 catch (Throwable t) { unexpected(t); }
667 }
668
669 private static void throwsConsistently(Class<? extends Throwable> k,
670 Iterable<Fun> fs) {
671 List<Class<? extends Throwable>> threw
672 = new ArrayList<Class<? extends Throwable>>();
673 for (Fun f : fs)
674 try { f.f(); threw.add(null); }
675 catch (Throwable t) {
676 check(k.isAssignableFrom(t.getClass()));
677 threw.add(t.getClass());
678 }
679 if (new HashSet<Object>(threw).size() != 1)
680 fail(threw.toString());
681 }
682
683 private static <T> void checkNPEConsistency(final Map<T,Integer> m) {
684 m.clear();
685 final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap)
686 ? (ConcurrentMap<T,Integer>) m
687 : null;
688 List<Fun> fs = new ArrayList<Fun>();
689 fs.add(new Fun(){void f(){ check(! m.containsKey(null));}});
690 fs.add(new Fun(){void f(){ equal(m.remove(null), null);}});
691 fs.add(new Fun(){void f(){ equal(m.get(null), null);}});
692 if (cm != null) {
693 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});}
694 throwsConsistently(NullPointerException.class, fs);
695
696 fs.clear();
697 final Map<T,Integer> sm = singletonMap(null,1);
698 fs.add(new Fun(){void f(){ equal(m.put(null,1), null); m.clear();}});
699 fs.add(new Fun(){void f(){ m.putAll(sm); m.clear();}});
700 if (cm != null) {
701 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});
702 fs.add(new Fun(){void f(){ equal(cm.putIfAbsent(null,1), 1);}});
703 fs.add(new Fun(){void f(){ equal(cm.replace(null,1), null);}});
704 fs.add(new Fun(){void f(){ equal(cm.replace(null,1, 1), 1);}});
705 }
706 throwsConsistently(NullPointerException.class, fs);
707 }
708
709 //----------------------------------------------------------------
710 // NavigableMap
711 //----------------------------------------------------------------
712 private static void
713 checkNavigableMapKeys(NavigableMap<Integer,Integer> m,
714 Integer i,
715 Integer lower,
716 Integer floor,
717 Integer ceiling,
718 Integer higher) {
719 equal(m.lowerKey(i), lower);
720 equal(m.floorKey(i), floor);
721 equal(m.ceilingKey(i), ceiling);
722 equal(m.higherKey(i), higher);
723 }
724
725 private static void
726 checkNavigableSetKeys(NavigableSet<Integer> m,
727 Integer i,
728 Integer lower,
729 Integer floor,
730 Integer ceiling,
731 Integer higher) {
732 equal(m.lower(i), lower);
733 equal(m.floor(i), floor);
734 equal(m.ceiling(i), ceiling);
735 equal(m.higher(i), higher);
736 }
737
738 static final Random rnd = new Random();
739 static void equalNext(final Iterator<?> it, Object expected) {
740 if (rnd.nextBoolean())
741 check(it.hasNext());
742 equal(it.next(), expected);
743 }
744
745 private static void testNavigableMap(NavigableMap<Integer,Integer> m)
746 {
duke6e45e102007-12-01 00:00:00 +0000747 clear(m);
748 checkNavigableMapKeys(m, 1, null, null, null, null);
749
750 equal(m.put(1, 2), null);
751 equal(m.put(3, 4), null);
752 equal(m.put(5, 9), null);
753
754 equal(m.put(1, 2), 2);
755 equal(m.put(3, 4), 4);
756 equal(m.put(5, 6), 9);
757
758 checkNavigableMapKeys(m, 0, null, null, 1, 1);
759 checkNavigableMapKeys(m, 1, null, 1, 1, 3);
760 checkNavigableMapKeys(m, 2, 1, 1, 3, 3);
761 checkNavigableMapKeys(m, 3, 1, 3, 3, 5);
762 checkNavigableMapKeys(m, 5, 3, 5, 5, null);
763 checkNavigableMapKeys(m, 6, 5, 5, null, null);
764
martineb76fa62008-05-10 11:49:25 -0700765 for (final Iterator<Integer> it :
766 (Iterator<Integer>[])
767 new Iterator<?>[] {
768 m.descendingKeySet().iterator(),
769 m.navigableKeySet().descendingIterator()}) {
duke6e45e102007-12-01 00:00:00 +0000770 equalNext(it, 5);
771 equalNext(it, 3);
772 equalNext(it, 1);
773 check(! it.hasNext());
774 THROWS(NoSuchElementException.class,
775 new Fun(){void f(){it.next();}});
776 }
777
778 {
779 final Iterator<Map.Entry<Integer,Integer>> it
780 = m.descendingMap().entrySet().iterator();
781 check(it.hasNext()); equal(it.next().getKey(), 5);
782 check(it.hasNext()); equal(it.next().getKey(), 3);
783 check(it.hasNext()); equal(it.next().getKey(), 1);
784 check(! it.hasNext());
785 THROWS(NoSuchElementException.class,
786 new Fun(){void f(){it.next();}});
787 }
788 }
789
790
791 private static void testNavigableSet(NavigableSet<Integer> s) {
duke6e45e102007-12-01 00:00:00 +0000792 clear(s);
793 checkNavigableSetKeys(s, 1, null, null, null, null);
794
795 check(s.add(1));
796 check(s.add(3));
797 check(s.add(5));
798
799 check(! s.add(1));
800 check(! s.add(3));
801 check(! s.add(5));
802
803 checkNavigableSetKeys(s, 0, null, null, 1, 1);
804 checkNavigableSetKeys(s, 1, null, 1, 1, 3);
805 checkNavigableSetKeys(s, 2, 1, 1, 3, 3);
806 checkNavigableSetKeys(s, 3, 1, 3, 3, 5);
807 checkNavigableSetKeys(s, 5, 3, 5, 5, null);
808 checkNavigableSetKeys(s, 6, 5, 5, null, null);
809
martineb76fa62008-05-10 11:49:25 -0700810 for (final Iterator<Integer> it :
811 (Iterator<Integer>[])
812 new Iterator<?>[] {
813 s.descendingIterator(),
814 s.descendingSet().iterator()}) {
duke6e45e102007-12-01 00:00:00 +0000815 equalNext(it, 5);
816 equalNext(it, 3);
817 equalNext(it, 1);
818 check(! it.hasNext());
819 THROWS(NoSuchElementException.class,
820 new Fun(){void f(){it.next();}});
821 }
822 }
823
824 //--------------------- Infrastructure ---------------------------
825 static volatile int passed = 0, failed = 0;
826 static void pass() { passed++; }
827 static void fail() { failed++; Thread.dumpStack(); }
828 static void fail(String msg) { System.out.println(msg); fail(); }
829 static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
830 static void check(boolean cond) { if (cond) pass(); else fail(); }
831 static void equal(Object x, Object y) {
832 if (x == null ? y == null : x.equals(y)) pass();
833 else {System.out.println(x + " not equal to " + y); fail();}}
834 static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
835 public static void main(String[] args) throws Throwable {
836 try { realMain(args); } catch (Throwable t) { unexpected(t); }
837
838 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
839 if (failed > 0) throw new Exception("Some tests failed");
840 }
841 private static abstract class Fun {abstract void f() throws Throwable;}
842 private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
843 for (Fun f : fs)
844 try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
845 catch (Throwable t) {
846 if (k.isAssignableFrom(t.getClass())) pass();
847 else unexpected(t);}}
848 static byte[] serializedForm(Object obj) {
849 try {
850 ByteArrayOutputStream baos = new ByteArrayOutputStream();
851 new ObjectOutputStream(baos).writeObject(obj);
852 return baos.toByteArray();
853 } catch (IOException e) { throw new Error(e); }}
854 static Object readObject(byte[] bytes)
855 throws IOException, ClassNotFoundException {
856 InputStream is = new ByteArrayInputStream(bytes);
857 return new ObjectInputStream(is).readObject();}
858 @SuppressWarnings("unchecked")
859 static <T> T serialClone(T obj) {
860 try { return (T) readObject(serializedForm(obj)); }
861 catch (Exception e) { throw new Error(e); }}
862}