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