jdk/test/java/util/Collections/EmptyIterator.java
author dl
Tue, 13 Oct 2015 16:45:35 -0700
changeset 32991 b27c76b82713
parent 28060 4019fb4fb1fb
permissions -rw-r--r--
8134853: Bulk integration of java.util.concurrent classes 8080939: ForkJoinPool and Phaser deadlock 8044616: Clients of Unsafe.compareAndSwapLong need to beware of using direct stores to the same field 8071638: [JAVADOC] Buggy example in javadoc for afterExecute to access a submitted job's Throwable 8043743: Data missed in java.util.concurrent.LinkedTransferQueue 8054446: Repeated offer and remove on ConcurrentLinkedQueue lead to an OutOfMemoryError 8031374: TEST_BUG: java/util/concurrent/ConcurrentQueues/OfferRemoveLoops.java fails Intermittently 8034208: Cleanup to test/java/util/concurrent/BlockingQueue/Interrupt.java 8035661: Test fix java/util/concurrent/ConcurrentQueues/OfferRemoveLoops.java from jsr166 CVS 8062841: ConcurrentHashMap.computeIfAbsent stuck in an endless loop 8073208: javadoc typo in java.util.concurrent.Executor 8073704: FutureTask.isDone returns true when task has not yet completed 8037093: java/util/concurrent/locks/Lock/TimedAcquireLeak.java fails intermittently 8022642: ScheduledThreadPoolExecutor with zero corePoolSize create endlessly threads 8065320: Busy loop in ThreadPoolExecutor.getTask for ScheduledThreadPoolExecutor 8129861: High processor load for ScheduledThreadPoolExecutor with 0 core threads 8051859: ScheduledExecutorService.scheduleWithFixedDelay fails with max delay 7146994: example afterExecute for ScheduledThreadPoolExecutor hangs Reviewed-by: martin, psandoz, chegar

/*
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 5017904 6356890 8004928
 * @summary Test empty iterators, enumerations, and collections
 */

import static java.util.Collections.*;
import java.util.*;
import java.util.concurrent.SynchronousQueue;

public class EmptyIterator {

    void test(String[] args) throws Throwable {
        testEmptyCollection(emptyList());
        testEmptyCollection(emptySet());
        testEmptyCollection(new SynchronousQueue<Object>());
        testEmptyMap(emptyMap());

        Hashtable<?,?> emptyTable = new Hashtable<>();
        testEmptyEnumeration(emptyTable.keys());
        testEmptyEnumeration(emptyTable.elements());
        testEmptyIterator(emptyTable.keySet().iterator());
        testEmptyIterator(emptyTable.values().iterator());
        testEmptyIterator(emptyTable.entrySet().iterator());

        final Enumeration<EmptyIterator> finalEmptyTyped = emptyEnumeration();
        testEmptyEnumeration(finalEmptyTyped);

        final Enumeration<?> finalEmptyAbstract = emptyEnumeration();
        testEmptyEnumeration(finalEmptyAbstract);

        testEmptyIterator(emptyIterator());
    }

    void testEmptyEnumeration(final Enumeration<?> e) {
        check(e == emptyEnumeration());
        check(!e.hasMoreElements());
        THROWS(NoSuchElementException.class,
               new F(){void f(){ e.nextElement(); }});
    }

    void testEmptyIterator(final Iterator<?> it) {
        check(it == emptyIterator());
        check(! it.hasNext());
        THROWS(NoSuchElementException.class,
               new F(){void f(){ it.next(); }});
        THROWS(IllegalStateException.class,
               new F(){void f(){ it.remove(); }});
    }

    void testEmptyMap(Map<?,?> m) {
        check(m == emptyMap());
        check(m.entrySet().iterator() ==
              Collections.<Map.Entry<?,?>>emptyIterator());
        check(m.values().iterator() == emptyIterator());
        check(m.keySet().iterator() == emptyIterator());
        equal(m, unmodifiableMap(m));

        testEmptyCollection(m.keySet());
        testEmptyCollection(m.entrySet());
        testEmptyCollection(m.values());
    }

    void testToArray(final Collection<?> c) {
        Object[] a = c.toArray();
        equal(a.length, 0);
        equal(a.getClass().getComponentType(), Object.class);
        THROWS(NullPointerException.class,
               new F(){void f(){ c.toArray((Object[])null); }});

        {
            String[] t = new String[0];
            check(c.toArray(t) == t);
        }

        {
            String[] t = nCopies(10, "").toArray(new String[0]);
            check(c.toArray(t) == t);
            check(t[0] == null);
            for (int i=1; i<t.length; i++)
                check(t[i] == "");
        }
    }

    void testEmptyCollection(final Collection<?> c) {
        testEmptyIterator(c.iterator());

        check(c.iterator() == emptyIterator());
        if (c instanceof List)
            check(((List<?>)c).listIterator() == emptyListIterator());

        testToArray(c);
    }

    //--------------------- Infrastructure ---------------------------
    volatile int passed = 0, failed = 0;
    void pass() {passed++;}
    void fail() {failed++; Thread.dumpStack();}
    void fail(String msg) {System.err.println(msg); fail();}
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
    void check(boolean cond) {if (cond) pass(); else fail();}
    void equal(Object x, Object y) {
        if (x == null ? y == null : x.equals(y)) pass();
        else fail(x + " not equal to " + y);}
    public static void main(String[] args) throws Throwable {
        new EmptyIterator().instanceMain(args);}
    void instanceMain(String[] args) throws Throwable {
        try {test(args);} catch (Throwable t) {unexpected(t);}
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
        if (failed > 0) throw new AssertionError("Some tests failed");}
    abstract class F {abstract void f() throws Throwable;}
    void THROWS(Class<? extends Throwable> k, F... fs) {
        for (F f : fs)
            try {f.f(); fail("Expected " + k.getName() + " not thrown");}
            catch (Throwable t) {
                if (k.isAssignableFrom(t.getClass())) pass();
                else unexpected(t);}}
}