blob: 677de4e317f7db108668b177a289d526d6312665 [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
martineb76fa62008-05-10 11:49:25 -070028 * 6431845 4802633 6570566 6570575 6570631 6570924 6691185
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());
250 }
251
252 private static void testImmutableMap(final Map<Integer,Integer> m) {
253 THROWS(UnsupportedOperationException.class,
254 new Fun(){void f(){ m.put(1,1); }},
255 new Fun(){void f(){ m.putAll(singletonMap(1,1)); }});
256 if (! m.isEmpty()) {
257 final Integer first = m.keySet().iterator().next();
258 THROWS(UnsupportedOperationException.class,
259 new Fun(){void f(){ m.remove(first); }},
260 new Fun(){void f(){ m.clear(); }});
261 final Map.Entry<Integer,Integer> me
262 = m.entrySet().iterator().next();
263 Integer key = me.getKey();
264 Integer val = me.getValue();
265 THROWS(UnsupportedOperationException.class,
266 new Fun(){void f(){ me.setValue(3); }});
267 equal(key, me.getKey());
268 equal(val, me.getValue());
269 }
270 testImmutableSet(m.keySet());
271 testImmutableCollection(m.values());
272 //testImmutableSet(m.entrySet());
273 }
274
275 private static void clear(Map<?,?> m) {
276 try { m.clear(); }
277 catch (Throwable t) { unexpected(t); }
278 testEmptyMap(m);
279 }
280
281 private static void oneElement(Collection<Integer> c) {
282 clear(c);
283 try {
284 check(c.add(-42));
285 equal(c.toString(), "[-42]");
286 if (c instanceof Set) check(! c.add(-42));
287 } catch (Throwable t) { unexpected(t); }
288 check(! c.isEmpty()); check(c.size() == 1);
289 }
290
291 private static boolean supportsAdd(Collection<Integer> c) {
292 try { check(c.add(778347983)); }
293 catch (UnsupportedOperationException t) { return false; }
294 catch (Throwable t) { unexpected(t); }
295
296 try {
297 check(c.contains(778347983));
298 check(c.remove(778347983));
299 } catch (Throwable t) { unexpected(t); }
300 return true;
301 }
302
303 private static boolean supportsRemove(Collection<Integer> c) {
304 try { check(! c.remove(19134032)); }
305 catch (UnsupportedOperationException t) { return false; }
306 catch (Throwable t) { unexpected(t); }
307 return true;
308 }
309
310 private static void checkFunctionalInvariants(Collection<Integer> c) {
311 try {
312 checkContainsSelf(c);
313 checkContainsEmpty(c);
314 check(c.size() != 0 ^ c.isEmpty());
315
316 {
317 int size = 0;
318 for (Integer i : c) size++;
319 check(c.size() == size);
320 }
321
322 check(c.toArray().length == c.size());
323 check(c.toArray().getClass() == Object[].class
324 ||
325 // !!!!
326 // 6260652: (coll) Arrays.asList(x).toArray().getClass()
327 // should be Object[].class
328 (c.getClass().getName().equals("java.util.Arrays$ArrayList"))
329 );
330 for (int size : new int[]{0,1,c.size(), c.size()+1}) {
331 Integer[] a = c.toArray(new Integer[size]);
332 check((size > c.size()) || a.length == c.size());
333 int i = 0; for (Integer j : c) check(a[i++] == j);
334 check((size <= c.size()) || (a[c.size()] == null));
335 check(a.getClass() == Integer[].class);
336 }
337
338 check(c.equals(c));
339 if (c instanceof Serializable) {
340 //System.out.printf("Serializing %s%n", c.getClass().getName());
341 try {
342 Object clone = serialClone(c);
343 equal(c instanceof Serializable,
344 clone instanceof Serializable);
345 equal(c instanceof RandomAccess,
346 clone instanceof RandomAccess);
347 if ((c instanceof List) || (c instanceof Set))
348 equal(c, clone);
349 else
350 equal(new HashSet<Integer>(c),
351 new HashSet<Integer>(serialClone(c)));
352 } catch (Error xxx) {
353 if (! (xxx.getCause() instanceof NotSerializableException))
354 throw xxx;
355 }
356 }
357 }
358 catch (Throwable t) { unexpected(t); }
359 }
360
361 //----------------------------------------------------------------
362 // If add(null) succeeds, contains(null) & remove(null) should succeed
363 //----------------------------------------------------------------
364 private static void testNullElement(Collection<Integer> c) {
365 // !!!! 5018849: (coll) TreeSet.contains(null) does not agree with Javadoc
366 if (c instanceof TreeSet) return;
367
368 try {
369 check(c.add(null));
370 if (c.size() == 1)
371 equal(c.toString(), "[null]");
372 try {
373 checkFunctionalInvariants(c);
374 check(c.contains(null));
375 check(c.remove(null));
376 }
377 catch (Throwable t) { unexpected(t); }
378 }
379 catch (NullPointerException e) { /* OK */ }
380 catch (Throwable t) { unexpected(t); }
381 }
382
383
384 //----------------------------------------------------------------
385 // If add("x") succeeds, contains("x") & remove("x") should succeed
386 //----------------------------------------------------------------
387 @SuppressWarnings("unchecked")
388 private static void testStringElement(Collection<Integer> c) {
389 Collection x = (Collection)c; // Make type-unsafe
390 try {
391 check(x.add("x"));
392 try {
393 check(x.contains("x"));
394 check(x.remove("x"));
395 } catch (Throwable t) { unexpected(t); }
396 }
397 catch (ClassCastException e) { /* OK */ }
398 catch (Throwable t) { unexpected(t); }
399 }
400
401 private static void testConcurrentCollection(Collection<Integer> c) {
402 try {
403 c.add(1);
404 Iterator<Integer> it = c.iterator();
405 check(it.hasNext());
406 clear(c);
407 check(it.next() instanceof Integer); // No CME
408 check(c.isEmpty());
409 }
410 catch (Throwable t) { unexpected(t); }
411 }
412
413 private static void testQueue(Queue<Integer> q) {
414 q.clear();
415 for (int i = 0; i < 5; i++)
416 q.add(i);
417 equal(q.size(), 5);
418 checkFunctionalInvariants(q);
419 q.poll();
420 equal(q.size(), 4);
421 checkFunctionalInvariants(q);
422 }
423
424 private static void testList(final List<Integer> l) {
425 //----------------------------------------------------------------
426 // 4802633: (coll) AbstractList.addAll(-1,emptyCollection)
427 // doesn't throw IndexOutOfBoundsException
428 //----------------------------------------------------------------
429 try {
430 l.addAll(-1, Collections.<Integer>emptyList());
431 fail("Expected IndexOutOfBoundsException not thrown");
432 }
433 catch (UnsupportedOperationException _) {/* OK */}
434 catch (IndexOutOfBoundsException _) {/* OK */}
435 catch (Throwable t) { unexpected(t); }
436
437// equal(l instanceof Serializable,
438// l.subList(0,0) instanceof Serializable);
439 if (l.subList(0,0) instanceof Serializable)
440 check(l instanceof Serializable);
441
442 equal(l instanceof RandomAccess,
443 l.subList(0,0) instanceof RandomAccess);
444 }
445
446 private static void testCollection(Collection<Integer> c) {
447
448 System.out.println("\n==> " + c.getClass().getName());
449
450 checkFunctionalInvariants(c);
451
452 if (! supportsAdd(c)) return;
453 //System.out.println("add() supported");
454
martineb76fa62008-05-10 11:49:25 -0700455 if (c instanceof NavigableSet) {
456 System.out.println("NavigableSet tests...");
457
458 NavigableSet<Integer> ns = (NavigableSet<Integer>)c;
459 testNavigableSet(ns);
460 testNavigableSet(ns.headSet(6, false));
461 testNavigableSet(ns.headSet(5, true));
462 testNavigableSet(ns.tailSet(0, false));
463 testNavigableSet(ns.tailSet(1, true));
464 testNavigableSet(ns.subSet(0, false, 5, true));
465 testNavigableSet(ns.subSet(1, true, 6, false));
466 }
duke6e45e102007-12-01 00:00:00 +0000467
468 if (c instanceof Queue)
469 testQueue((Queue<Integer>)c);
470
471 if (c instanceof List)
472 testList((List<Integer>)c);
473
474 check(supportsRemove(c));
475
476 try {
477 oneElement(c);
478 checkFunctionalInvariants(c);
479 }
480 catch (Throwable t) { unexpected(t); }
481
482 clear(c); testNullElement(c);
483 oneElement(c); testNullElement(c);
484
485 clear(c); testStringElement(c);
486 oneElement(c); testStringElement(c);
487
488 if (c.getClass().getName().matches(".*concurrent.*"))
489 testConcurrentCollection(c);
490
491 //----------------------------------------------------------------
492 // The "all" operations should throw NPE when passed null
493 //----------------------------------------------------------------
494 {
495 oneElement(c);
496 try {
497 c.removeAll(null);
498 fail("Expected NullPointerException");
499 }
500 catch (NullPointerException e) { pass(); }
501 catch (Throwable t) { unexpected(t); }
502
503 oneElement(c);
504 try {
505 c.retainAll(null);
506 fail("Expected NullPointerException");
507 }
508 catch (NullPointerException e) { pass(); }
509 catch (Throwable t) { unexpected(t); }
510
511 oneElement(c);
512 try {
513 c.addAll(null);
514 fail("Expected NullPointerException");
515 }
516 catch (NullPointerException e) { pass(); }
517 catch (Throwable t) { unexpected(t); }
518
519 oneElement(c);
520 try {
521 c.containsAll(null);
522 fail("Expected NullPointerException");
523 }
524 catch (NullPointerException e) { pass(); }
525 catch (Throwable t) { unexpected(t); }
526 }
527 }
528
529 //----------------------------------------------------------------
530 // Map
531 //----------------------------------------------------------------
532 private static void checkFunctionalInvariants(Map<Integer,Integer> m) {
533 check(m.keySet().size() == m.entrySet().size());
534 check(m.keySet().size() == m.size());
535 checkFunctionalInvariants(m.keySet());
536 checkFunctionalInvariants(m.values());
537 check(m.size() != 0 ^ m.isEmpty());
538 }
539
540 private static void testMap(Map<Integer,Integer> m) {
541 System.out.println("\n==> " + m.getClass().getName());
542
543 if (m instanceof ConcurrentMap)
544 testConcurrentMap((ConcurrentMap<Integer,Integer>) m);
545
martineb76fa62008-05-10 11:49:25 -0700546 if (m instanceof NavigableMap) {
547 System.out.println("NavigableMap tests...");
548
549 NavigableMap<Integer,Integer> nm =
550 (NavigableMap<Integer,Integer>) m;
551 testNavigableMap(nm);
552 testNavigableMap(nm.headMap(6, false));
553 testNavigableMap(nm.headMap(5, true));
554 testNavigableMap(nm.tailMap(0, false));
555 testNavigableMap(nm.tailMap(1, true));
556 testNavigableMap(nm.subMap(1, true, 6, false));
557 testNavigableMap(nm.subMap(0, false, 5, true));
558 }
duke6e45e102007-12-01 00:00:00 +0000559
560 checkFunctionalInvariants(m);
561
562 if (supportsClear(m)) {
563 try { clear(m); }
564 catch (Throwable t) { unexpected(t); }
565 }
566
567 if (supportsPut(m)) {
568 try {
569 check(m.put(3333, 77777) == null);
570 check(m.put(9134, 74982) == null);
571 check(m.get(9134) == 74982);
572 check(m.put(9134, 1382) == 74982);
573 check(m.get(9134) == 1382);
574 check(m.size() == 2);
575 checkFunctionalInvariants(m);
576 checkNPEConsistency(m);
577 }
578 catch (Throwable t) { unexpected(t); }
579 }
580 }
581
582 private static boolean supportsPut(Map<Integer,Integer> m) {
583 // We're asking for .equals(...) semantics
584 if (m instanceof IdentityHashMap) return false;
585
586 try { check(m.put(778347983,12735) == null); }
587 catch (UnsupportedOperationException t) { return false; }
588 catch (Throwable t) { unexpected(t); }
589
590 try {
591 check(m.containsKey(778347983));
592 check(m.remove(778347983) != null);
593 } catch (Throwable t) { unexpected(t); }
594 return true;
595 }
596
597 private static boolean supportsClear(Map<?,?> m) {
598 try { m.clear(); }
599 catch (UnsupportedOperationException t) { return false; }
600 catch (Throwable t) { unexpected(t); }
601 return true;
602 }
603
604 //----------------------------------------------------------------
605 // ConcurrentMap
606 //----------------------------------------------------------------
607 private static void testConcurrentMap(ConcurrentMap<Integer,Integer> m) {
608 System.out.println("ConcurrentMap tests...");
609
610 try {
611 clear(m);
612
613 check(m.putIfAbsent(18357,7346) == null);
614 check(m.containsKey(18357));
615 check(m.putIfAbsent(18357,8263) == 7346);
616 try { m.putIfAbsent(18357,null); fail("NPE"); }
617 catch (NullPointerException t) { }
618 check(m.containsKey(18357));
619
620 check(! m.replace(18357,8888,7777));
621 check(m.containsKey(18357));
622 try { m.replace(18357,null,7777); fail("NPE"); }
623 catch (NullPointerException t) { }
624 check(m.containsKey(18357));
625 check(m.get(18357) == 7346);
626 check(m.replace(18357,7346,5555));
627 check(m.replace(18357,5555,7346));
628 check(m.get(18357) == 7346);
629
630 check(m.replace(92347,7834) == null);
631 try { m.replace(18357,null); fail("NPE"); }
632 catch (NullPointerException t) { }
633 check(m.replace(18357,7346) == 7346);
634 check(m.replace(18357,5555) == 7346);
635 check(m.get(18357) == 5555);
636 check(m.replace(18357,7346) == 5555);
637 check(m.get(18357) == 7346);
638
639 check(! m.remove(18357,9999));
640 check(m.get(18357) == 7346);
641 check(m.containsKey(18357));
642 check(! m.remove(18357,null)); // 6272521
643 check(m.get(18357) == 7346);
644 check(m.remove(18357,7346));
645 check(m.get(18357) == null);
646 check(! m.containsKey(18357));
647 check(m.isEmpty());
648
649 m.putIfAbsent(1,2);
650 check(m.size() == 1);
651 check(! m.remove(1,null));
652 check(! m.remove(1,null));
653 check(! m.remove(1,1));
654 check(m.remove(1,2));
655 check(m.isEmpty());
656
657 testEmptyMap(m);
658 }
659 catch (Throwable t) { unexpected(t); }
660 }
661
662 private static void throwsConsistently(Class<? extends Throwable> k,
663 Iterable<Fun> fs) {
664 List<Class<? extends Throwable>> threw
665 = new ArrayList<Class<? extends Throwable>>();
666 for (Fun f : fs)
667 try { f.f(); threw.add(null); }
668 catch (Throwable t) {
669 check(k.isAssignableFrom(t.getClass()));
670 threw.add(t.getClass());
671 }
672 if (new HashSet<Object>(threw).size() != 1)
673 fail(threw.toString());
674 }
675
676 private static <T> void checkNPEConsistency(final Map<T,Integer> m) {
677 m.clear();
678 final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap)
679 ? (ConcurrentMap<T,Integer>) m
680 : null;
681 List<Fun> fs = new ArrayList<Fun>();
682 fs.add(new Fun(){void f(){ check(! m.containsKey(null));}});
683 fs.add(new Fun(){void f(){ equal(m.remove(null), null);}});
684 fs.add(new Fun(){void f(){ equal(m.get(null), null);}});
685 if (cm != null) {
686 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});}
687 throwsConsistently(NullPointerException.class, fs);
688
689 fs.clear();
690 final Map<T,Integer> sm = singletonMap(null,1);
691 fs.add(new Fun(){void f(){ equal(m.put(null,1), null); m.clear();}});
692 fs.add(new Fun(){void f(){ m.putAll(sm); m.clear();}});
693 if (cm != null) {
694 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});
695 fs.add(new Fun(){void f(){ equal(cm.putIfAbsent(null,1), 1);}});
696 fs.add(new Fun(){void f(){ equal(cm.replace(null,1), null);}});
697 fs.add(new Fun(){void f(){ equal(cm.replace(null,1, 1), 1);}});
698 }
699 throwsConsistently(NullPointerException.class, fs);
700 }
701
702 //----------------------------------------------------------------
703 // NavigableMap
704 //----------------------------------------------------------------
705 private static void
706 checkNavigableMapKeys(NavigableMap<Integer,Integer> m,
707 Integer i,
708 Integer lower,
709 Integer floor,
710 Integer ceiling,
711 Integer higher) {
712 equal(m.lowerKey(i), lower);
713 equal(m.floorKey(i), floor);
714 equal(m.ceilingKey(i), ceiling);
715 equal(m.higherKey(i), higher);
716 }
717
718 private static void
719 checkNavigableSetKeys(NavigableSet<Integer> m,
720 Integer i,
721 Integer lower,
722 Integer floor,
723 Integer ceiling,
724 Integer higher) {
725 equal(m.lower(i), lower);
726 equal(m.floor(i), floor);
727 equal(m.ceiling(i), ceiling);
728 equal(m.higher(i), higher);
729 }
730
731 static final Random rnd = new Random();
732 static void equalNext(final Iterator<?> it, Object expected) {
733 if (rnd.nextBoolean())
734 check(it.hasNext());
735 equal(it.next(), expected);
736 }
737
738 private static void testNavigableMap(NavigableMap<Integer,Integer> m)
739 {
duke6e45e102007-12-01 00:00:00 +0000740 clear(m);
741 checkNavigableMapKeys(m, 1, null, null, null, null);
742
743 equal(m.put(1, 2), null);
744 equal(m.put(3, 4), null);
745 equal(m.put(5, 9), null);
746
747 equal(m.put(1, 2), 2);
748 equal(m.put(3, 4), 4);
749 equal(m.put(5, 6), 9);
750
751 checkNavigableMapKeys(m, 0, null, null, 1, 1);
752 checkNavigableMapKeys(m, 1, null, 1, 1, 3);
753 checkNavigableMapKeys(m, 2, 1, 1, 3, 3);
754 checkNavigableMapKeys(m, 3, 1, 3, 3, 5);
755 checkNavigableMapKeys(m, 5, 3, 5, 5, null);
756 checkNavigableMapKeys(m, 6, 5, 5, null, null);
757
martineb76fa62008-05-10 11:49:25 -0700758 for (final Iterator<Integer> it :
759 (Iterator<Integer>[])
760 new Iterator<?>[] {
761 m.descendingKeySet().iterator(),
762 m.navigableKeySet().descendingIterator()}) {
duke6e45e102007-12-01 00:00:00 +0000763 equalNext(it, 5);
764 equalNext(it, 3);
765 equalNext(it, 1);
766 check(! it.hasNext());
767 THROWS(NoSuchElementException.class,
768 new Fun(){void f(){it.next();}});
769 }
770
771 {
772 final Iterator<Map.Entry<Integer,Integer>> it
773 = m.descendingMap().entrySet().iterator();
774 check(it.hasNext()); equal(it.next().getKey(), 5);
775 check(it.hasNext()); equal(it.next().getKey(), 3);
776 check(it.hasNext()); equal(it.next().getKey(), 1);
777 check(! it.hasNext());
778 THROWS(NoSuchElementException.class,
779 new Fun(){void f(){it.next();}});
780 }
781 }
782
783
784 private static void testNavigableSet(NavigableSet<Integer> s) {
duke6e45e102007-12-01 00:00:00 +0000785 clear(s);
786 checkNavigableSetKeys(s, 1, null, null, null, null);
787
788 check(s.add(1));
789 check(s.add(3));
790 check(s.add(5));
791
792 check(! s.add(1));
793 check(! s.add(3));
794 check(! s.add(5));
795
796 checkNavigableSetKeys(s, 0, null, null, 1, 1);
797 checkNavigableSetKeys(s, 1, null, 1, 1, 3);
798 checkNavigableSetKeys(s, 2, 1, 1, 3, 3);
799 checkNavigableSetKeys(s, 3, 1, 3, 3, 5);
800 checkNavigableSetKeys(s, 5, 3, 5, 5, null);
801 checkNavigableSetKeys(s, 6, 5, 5, null, null);
802
martineb76fa62008-05-10 11:49:25 -0700803 for (final Iterator<Integer> it :
804 (Iterator<Integer>[])
805 new Iterator<?>[] {
806 s.descendingIterator(),
807 s.descendingSet().iterator()}) {
duke6e45e102007-12-01 00:00:00 +0000808 equalNext(it, 5);
809 equalNext(it, 3);
810 equalNext(it, 1);
811 check(! it.hasNext());
812 THROWS(NoSuchElementException.class,
813 new Fun(){void f(){it.next();}});
814 }
815 }
816
817 //--------------------- Infrastructure ---------------------------
818 static volatile int passed = 0, failed = 0;
819 static void pass() { passed++; }
820 static void fail() { failed++; Thread.dumpStack(); }
821 static void fail(String msg) { System.out.println(msg); fail(); }
822 static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
823 static void check(boolean cond) { if (cond) pass(); else fail(); }
824 static void equal(Object x, Object y) {
825 if (x == null ? y == null : x.equals(y)) pass();
826 else {System.out.println(x + " not equal to " + y); fail();}}
827 static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
828 public static void main(String[] args) throws Throwable {
829 try { realMain(args); } catch (Throwable t) { unexpected(t); }
830
831 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
832 if (failed > 0) throw new Exception("Some tests failed");
833 }
834 private static abstract class Fun {abstract void f() throws Throwable;}
835 private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
836 for (Fun f : fs)
837 try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
838 catch (Throwable t) {
839 if (k.isAssignableFrom(t.getClass())) pass();
840 else unexpected(t);}}
841 static byte[] serializedForm(Object obj) {
842 try {
843 ByteArrayOutputStream baos = new ByteArrayOutputStream();
844 new ObjectOutputStream(baos).writeObject(obj);
845 return baos.toByteArray();
846 } catch (IOException e) { throw new Error(e); }}
847 static Object readObject(byte[] bytes)
848 throws IOException, ClassNotFoundException {
849 InputStream is = new ByteArrayInputStream(bytes);
850 return new ObjectInputStream(is).readObject();}
851 @SuppressWarnings("unchecked")
852 static <T> T serialClone(T obj) {
853 try { return (T) readObject(serializedForm(obj)); }
854 catch (Exception e) { throw new Error(e); }}
855}