jdk/test/java/util/PriorityQueue/ForgetMeNot.java
author igerasim
Mon, 02 Jun 2014 19:49:57 +0400
changeset 24692 268fbc344d53
parent 5506 202f599c92aa
child 41131 87edc8451f8a
permissions -rw-r--r--
8037866: Replace the Fun class in tests with lambdas 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: 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class ForgetMeNot {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private static void checkQ(PriorityQueue<Integer> q, Integer...elts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        check(Arrays.equals(q.toArray(), elts));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static void noMoreElements(final Iterator<Integer> it) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        for (int j = 0; j < 2; j++) {
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 5506
diff changeset
    40
            THROWS(NoSuchElementException.class, () -> it.next());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static void removeIsCurrentlyIllegal(final Iterator<Integer> it) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        for (int j = 0; j < 2; j++) {
24692
268fbc344d53 8037866: Replace the Fun class in tests with lambdas
igerasim
parents: 5506
diff changeset
    47
            THROWS(IllegalStateException.class, () -> it.remove());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static void remove(Iterator<Integer> it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                               Queue<Integer> q) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int size = q.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        equal(size, q.size()+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        final PriorityQueue<Integer> q = new PriorityQueue<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Iterator<Integer> it;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // Empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        checkQ(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        check(q.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        check(! q.contains(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        it = q.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        noMoreElements(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        q.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        check(q.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // Singleton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        q.add(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        checkQ(q, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        check(! q.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        check(q.contains(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        it = q.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        equal(it.next(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        noMoreElements(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        remove(it, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        check(q.isEmpty());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        noMoreElements(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        checkQ(q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        q.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // @see PriorityQueue.forgetMeNot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        final Integer[] a = {0, 4, 1, 6, 7, 2, 3}; // Carefully chosen!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        q.addAll(Arrays.asList(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        it = q.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        equal(it.next(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        equal(it.next(), 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        equal(it.next(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        equal(it.next(), 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        checkQ(q, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        remove(it, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        checkQ(q, 0, 3, 1, 4, 7, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        equal(it.next(), 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        remove(it, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        checkQ(q, 0, 2, 1, 4, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        check(it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        equal(it.next(), 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        equal(it.next(), 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        remove(it, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        checkQ(q, 0, 3, 1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        check(! it.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        noMoreElements(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        removeIsCurrentlyIllegal(it);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        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
   147
    interface Fun {void f() throws Throwable;}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        for (Fun f : fs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (k.isAssignableFrom(t.getClass())) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                else unexpected(t);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}