blob: d2b6b6869037bee4d5e1062c3cdd47394811596a [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);
dl8e56eb52009-07-28 17:17:55 -0700429 if ((q instanceof LinkedBlockingQueue) ||
430 (q instanceof LinkedBlockingDeque) ||
431 (q instanceof ConcurrentLinkedQueue)) {
432 testQueueIteratorRemove(q);
433 }
434 }
435
436 private static void testQueueIteratorRemove(Queue<Integer> q) {
437 System.err.printf("testQueueIteratorRemove %s%n",
438 q.getClass().getSimpleName());
439 q.clear();
440 for (int i = 0; i < 5; i++)
441 q.add(i);
442 Iterator<Integer> it = q.iterator();
443 check(it.hasNext());
444 for (int i = 3; i >= 0; i--)
445 q.remove(i);
446 equal(it.next(), 0);
447 equal(it.next(), 4);
448
449 q.clear();
450 for (int i = 0; i < 5; i++)
451 q.add(i);
452 it = q.iterator();
453 equal(it.next(), 0);
454 check(it.hasNext());
455 for (int i = 1; i < 4; i++)
456 q.remove(i);
457 equal(it.next(), 1);
458 equal(it.next(), 4);
duke6e45e102007-12-01 00:00:00 +0000459 }
460
461 private static void testList(final List<Integer> l) {
462 //----------------------------------------------------------------
463 // 4802633: (coll) AbstractList.addAll(-1,emptyCollection)
464 // doesn't throw IndexOutOfBoundsException
465 //----------------------------------------------------------------
466 try {
467 l.addAll(-1, Collections.<Integer>emptyList());
468 fail("Expected IndexOutOfBoundsException not thrown");
469 }
470 catch (UnsupportedOperationException _) {/* OK */}
471 catch (IndexOutOfBoundsException _) {/* OK */}
472 catch (Throwable t) { unexpected(t); }
473
474// equal(l instanceof Serializable,
475// l.subList(0,0) instanceof Serializable);
476 if (l.subList(0,0) instanceof Serializable)
477 check(l instanceof Serializable);
478
479 equal(l instanceof RandomAccess,
480 l.subList(0,0) instanceof RandomAccess);
481 }
482
483 private static void testCollection(Collection<Integer> c) {
dl8e56eb52009-07-28 17:17:55 -0700484 try { testCollection1(c); }
485 catch (Throwable t) { unexpected(t); }
486 }
487
488 private static void testCollection1(Collection<Integer> c) {
duke6e45e102007-12-01 00:00:00 +0000489
490 System.out.println("\n==> " + c.getClass().getName());
491
492 checkFunctionalInvariants(c);
493
494 if (! supportsAdd(c)) return;
495 //System.out.println("add() supported");
496
martineb76fa62008-05-10 11:49:25 -0700497 if (c instanceof NavigableSet) {
498 System.out.println("NavigableSet tests...");
499
500 NavigableSet<Integer> ns = (NavigableSet<Integer>)c;
501 testNavigableSet(ns);
502 testNavigableSet(ns.headSet(6, false));
503 testNavigableSet(ns.headSet(5, true));
504 testNavigableSet(ns.tailSet(0, false));
505 testNavigableSet(ns.tailSet(1, true));
506 testNavigableSet(ns.subSet(0, false, 5, true));
507 testNavigableSet(ns.subSet(1, true, 6, false));
508 }
duke6e45e102007-12-01 00:00:00 +0000509
510 if (c instanceof Queue)
511 testQueue((Queue<Integer>)c);
512
513 if (c instanceof List)
514 testList((List<Integer>)c);
515
516 check(supportsRemove(c));
517
518 try {
519 oneElement(c);
520 checkFunctionalInvariants(c);
521 }
522 catch (Throwable t) { unexpected(t); }
523
524 clear(c); testNullElement(c);
525 oneElement(c); testNullElement(c);
526
527 clear(c); testStringElement(c);
528 oneElement(c); testStringElement(c);
529
530 if (c.getClass().getName().matches(".*concurrent.*"))
531 testConcurrentCollection(c);
532
533 //----------------------------------------------------------------
534 // The "all" operations should throw NPE when passed null
535 //----------------------------------------------------------------
536 {
537 oneElement(c);
538 try {
539 c.removeAll(null);
540 fail("Expected NullPointerException");
541 }
542 catch (NullPointerException e) { pass(); }
543 catch (Throwable t) { unexpected(t); }
544
545 oneElement(c);
546 try {
547 c.retainAll(null);
548 fail("Expected NullPointerException");
549 }
550 catch (NullPointerException e) { pass(); }
551 catch (Throwable t) { unexpected(t); }
552
553 oneElement(c);
554 try {
555 c.addAll(null);
556 fail("Expected NullPointerException");
557 }
558 catch (NullPointerException e) { pass(); }
559 catch (Throwable t) { unexpected(t); }
560
561 oneElement(c);
562 try {
563 c.containsAll(null);
564 fail("Expected NullPointerException");
565 }
566 catch (NullPointerException e) { pass(); }
567 catch (Throwable t) { unexpected(t); }
568 }
569 }
570
571 //----------------------------------------------------------------
572 // Map
573 //----------------------------------------------------------------
574 private static void checkFunctionalInvariants(Map<Integer,Integer> m) {
575 check(m.keySet().size() == m.entrySet().size());
576 check(m.keySet().size() == m.size());
577 checkFunctionalInvariants(m.keySet());
578 checkFunctionalInvariants(m.values());
579 check(m.size() != 0 ^ m.isEmpty());
580 }
581
582 private static void testMap(Map<Integer,Integer> m) {
583 System.out.println("\n==> " + m.getClass().getName());
584
585 if (m instanceof ConcurrentMap)
586 testConcurrentMap((ConcurrentMap<Integer,Integer>) m);
587
martineb76fa62008-05-10 11:49:25 -0700588 if (m instanceof NavigableMap) {
589 System.out.println("NavigableMap tests...");
590
591 NavigableMap<Integer,Integer> nm =
592 (NavigableMap<Integer,Integer>) m;
dl0ad61612009-03-24 19:42:23 -0700593 testNavigableMapRemovers(nm);
martineb76fa62008-05-10 11:49:25 -0700594 testNavigableMap(nm);
595 testNavigableMap(nm.headMap(6, false));
596 testNavigableMap(nm.headMap(5, true));
597 testNavigableMap(nm.tailMap(0, false));
598 testNavigableMap(nm.tailMap(1, true));
599 testNavigableMap(nm.subMap(1, true, 6, false));
600 testNavigableMap(nm.subMap(0, false, 5, true));
601 }
duke6e45e102007-12-01 00:00:00 +0000602
603 checkFunctionalInvariants(m);
604
605 if (supportsClear(m)) {
606 try { clear(m); }
607 catch (Throwable t) { unexpected(t); }
608 }
609
610 if (supportsPut(m)) {
611 try {
612 check(m.put(3333, 77777) == null);
613 check(m.put(9134, 74982) == null);
614 check(m.get(9134) == 74982);
615 check(m.put(9134, 1382) == 74982);
616 check(m.get(9134) == 1382);
617 check(m.size() == 2);
618 checkFunctionalInvariants(m);
619 checkNPEConsistency(m);
620 }
621 catch (Throwable t) { unexpected(t); }
622 }
623 }
624
625 private static boolean supportsPut(Map<Integer,Integer> m) {
626 // We're asking for .equals(...) semantics
627 if (m instanceof IdentityHashMap) return false;
628
629 try { check(m.put(778347983,12735) == null); }
630 catch (UnsupportedOperationException t) { return false; }
631 catch (Throwable t) { unexpected(t); }
632
633 try {
634 check(m.containsKey(778347983));
635 check(m.remove(778347983) != null);
636 } catch (Throwable t) { unexpected(t); }
637 return true;
638 }
639
640 private static boolean supportsClear(Map<?,?> m) {
641 try { m.clear(); }
642 catch (UnsupportedOperationException t) { return false; }
643 catch (Throwable t) { unexpected(t); }
644 return true;
645 }
646
647 //----------------------------------------------------------------
648 // ConcurrentMap
649 //----------------------------------------------------------------
650 private static void testConcurrentMap(ConcurrentMap<Integer,Integer> m) {
651 System.out.println("ConcurrentMap tests...");
652
653 try {
654 clear(m);
655
656 check(m.putIfAbsent(18357,7346) == null);
657 check(m.containsKey(18357));
658 check(m.putIfAbsent(18357,8263) == 7346);
659 try { m.putIfAbsent(18357,null); fail("NPE"); }
660 catch (NullPointerException t) { }
661 check(m.containsKey(18357));
662
663 check(! m.replace(18357,8888,7777));
664 check(m.containsKey(18357));
665 try { m.replace(18357,null,7777); fail("NPE"); }
666 catch (NullPointerException t) { }
667 check(m.containsKey(18357));
668 check(m.get(18357) == 7346);
669 check(m.replace(18357,7346,5555));
670 check(m.replace(18357,5555,7346));
671 check(m.get(18357) == 7346);
672
673 check(m.replace(92347,7834) == null);
674 try { m.replace(18357,null); fail("NPE"); }
675 catch (NullPointerException t) { }
676 check(m.replace(18357,7346) == 7346);
677 check(m.replace(18357,5555) == 7346);
678 check(m.get(18357) == 5555);
679 check(m.replace(18357,7346) == 5555);
680 check(m.get(18357) == 7346);
681
682 check(! m.remove(18357,9999));
683 check(m.get(18357) == 7346);
684 check(m.containsKey(18357));
685 check(! m.remove(18357,null)); // 6272521
686 check(m.get(18357) == 7346);
687 check(m.remove(18357,7346));
688 check(m.get(18357) == null);
689 check(! m.containsKey(18357));
690 check(m.isEmpty());
691
692 m.putIfAbsent(1,2);
693 check(m.size() == 1);
694 check(! m.remove(1,null));
695 check(! m.remove(1,null));
696 check(! m.remove(1,1));
697 check(m.remove(1,2));
698 check(m.isEmpty());
699
700 testEmptyMap(m);
701 }
702 catch (Throwable t) { unexpected(t); }
703 }
704
705 private static void throwsConsistently(Class<? extends Throwable> k,
706 Iterable<Fun> fs) {
707 List<Class<? extends Throwable>> threw
708 = new ArrayList<Class<? extends Throwable>>();
709 for (Fun f : fs)
710 try { f.f(); threw.add(null); }
711 catch (Throwable t) {
712 check(k.isAssignableFrom(t.getClass()));
713 threw.add(t.getClass());
714 }
715 if (new HashSet<Object>(threw).size() != 1)
716 fail(threw.toString());
717 }
718
719 private static <T> void checkNPEConsistency(final Map<T,Integer> m) {
720 m.clear();
721 final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap)
722 ? (ConcurrentMap<T,Integer>) m
723 : null;
724 List<Fun> fs = new ArrayList<Fun>();
725 fs.add(new Fun(){void f(){ check(! m.containsKey(null));}});
726 fs.add(new Fun(){void f(){ equal(m.remove(null), null);}});
727 fs.add(new Fun(){void f(){ equal(m.get(null), null);}});
728 if (cm != null) {
729 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});}
730 throwsConsistently(NullPointerException.class, fs);
731
732 fs.clear();
733 final Map<T,Integer> sm = singletonMap(null,1);
734 fs.add(new Fun(){void f(){ equal(m.put(null,1), null); m.clear();}});
735 fs.add(new Fun(){void f(){ m.putAll(sm); m.clear();}});
736 if (cm != null) {
737 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});
738 fs.add(new Fun(){void f(){ equal(cm.putIfAbsent(null,1), 1);}});
739 fs.add(new Fun(){void f(){ equal(cm.replace(null,1), null);}});
740 fs.add(new Fun(){void f(){ equal(cm.replace(null,1, 1), 1);}});
741 }
742 throwsConsistently(NullPointerException.class, fs);
743 }
744
745 //----------------------------------------------------------------
746 // NavigableMap
747 //----------------------------------------------------------------
748 private static void
749 checkNavigableMapKeys(NavigableMap<Integer,Integer> m,
750 Integer i,
751 Integer lower,
752 Integer floor,
753 Integer ceiling,
754 Integer higher) {
755 equal(m.lowerKey(i), lower);
756 equal(m.floorKey(i), floor);
757 equal(m.ceilingKey(i), ceiling);
758 equal(m.higherKey(i), higher);
759 }
760
761 private static void
762 checkNavigableSetKeys(NavigableSet<Integer> m,
763 Integer i,
764 Integer lower,
765 Integer floor,
766 Integer ceiling,
767 Integer higher) {
768 equal(m.lower(i), lower);
769 equal(m.floor(i), floor);
770 equal(m.ceiling(i), ceiling);
771 equal(m.higher(i), higher);
772 }
773
774 static final Random rnd = new Random();
775 static void equalNext(final Iterator<?> it, Object expected) {
776 if (rnd.nextBoolean())
777 check(it.hasNext());
778 equal(it.next(), expected);
779 }
780
dl0ad61612009-03-24 19:42:23 -0700781 static void equalMaps(Map m1, Map m2) {
782 equal(m1, m2);
783 equal(m2, m1);
784 equal(m1.size(), m2.size());
785 equal(m1.isEmpty(), m2.isEmpty());
786 equal(m1.toString(), m2.toString());
787 check(Arrays.equals(m1.entrySet().toArray(), m2.entrySet().toArray()));
788 }
789
790 @SuppressWarnings({"unchecked", "rawtypes"})
791 static void testNavigableMapRemovers(NavigableMap m)
792 {
793 final Map emptyMap = new HashMap();
794
795 final Map singletonMap = new HashMap();
796 singletonMap.put(1, 2);
797
798 abstract class NavigableMapView {
799 abstract NavigableMap view(NavigableMap m);
800 }
801
802 NavigableMapView[] views = {
803 new NavigableMapView() { NavigableMap view(NavigableMap m) {
804 return m; }},
805 new NavigableMapView() { NavigableMap view(NavigableMap m) {
806 return m.headMap(99, true); }},
807 new NavigableMapView() { NavigableMap view(NavigableMap m) {
808 return m.tailMap(-99, false); }},
809 new NavigableMapView() { NavigableMap view(NavigableMap m) {
810 return m.subMap(-99, true, 99, false); }},
811 };
812
813 abstract class Remover {
814 abstract void remove(NavigableMap m, Object k, Object v);
815 }
816
817 Remover[] removers = {
818 new Remover() { void remove(NavigableMap m, Object k, Object v) {
819 equal(m.remove(k), v); }},
820
821 new Remover() { void remove(NavigableMap m, Object k, Object v) {
822 equal(m.descendingMap().remove(k), v); }},
823 new Remover() { void remove(NavigableMap m, Object k, Object v) {
824 equal(m.descendingMap().headMap(-86, false).remove(k), v); }},
825 new Remover() { void remove(NavigableMap m, Object k, Object v) {
826 equal(m.descendingMap().tailMap(86, true).remove(k), v); }},
827
828 new Remover() { void remove(NavigableMap m, Object k, Object v) {
829 equal(m.headMap(86, true).remove(k), v); }},
830 new Remover() { void remove(NavigableMap m, Object k, Object v) {
831 equal(m.tailMap(-86, true).remove(k), v); }},
832 new Remover() { void remove(NavigableMap m, Object k, Object v) {
833 equal(m.subMap(-86, false, 86, true).remove(k), v); }},
834
835 new Remover() { void remove(NavigableMap m, Object k, Object v) {
836 check(m.keySet().remove(k)); }},
837 new Remover() { void remove(NavigableMap m, Object k, Object v) {
838 check(m.navigableKeySet().remove(k)); }},
839
840 new Remover() { void remove(NavigableMap m, Object k, Object v) {
841 check(m.navigableKeySet().headSet(86, true).remove(k)); }},
842 new Remover() { void remove(NavigableMap m, Object k, Object v) {
843 check(m.navigableKeySet().tailSet(-86, false).remove(k)); }},
844 new Remover() { void remove(NavigableMap m, Object k, Object v) {
845 check(m.navigableKeySet().subSet(-86, true, 86, false)
846 .remove(k)); }},
847
848 new Remover() { void remove(NavigableMap m, Object k, Object v) {
849 check(m.descendingKeySet().headSet(-86, false).remove(k)); }},
850 new Remover() { void remove(NavigableMap m, Object k, Object v) {
851 check(m.descendingKeySet().tailSet(86, true).remove(k)); }},
852 new Remover() { void remove(NavigableMap m, Object k, Object v) {
853 check(m.descendingKeySet().subSet(86, true, -86, false)
854 .remove(k)); }},
855 };
856
857 for (NavigableMapView view : views) {
858 for (Remover remover : removers) {
859 try {
860 m.clear();
861 equalMaps(m, emptyMap);
862 equal(m.put(1, 2), null);
863 equalMaps(m, singletonMap);
864 NavigableMap v = view.view(m);
865 remover.remove(v, 1, 2);
866 equalMaps(m, emptyMap);
867 } catch (Throwable t) { unexpected(t); }
868 }
869 }
870 }
871
duke6e45e102007-12-01 00:00:00 +0000872 private static void testNavigableMap(NavigableMap<Integer,Integer> m)
873 {
duke6e45e102007-12-01 00:00:00 +0000874 clear(m);
875 checkNavigableMapKeys(m, 1, null, null, null, null);
876
877 equal(m.put(1, 2), null);
878 equal(m.put(3, 4), null);
879 equal(m.put(5, 9), null);
880
881 equal(m.put(1, 2), 2);
882 equal(m.put(3, 4), 4);
883 equal(m.put(5, 6), 9);
884
885 checkNavigableMapKeys(m, 0, null, null, 1, 1);
886 checkNavigableMapKeys(m, 1, null, 1, 1, 3);
887 checkNavigableMapKeys(m, 2, 1, 1, 3, 3);
888 checkNavigableMapKeys(m, 3, 1, 3, 3, 5);
889 checkNavigableMapKeys(m, 5, 3, 5, 5, null);
890 checkNavigableMapKeys(m, 6, 5, 5, null, null);
891
martineb76fa62008-05-10 11:49:25 -0700892 for (final Iterator<Integer> it :
893 (Iterator<Integer>[])
894 new Iterator<?>[] {
895 m.descendingKeySet().iterator(),
896 m.navigableKeySet().descendingIterator()}) {
duke6e45e102007-12-01 00:00:00 +0000897 equalNext(it, 5);
898 equalNext(it, 3);
899 equalNext(it, 1);
900 check(! it.hasNext());
901 THROWS(NoSuchElementException.class,
902 new Fun(){void f(){it.next();}});
903 }
904
905 {
906 final Iterator<Map.Entry<Integer,Integer>> it
907 = m.descendingMap().entrySet().iterator();
908 check(it.hasNext()); equal(it.next().getKey(), 5);
909 check(it.hasNext()); equal(it.next().getKey(), 3);
910 check(it.hasNext()); equal(it.next().getKey(), 1);
911 check(! it.hasNext());
912 THROWS(NoSuchElementException.class,
913 new Fun(){void f(){it.next();}});
914 }
915 }
916
917
918 private static void testNavigableSet(NavigableSet<Integer> s) {
duke6e45e102007-12-01 00:00:00 +0000919 clear(s);
920 checkNavigableSetKeys(s, 1, null, null, null, null);
921
922 check(s.add(1));
923 check(s.add(3));
924 check(s.add(5));
925
926 check(! s.add(1));
927 check(! s.add(3));
928 check(! s.add(5));
929
930 checkNavigableSetKeys(s, 0, null, null, 1, 1);
931 checkNavigableSetKeys(s, 1, null, 1, 1, 3);
932 checkNavigableSetKeys(s, 2, 1, 1, 3, 3);
933 checkNavigableSetKeys(s, 3, 1, 3, 3, 5);
934 checkNavigableSetKeys(s, 5, 3, 5, 5, null);
935 checkNavigableSetKeys(s, 6, 5, 5, null, null);
936
martineb76fa62008-05-10 11:49:25 -0700937 for (final Iterator<Integer> it :
938 (Iterator<Integer>[])
939 new Iterator<?>[] {
940 s.descendingIterator(),
941 s.descendingSet().iterator()}) {
duke6e45e102007-12-01 00:00:00 +0000942 equalNext(it, 5);
943 equalNext(it, 3);
944 equalNext(it, 1);
945 check(! it.hasNext());
946 THROWS(NoSuchElementException.class,
947 new Fun(){void f(){it.next();}});
948 }
949 }
950
951 //--------------------- Infrastructure ---------------------------
952 static volatile int passed = 0, failed = 0;
953 static void pass() { passed++; }
954 static void fail() { failed++; Thread.dumpStack(); }
955 static void fail(String msg) { System.out.println(msg); fail(); }
956 static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
957 static void check(boolean cond) { if (cond) pass(); else fail(); }
958 static void equal(Object x, Object y) {
959 if (x == null ? y == null : x.equals(y)) pass();
960 else {System.out.println(x + " not equal to " + y); fail();}}
961 static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
962 public static void main(String[] args) throws Throwable {
963 try { realMain(args); } catch (Throwable t) { unexpected(t); }
964
965 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
966 if (failed > 0) throw new Exception("Some tests failed");
967 }
968 private static abstract class Fun {abstract void f() throws Throwable;}
969 private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
970 for (Fun f : fs)
971 try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
972 catch (Throwable t) {
973 if (k.isAssignableFrom(t.getClass())) pass();
974 else unexpected(t);}}
975 static byte[] serializedForm(Object obj) {
976 try {
977 ByteArrayOutputStream baos = new ByteArrayOutputStream();
978 new ObjectOutputStream(baos).writeObject(obj);
979 return baos.toByteArray();
980 } catch (IOException e) { throw new Error(e); }}
981 static Object readObject(byte[] bytes)
982 throws IOException, ClassNotFoundException {
983 InputStream is = new ByteArrayInputStream(bytes);
984 return new ObjectInputStream(is).readObject();}
985 @SuppressWarnings("unchecked")
986 static <T> T serialClone(T obj) {
987 try { return (T) readObject(serializedForm(obj)); }
988 catch (Exception e) { throw new Error(e); }}
989}