jdk/test/java/util/PriorityQueue/ForgetMeNot.java
author dl
Fri, 23 Sep 2016 13:24:33 -0700
changeset 41131 87edc8451f8a
parent 24692 268fbc344d53
child 43522 f9c6f543c4db
permissions -rw-r--r--
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21 Reviewed-by: martin, chegar, shade
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: 5506
diff changeset
     2
 * Copyright (c) 2006, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 6394004
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test ForgetMeNot implementation feature (and more)
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
41131
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 24692
diff changeset
    31
import java.util.Arrays;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 24692
diff changeset
    32
import java.util.Iterator;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 24692
diff changeset
    33
import java.util.NoSuchElementException;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 24692
diff changeset
    34
import java.util.PriorityQueue;
87edc8451f8a 8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents: 24692
diff changeset
    35
import java.util.Queue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class ForgetMeNot {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static void checkQ(PriorityQueue<Integer> q, Integer...elts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        check(Arrays.equals(q.toArray(), elts));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static void noMoreElements(final Iterator<Integer> it) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        for (int j = 0; j < 2; j++) {
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 5506
diff changeset
    44
            THROWS(NoSuchElementException.class, () -> it.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static void removeIsCurrentlyIllegal(final Iterator<Integer> it) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        for (int j = 0; j < 2; j++) {
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 5506
diff changeset
    51
            THROWS(IllegalStateException.class, () -> it.remove());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static void remove(Iterator<Integer> it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                               Queue<Integer> q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        int size = q.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        equal(size, q.size()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        final PriorityQueue<Integer> q = new PriorityQueue<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        Iterator<Integer> it;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // Empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        checkQ(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        check(q.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        check(! q.contains(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        it = q.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        noMoreElements(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        q.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        check(q.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // Singleton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        q.add(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        checkQ(q, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        check(! q.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        check(q.contains(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        it = q.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        equal(it.next(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        noMoreElements(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        remove(it, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        check(q.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        noMoreElements(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        checkQ(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        q.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // @see PriorityQueue.forgetMeNot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        final Integer[] a = {0, 4, 1, 6, 7, 2, 3}; // Carefully chosen!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        q.addAll(Arrays.asList(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        it = q.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        equal(it.next(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        equal(it.next(), 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        equal(it.next(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        equal(it.next(), 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        remove(it, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        checkQ(q, 0, 3, 1, 4, 7, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        equal(it.next(), 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        remove(it, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        checkQ(q, 0, 2, 1, 4, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        equal(it.next(), 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        equal(it.next(), 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        remove(it, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        checkQ(q, 0, 3, 1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        noMoreElements(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (failed > 0) throw new AssertionError("Some tests failed");}
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 5506
diff changeset
   151
    interface Fun {void f() throws Throwable;}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}