MOAT.java revision 1468
0N/A/*
3909N/A * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2362N/A * CA 95054 USA or visit www.sun.com if you need additional information or
2362N/A * have any questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
4374N/A * @bug 6207984 6272521 6192552 6269713 6197726 6260652 5073546 4137464
0N/A * 4155650 4216399 4294891 6282555 6318622 6355327 6383475 6420753
0N/A * 6431845 4802633 6570566 6570575 6570631 6570924 6691185 6691215
0N/A * @summary Run many tests on many Collection and Map implementations
0N/A * @author Martin Buchholz
0N/A */
0N/A
0N/A/* Mother Of All (Collection) Tests
0N/A *
0N/A * Testing of collection classes is often spotty, because many tests
0N/A * need to be performed on many implementations, but the onus on
0N/A * writing the tests falls on the engineer introducing the new
0N/A * implementation.
0N/A *
0N/A * The idea of this mega-test is that:
0N/A *
0N/A * An engineer adding a new collection implementation could simply add
0N/A * their new implementation to a list of implementations in this
0N/A * test's main method. Any general purpose Collection<Integer> or
0N/A * Map<Integer,Integer> class is appropriate.
0N/A *
0N/A * An engineer fixing a regression could add their regression test here and
0N/A * simultaneously test all other implementations.
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.util.concurrent.*;
0N/Aimport static java.util.Collections.*;
0N/A
0N/Apublic class MOAT {
0N/A public static void realMain(String[] args) {
0N/A
0N/A testCollection(new LinkedHashSet<Integer>());
0N/A testCollection(new HashSet<Integer>());
0N/A testCollection(new Vector<Integer>());
0N/A testCollection(new Vector<Integer>().subList(0,0));
0N/A testCollection(new ArrayDeque<Integer>());
0N/A testCollection(new ArrayList<Integer>());
0N/A testCollection(new ArrayList<Integer>().subList(0,0));
0N/A testCollection(new LinkedList<Integer>());
0N/A testCollection(new LinkedList<Integer>().subList(0,0));
0N/A testCollection(new TreeSet<Integer>());
0N/A
0N/A testCollection(new CopyOnWriteArrayList<Integer>());
0N/A testCollection(new CopyOnWriteArrayList<Integer>().subList(0,0));
0N/A testCollection(new CopyOnWriteArraySet<Integer>());
0N/A testCollection(new PriorityQueue<Integer>());
0N/A testCollection(new PriorityBlockingQueue<Integer>());
0N/A testCollection(new ArrayBlockingQueue<Integer>(20));
0N/A testCollection(new LinkedBlockingQueue<Integer>(20));
0N/A testCollection(new LinkedBlockingDeque<Integer>(20));
0N/A testCollection(new ConcurrentLinkedQueue<Integer>());
0N/A testCollection(new ConcurrentSkipListSet<Integer>());
0N/A testCollection(Arrays.asList(new Integer(42)));
0N/A testCollection(Arrays.asList(1,2,3));
0N/A testCollection(nCopies(25,1));
0N/A testImmutableList(nCopies(25,1));
0N/A testImmutableList(unmodifiableList(Arrays.asList(1,2,3)));
0N/A
0N/A testMap(new HashMap<Integer,Integer>());
0N/A testMap(new LinkedHashMap<Integer,Integer>());
0N/A testMap(new WeakHashMap<Integer,Integer>());
0N/A testMap(new IdentityHashMap<Integer,Integer>());
0N/A testMap(new TreeMap<Integer,Integer>());
0N/A testMap(new Hashtable<Integer,Integer>());
0N/A testMap(new ConcurrentHashMap<Integer,Integer>(10, 0.5f));
0N/A testMap(new ConcurrentSkipListMap<Integer,Integer>());
0N/A
0N/A // Empty collections
0N/A final List<Integer> emptyArray = Arrays.asList(new Integer[]{});
0N/A testCollection(emptyArray);
0N/A testEmptyList(emptyArray);
0N/A THROWS(IndexOutOfBoundsException.class,
0N/A new Fun(){void f(){ emptyArray.set(0,1); }});
0N/A THROWS(UnsupportedOperationException.class,
0N/A new Fun(){void f(){ emptyArray.add(0,1); }});
0N/A
0N/A List<Integer> noOne = nCopies(0,1);
0N/A testCollection(noOne);
0N/A testEmptyList(noOne);
0N/A testImmutableList(noOne);
0N/A
0N/A Set<Integer> emptySet = emptySet();
0N/A testCollection(emptySet);
0N/A testEmptySet(emptySet);
0N/A testEmptySet(EMPTY_SET);
0N/A testImmutableSet(emptySet);
0N/A
0N/A List<Integer> emptyList = emptyList();
0N/A testCollection(emptyList);
0N/A testEmptyList(emptyList);
0N/A testEmptyList(EMPTY_LIST);
0N/A testImmutableList(emptyList);
0N/A
0N/A Map<Integer,Integer> emptyMap = emptyMap();
0N/A testMap(emptyMap);
0N/A testEmptyMap(emptyMap);
0N/A testEmptyMap(EMPTY_MAP);
0N/A testImmutableMap(emptyMap);
0N/A
0N/A // Singleton collections
0N/A Set<Integer> singletonSet = singleton(1);
0N/A equal(singletonSet.size(), 1);
0N/A testCollection(singletonSet);
0N/A testImmutableSet(singletonSet);
0N/A
0N/A List<Integer> singletonList = singletonList(1);
0N/A equal(singletonList.size(), 1);
0N/A testCollection(singletonList);
0N/A testImmutableList(singletonList);
0N/A testImmutableList(singletonList.subList(0,1));
0N/A testImmutableList(singletonList.subList(0,1).subList(0,1));
0N/A testEmptyList(singletonList.subList(0,0));
0N/A testEmptyList(singletonList.subList(0,0).subList(0,0));
0N/A
0N/A Map<Integer,Integer> singletonMap = singletonMap(1,2);
0N/A equal(singletonMap.size(), 1);
0N/A testMap(singletonMap);
0N/A testImmutableMap(singletonMap);
0N/A }
0N/A
0N/A private static void checkContainsSelf(Collection<Integer> c) {
0N/A check(c.containsAll(c));
0N/A check(c.containsAll(Arrays.asList(c.toArray())));
0N/A check(c.containsAll(Arrays.asList(c.toArray(new Integer[0]))));
0N/A }
0N/A
0N/A private static void checkContainsEmpty(Collection<Integer> c) {
0N/A check(c.containsAll(new ArrayList<Integer>()));
0N/A }
0N/A
0N/A private static <T> void testEmptyCollection(Collection<T> c) {
0N/A check(c.isEmpty());
0N/A equal(c.size(), 0);
0N/A equal(c.toString(),"[]");
0N/A equal(c.toArray().length, 0);
0N/A equal(c.toArray(new Object[0]).length, 0);
0N/A
0N/A Object[] a = new Object[1]; a[0] = Boolean.TRUE;
0N/A equal(c.toArray(a), a);
0N/A equal(a[0], null);
0N/A testEmptyIterator(c.iterator());
0N/A }
0N/A
0N/A static <T> void testEmptyIterator(final Iterator<T> it) {
0N/A if (rnd.nextBoolean())
0N/A check(! it.hasNext());
0N/A
0N/A THROWS(NoSuchElementException.class,
0N/A new Fun(){void f(){ it.next(); }});
0N/A
0N/A try { it.remove(); }
0N/A catch (IllegalStateException _) { pass(); }
0N/A catch (UnsupportedOperationException _) { pass(); }
0N/A catch (Throwable t) { unexpected(t); }
0N/A
0N/A if (rnd.nextBoolean())
0N/A check(! it.hasNext());
0N/A }
0N/A
0N/A private static void testEmptyList(List<?> c) {
0N/A testEmptyCollection(c);
0N/A equal(c.hashCode(), 1);
0N/A equal2(c, Collections.<Integer>emptyList());
0N/A }
0N/A
0N/A private static <T> void testEmptySet(Set<T> c) {
0N/A testEmptyCollection(c);
0N/A equal(c.hashCode(), 0);
0N/A equal2(c, Collections.<Integer>emptySet());
0N/A if (c instanceof NavigableSet<?>)
0N/A testEmptyIterator(((NavigableSet<T>)c).descendingIterator());
0N/A }
0N/A
0N/A private static void testImmutableCollection(final Collection<Integer> c) {
0N/A THROWS(UnsupportedOperationException.class,
0N/A new Fun(){void f(){ c.add(99); }},
0N/A new Fun(){void f(){ c.addAll(singleton(99)); }});
0N/A if (! c.isEmpty()) {
0N/A final Integer first = c.iterator().next();
0N/A THROWS(UnsupportedOperationException.class,
0N/A new Fun(){void f(){ c.clear(); }},
0N/A new Fun(){void f(){ c.remove(first); }},
0N/A new Fun(){void f(){ c.removeAll(singleton(first)); }},
0N/A new Fun(){void f(){ c.retainAll(emptyList()); }}
0N/A );
0N/A }
0N/A }
0N/A
0N/A private static void testImmutableSet(final Set<Integer> c) {
0N/A testImmutableCollection(c);
0N/A }
0N/A
0N/A private static void testImmutableList(final List<Integer> c) {
0N/A testList(c);
0N/A testImmutableCollection(c);
0N/A THROWS(UnsupportedOperationException.class,
0N/A new Fun(){void f(){ c.set(0,42); }},
0N/A new Fun(){void f(){ c.add(0,42); }},
0N/A new Fun(){void f(){ c.addAll(0,singleton(86)); }});
0N/A if (! c.isEmpty())
0N/A THROWS(UnsupportedOperationException.class,
0N/A new Fun(){void f(){
0N/A Iterator<Integer> it = c.iterator();
0N/A it.next(); it.remove();}},
0N/A new Fun(){void f(){
0N/A ListIterator<Integer> it = c.listIterator();
0N/A it.next(); it.remove();}});
0N/A }
0N/A
0N/A private static void clear(Collection<Integer> c) {
0N/A try { c.clear(); }
0N/A catch (Throwable t) { unexpected(t); }
0N/A testEmptyCollection(c);
0N/A }
0N/A
0N/A private static <K,V> void testEmptyMap(final Map<K,V> m) {
0N/A check(m.isEmpty());
0N/A equal(m.size(), 0);
0N/A equal(m.toString(),"{}");
0N/A testEmptySet(m.keySet());
0N/A testEmptySet(m.entrySet());
0N/A testEmptyCollection(m.values());
0N/A
0N/A try { check(! m.containsValue(null)); }
0N/A catch (NullPointerException _) { /* OK */ }
0N/A try { check(! m.containsKey(null)); }
0N/A catch (NullPointerException _) { /* OK */ }
0N/A check(! m.containsValue(1));
0N/A check(! m.containsKey(1));
0N/A }
0N/A
0N/A private static void testImmutableMap(final Map<Integer,Integer> m) {
0N/A THROWS(UnsupportedOperationException.class,
0N/A new Fun(){void f(){ m.put(1,1); }},
0N/A new Fun(){void f(){ m.putAll(singletonMap(1,1)); }});
0N/A if (! m.isEmpty()) {
0N/A final Integer first = m.keySet().iterator().next();
0N/A THROWS(UnsupportedOperationException.class,
0N/A new Fun(){void f(){ m.remove(first); }},
0N/A new Fun(){void f(){ m.clear(); }});
0N/A final Map.Entry<Integer,Integer> me
0N/A = m.entrySet().iterator().next();
0N/A Integer key = me.getKey();
0N/A Integer val = me.getValue();
0N/A THROWS(UnsupportedOperationException.class,
0N/A new Fun(){void f(){ me.setValue(3); }});
0N/A equal(key, me.getKey());
0N/A equal(val, me.getValue());
0N/A }
0N/A testImmutableSet(m.keySet());
0N/A testImmutableCollection(m.values());
0N/A //testImmutableSet(m.entrySet());
0N/A }
0N/A
0N/A private static void clear(Map<?,?> m) {
0N/A try { m.clear(); }
0N/A catch (Throwable t) { unexpected(t); }
0N/A testEmptyMap(m);
0N/A }
0N/A
0N/A private static void oneElement(Collection<Integer> c) {
0N/A clear(c);
0N/A try {
0N/A check(c.add(-42));
0N/A equal(c.toString(), "[-42]");
0N/A if (c instanceof Set) check(! c.add(-42));
0N/A } catch (Throwable t) { unexpected(t); }
0N/A check(! c.isEmpty()); check(c.size() == 1);
0N/A }
0N/A
0N/A private static boolean supportsAdd(Collection<Integer> c) {
0N/A try { check(c.add(778347983)); }
0N/A catch (UnsupportedOperationException t) { return false; }
0N/A catch (Throwable t) { unexpected(t); }
0N/A
0N/A try {
0N/A check(c.contains(778347983));
0N/A check(c.remove(778347983));
0N/A } catch (Throwable t) { unexpected(t); }
0N/A return true;
0N/A }
0N/A
0N/A private static boolean supportsRemove(Collection<Integer> c) {
0N/A try { check(! c.remove(19134032)); }
0N/A catch (UnsupportedOperationException t) { return false; }
0N/A catch (Throwable t) { unexpected(t); }
0N/A return true;
0N/A }
0N/A
0N/A private static void checkFunctionalInvariants(Collection<Integer> c) {
0N/A try {
0N/A checkContainsSelf(c);
0N/A checkContainsEmpty(c);
0N/A check(c.size() != 0 ^ c.isEmpty());
0N/A
0N/A {
0N/A int size = 0;
0N/A for (Integer i : c) size++;
0N/A check(c.size() == size);
0N/A }
0N/A
0N/A check(c.toArray().length == c.size());
0N/A check(c.toArray().getClass() == Object[].class
0N/A ||
0N/A // !!!!
0N/A // 6260652: (coll) Arrays.asList(x).toArray().getClass()
0N/A // should be Object[].class
0N/A (c.getClass().getName().equals("java.util.Arrays$ArrayList"))
0N/A );
0N/A for (int size : new int[]{0,1,c.size(), c.size()+1}) {
0N/A Integer[] a = c.toArray(new Integer[size]);
0N/A check((size > c.size()) || a.length == c.size());
0N/A int i = 0; for (Integer j : c) check(a[i++] == j);
0N/A check((size <= c.size()) || (a[c.size()] == null));
0N/A check(a.getClass() == Integer[].class);
0N/A }
0N/A
0N/A check(c.equals(c));
0N/A if (c instanceof Serializable) {
0N/A //System.out.printf("Serializing %s%n", c.getClass().getName());
0N/A try {
0N/A Object clone = serialClone(c);
0N/A equal(c instanceof Serializable,
0N/A clone instanceof Serializable);
0N/A equal(c instanceof RandomAccess,
0N/A clone instanceof RandomAccess);
0N/A if ((c instanceof List) || (c instanceof Set))
0N/A equal(c, clone);
0N/A else
0N/A equal(new HashSet<Integer>(c),
0N/A new HashSet<Integer>(serialClone(c)));
0N/A } catch (Error xxx) {
0N/A if (! (xxx.getCause() instanceof NotSerializableException))
0N/A throw xxx;
0N/A }
0N/A }
0N/A }
0N/A catch (Throwable t) { unexpected(t); }
0N/A }
0N/A
0N/A //----------------------------------------------------------------
0N/A // If add(null) succeeds, contains(null) & remove(null) should succeed
0N/A //----------------------------------------------------------------
0N/A private static void testNullElement(Collection<Integer> c) {
0N/A // !!!! 5018849: (coll) TreeSet.contains(null) does not agree with Javadoc
0N/A if (c instanceof TreeSet) return;
0N/A
0N/A try {
0N/A check(c.add(null));
0N/A if (c.size() == 1)
0N/A equal(c.toString(), "[null]");
0N/A try {
0N/A checkFunctionalInvariants(c);
0N/A check(c.contains(null));
0N/A check(c.remove(null));
0N/A }
0N/A catch (Throwable t) { unexpected(t); }
0N/A }
0N/A catch (NullPointerException e) { /* OK */ }
0N/A catch (Throwable t) { unexpected(t); }
0N/A }
0N/A
0N/A
0N/A //----------------------------------------------------------------
0N/A // If add("x") succeeds, contains("x") & remove("x") should succeed
0N/A //----------------------------------------------------------------
0N/A @SuppressWarnings("unchecked")
0N/A private static void testStringElement(Collection<Integer> c) {
0N/A Collection x = (Collection)c; // Make type-unsafe
0N/A try {
0N/A check(x.add("x"));
0N/A try {
0N/A check(x.contains("x"));
0N/A check(x.remove("x"));
0N/A } catch (Throwable t) { unexpected(t); }
0N/A }
0N/A catch (ClassCastException e) { /* OK */ }
0N/A catch (Throwable t) { unexpected(t); }
0N/A }
0N/A
0N/A private static void testConcurrentCollection(Collection<Integer> c) {
0N/A try {
0N/A c.add(1);
0N/A Iterator<Integer> it = c.iterator();
0N/A check(it.hasNext());
0N/A clear(c);
0N/A check(it.next() instanceof Integer); // No CME
0N/A check(c.isEmpty());
0N/A }
0N/A catch (Throwable t) { unexpected(t); }
0N/A }
0N/A
0N/A private static void testQueue(Queue<Integer> q) {
0N/A q.clear();
0N/A for (int i = 0; i < 5; i++)
0N/A q.add(i);
0N/A equal(q.size(), 5);
0N/A checkFunctionalInvariants(q);
0N/A q.poll();
0N/A equal(q.size(), 4);
0N/A checkFunctionalInvariants(q);
0N/A if ((q instanceof LinkedBlockingQueue) ||
0N/A (q instanceof LinkedBlockingDeque) ||
0N/A (q instanceof ConcurrentLinkedQueue)) {
0N/A testQueueIteratorRemove(q);
0N/A }
0N/A }
0N/A
0N/A private static void testQueueIteratorRemove(Queue<Integer> q) {
0N/A System.err.printf("testQueueIteratorRemove %s%n",
0N/A q.getClass().getSimpleName());
0N/A q.clear();
0N/A for (int i = 0; i < 5; i++)
0N/A q.add(i);
0N/A Iterator<Integer> it = q.iterator();
0N/A check(it.hasNext());
0N/A for (int i = 3; i >= 0; i--)
0N/A q.remove(i);
0N/A equal(it.next(), 0);
0N/A equal(it.next(), 4);
0N/A
0N/A q.clear();
0N/A for (int i = 0; i < 5; i++)
0N/A q.add(i);
0N/A it = q.iterator();
0N/A equal(it.next(), 0);
0N/A check(it.hasNext());
0N/A for (int i = 1; i < 4; i++)
0N/A q.remove(i);
0N/A equal(it.next(), 1);
0N/A equal(it.next(), 4);
0N/A }
0N/A
0N/A private static void testList(final List<Integer> l) {
0N/A //----------------------------------------------------------------
0N/A // 4802633: (coll) AbstractList.addAll(-1,emptyCollection)
0N/A // doesn't throw IndexOutOfBoundsException
0N/A //----------------------------------------------------------------
0N/A try {
0N/A l.addAll(-1, Collections.<Integer>emptyList());
0N/A fail("Expected IndexOutOfBoundsException not thrown");
0N/A }
0N/A catch (UnsupportedOperationException _) {/* OK */}
0N/A catch (IndexOutOfBoundsException _) {/* OK */}
0N/A catch (Throwable t) { unexpected(t); }
0N/A
0N/A// equal(l instanceof Serializable,
0N/A// l.subList(0,0) instanceof Serializable);
0N/A if (l.subList(0,0) instanceof Serializable)
0N/A check(l instanceof Serializable);
0N/A
0N/A equal(l instanceof RandomAccess,
0N/A l.subList(0,0) instanceof RandomAccess);
0N/A }
0N/A
0N/A private static void testCollection(Collection<Integer> c) {
0N/A try { testCollection1(c); }
0N/A catch (Throwable t) { unexpected(t); }
0N/A }
0N/A
0N/A private static void testCollection1(Collection<Integer> c) {
0N/A
0N/A System.out.println("\n==> " + c.getClass().getName());
0N/A
0N/A checkFunctionalInvariants(c);
0N/A
0N/A if (! supportsAdd(c)) return;
0N/A //System.out.println("add() supported");
0N/A
0N/A if (c instanceof NavigableSet) {
0N/A System.out.println("NavigableSet tests...");
0N/A
0N/A NavigableSet<Integer> ns = (NavigableSet<Integer>)c;
0N/A testNavigableSet(ns);
0N/A testNavigableSet(ns.headSet(6, false));
0N/A testNavigableSet(ns.headSet(5, true));
0N/A testNavigableSet(ns.tailSet(0, false));
0N/A testNavigableSet(ns.tailSet(1, true));
0N/A testNavigableSet(ns.subSet(0, false, 5, true));
0N/A testNavigableSet(ns.subSet(1, true, 6, false));
0N/A }
0N/A
0N/A if (c instanceof Queue)
0N/A testQueue((Queue<Integer>)c);
0N/A
0N/A if (c instanceof List)
0N/A testList((List<Integer>)c);
0N/A
0N/A check(supportsRemove(c));
0N/A
0N/A try {
0N/A oneElement(c);
0N/A checkFunctionalInvariants(c);
0N/A }
0N/A catch (Throwable t) { unexpected(t); }
0N/A
0N/A clear(c); testNullElement(c);
0N/A oneElement(c); testNullElement(c);
0N/A
0N/A clear(c); testStringElement(c);
0N/A oneElement(c); testStringElement(c);
0N/A
0N/A if (c.getClass().getName().matches(".*concurrent.*"))
0N/A testConcurrentCollection(c);
0N/A
0N/A //----------------------------------------------------------------
0N/A // The "all" operations should throw NPE when passed null
0N/A //----------------------------------------------------------------
0N/A {
0N/A oneElement(c);
0N/A try {
0N/A c.removeAll(null);
0N/A fail("Expected NullPointerException");
0N/A }
0N/A catch (NullPointerException e) { pass(); }
0N/A catch (Throwable t) { unexpected(t); }
0N/A
0N/A oneElement(c);
0N/A try {
0N/A c.retainAll(null);
0N/A fail("Expected NullPointerException");
0N/A }
0N/A catch (NullPointerException e) { pass(); }
0N/A catch (Throwable t) { unexpected(t); }
0N/A
0N/A oneElement(c);
0N/A try {
0N/A c.addAll(null);
0N/A fail("Expected NullPointerException");
0N/A }
0N/A catch (NullPointerException e) { pass(); }
0N/A catch (Throwable t) { unexpected(t); }
0N/A
0N/A oneElement(c);
0N/A try {
0N/A c.containsAll(null);
0N/A fail("Expected NullPointerException");
0N/A }
0N/A catch (NullPointerException e) { pass(); }
0N/A catch (Throwable t) { unexpected(t); }
0N/A }
0N/A }
0N/A
0N/A //----------------------------------------------------------------
0N/A // Map
0N/A //----------------------------------------------------------------
0N/A private static void checkFunctionalInvariants(Map<Integer,Integer> m) {
0N/A check(m.keySet().size() == m.entrySet().size());
0N/A check(m.keySet().size() == m.size());
0N/A checkFunctionalInvariants(m.keySet());
0N/A checkFunctionalInvariants(m.values());
0N/A check(m.size() != 0 ^ m.isEmpty());
0N/A }
0N/A
0N/A private static void testMap(Map<Integer,Integer> m) {
0N/A System.out.println("\n==> " + m.getClass().getName());
0N/A
0N/A if (m instanceof ConcurrentMap)
0N/A testConcurrentMap((ConcurrentMap<Integer,Integer>) m);
0N/A
0N/A if (m instanceof NavigableMap) {
0N/A System.out.println("NavigableMap tests...");
0N/A
0N/A NavigableMap<Integer,Integer> nm =
0N/A (NavigableMap<Integer,Integer>) m;
0N/A testNavigableMapRemovers(nm);
0N/A testNavigableMap(nm);
0N/A testNavigableMap(nm.headMap(6, false));
0N/A testNavigableMap(nm.headMap(5, true));
0N/A testNavigableMap(nm.tailMap(0, false));
0N/A testNavigableMap(nm.tailMap(1, true));
0N/A testNavigableMap(nm.subMap(1, true, 6, false));
0N/A testNavigableMap(nm.subMap(0, false, 5, true));
0N/A }
0N/A
0N/A checkFunctionalInvariants(m);
0N/A
0N/A if (supportsClear(m)) {
0N/A try { clear(m); }
0N/A catch (Throwable t) { unexpected(t); }
0N/A }
0N/A
0N/A if (supportsPut(m)) {
0N/A try {
0N/A check(m.put(3333, 77777) == null);
0N/A check(m.put(9134, 74982) == null);
0N/A check(m.get(9134) == 74982);
0N/A check(m.put(9134, 1382) == 74982);
0N/A check(m.get(9134) == 1382);
0N/A check(m.size() == 2);
0N/A checkFunctionalInvariants(m);
0N/A checkNPEConsistency(m);
0N/A }
0N/A catch (Throwable t) { unexpected(t); }
0N/A }
0N/A }
0N/A
0N/A private static boolean supportsPut(Map<Integer,Integer> m) {
0N/A // We're asking for .equals(...) semantics
0N/A if (m instanceof IdentityHashMap) return false;
0N/A
0N/A try { check(m.put(778347983,12735) == null); }
0N/A catch (UnsupportedOperationException t) { return false; }
0N/A catch (Throwable t) { unexpected(t); }
0N/A
0N/A try {
0N/A check(m.containsKey(778347983));
0N/A check(m.remove(778347983) != null);
0N/A } catch (Throwable t) { unexpected(t); }
0N/A return true;
0N/A }
0N/A
0N/A private static boolean supportsClear(Map<?,?> m) {
0N/A try { m.clear(); }
0N/A catch (UnsupportedOperationException t) { return false; }
0N/A catch (Throwable t) { unexpected(t); }
0N/A return true;
0N/A }
0N/A
0N/A //----------------------------------------------------------------
0N/A // ConcurrentMap
0N/A //----------------------------------------------------------------
0N/A private static void testConcurrentMap(ConcurrentMap<Integer,Integer> m) {
0N/A System.out.println("ConcurrentMap tests...");
0N/A
0N/A try {
0N/A clear(m);
0N/A
0N/A check(m.putIfAbsent(18357,7346) == null);
0N/A check(m.containsKey(18357));
0N/A check(m.putIfAbsent(18357,8263) == 7346);
0N/A try { m.putIfAbsent(18357,null); fail("NPE"); }
0N/A catch (NullPointerException t) { }
0N/A check(m.containsKey(18357));
0N/A
0N/A check(! m.replace(18357,8888,7777));
0N/A check(m.containsKey(18357));
0N/A try { m.replace(18357,null,7777); fail("NPE"); }
0N/A catch (NullPointerException t) { }
0N/A check(m.containsKey(18357));
0N/A check(m.get(18357) == 7346);
0N/A check(m.replace(18357,7346,5555));
0N/A check(m.replace(18357,5555,7346));
0N/A check(m.get(18357) == 7346);
0N/A
0N/A check(m.replace(92347,7834) == null);
0N/A try { m.replace(18357,null); fail("NPE"); }
0N/A catch (NullPointerException t) { }
0N/A check(m.replace(18357,7346) == 7346);
0N/A check(m.replace(18357,5555) == 7346);
0N/A check(m.get(18357) == 5555);
0N/A check(m.replace(18357,7346) == 5555);
0N/A check(m.get(18357) == 7346);
0N/A
0N/A check(! m.remove(18357,9999));
0N/A check(m.get(18357) == 7346);
0N/A check(m.containsKey(18357));
0N/A check(! m.remove(18357,null)); // 6272521
0N/A check(m.get(18357) == 7346);
0N/A check(m.remove(18357,7346));
0N/A check(m.get(18357) == null);
0N/A check(! m.containsKey(18357));
0N/A check(m.isEmpty());
0N/A
0N/A m.putIfAbsent(1,2);
0N/A check(m.size() == 1);
0N/A check(! m.remove(1,null));
0N/A check(! m.remove(1,null));
0N/A check(! m.remove(1,1));
0N/A check(m.remove(1,2));
0N/A check(m.isEmpty());
0N/A
0N/A testEmptyMap(m);
0N/A }
0N/A catch (Throwable t) { unexpected(t); }
0N/A }
0N/A
0N/A private static void throwsConsistently(Class<? extends Throwable> k,
0N/A Iterable<Fun> fs) {
0N/A List<Class<? extends Throwable>> threw
0N/A = new ArrayList<Class<? extends Throwable>>();
0N/A for (Fun f : fs)
0N/A try { f.f(); threw.add(null); }
0N/A catch (Throwable t) {
0N/A check(k.isAssignableFrom(t.getClass()));
0N/A threw.add(t.getClass());
0N/A }
0N/A if (new HashSet<Object>(threw).size() != 1)
0N/A fail(threw.toString());
0N/A }
0N/A
0N/A private static <T> void checkNPEConsistency(final Map<T,Integer> m) {
0N/A m.clear();
0N/A final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap)
0N/A ? (ConcurrentMap<T,Integer>) m
0N/A : null;
0N/A List<Fun> fs = new ArrayList<Fun>();
0N/A fs.add(new Fun(){void f(){ check(! m.containsKey(null));}});
0N/A fs.add(new Fun(){void f(){ equal(m.remove(null), null);}});
0N/A fs.add(new Fun(){void f(){ equal(m.get(null), null);}});
0N/A if (cm != null) {
0N/A fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});}
0N/A throwsConsistently(NullPointerException.class, fs);
0N/A
0N/A fs.clear();
0N/A final Map<T,Integer> sm = singletonMap(null,1);
0N/A fs.add(new Fun(){void f(){ equal(m.put(null,1), null); m.clear();}});
0N/A fs.add(new Fun(){void f(){ m.putAll(sm); m.clear();}});
0N/A if (cm != null) {
0N/A fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});
0N/A fs.add(new Fun(){void f(){ equal(cm.putIfAbsent(null,1), 1);}});
0N/A fs.add(new Fun(){void f(){ equal(cm.replace(null,1), null);}});
0N/A fs.add(new Fun(){void f(){ equal(cm.replace(null,1, 1), 1);}});
0N/A }
0N/A throwsConsistently(NullPointerException.class, fs);
0N/A }
0N/A
0N/A //----------------------------------------------------------------
0N/A // NavigableMap
0N/A //----------------------------------------------------------------
0N/A private static void
0N/A checkNavigableMapKeys(NavigableMap<Integer,Integer> m,
0N/A Integer i,
0N/A Integer lower,
0N/A Integer floor,
0N/A Integer ceiling,
0N/A Integer higher) {
0N/A equal(m.lowerKey(i), lower);
0N/A equal(m.floorKey(i), floor);
0N/A equal(m.ceilingKey(i), ceiling);
0N/A equal(m.higherKey(i), higher);
0N/A }
0N/A
0N/A private static void
0N/A checkNavigableSetKeys(NavigableSet<Integer> m,
0N/A Integer i,
0N/A Integer lower,
0N/A Integer floor,
0N/A Integer ceiling,
0N/A Integer higher) {
0N/A equal(m.lower(i), lower);
0N/A equal(m.floor(i), floor);
0N/A equal(m.ceiling(i), ceiling);
0N/A equal(m.higher(i), higher);
0N/A }
0N/A
0N/A static final Random rnd = new Random();
0N/A static void equalNext(final Iterator<?> it, Object expected) {
0N/A if (rnd.nextBoolean())
0N/A check(it.hasNext());
0N/A equal(it.next(), expected);
0N/A }
0N/A
0N/A static void equalMaps(Map m1, Map m2) {
0N/A equal(m1, m2);
0N/A equal(m2, m1);
0N/A equal(m1.size(), m2.size());
0N/A equal(m1.isEmpty(), m2.isEmpty());
0N/A equal(m1.toString(), m2.toString());
0N/A check(Arrays.equals(m1.entrySet().toArray(), m2.entrySet().toArray()));
0N/A }
0N/A
0N/A @SuppressWarnings({"unchecked", "rawtypes"})
0N/A static void testNavigableMapRemovers(NavigableMap m)
0N/A {
0N/A final Map emptyMap = new HashMap();
0N/A
0N/A final Map singletonMap = new HashMap();
0N/A singletonMap.put(1, 2);
0N/A
0N/A abstract class NavigableMapView {
0N/A abstract NavigableMap view(NavigableMap m);
0N/A }
0N/A
0N/A NavigableMapView[] views = {
0N/A new NavigableMapView() { NavigableMap view(NavigableMap m) {
0N/A return m; }},
0N/A new NavigableMapView() { NavigableMap view(NavigableMap m) {
0N/A return m.headMap(99, true); }},
0N/A new NavigableMapView() { NavigableMap view(NavigableMap m) {
0N/A return m.tailMap(-99, false); }},
0N/A new NavigableMapView() { NavigableMap view(NavigableMap m) {
0N/A return m.subMap(-99, true, 99, false); }},
0N/A };
0N/A
0N/A abstract class Remover {
0N/A abstract void remove(NavigableMap m, Object k, Object v);
0N/A }
0N/A
0N/A Remover[] removers = {
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A equal(m.remove(k), v); }},
0N/A
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A equal(m.descendingMap().remove(k), v); }},
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A equal(m.descendingMap().headMap(-86, false).remove(k), v); }},
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A equal(m.descendingMap().tailMap(86, true).remove(k), v); }},
0N/A
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A equal(m.headMap(86, true).remove(k), v); }},
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A equal(m.tailMap(-86, true).remove(k), v); }},
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A equal(m.subMap(-86, false, 86, true).remove(k), v); }},
0N/A
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A check(m.keySet().remove(k)); }},
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A check(m.navigableKeySet().remove(k)); }},
0N/A
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A check(m.navigableKeySet().headSet(86, true).remove(k)); }},
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A check(m.navigableKeySet().tailSet(-86, false).remove(k)); }},
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A check(m.navigableKeySet().subSet(-86, true, 86, false)
0N/A .remove(k)); }},
0N/A
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A check(m.descendingKeySet().headSet(-86, false).remove(k)); }},
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A check(m.descendingKeySet().tailSet(86, true).remove(k)); }},
0N/A new Remover() { void remove(NavigableMap m, Object k, Object v) {
0N/A check(m.descendingKeySet().subSet(86, true, -86, false)
0N/A .remove(k)); }},
0N/A };
0N/A
0N/A for (NavigableMapView view : views) {
0N/A for (Remover remover : removers) {
0N/A try {
0N/A m.clear();
0N/A equalMaps(m, emptyMap);
0N/A equal(m.put(1, 2), null);
0N/A equalMaps(m, singletonMap);
0N/A NavigableMap v = view.view(m);
0N/A remover.remove(v, 1, 2);
0N/A equalMaps(m, emptyMap);
0N/A } catch (Throwable t) { unexpected(t); }
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void testNavigableMap(NavigableMap<Integer,Integer> m)
0N/A {
0N/A clear(m);
0N/A checkNavigableMapKeys(m, 1, null, null, null, null);
0N/A
0N/A equal(m.put(1, 2), null);
0N/A equal(m.put(3, 4), null);
0N/A equal(m.put(5, 9), null);
0N/A
0N/A equal(m.put(1, 2), 2);
0N/A equal(m.put(3, 4), 4);
0N/A equal(m.put(5, 6), 9);
0N/A
0N/A checkNavigableMapKeys(m, 0, null, null, 1, 1);
0N/A checkNavigableMapKeys(m, 1, null, 1, 1, 3);
0N/A checkNavigableMapKeys(m, 2, 1, 1, 3, 3);
0N/A checkNavigableMapKeys(m, 3, 1, 3, 3, 5);
0N/A checkNavigableMapKeys(m, 5, 3, 5, 5, null);
0N/A checkNavigableMapKeys(m, 6, 5, 5, null, null);
0N/A
0N/A for (final Iterator<Integer> it :
0N/A (Iterator<Integer>[])
0N/A new Iterator<?>[] {
0N/A m.descendingKeySet().iterator(),
0N/A m.navigableKeySet().descendingIterator()}) {
0N/A equalNext(it, 5);
0N/A equalNext(it, 3);
0N/A equalNext(it, 1);
0N/A check(! it.hasNext());
0N/A THROWS(NoSuchElementException.class,
0N/A new Fun(){void f(){it.next();}});
0N/A }
0N/A
0N/A {
0N/A final Iterator<Map.Entry<Integer,Integer>> it
0N/A = m.descendingMap().entrySet().iterator();
0N/A check(it.hasNext()); equal(it.next().getKey(), 5);
0N/A check(it.hasNext()); equal(it.next().getKey(), 3);
0N/A check(it.hasNext()); equal(it.next().getKey(), 1);
0N/A check(! it.hasNext());
0N/A THROWS(NoSuchElementException.class,
0N/A new Fun(){void f(){it.next();}});
0N/A }
0N/A }
0N/A
0N/A
0N/A private static void testNavigableSet(NavigableSet<Integer> s) {
0N/A clear(s);
0N/A checkNavigableSetKeys(s, 1, null, null, null, null);
0N/A
0N/A check(s.add(1));
0N/A check(s.add(3));
0N/A check(s.add(5));
0N/A
0N/A check(! s.add(1));
0N/A check(! s.add(3));
0N/A check(! s.add(5));
0N/A
0N/A checkNavigableSetKeys(s, 0, null, null, 1, 1);
0N/A checkNavigableSetKeys(s, 1, null, 1, 1, 3);
0N/A checkNavigableSetKeys(s, 2, 1, 1, 3, 3);
0N/A checkNavigableSetKeys(s, 3, 1, 3, 3, 5);
0N/A checkNavigableSetKeys(s, 5, 3, 5, 5, null);
0N/A checkNavigableSetKeys(s, 6, 5, 5, null, null);
0N/A
0N/A for (final Iterator<Integer> it :
0N/A (Iterator<Integer>[])
0N/A new Iterator<?>[] {
0N/A s.descendingIterator(),
0N/A s.descendingSet().iterator()}) {
0N/A equalNext(it, 5);
0N/A equalNext(it, 3);
0N/A equalNext(it, 1);
0N/A check(! it.hasNext());
0N/A THROWS(NoSuchElementException.class,
0N/A new Fun(){void f(){it.next();}});
0N/A }
0N/A }
0N/A
0N/A //--------------------- Infrastructure ---------------------------
0N/A static volatile int passed = 0, failed = 0;
0N/A static void pass() { passed++; }
0N/A static void fail() { failed++; Thread.dumpStack(); }
0N/A static void fail(String msg) { System.out.println(msg); fail(); }
0N/A static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
0N/A static void check(boolean cond) { if (cond) pass(); else fail(); }
0N/A static void equal(Object x, Object y) {
0N/A if (x == null ? y == null : x.equals(y)) pass();
0N/A else {System.out.println(x + " not equal to " + y); fail();}}
0N/A static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
0N/A public static void main(String[] args) throws Throwable {
0N/A try { realMain(args); } catch (Throwable t) { unexpected(t); }
0N/A
0N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
0N/A if (failed > 0) throw new Exception("Some tests failed");
0N/A }
0N/A private static abstract class Fun {abstract void f() throws Throwable;}
0N/A private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
0N/A for (Fun f : fs)
0N/A try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
0N/A catch (Throwable t) {
0N/A if (k.isAssignableFrom(t.getClass())) pass();
0N/A else unexpected(t);}}
0N/A static byte[] serializedForm(Object obj) {
0N/A try {
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A new ObjectOutputStream(baos).writeObject(obj);
0N/A return baos.toByteArray();
0N/A } catch (IOException e) { throw new Error(e); }}
0N/A static Object readObject(byte[] bytes)
0N/A throws IOException, ClassNotFoundException {
0N/A InputStream is = new ByteArrayInputStream(bytes);
0N/A return new ObjectInputStream(is).readObject();}
0N/A @SuppressWarnings("unchecked")
0N/A static <T> T serialClone(T obj) {
0N/A try { return (T) readObject(serializedForm(obj)); }
0N/A catch (Exception e) { throw new Error(e); }}
0N/A}
0N/A