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