test/jdk/java/util/Collection/IteratorAtEnd.java
author dl
Tue, 16 Jan 2018 18:28:39 -0800
changeset 48541 946e34c2dec9
parent 47216 71c04702a3d5
permissions -rw-r--r--
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01 Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
     2
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6529795
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary next() does not change iterator state if throws NoSuchElementException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    31
import java.util.ArrayDeque;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    32
import java.util.ArrayList;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    33
import java.util.Collection;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    34
import java.util.HashMap;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    35
import java.util.Hashtable;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    36
import java.util.IdentityHashMap;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    37
import java.util.Iterator;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    38
import java.util.LinkedHashMap;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    39
import java.util.LinkedList;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    40
import java.util.List;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    41
import java.util.ListIterator;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    42
import java.util.Map;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    43
import java.util.NoSuchElementException;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    44
import java.util.PriorityQueue;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    45
import java.util.TreeMap;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    46
import java.util.TreeSet;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    47
import java.util.Vector;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    48
import java.util.WeakHashMap;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    49
import java.util.concurrent.ArrayBlockingQueue;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    50
import java.util.concurrent.ConcurrentHashMap;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    51
import java.util.concurrent.ConcurrentLinkedDeque;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    52
import java.util.concurrent.ConcurrentLinkedQueue;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    53
import java.util.concurrent.ConcurrentSkipListMap;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    54
import java.util.concurrent.ConcurrentSkipListSet;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    55
import java.util.concurrent.CopyOnWriteArrayList;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    56
import java.util.concurrent.CopyOnWriteArraySet;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    57
import java.util.concurrent.LinkedBlockingQueue;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47216
diff changeset
    58
import java.util.concurrent.LinkedTransferQueue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
@SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class IteratorAtEnd {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final int SIZE = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        testCollection(new ArrayList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        testCollection(new Vector());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        testCollection(new LinkedList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        testCollection(new ArrayDeque());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        testCollection(new TreeSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        testCollection(new CopyOnWriteArrayList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        testCollection(new CopyOnWriteArraySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        testCollection(new ConcurrentSkipListSet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        testCollection(new PriorityQueue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        testCollection(new LinkedBlockingQueue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        testCollection(new ArrayBlockingQueue(100));
6672
f01ef94a63e7 6981113: Add ConcurrentLinkedDeque
dl
parents: 5506
diff changeset
    77
        testCollection(new ConcurrentLinkedDeque());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        testCollection(new ConcurrentLinkedQueue());
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 3708
diff changeset
    79
        testCollection(new LinkedTransferQueue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        testMap(new HashMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        testMap(new Hashtable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        testMap(new LinkedHashMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        testMap(new WeakHashMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        testMap(new IdentityHashMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        testMap(new ConcurrentHashMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        testMap(new ConcurrentSkipListMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        testMap(new TreeMap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static void testCollection(Collection c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            for (int i = 0; i < SIZE; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                c.add(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            test(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static void testMap(Map m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            for (int i = 0; i < 3*SIZE; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                m.put(i, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            test(m.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            test(m.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            test(m.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static void test(Collection c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            final Iterator it = c.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            THROWS(NoSuchElementException.class,
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
   113
                   () -> { while (true) it.next(); });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            try { it.remove(); }
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
   115
            catch (UnsupportedOperationException exc) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (c instanceof List) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            final List list = (List) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                final ListIterator it = list.listIterator(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                final Object x = it.previous();
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
   125
                THROWS(NoSuchElementException.class, () -> it.previous());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                try { it.remove(); }
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
   127
                catch (UnsupportedOperationException exc) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                check(! list.get(0).equals(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                final ListIterator it = list.listIterator(list.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                it.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                final Object x = it.next();
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
   136
                THROWS(NoSuchElementException.class, () -> it.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                try { it.remove(); }
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
   138
                catch (UnsupportedOperationException exc) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                check(! list.get(list.size()-1).equals(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (failed > 0) throw new AssertionError("Some tests failed");}
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 7668
diff changeset
   159
    interface Fun {void f() throws Throwable;}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}